View Single Post
  #3 (permalink)  
Old 06-04-2007, 06:06 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,433
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

Ok, I've taken a look at your code and played with it for a little bit. Here is what I've come up with for the AI.

The AI code will be placed in the moveComputer() method/function. What you want to do is check every possibility. When the computer checks, it wants to check these in order: Offensive moves, Defensive moves, Random move. An example of an offensive move (assuming the computer is O) would be
Code:
if(board[0] == 'O' && board[1] == 'O' && board[0] != " ") {
board[2] == 'O';
}
It checks to see if there are two O's in places 0 and 1, if there are, the computer will place an O in place 2 and win the game. There are 24 offensive "moves" all which are shown in my other tutorial. A defensive move would be similar:
Code:
else if(board[0] == 'X' && board[1] == 'X' && board[0] != " ") {
board[2] == 'O';
}
The computer would obviously want to win the game before he blocked you from winning the game so all offensive moves need to be placed before the defensive moves or it wouldn't make much sense [unless you want easy/hard modes]

And in the event there are no win possibilities [2 X's or O's in a row] for either player, the computer would then pick a random place to go [I'm sure there is a function in C++ to compute a random number between 0 and 8 inclusive] and of course you would have to make sure that the random number generated == ' ' and not 'X' or 'O'

Code:
else {
//random placement
}
//drawboard
//x's turn
As for the tie, I don't see any bug?

Last edited by John; 06-04-2007 at 06:27 PM.
Reply With Quote