| | |
C++ code help!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 5
Reputation:
Solved Threads: 0
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; } }
Consider initalizing your grid properly...
Simply doing one like you did in your code only initalizes the first element in the array.
Anyway, I don't have time right now to find the actual error in your code; perhaps someone else can? It's got to be something in the function which checks whether someone has won yet.
C++ Syntax (Toggle Plain Text)
char board[SIZE][SIZE] = {' ',' ', ' ', ' ',' ', ' ', ' ',' ', ' '},
Anyway, I don't have time right now to find the actual error in your code; perhaps someone else can? It's got to be something in the function which checks whether someone has won yet.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
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; } }
Edit: it didn't stop when I had won...lol =/
Do you know how I can insert images, and maybe get it on fullscreen?
Last edited by Andy-Pandy; Feb 16th, 2007 at 1:08 pm.
You can do some ugly imaging by using API calls to set pixel values. A much better idea for images would be to make the game into a GUI application, however that would require knowledge of the GUI API calls for your system, and a good base understanding of C/C++.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the C++ Forum
- Previous Thread: Please Help Me
- Next Thread: C++ Static Linked Libraries Question
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






