Tic Tac Toe again.

This is a discussion on Tic Tac Toe again. within the Game Programming forums, part of the General Programming Boards category; I made a SOMEWHAT smart tic tac toe that uses a board class......enjoy ! Code: #include <iostream> using namespace std; ...

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    Tic Tac Toe again.

    I made a SOMEWHAT smart tic tac toe that uses a board class......enjoy !


    Code:
    #include <iostream>
    using namespace std;
    
    class board {
    	protected:
    		char board[3][3];
    
    	public:
    		board() {
    			for(int i = 0; i < 3; i++) {
    				for(int j = 0; j < 3; j++) {
    					board[i][j] = ' ';
    				}
    			}
    		}
    
    		void play() {
    			char win;
    			do {
    				display();
    				playerMove();
    				win = checkWinner();
    				if(win != ' ') {
    					break;
    				}
    				computerMove();
    				win = checkWinner();
    			} while(win == ' ');
    			if(win == 'X') {
    				cout << "\nYOU WIN!\n\n";
    			}
    			if(win == 'O'){
    				cout << "\nTHE COMPUTER WINS!\n\n";
    			}
    			if(win == 'D') {
    				cout << "\nCAT'S GAME!\n\n";
    			}
    			display();
    		}
    
    		void display() {
    			cout << "     X     \n";
    			cout << " 0 | 1 | 2\n\n";
    			for(int i = 0; i < 3; i++) {
    				cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << " ";
    				cout << "  " << i;
    				if(i == 1) {
    					cout << " Y ";
    				}
    				if(i != 2) {
    					cout << "\n---|---|---  -\n";
    				}
    			}
    		}
    
    		void playerMove() {
    			int x, y;
    			cout << "\nEnter x coordinate: ";
    			cin >> x;
    			cout << "\nEnter y coordinate: ";
    			cin >> y;
    			if(board[y][x] != ' ') {
    				cout << "Incorrect coordinate. Please enter again.\n";
    				playerMove();
    			} else {
    				board[y][x] = 'X';
    			}
    		}
    
    		void computerMove() {
    			if((board[0][0] == board[1][1]) && (board[2][2] == ' ') && (board[0][0] != ' ') && (board[1][1] != ' ')) {
    				board[2][2] = 'O';
    				return;
    			}
    			if((board[1][1] == board[2][2]) && (board[0][0] == ' ') && (board[1][1] != ' ') && (board[2][2] != ' ')) {
    				board[0][0] = 'O';
    				return;
    			}
    			if((board[0][0] == board[2][2]) && (board[1][1] == ' ') && (board[0][0] != ' ') && (board[2][2] != ' ')) {
    				board[1][1] = 'O';
    				return;
    			}
    			if((board[0][2] == board[1][1]) && (board[2][0] == ' ') && (board[0][2] != ' ') && (board[1][1] != ' ')) {
    				board[2][0] = 'O';
    				return;
    			}
    			if((board[2][0] == board[1][1]) && (board[0][2] == ' ') && (board[2][0] != ' ') && (board[1][1] != ' ')) {
    				board[0][2] = 'O';
    				return;
    			}
    			if((board[2][0] == board[0][2]) && (board[1][1] == ' ') && (board[2][0] != ' ') && (board[0][2] != ' ')) {
    				board[1][1] = 'O';
    				return;
    			}
    			if((board[0][0] == board[1][0]) && (board[2][0] == ' ') && (board[0][0] != ' ') && (board[1][0] != ' ')) {
    				board[2][0] = 'O';
    				return;
    			}
    			if((board[0][1] == board[1][1]) && (board[2][1] == ' ') && (board[0][1] != ' ') && (board[1][1] != ' ')) {
    				board[2][1] = 'O';
    				return;
    			}
    			if((board[0][2] == board[1][2]) && (board[2][1] == ' ') && (board[0][2] != ' ') && (board[1][2] != ' ')) {
    				board[2][1] = 'O';
    				return;
    			}
    			if((board[0][0] == board[0][1]) && (board[0][2] == ' ') && (board[0][0] != ' ') && (board[0][1] != ' ')) {
    				board[0][2] = 'O';
    				return;
    			}
    			if((board[1][0] == board[1][1]) && (board[1][2] == ' ') && (board[1][0] != ' ') && (board[1][1] != ' ')) {
    				board[1][2] = 'O';
    				return;
    			}
    			if((board[2][0] == board[2][1]) && (board[2][2] == ' ') && (board[2][0] != ' ') && (board[2][1] != ' ')) {
    				board[2][2] = 'O';
    				return;
    			}
    			for(int i = 0; i < 3; i++) {
    				for(int j = 0; j < 3; j++) {
    					if(board[i][j] == ' ') {
    						board[i][j] = 'O';
    						return;
    					}
    				}
    			}
    		}
    
    		char checkWinner() {
    			for(int i = 0; i < 3; i++) {
    				if((board[i][0] == board[i][1]) && (board[i][1] == board[i][2])) {
    					return board[i][0];
    				}
    				if((board[0][i] == board[1][i]) && (board[1][i] == board[2][i])) {
    					return board[0][i];
    				}
    			}
    			if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) {
    				return board[0][0];
    			}
    			if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) {
    				return board[0][2];
    			}
    			unsigned int a = 0;
    			for(int i = 0; i < 3; i++) {
    				for(int j = 0; j < 3; j++) {
    					if(board[i][j] == ' ') {
    						break;
    					} else {
    						a++;
    					}
    				}
    			}
    			if(a == 9) {
    				return 'D';
    			}
    			return ' ';
    		}
    };
    
    int main() {
    	char ch;
    	do {
    		board b;
    		b.play();
    		cout << "\n\nDo you wish to play again? (Y/N):";
    		cin >> ch;
    	} while(ch == 'Y');
    	cout << "\n\nThanks for playing!!!";
    	return 0;
    }
    Do not make direct eye contact with me.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Got 3 errors under MSVC:

    Code:
    error C2380: type(s) preceding 'board' (constructor with return type, or illegal redefinition of current class-name?)
    error C2059: syntax error : '['
    error C2238: unexpected token(s) preceding ';'
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You should probably zip the code and attach it. It's easier that way.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    its bishing about the name of the class and the name of the "char board" in protected

    rename
    class board to something else

    (i changed class board to class cboard, changed the name of the constructor, and changed the name in main) and its down to 1 error now that i cant look at, cause someone just called me to fix their computer

    the other error is easy, redef of i or something

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    i fixed it

    Code:
    #include <iostream>
    using namespace std;
    
    class board_c {
    	protected:
    		char board[3][3];
    
    	public:
    		board_c() {
    			for(int i = 0; i < 3; i++) {
    				for(int j = 0; j < 3; j++) {
    					board[i][j] = ' ';
    				}
    			}
    		}
    
    		void play() {
    			char win;
    			do {
    				display();
    				playerMove();
    				win = checkWinner();
    				if(win != ' ') {
    					break;
    				}
    				computerMove();
    				win = checkWinner();
    			} while(win == ' ');
    			if(win == 'X') {
    				cout << "\nYOU WIN!\n\n";
    			}
    			if(win == 'O'){
    				cout << "\nTHE COMPUTER WINS!\n\n";
    			}
    			if(win == 'D') {
    				cout << "\nCAT'S GAME!\n\n";
    			}
    			display();
    		}
    
    		void display() {
    			cout << "     X     \n";
    			cout << " 0 | 1 | 2\n\n";
    			for(int i = 0; i < 3; i++) {
    				cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << " ";
    				cout << "  " << i;
    				if(i == 1) {
    					cout << " Y ";
    				}
    				if(i != 2) {
    					cout << "\n---|---|---  -\n";
    				}
    			}
    		}
    
    		void playerMove() {
    			int x, y;
    			cout << "\nEnter x coordinate: ";
    			cin >> x;
    			cout << "\nEnter y coordinate: ";
    			cin >> y;
    			if(board[y][x] != ' ') {
    				cout << "Incorrect coordinate. Please enter again.\n";
    				playerMove();
    			} else {
    				board[y][x] = 'X';
    			}
    		}
    
    		void computerMove() {
    			if((board[0][0] == board[1][1]) && (board[2][2] == ' ') && (board[0][0] != ' ') && (board[1][1] != ' ')) {
    				board[2][2] = 'O';
    				return;
    			}
    			if((board[1][1] == board[2][2]) && (board[0][0] == ' ') && (board[1][1] != ' ') && (board[2][2] != ' ')) {
    				board[0][0] = 'O';
    				return;
    			}
    			if((board[0][0] == board[2][2]) && (board[1][1] == ' ') && (board[0][0] != ' ') && (board[2][2] != ' ')) {
    				board[1][1] = 'O';
    				return;
    			}
    			if((board[0][2] == board[1][1]) && (board[2][0] == ' ') && (board[0][2] != ' ') && (board[1][1] != ' ')) {
    				board[2][0] = 'O';
    				return;
    			}
    			if((board[2][0] == board[1][1]) && (board[0][2] == ' ') && (board[2][0] != ' ') && (board[1][1] != ' ')) {
    				board[0][2] = 'O';
    				return;
    			}
    			if((board[2][0] == board[0][2]) && (board[1][1] == ' ') && (board[2][0] != ' ') && (board[0][2] != ' ')) {
    				board[1][1] = 'O';
    				return;
    			}
    			if((board[0][0] == board[1][0]) && (board[2][0] == ' ') && (board[0][0] != ' ') && (board[1][0] != ' ')) {
    				board[2][0] = 'O';
    				return;
    			}
    			if((board[0][1] == board[1][1]) && (board[2][1] == ' ') && (board[0][1] != ' ') && (board[1][1] != ' ')) {
    				board[2][1] = 'O';
    				return;
    			}
    			if((board[0][2] == board[1][2]) && (board[2][1] == ' ') && (board[0][2] != ' ') && (board[1][2] != ' ')) {
    				board[2][1] = 'O';
    				return;
    			}
    			if((board[0][0] == board[0][1]) && (board[0][2] == ' ') && (board[0][0] != ' ') && (board[0][1] != ' ')) {
    				board[0][2] = 'O';
    				return;
    			}
    			if((board[1][0] == board[1][1]) && (board[1][2] == ' ') && (board[1][0] != ' ') && (board[1][1] != ' ')) {
    				board[1][2] = 'O';
    				return;
    			}
    			if((board[2][0] == board[2][1]) && (board[2][2] == ' ') && (board[2][0] != ' ') && (board[2][1] != ' ')) {
    				board[2][2] = 'O';
    				return;
    			}
    			for(int i = 0; i < 3; i++) {
    				for(int j = 0; j < 3; j++) {
    					if(board[i][j] == ' ') {
    						board[i][j] = 'O';
    						return;
    					}
    				}
    			}
    		}
    
    		char checkWinner() {
    			for(int i = 0; i < 3; i++) {
    				if((board[i][0] == board[i][1]) && (board[i][1] == board[i][2])) {
    					return board[i][0];
    				}
    				if((board[0][i] == board[1][i]) && (board[1][i] == board[2][i])) {
    					return board[0][i];
    				}
    			}
    			if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) {
    				return board[0][0];
    			}
    			if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) {
    				return board[0][2];
    			}
    			unsigned int a = 0;
    			for(int _i = 0; _i < 3; i++) {
    				for(int j = 0; j < 3; j++) {
    					if(board[i][j] == ' ') {
    						break;
    					} else {
    						a++;
    					}
    				}
    			}
    			if(a == 9) {
    				return 'D';
    			}
    			return ' ';
    		}
    };
    
    int main() {
    	char ch;
    	do {
    		board_c b;
    		b.play();
    		cout << "\n\nDo you wish to play again? (Y/N):";
    		cin >> ch;
    	} while(ch == 'Y');
    	cout << "\n\nThanks for playing!!!";
    	return 0;
    }

  6. #6
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Interesting - i never got any errors like those (using Borland 5.5 command line). All i got were warnings about "so and so" functions should be inline.
    Do not make direct eye contact with me.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    not dissin ur game at all
    just compiler issues
    just wanna make sure that u dont think im putting urs down.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    All i got were warnings
    Some warnings on one compiler can be errors on others, that's why you should make sure you have no warnings; don't just ignore them.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  9. #9
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by revelation437
    not dissin ur game at all
    just compiler issues
    just wanna make sure that u dont think im putting urs down.
    Didnt think that at all .
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 04-24-2002, 03:24 AM
  5. my tic tac toe game, please try it
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21