/* Tic-Tac-Toe This program will allow 2 players to play a game of tic-tac-toe. Input: Interactive keyboard input from the players. Output: The tic-tac-toe board */ #include <iostream> #include <iomanip> using namespace std; const int SIZE = 3; //size of tic-tac-toe board array const int SIZE2 = 2; //size of tic-tac-toe piece array void display_board(const char[][SIZE]); //displays board void get_location(int&, int&,const int, const char[]); //gets location from player bool check_valid(const char[][SIZE], int, int); //checks for valid location bool check_win(const char[][SIZE],const int, const char[]); //checks if players wins bool check_row(const char[][SIZE],const int, const char[]); //checks for win on each row bool check_column(const char[][SIZE],const int, const char[]); //checks for win on each column bool check_diagonal(const char[][SIZE],const int, const char[]); //checks for on both diaganols int main() { char board[SIZE][SIZE] = {' '}, //tic-tac-toe board mark[SIZE2] = {'X', 'O'}; //both tic-tac-toe pieces int count = 0, //move counter r, //row c, //column p; //player bool valid, //valid move variable winner = false; //winner variable while(!winner && count < 9) { for(p = 1; p < SIZE; p++) { if(count < 9) { count++; display_board(board); get_location(r, c, p, mark); valid = check_valid(board, r, c); while(!valid) { cout << "Sorry, please choose another row and column." << endl << "Row(1-3): "; cin >> r; cout << endl << "Column(1-3): "; cin >> c; valid = check_valid(board, r, c); } board[r - 1][c - 1] = mark[p - 1]; winner = check_win(board, p, mark); if(winner) { display_board(board); break; } } } } if(winner) { cout << "Winner is Player " << p << "!" << endl << endl; } else if(count == 9) { display_board(board); cout << "Players Tied!" << endl << endl; } system("PAUSE"); return 0; } //***************************************************************************** //function display_board //parameters: board(char array) //returns: void //***************************************************************************** void display_board(const char board[SIZE][SIZE]) { cout << endl << " 1 2 3" << endl <<endl << " 1 " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl << " ---|---|---" << endl << " 2 " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl << " ---|---|---" << endl << " 3 " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl << endl; } //***************************************************************************** //function get_location //parameters: row(int), column(int), player(int), and mark(char array) //returns: row(int) and column(int) //***************************************************************************** void get_location(int& r, int& c,const int p, const char mark[SIZE2]) { cout << "Player " << p << " (" << mark[p - 1] << ") choose the row " << "and column of your next move." << endl << "Row(1-3): "; cin >> r; cout << endl << "Column(1-3): "; cin >> c; } //***************************************************************************** //function check_valid //parameters: board(char array), row(int), and column(int) //returns: bool(true or false) //***************************************************************************** bool check_valid(const char board[SIZE][SIZE], int r, int c) { if(r >= 1 && r <= SIZE && c >= 1 && c <= SIZE && board[r - 1][c - 1] != 'X' && board[r - 1][c - 1] != 'O') { return true; } else { return false; } } //***************************************************************************** //function check_win //parameters: board(char array), player(int), and mark(char array) //returns: bool(true or false) //***************************************************************************** bool check_win(const char board[SIZE][SIZE],const int p, const char mark[SIZE2]) { bool win; //local variable win = check_row(board, p, mark); if(win) { return true; } win = check_column(board, p, mark); if(win) { return true; } win = check_diagonal(board, p, mark); if(win) { return true; } return false; } //***************************************************************************** //function check_row //parameters: board(char array), player(int), and mark(char array) //returns: bool(true or false) //***************************************************************************** bool check_row(const char board[SIZE][SIZE],const int p, const char mark[SIZE2]) { for(int r = 0; r < SIZE; r++) { if(board[r][0] == mark[p - 1] && board[r][1] == mark[p - 1] && board[r][2] == mark[p - 1]) { return true; } else { return false; } } } //***************************************************************************** //function check_column //parameters: board(char array), player(int), and mark(char array) //returns: bool(true or false) //***************************************************************************** bool check_column(const char board[SIZE][SIZE],const int p, const char mark[SIZE2]) { for(int c = 0; c < SIZE; c++) { if(board[0][c] == mark[p - 1] && board[1][c] == mark[p - 1] && board[2][c] == mark[p - 1]) { return true; } else { return false; } } } //***************************************************************************** //function check_diagonal //parameters: board(char array), player(int), and mark(char array) //returns: bool(true or false) //***************************************************************************** bool check_diagonal(const char board[SIZE][SIZE],const int p, const char mark[SIZE2]) { if(board[0][0] == mark[p - 1] && board[1][1] == mark[p - 1] && board[2][2] == mark[p - 1]) { return true; } else if (board[0][2] == mark[p - 1] && board[1][1] == mark[p - 1] && board[2][0] == mark[p - 1]) { return true; } else { return false; } }
char board[SIZE][SIZE] = {' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' '},
Ok, I'm writing code for Tic-tac-toe game and I'm almost done I just can't get it to stop when Player 2 wins. It works fine when Player 1 wins or if there is a tie. I know it's short notice, but can anyone help me before tomorrow morning. I would really appreciate any help at all. Here is what I've got so far:
C++ Syntax (Toggle Plain Text)
/* Tic-Tac-Toe This program will allow 2 players to play a game of tic-tac-toe. Input: Interactive keyboard input from the players. Output: The tic-tac-toe board */ #include <iostream> #include <iomanip> using namespace std; const int SIZE = 3; //size of tic-tac-toe board array const int SIZE2 = 2; //size of tic-tac-toe piece array void display_board(const char[][SIZE]); //displays board void get_location(int&, int&,const int, const char[]); //gets location from player bool check_valid(const char[][SIZE], int, int); //checks for valid location bool check_win(const char[][SIZE],const int, const char[]); //checks if players wins bool check_row(const char[][SIZE],const int, const char[]); //checks for win on each row bool check_column(const char[][SIZE],const int, const char[]); //checks for win on each column bool check_diagonal(const char[][SIZE],const int, const char[]); //checks for on both diaganols int main() { char board[SIZE][SIZE] = {' '}, //tic-tac-toe board mark[SIZE2] = {'X', 'O'}; //both tic-tac-toe pieces int count = 0, //move counter r, //row c, //column p; //player bool valid, //valid move variable winner = false; //winner variable while(!winner && count < 9) { for(p = 1; p < SIZE; p++) { if(count < 9) { count++; display_board(board); get_location(r, c, p, mark); valid = check_valid(board, r, c); while(!valid) { cout << "Sorry, please choose another row and column." << endl << "Row(1-3): "; cin >> r; cout << endl << "Column(1-3): "; cin >> c; valid = check_valid(board, r, c); } board[r - 1][c - 1] = mark[p - 1]; winner = check_win(board, p, mark); if(winner) { display_board(board); break; } } } } if(winner) { cout << "Winner is Player " << p << "!" << endl << endl; } else if(count == 9) { display_board(board); cout << "Players Tied!" << endl << endl; } system("PAUSE"); return 0; } //***************************************************************************** //function display_board //parameters: board(char array) //returns: void //***************************************************************************** void display_board(const char board[SIZE][SIZE]) { cout << endl << " 1 2 3" << endl <<endl << " 1 " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl << " ---|---|---" << endl << " 2 " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl << " ---|---|---" << endl << " 3 " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl << endl; } //***************************************************************************** //function get_location //parameters: row(int), column(int), player(int), and mark(char array) //returns: row(int) and column(int) //***************************************************************************** void get_location(int& r, int& c,const int p, const char mark[SIZE2]) { cout << "Player " << p << " (" << mark[p - 1] << ") choose the row " << "and column of your next move." << endl << "Row(1-3): "; cin >> r; cout << endl << "Column(1-3): "; cin >> c; } //***************************************************************************** //function check_valid //parameters: board(char array), row(int), and column(int) //returns: bool(true or false) //***************************************************************************** bool check_valid(const char board[SIZE][SIZE], int r, int c) { if(r >= 1 && r <= SIZE && c >= 1 && c <= SIZE && board[r - 1][c - 1] != 'X' && board[r - 1][c - 1] != 'O') { return true; } else { return false; } } //***************************************************************************** //function check_win //parameters: board(char array), player(int), and mark(char array) //returns: bool(true or false) //***************************************************************************** bool check_win(const char board[SIZE][SIZE],const int p, const char mark[SIZE2]) { bool win; //local variable win = check_row(board, p, mark); if(win) { return true; } win = check_column(board, p, mark); if(win) { return true; } win = check_diagonal(board, p, mark); if(win) { return true; } return false; } //***************************************************************************** //function check_row //parameters: board(char array), player(int), and mark(char array) //returns: bool(true or false) //***************************************************************************** bool check_row(const char board[SIZE][SIZE],const int p, const char mark[SIZE2]) { for(int r = 0; r < SIZE; r++) { if(board[r][0] == mark[p - 1] && board[r][1] == mark[p - 1] && board[r][2] == mark[p - 1]) { return true; } else { return false; } } } //***************************************************************************** //function check_column //parameters: board(char array), player(int), and mark(char array) //returns: bool(true or false) //***************************************************************************** bool check_column(const char board[SIZE][SIZE],const int p, const char mark[SIZE2]) { for(int c = 0; c < SIZE; c++) { if(board[0][c] == mark[p - 1] && board[1][c] == mark[p - 1] && board[2][c] == mark[p - 1]) { return true; } else { return false; } } } //***************************************************************************** //function check_diagonal //parameters: board(char array), player(int), and mark(char array) //returns: bool(true or false) //***************************************************************************** bool check_diagonal(const char board[SIZE][SIZE],const int p, const char mark[SIZE2]) { if(board[0][0] == mark[p - 1] && board[1][1] == mark[p - 1] && board[2][2] == mark[p - 1]) { return true; } else if (board[0][2] == mark[p - 1] && board[1][1] == mark[p - 1] && board[2][0] == mark[p - 1]) { return true; } else { return false; } }
I tried your code, and it works fine for me.
Edit: it didn't stop when I had won...lol =/
Do you know how I can insert images, and maybe get it on fullscreen?
| DaniWeb Message | |
| Cancel Changes | |