# tic toe game... probably a little mistake

• 11-28-2009
jamort
tic toe game... probably a little mistake
ok so the idea behind this was to make it for two players for one-on-one competition and eventually start working on AI for it...
so with the code thats here it has the ability to determine if somebody wins, it also has the ability to figure out what to display(X, O or -)... The only problem is that when I run it right now it only displays two rows and I'm not positive why its doing it but evidentaly something in the code is not right;
in the switch/cases of play() is where it is decided what it needs to display...
help appreciated,
CJ

Code:

```#include <iostream> using namespace std; class game{     int board[3][3];     int row1, row2, row3;     int col1, col2, col3;     int dig1, dig2;     bool winner;     string player1, player2;     public:     game(){         board[0][0] = 0;//makes all variables of the board array 0         board[0][1] = 0;         board[0][2] = 0;         board[1][0] = 0;         board[1][1] = 0;         board[1][2] = 0;         board[2][0] = 0;         board[2][1] = 0;         board[2][2] = 0;         winner = false; //makes winner false         player1 = "X";//player 1 is X         player2 = "O";//player 2 is O     }     int row(){         row1 = board[0][0] + board[0][1] + board[0][2]; //adds all of the rows up         row2 = board[1][0] + board[1][1] + board[1][2];         row3 = board[2][0] + board[2][1] + board[2][2];         if (row1 || row2 || row3 >= 3){        //checks to see if any rows are greater than or = to 3             if(board[0][0] == board[0][1] == board[0][2]){ //if so it checks to see if anybody won by comparing values of the variables in the row                 winner = true;             }             if(board[0][0] == board[0][1] == board[0][2]){             winner = true;             }             if(board[2][0] == board[2][1] == board[2][2]){             winner = true;             }         }     }     int col(){         col1 = board[0][0] + board[1][0] + board[2][0];//adds all columns up         col2 = board[0][1] + board[1][1] + board[2][1];         col3 = board[0][2] + board[1][2] + board[2][2];         if (col1 || col2 || col3 >= 3){ //checks to see if any colum is greater or = to 3             if (board[0][0] == board[1][0] == board[2][0]){             winner = true;             }             if (board[0][1] == board[1][1] == board[2][1]){             winner = true;             }             if (board[0][2] == board[1][2] == board[2][2]){             winner = true;             }         }     }     int dig(){         dig1 = board[0][0] + board[1][1] + board[2][2];//adds the diagonals up         dig2 = board[0][2] + board[1][1] +board[2][0];         if (dig1 || dig2 >= 3){ // checks to see if any of the diagonals values is greater than 3             if (dig1 >= 3){                 if (board[0][0] == board[1][1] == board[2][2]){                 winner = true;                 }             }             if (dig2 >= 3){                 if (board[2][0] == board[2][1] == board[0][2]){                 winner = true;                 }             }         }     }     int disp(){     switch(board[0][0]){ // looks to see if each value is 0, 1 or 2 for each position on the board to determine what to display in its spot         case 0:             cout<< "- ";             break;         case 1:             cout<< player1 << " ";             break;         case 2:             cout<< player2 << " ";             break;         }     switch(board[0][1]){//same for the rest of the switch/cases         case 0:             cout<< " - ";             break;         case 1:             cout<< player1 << " ";             break;         case 2:             cout<< player2 << " ";             break;         }     switch(board[0][2]){         case 0:             cout<< " -" << endl;             break;         case 1:             cout<< player1 << endl;             break;         case 2:             cout<< player2 << endl;             break;         }     switch(board[1][0]){         case 0:             cout<< "- ";             break;         case 1:             cout<< player1 << " ";             break;         case 2:             cout<< player2 << " ";             break;         }     switch(board[1][1]){         case 0:             cout<< " - ";             break;         case 1:             cout<< player1 << " ";             break;         case 2:             cout<< player2 << " ";             break;         }     switch(board[1][2]){         case 0:             cout<< " -" << endl;             break;         case 1:             cout<< player1 << endl;             break;         case 2:             cout<< player2 << endl;             break;     switch(board[2][0]){         case 0:             cout<< "- ";             break;         case 1:             cout<< player1 << " ";             break;         case 2:             cout<< player2 << " ";             break;         }         switch(board[2][1]){         case 0:             cout<< " - ";             break;         case 1:             cout<< player1 << " ";             break;         case 2:             cout<< player2 << " ";             break;         }         switch(board[2][2]){         case 0:             cout<< " - " << endl;             break;         case 1:             cout<< player1 << endl;             break;         case 2:             cout<< player2 << endl;             break;         }         }     }     int play(){         row();//does row computation         col();//does column computation         dig();//does diagonal computation         disp();     } }; int main(){     game toe;     toe.play(); }```
• 11-29-2009
Daved
Your braces don't match up properly.

Also, don't forget to #include <string> since you use the string class. On my compiler that will give an error, and it might not work right on yours if you leave it out. Also, your functions specify a return value of int, but don't actually return anything. If you don't want them to return something, switch them to be void. The only exception is main(), which always must specify int as the return value, but doesn't actually have to return anything (it returns 0 automatically if you leave it out).

Note that all of these issues can probably be found by looking at your compiler's errors or warnings. Make sure you turn the warning level up and pay attention to what the warnings tell you.
• 11-30-2009
jamort
thanks i finally figured that out... lol umm ive got it to the point that it will work with two players now... and I'm working on AI for it... I know how to do AI but the one thing that i dont know how to go about doing is it checking to see what option would be the best(right now just the small amount of AI I have it will go for one thing say making mine have have two in a row but now block the other man from getting three in a row because the checking for what to add comes first in the sequence... so anyway to make it run through all options and then decide what it wants to do...