First I'll apologize for any repeat threads. I'm new to using forums and after searching around I don't see any other way. I've done the following tic-tac-toe program for C++ (2 players), and I can't seem to get it to move to the get_move function. Can you see where I messed something up? It's been 6 hours and I'm going code blind. Thanks. (I also don't think I did the "tags" correctly. Sorry.
It initializes the gamefield with "*", then moves in an "X" or "O".
Code:#include <iostream> #include <string> using namespace std; char board[3][3]; //tic tac toe board bool gameover; int player; void init_board(); void disp_board(); void get_move(); void checkwin(); void win(bool); int main() { gameover = false; player = 1; system("color f4"); system("cls"); cout << ("Tic Tac Toe.\n"); cout << ("Player 1 (X) - Player 2 (O)\n\n"); init_board(); disp_board(); while (gameover == false); { get_move(); disp_board(); checkwin(); } return 0; } //Initialize board void init_board(void) { int i, j; for(i=0; i<3; i++) for(j=0; j<3; j++) board[i][j] = '*'; } //Display board void disp_board() { cout << " Coordinates \n"; cout << " 1 2 3 \n"; cout << " | | \n"; cout << " 1 " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl; cout << " _____|_____|_____\n"; cout << " | | \n"; cout << " 2 " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl; cout << " _____|_____|_____\n"; cout << " | | \n"; cout << " 3 " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl; cout << " | | \n\n"; } //Get a player's move void get_move() { int x = 0, y = 0; cout << "Player " << player << ", enter X,Y coordinates for your move: "; cin >> board[x][y]; x--; y--; if(board[x][y]!= '*') { cout << ("Invalid move, try again.\n"); get_move(); } if ((board[0][0]) && (board[0][0] == '*')) { board[0][0] = (player == 1) ? 'X' : 'O'; player = (player == 1) ? 2 : 1; } if ((board[0][1]) && (board[0][1] == '*')) { board[0][1] = (player == 1) ? 'X' : 'O'; player = (player == 1) ? 2 : 1; } if ((board[0][2]) && (board[0][2] == '*')) { board[0][2] = (player == 1) ? 'X' : 'O'; player = (player == 1) ? 2 : 1; } if ((board[1][0]) && (board[1][0] == '*')) { board[1][0] = (player == 1) ? 'X' : 'O'; player = (player == 1) ? 2 : 1; } if ((board[1][1]) && (board[1][1] == '*')) { board[1][1] = (player == 1) ? 'X' : 'O'; player = (player == 1) ? 2 : 1; } if ((board[1][2]) && (board[1][2] == '*')) { board[1][2] = (player == 1) ? 'X' : 'O'; player = (player == 1) ? 2 : 1; } if ((board[2][0]) && (board[2][0] == '*')) { board[2][0] = (player == 1) ? 'X' : 'O'; player = (player == 1) ? 2 : 1; } if ((board[2][1]) && (board[2][1] == '*')) { board[2][1] = (player == 1) ? 'X' : 'O'; player = (player == 1) ? 2 : 1; } if ((board[2][2]) && (board[2][2] == '*')) { board[2][2] = (player == 1) ? 'X' : 'O'; player = (player == 1) ? 2 : 1; } } //Check for winner void checkwin() { int i; //check rows for(i=0; i<3; i++) if(board[i][0]==board[i][1] && board[i][0]==board[i][2]) win(true); //check columns for(i=0; i<3; i++) if(board[0][i]==board[1][i] && board[0][i]==board[2][i]) win(true); //check diagonals if(board[0][0]==board[1][1] && board[1][1]==board[2][2]) win(true); else if (board[0][2]==board[1][1] && board[1][1]==board[2][0]) win(true); //no winner yet else win(false); } void win(bool x) { if (x == true) { player = (player == 1) ? 2 : 1; cout << "Player " << player << " Won!!!" << endl << endl; } else cout << "Tie Game!" << endl << endl; gameover = true; system("pause"); }