943,768 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1369
  • C++ RSS
Feb 15th, 2007
0

C++ code help!!!

Expand Post »
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)
  1. /*
  2. Tic-Tac-Toe
  3.   This program will allow 2 players to play a game of tic-tac-toe.
  4.   Input: Interactive keyboard input from the players.
  5.   Output: The tic-tac-toe board
  6. */
  7. #include <iostream>
  8. #include <iomanip>
  9. using namespace std;
  10. const int SIZE = 3; //size of tic-tac-toe board array
  11. const int SIZE2 = 2; //size of tic-tac-toe piece array
  12. void display_board(const char[][SIZE]); //displays board
  13. void get_location(int&, int&,const int, const char[]);
  14. //gets location from player
  15. bool check_valid(const char[][SIZE], int, int);
  16. //checks for valid location
  17. bool check_win(const char[][SIZE],const int, const char[]);
  18. //checks if players wins
  19. bool check_row(const char[][SIZE],const int, const char[]);
  20. //checks for win on each row
  21. bool check_column(const char[][SIZE],const int, const char[]);
  22. //checks for win on each column
  23. bool check_diagonal(const char[][SIZE],const int, const char[]);
  24. //checks for on both diaganols
  25. int main()
  26. {
  27. char board[SIZE][SIZE] = {' '}, //tic-tac-toe board
  28. mark[SIZE2] = {'X', 'O'}; //both tic-tac-toe pieces
  29. int count = 0, //move counter
  30. r, //row
  31. c, //column
  32. p; //player
  33. bool valid, //valid move variable
  34. winner = false; //winner variable
  35.  
  36. while(!winner && count < 9)
  37. {
  38. for(p = 1; p < SIZE; p++)
  39. {
  40. if(count < 9)
  41. {
  42. count++;
  43. display_board(board);
  44. get_location(r, c, p, mark);
  45. valid = check_valid(board, r, c);
  46. while(!valid)
  47. {
  48. cout << "Sorry, please choose another row and column."
  49. << endl << "Row(1-3): ";
  50. cin >> r;
  51. cout << endl << "Column(1-3): ";
  52. cin >> c;
  53. valid = check_valid(board, r, c);
  54. }
  55.  
  56. board[r - 1][c - 1] = mark[p - 1];
  57. winner = check_win(board, p, mark);
  58. if(winner)
  59. {
  60. display_board(board);
  61. break;
  62. }
  63. }
  64.  
  65. }
  66. }
  67.  
  68. if(winner)
  69. {
  70. cout << "Winner is Player " << p << "!" << endl << endl;
  71. }
  72. else if(count == 9)
  73. {
  74. display_board(board);
  75. cout << "Players Tied!" << endl << endl;
  76. }
  77.  
  78. system("PAUSE");
  79. return 0;
  80. }
  81. //*****************************************************************************
  82. //function display_board
  83. //parameters: board(char array)
  84. //returns: void
  85. //*****************************************************************************
  86. void display_board(const char board[SIZE][SIZE])
  87. {
  88. cout << endl << " 1 2 3" << endl <<endl
  89. << " 1 " << board[0][0] << " | " << board[0][1] << " | "
  90. << board[0][2] << endl
  91. << " ---|---|---" << endl
  92. << " 2 " << board[1][0] << " | " << board[1][1] << " | "
  93. << board[1][2] << endl
  94. << " ---|---|---" << endl
  95. << " 3 " << board[2][0] << " | " << board[2][1] << " | "
  96. << board[2][2] << endl << endl;
  97. }
  98. //*****************************************************************************
  99. //function get_location
  100. //parameters: row(int), column(int), player(int), and mark(char array)
  101. //returns: row(int) and column(int)
  102. //*****************************************************************************
  103. void get_location(int& r, int& c,const int p, const char mark[SIZE2])
  104. {
  105. cout << "Player " << p << " (" << mark[p - 1] << ") choose the row "
  106. << "and column of your next move."
  107. << endl << "Row(1-3): ";
  108. cin >> r;
  109. cout << endl << "Column(1-3): ";
  110. cin >> c;
  111. }
  112. //*****************************************************************************
  113. //function check_valid
  114. //parameters: board(char array), row(int), and column(int)
  115. //returns: bool(true or false)
  116. //*****************************************************************************
  117. bool check_valid(const char board[SIZE][SIZE], int r, int c)
  118. {
  119. if(r >= 1 && r <= SIZE && c >= 1 && c <= SIZE &&
  120. board[r - 1][c - 1] != 'X' && board[r - 1][c - 1] != 'O')
  121. {
  122. return true;
  123. }
  124. else
  125. {
  126. return false;
  127. }
  128. }
  129. //*****************************************************************************
  130. //function check_win
  131. //parameters: board(char array), player(int), and mark(char array)
  132. //returns: bool(true or false)
  133. //*****************************************************************************
  134. bool check_win(const char board[SIZE][SIZE],const int p, const char mark[SIZE2])
  135. {
  136. bool win; //local variable
  137. win = check_row(board, p, mark);
  138. if(win)
  139. {
  140. return true;
  141. }
  142. win = check_column(board, p, mark);
  143. if(win)
  144. {
  145. return true;
  146. }
  147. win = check_diagonal(board, p, mark);
  148. if(win)
  149. {
  150. return true;
  151. }
  152. return false;
  153.  
  154. }
  155. //*****************************************************************************
  156. //function check_row
  157. //parameters: board(char array), player(int), and mark(char array)
  158. //returns: bool(true or false)
  159. //*****************************************************************************
  160. bool check_row(const char board[SIZE][SIZE],const int p, const char mark[SIZE2])
  161. {
  162. for(int r = 0; r < SIZE; r++)
  163. {
  164. if(board[r][0] == mark[p - 1] &&
  165. board[r][1] == mark[p - 1] &&
  166. board[r][2] == mark[p - 1])
  167. {
  168. return true;
  169. }
  170. else
  171. {
  172. return false;
  173. }
  174. }
  175. }
  176. //*****************************************************************************
  177. //function check_column
  178. //parameters: board(char array), player(int), and mark(char array)
  179. //returns: bool(true or false)
  180. //*****************************************************************************
  181. bool check_column(const char board[SIZE][SIZE],const int p,
  182. const char mark[SIZE2])
  183. {
  184. for(int c = 0; c < SIZE; c++)
  185. {
  186. if(board[0][c] == mark[p - 1] &&
  187. board[1][c] == mark[p - 1] &&
  188. board[2][c] == mark[p - 1])
  189. {
  190. return true;
  191. }
  192. else
  193. {
  194. return false;
  195. }
  196. }
  197. }
  198. //*****************************************************************************
  199. //function check_diagonal
  200. //parameters: board(char array), player(int), and mark(char array)
  201. //returns: bool(true or false)
  202. //*****************************************************************************
  203. bool check_diagonal(const char board[SIZE][SIZE],const int p,
  204. const char mark[SIZE2])
  205. {
  206. if(board[0][0] == mark[p - 1] && board[1][1] == mark[p - 1] &&
  207. board[2][2] == mark[p - 1])
  208. {
  209. return true;
  210. }
  211. else if (board[0][2] == mark[p - 1] && board[1][1] == mark[p - 1] &&
  212. board[2][0] == mark[p - 1])
  213. {
  214. return true;
  215. }
  216. else
  217. {
  218. return false;
  219. }
  220. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trunks1212 is offline Offline
5 posts
since Feb 2007
Feb 15th, 2007
0

Re: C++ code help!!!

Consider initalizing your grid properly...
C++ Syntax (Toggle Plain Text)
  1. char board[SIZE][SIZE] = {' ',' ', ' ',
  2. ' ',' ', ' ',
  3. ' ',' ', ' '},
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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 15th, 2007
0

Re: C++ code help!!!

Okay I think I've got it working now. Thank you for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trunks1212 is offline Offline
5 posts
since Feb 2007
Feb 15th, 2007
0

Re: C++ code help!!!

what was it?
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Feb 16th, 2007
0

Re: C++ code help!!!

Click to Expand / Collapse  Quote originally posted by trunks1212 ...
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)
  1. /*
  2. Tic-Tac-Toe
  3.   This program will allow 2 players to play a game of tic-tac-toe.
  4.   Input: Interactive keyboard input from the players.
  5.   Output: The tic-tac-toe board
  6. */
  7. #include <iostream>
  8. #include <iomanip>
  9. using namespace std;
  10. const int SIZE = 3; //size of tic-tac-toe board array
  11. const int SIZE2 = 2; //size of tic-tac-toe piece array
  12. void display_board(const char[][SIZE]); //displays board
  13. void get_location(int&, int&,const int, const char[]);
  14. //gets location from player
  15. bool check_valid(const char[][SIZE], int, int);
  16. //checks for valid location
  17. bool check_win(const char[][SIZE],const int, const char[]);
  18. //checks if players wins
  19. bool check_row(const char[][SIZE],const int, const char[]);
  20. //checks for win on each row
  21. bool check_column(const char[][SIZE],const int, const char[]);
  22. //checks for win on each column
  23. bool check_diagonal(const char[][SIZE],const int, const char[]);
  24. //checks for on both diaganols
  25. int main()
  26. {
  27. char board[SIZE][SIZE] = {' '}, //tic-tac-toe board
  28. mark[SIZE2] = {'X', 'O'}; //both tic-tac-toe pieces
  29. int count = 0, //move counter
  30. r, //row
  31. c, //column
  32. p; //player
  33. bool valid, //valid move variable
  34. winner = false; //winner variable
  35.  
  36. while(!winner && count < 9)
  37. {
  38. for(p = 1; p < SIZE; p++)
  39. {
  40. if(count < 9)
  41. {
  42. count++;
  43. display_board(board);
  44. get_location(r, c, p, mark);
  45. valid = check_valid(board, r, c);
  46. while(!valid)
  47. {
  48. cout << "Sorry, please choose another row and column."
  49. << endl << "Row(1-3): ";
  50. cin >> r;
  51. cout << endl << "Column(1-3): ";
  52. cin >> c;
  53. valid = check_valid(board, r, c);
  54. }
  55.  
  56. board[r - 1][c - 1] = mark[p - 1];
  57. winner = check_win(board, p, mark);
  58. if(winner)
  59. {
  60. display_board(board);
  61. break;
  62. }
  63. }
  64.  
  65. }
  66. }
  67.  
  68. if(winner)
  69. {
  70. cout << "Winner is Player " << p << "!" << endl << endl;
  71. }
  72. else if(count == 9)
  73. {
  74. display_board(board);
  75. cout << "Players Tied!" << endl << endl;
  76. }
  77.  
  78. system("PAUSE");
  79. return 0;
  80. }
  81. //*****************************************************************************
  82. //function display_board
  83. //parameters: board(char array)
  84. //returns: void
  85. //*****************************************************************************
  86. void display_board(const char board[SIZE][SIZE])
  87. {
  88. cout << endl << " 1 2 3" << endl <<endl
  89. << " 1 " << board[0][0] << " | " << board[0][1] << " | "
  90. << board[0][2] << endl
  91. << " ---|---|---" << endl
  92. << " 2 " << board[1][0] << " | " << board[1][1] << " | "
  93. << board[1][2] << endl
  94. << " ---|---|---" << endl
  95. << " 3 " << board[2][0] << " | " << board[2][1] << " | "
  96. << board[2][2] << endl << endl;
  97. }
  98. //*****************************************************************************
  99. //function get_location
  100. //parameters: row(int), column(int), player(int), and mark(char array)
  101. //returns: row(int) and column(int)
  102. //*****************************************************************************
  103. void get_location(int& r, int& c,const int p, const char mark[SIZE2])
  104. {
  105. cout << "Player " << p << " (" << mark[p - 1] << ") choose the row "
  106. << "and column of your next move."
  107. << endl << "Row(1-3): ";
  108. cin >> r;
  109. cout << endl << "Column(1-3): ";
  110. cin >> c;
  111. }
  112. //*****************************************************************************
  113. //function check_valid
  114. //parameters: board(char array), row(int), and column(int)
  115. //returns: bool(true or false)
  116. //*****************************************************************************
  117. bool check_valid(const char board[SIZE][SIZE], int r, int c)
  118. {
  119. if(r >= 1 && r <= SIZE && c >= 1 && c <= SIZE &&
  120. board[r - 1][c - 1] != 'X' && board[r - 1][c - 1] != 'O')
  121. {
  122. return true;
  123. }
  124. else
  125. {
  126. return false;
  127. }
  128. }
  129. //*****************************************************************************
  130. //function check_win
  131. //parameters: board(char array), player(int), and mark(char array)
  132. //returns: bool(true or false)
  133. //*****************************************************************************
  134. bool check_win(const char board[SIZE][SIZE],const int p, const char mark[SIZE2])
  135. {
  136. bool win; //local variable
  137. win = check_row(board, p, mark);
  138. if(win)
  139. {
  140. return true;
  141. }
  142. win = check_column(board, p, mark);
  143. if(win)
  144. {
  145. return true;
  146. }
  147. win = check_diagonal(board, p, mark);
  148. if(win)
  149. {
  150. return true;
  151. }
  152. return false;
  153.  
  154. }
  155. //*****************************************************************************
  156. //function check_row
  157. //parameters: board(char array), player(int), and mark(char array)
  158. //returns: bool(true or false)
  159. //*****************************************************************************
  160. bool check_row(const char board[SIZE][SIZE],const int p, const char mark[SIZE2])
  161. {
  162. for(int r = 0; r < SIZE; r++)
  163. {
  164. if(board[r][0] == mark[p - 1] &&
  165. board[r][1] == mark[p - 1] &&
  166. board[r][2] == mark[p - 1])
  167. {
  168. return true;
  169. }
  170. else
  171. {
  172. return false;
  173. }
  174. }
  175. }
  176. //*****************************************************************************
  177. //function check_column
  178. //parameters: board(char array), player(int), and mark(char array)
  179. //returns: bool(true or false)
  180. //*****************************************************************************
  181. bool check_column(const char board[SIZE][SIZE],const int p,
  182. const char mark[SIZE2])
  183. {
  184. for(int c = 0; c < SIZE; c++)
  185. {
  186. if(board[0][c] == mark[p - 1] &&
  187. board[1][c] == mark[p - 1] &&
  188. board[2][c] == mark[p - 1])
  189. {
  190. return true;
  191. }
  192. else
  193. {
  194. return false;
  195. }
  196. }
  197. }
  198. //*****************************************************************************
  199. //function check_diagonal
  200. //parameters: board(char array), player(int), and mark(char array)
  201. //returns: bool(true or false)
  202. //*****************************************************************************
  203. bool check_diagonal(const char board[SIZE][SIZE],const int p,
  204. const char mark[SIZE2])
  205. {
  206. if(board[0][0] == mark[p - 1] && board[1][1] == mark[p - 1] &&
  207. board[2][2] == mark[p - 1])
  208. {
  209. return true;
  210. }
  211. else if (board[0][2] == mark[p - 1] && board[1][1] == mark[p - 1] &&
  212. board[2][0] == mark[p - 1])
  213. {
  214. return true;
  215. }
  216. else
  217. {
  218. return false;
  219. }
  220. }
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?
Last edited by Andy-Pandy; Feb 16th, 2007 at 1:08 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Andy-Pandy is offline Offline
14 posts
since Oct 2006
Feb 16th, 2007
0

Re: C++ code help!!!

Click to Expand / Collapse  Quote originally posted by Andy-Pandy ...
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?
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++.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please Help Me
Next Thread in C++ Forum Timeline: C++ Static Linked Libraries Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC