AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Tic Tac Toe request
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Thrillski



Joined: 18 Jul 2007
Posts: 58
Location: South Florida

PostPosted: Thu Jan 17, 2008 1:05 am    Post subject: Tic Tac Toe request Reply with quote

Good day!

I have a request for those members who would like a challenge. This is a request for so that my 4 year old would be able to learn how to play tic-tac-toe. This is not an attempt to get school credit or anything of the sort. I have searched the forums and the one attempt that I found simply does not work as posted.
Why don't I do this? I simply don't have the time to devote to this project and it most likely will cover topics that I have not yet learnt. (Pictures, matrices, conditional statements, etc)

Objective: Create a playable tic-tac-toe game

Features:
(This assumes the computer is "X" and goes first.)
The ability to have multiple levels of difficulty.
Level 1: Computer places its moves purely at random. So that the center square is picked 1 chance in 9.
Level 2: Same as level 1 except that if the computer sees a potential losing situation, it will block.
Level 3: Same as level 2 except that if the computer sees a winning scenario, it will end the game.
Number of players can be 0, 1 or 2.
If number of players = 0, then a "how many games should be played? should be asked."
Would be interesting to see how many wins occur if 2 level 2's are played and how much of an advantage is there in going first?
I am not going big on the graphics. Simple (graphical) X's and O's on a colored background is sufficient.

Optional:
Level 4: The computer really tries to win. I would prefer a more soft approach than a brute force "cheating" method.
Level 5+: Here is where cheating methods would be placed.

Tips and tricks:
As there are only eight (8) possible ways to win, I allow "brute force" checking of conditions.
One way to check for conditions is to use a 3x3 matrix and assign a value of "1" for X and "-1" for O. This will allow for statements as If Abs(Val(1))+Val(4)+Val(7)) = 2 then block. [possible win condition on left column. Abs = Absolute Value. Val(5) = value of square(5).]
The wikipedia page of tic-tac-toe has some strategy hints that may be of assistance:
http://en.wikipedia.org/wiki/Tic-tac-toe
Back to top
View user's profile Send private message
jaco0646



Joined: 07 Oct 2006
Posts: 556
Location: MN, USA

PostPosted: Mon Jan 21, 2008 6:20 am    Post subject: Reply with quote

Thrillski, thank you for the excellent suggestion. This was a fun weekend project! I'm not sure if I spent more time coding the game or playing it, although I suspect the latter.

My code isn't very elegant, as it only employs what you termed "brute force" techniques; but it is playable, nonetheless. The game alternates between X and O making the first move, and the first 3 levels behave exactly as you requested. However, since I am not mathematically inclined enough to write a Tic-Tac-Toe algorithm, level 4 is simply what you called a cheating method. Level 4 is not perfect, since it will not always take advantage of a poor move by the player; but it should still be unbeatable, producing at least a tie in every game.

Due to the NFL Championship games today, I didn't get time to add an option for the computer to play its own games. Laughing I may not get to that until next weekend though, so I thought I'd post the code so far.

PLEASE let me know if there are any bugs. As I mentioned, I've played it quite extensively, Very Happy but I probably didn't cover every scenario.

ENJOY!

>DOWNLOAD<

EDIT: I made a simple icon for the game, for those who compile.
>DOWNLOAD Icon<
_________________
http://autohotkey.net/~jaco0646/


Last edited by jaco0646 on Fri Feb 08, 2008 6:26 pm; edited 3 times in total
Back to top
View user's profile Send private message Visit poster's website
BoBoĻ
Guest





PostPosted: Mon Jan 21, 2008 11:06 am    Post subject: Reply with quote

@ jaco0646
have you posted your NFL-TTT script already at the Scripts & Functions section? If not already done - think about it!
Thx 4 listening & sharing your time with us Cool
Back to top
Thrillski



Joined: 18 Jul 2007
Posts: 58
Location: South Florida

PostPosted: Tue Jan 22, 2008 4:03 am    Post subject: review and comments Reply with quote

Wow jaco0646!

Impressive little program you wrote there. I like what I see and I definitely want to see the "finished" product.

I used to be a high school math teacher, so I know how to draw up such challenging scenarios and how to be "critical" of perfection. (Part of the reason why I left. You overly praise below average students and are overly critical of above average students.)

I do like the option for background colors. Bonus points for that.

I know I would and I am sure some others would like a little more documentation in your code to try and figure out what it is that you are doing. Not a line-by-line but more of a routine-by-routine. I have no idea what win3 = and block8 = means.

I was able to beat the game in level 4. (Only once.)
Level 2 went for a win when there was a block it should have played instead.

But apart from those strange behaviours, it is playable and functional. For a "beta", I would say it is more complete than many professional software packages out there.

I understand about watching football. I did that too instead of cleaning house or the coding I wanted to do instead. Razz

Have a nice day!
Thrillski
Back to top
View user's profile Send private message
SoggyDog



Joined: 02 May 2006
Posts: 214
Location: Greeley, CO

PostPosted: Tue Jan 22, 2008 8:15 pm    Post subject: Reply with quote

I have a similar request;

I have been wanting to write a Mancala game in AHK.
Honestly, though, I don't do well when it comes to logic design.

Another game that I'd like to see is Mastermind.
[This link even provides the algorithm]

Anyone up to the challenge(s)?
_________________

SoggyDog
Download AutoHotKey Wallpaper
Does 'Fuzzy Logic' tickle?
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
DerRaphael



Joined: 23 Nov 2007
Posts: 456
Location: Heidelberg, Germany

PostPosted: Wed Jan 23, 2008 5:11 am    Post subject: Reply with quote

Mastermind is done up and running, see Scripts & Functions for details (just follow the Link)
http://www.autohotkey.com/forum/viewtopic.php?t=27849

greets
derRaphael
_________________
Back to top
View user's profile Send private message
SoggyDog



Joined: 02 May 2006
Posts: 214
Location: Greeley, CO

PostPosted: Wed Jan 23, 2008 6:29 am    Post subject: Reply with quote

What took you so long? Razz
_________________

SoggyDog
Download AutoHotKey Wallpaper
Does 'Fuzzy Logic' tickle?
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
skwire



Joined: 18 Jan 2006
Posts: 132
Location: Conway, Arkansas

PostPosted: Wed Jan 23, 2008 11:47 am    Post subject: Reply with quote

Very impressive, jaco0646.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Thrillski



Joined: 18 Jul 2007
Posts: 58
Location: South Florida

PostPosted: Thu Jan 24, 2008 2:00 am    Post subject: How to beat level 4. Reply with quote

Good morning!

I was able to duplicate how to beat level 4. This assumes you go first obviously.

X = 6; O = 5
X = 1; O = 7
X = 3; O = 2
X = 9 and you win...

Hope this helps w/ the debugging routine.

Have a nice day!
Thrillski
Back to top
View user's profile Send private message
jaco0646



Joined: 07 Oct 2006
Posts: 556
Location: MN, USA

PostPosted: Sat Jan 26, 2008 11:04 pm    Post subject: update Reply with quote

I've updated level 4 to fix the previously mentioned problem. Thanks to Thrillski for the input. It turned out that wasn't the only way to beat it either... so much for my claim of being unbeatable. Embarassed

I read through the Wikipedia article more thoroughly, so if their Strategy section is accurate (probably), and my coding is accurate (possibly), level 4 should now play "perfectly." And by that I mean please keep posting any bugs you find, in particular the level 2 bug that misses a block.

I've left a couple of message boxes in the code (commented out of course) that I used to help debug while I was writing. They should help clarify what the program is doing.

The variables labeled Win1-8 are the 8 possible ways to win a game of tic-tac-toe, along with their permutations. These are used to determine if the game has been won.

The variables labeled Block1-9 represent the 9 positions on the board, and the permutations that would require a block on each square to prevent a win. These are also used in level 4 to calculate potential "forks" and the ways to block them.

The original download link has been updated with the changes.
ENJOY!
_________________
http://autohotkey.net/~jaco0646/
Back to top
View user's profile Send private message Visit poster's website
Thrillski



Joined: 18 Jul 2007
Posts: 58
Location: South Florida

PostPosted: Sun Jan 27, 2008 7:57 pm    Post subject: Quick report on 1.1 Reply with quote

Good morning!

I briefly tried 1.1 and can definitely see the new logic taking over and being much more difficult, if not impossible, to beat.

That is where number players = 0 needs to be done. If two level 4 players play 10,000 games, we should get 10,000 ties.

I am assuming that this option is next on the list. Once that is done, I'll post another "challenge".

Congrats on a nice program.
Have a nice day!
Thrillski
Back to top
View user's profile Send private message
jaco0646



Joined: 07 Oct 2006
Posts: 556
Location: MN, USA

PostPosted: Mon Jan 28, 2008 5:17 am    Post subject: v2.0 Reply with quote

Ok, the computer can now play against itself. Note that the Escape key exits the program in case you decide to quit in mid-game. In level 4 it produces all ties, which should mean the code is working correctly there.

I plan on going through the code one last time and cleaning it up a little; but I'm posting it now because again I probably won't get to that until next weekend. No new features are planned, but please report any bugs; I haven't tested it as thoroughly this time.

The original download link has been updated with the changes.
ENJOY!
_________________
http://autohotkey.net/~jaco0646/
Back to top
View user's profile Send private message Visit poster's website
BoBoĻ
Guest





PostPosted: Mon Jan 28, 2008 11:01 am    Post subject: Reply with quote

Quote:
Ok, the computer can now play against itself
Shocked If I follow a Hollywood stereotype this is the perfect way to set my PC on fire because he/she went nuts in a endless loop, right? Laughing
Back to top
Thrillski



Joined: 18 Jul 2007
Posts: 58
Location: South Florida

PostPosted: Mon Jan 28, 2008 10:22 pm    Post subject: More comments Reply with quote

Actually BoBo, the computer should learn that Tic-Tac-Toe can not win and apply such logic to global thermonuclear war. (c:

jaco:

I've not found any in the little testing I have done. I do find it interesting that level 2 produces as many wins as it does.

Minor tweaks:
[1] The formating of the total wins goes off screen if the number of games played gets large. 10,000 games is easily doable, even if it is overkill.
[2] A way to get number players = 0 to be on different levels. I would like to see level 3 vs level 2.
Back to top
View user's profile Send private message
jaco0646



Joined: 07 Oct 2006
Posts: 556
Location: MN, USA

PostPosted: Sun Feb 03, 2008 3:32 am    Post subject: Reply with quote

The program has been updated one more time. Barring any glaring new bugs, this will be the final release.

Rather than setting the score to a smaller font (to keep it visible when the score is really high), the script now sends the score to the clipboard whenever you clear it. It can then be pasted anywhere to view/save.

The computer can now play games against itself using different levels for the opponents.

The original download link has been updated with the changes.
ENJOY!
_________________
http://autohotkey.net/~jaco0646/
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group