# Helpmeplz

• 10-21-2002
Neoground1
Helpmeplz
Hey guys there is some code that i need to finish my program, well at least the easy way, but I dont understand it. Im writting a tic tac toe program where the computer must never be beaten. This code is supposed to find how many x's there are in a row. But I dont know how to impliment it. Can any one help. Here it is.

Code:

`row(i) = (i-1)/3+1`
• 10-22-2002
master5001
I'd have to see a little more code. For example, you could be using bits to hold the information or you could be using an array (or a 2d array). Perfect AI on a game like tic-tac-toe is impossible.
• 10-22-2002
Neoground1
Code:

```#include <iostream> #include <string> using namespace std; bool endgame=false; int rungame(); char board[9]={0,1,2,3,4,5,6,7,8}; void printboard();         int main()         {                 char x;                 while (!endgame)                 {                         cout << "Welcome. Would you like to play a game? " << endl;                         cout << "Press Y for yes and N for no. Followed by enter" << endl;                         cin >> x;                         if (x=='y')                                 rungame();                         else                         if (x=='n')                                 endgame=true;                 }                 return 0;         }         int rungame()         {                 int move, x;                 board[0]=' ';                 board[1]=' ';                 board[2]=' ';                 board[3]=' ';                 board[4]=' ';                 board[5]=' ';                 board[6]=' ';                 board[7]=' ';                 board[8]=' ';                 for(x=1; x<=9; x++)                 {                         printboard();                         if ((x==1)||(x==3)||(x==5)||(x==7) || (x==9))                         {                                 cout << "Please make a move between 1 and 9. Followed by enter." << endl;                                 int move;                                 cin >> move;                                 if ((move==1)&&(board[0]!='x')&&(board[0]!='o'))                                 {                                         board[0]='x';                                         cout << board[0] << " takes square 1" << endl;                                                                 }else                                 if ((move==2)&&(board[1]!='x')&&(board[1]!='o'))                                 {                                         board[1]='x';                                         cout << board[1] << " takes square 2" << endl;                                                                 }else                                 if ((move==3)&&(board[2]!='x')&&(board[2]!='o'))                                 {                                         board[2]='x';                                         cout << board[2] << " takes square 3" << endl;                                                         }else                                 if ((move==4)&&(board[3]!='x')&&(board[3]!='o'))                                 {                                         board[3]='x';                                         cout << board[3] << " takes square 4" << endl;                                         }else                                 if ((move==5)&&(board[4]!='x')&&(board[4]!='o'))                                 {                                         board[4]='x';                                         cout << board[4] << " takes square 5" << endl;                                 }else                                 if ((move==6)&&(board[5]!='x')&&(board[5]!='o'))                                 {                                         board[5]='x';                                         cout << board[5] << " takes square 6" << endl;                                                                 }else                                 if ((move==7)&&(board[6]!='x')&&(board[6]!='o'))                                 {                                         board[6]='x';                                         cout << board[6] << " takes square 7" << endl;                                                 }else                                 if ((move==8)&&(board[7]!='x')&&(board[7]!='o'))                                 {                                         board[7]='x';                                         cout << board[7] << " takes square 8" << endl;                                 }else                                 if ((move==9)&&(board[8]!='x')&&(board[8]!='o'))                                 {                                         board[8]='x';                                         cout << board[8] << " takes square 9" << endl;                                                         }                                 if (move>10)                                         cout << "Illegal move!" << endl;                                 }else                                         if ((x==2) || (x==4) || (x==6) || (x==8))                                         {                                                 //Computers first move                                                 if (x==2)                                                 {                                                         if ((board[4]!='x')&&(board[4]!='o'))                                                         {                                                                 board[4]='o';                                                         }else                                                                 if ((board[0]!='x')&&(board[0]!='o'))                                                                 {                                                                         board[0]='o';                                                                 }                                                 }                                                 //Block x                                         }                         }                         cout << "CAT" << endl;                         return 0;         }         void printboard()         {                 cout << board[0] << "|" << board[1] << "|" << board[2] << endl;                 cout << board[3] << "|" << board[4] << "|" << board[5] << endl;                 cout << board[6] << "|" << board[7] << "|" << board[8] << endl;         }```
There is my source. Im not trying to make a perfect tic tac toe AI, it juz can't loose.
• 10-22-2002
Neoground1
Basically im trying to write a algorithm that will check all the differant squares for an 'x'. If there are 2 x's in a row, colum, or dialgnal then move to the square to block it. Thats all i need. After that I know how to program the win. Any ideas?
• 10-22-2002
ammar
You can store the moves in a double subscripted array and check for certain patterns, and that's it, the only you have to do is to decide what patterns to look for.
• 10-22-2002
pianorain
Quote:

Originally posted by master5001
<snip>Perfect AI on a game like tic-tac-toe is impossible.
Wow...that's saying quite a bit. I guess it'd depend on what you call 'perfect AI', but I'd sure hesitate a lot before I said it was impossible.
• 10-22-2002
JasonLikesJava
I'm curious too as to what you mean by "Perfect AI"....
• 10-22-2002
salvelinus
The only way I know to do it is to just check each possible combination. There are eight ways to win. I suppose you could make an algorithm by checking each perimeter square and checking it's two immediate neighbors, one horizontal, one vertical, and only check the third if a match is made in the first check, and add a similar diagonal check at the corner, but that would be more work. Might be useful on a larger grid board, 10 x 10 or so.
• 10-22-2002
Captain Penguin
I can see it being impossible to create an AI that always WINS - but I think it would be entirely possible to create one that always ties with the human player.

Tic-tac-toe is the sort of game where if both player pays attention and isn't a moron, the game will ALWAYS end up a tie.

Thats why its sort of a dumb game to play once you're older than 10... but if you're bored, hey who cares, right? ;)
• 10-22-2002
pianorain
I agree...this was an assignment I had for one of my classes. Programming it so that the computer never loses isn't too hard; I doubt you could program it so that the computer always wins. Maybe that's the "perfect AI"?
• 10-22-2002
Azuth
That would have made an interesting end to 'War Games'.
"Sir, the computer just launched a full first strike package at the Russians!"
"My god, but why?"
"Well, the kid was playing Tic-Tac-Toe, trying to prove that with two smart opponents, in some games, there can be no winnerr; the computer decided in order for there to be a winner, one player has to not finish."
• 10-22-2002
moi
Quote:

Originally posted by master5001
Perfect AI on a game like tic-tac-toe is impossible.
hahaha.

try working it out a little: its not to difficult to create a tic tac toe AI that will never lose.