C++ code help!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 5
Reputation: trunks1212 is an unknown quantity at this point 
Solved Threads: 0
trunks1212 trunks1212 is offline Offline
Newbie Poster

C++ code help!!!

 
0
  #1
Feb 15th, 2007
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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: C++ code help!!!

 
0
  #2
Feb 15th, 2007
Consider initalizing your grid properly...
  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.
"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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 5
Reputation: trunks1212 is an unknown quantity at this point 
Solved Threads: 0
trunks1212 trunks1212 is offline Offline
Newbie Poster

Re: C++ code help!!!

 
0
  #3
Feb 15th, 2007
Okay I think I've got it working now. Thank you for your help.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: C++ code help!!!

 
0
  #4
Feb 15th, 2007
what was it?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 14
Reputation: Andy-Pandy is an unknown quantity at this point 
Solved Threads: 0
Andy-Pandy Andy-Pandy is offline Offline
Newbie Poster

Re: C++ code help!!!

 
0
  #5
Feb 16th, 2007
Originally Posted by trunks1212 View 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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: C++ code help!!!

 
0
  #6
Feb 16th, 2007
Originally Posted by Andy-Pandy View Post
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++.
"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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC