944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 786
  • C++ RSS
Jun 1st, 2007
0

help if possible (asap)

Expand Post »
my code was working, but when i added in a new section to enter playing against a computer player, it now hates me, and wont stop trying to kill me.
c++ Syntax (Toggle Plain Text)
  1. //name: *******
  2. //Lab: *
  3. //purpose: *******
  4. //modification date:***********
  5. #include <iostream>
  6. #include <cstdlib>
  7. #include <ctime>
  8. using namespace std;
  9.  
  10. const int ROW=6, COL=8;
  11. const char player1 = 'O';
  12. const char player2 = 'X';
  13.  
  14.  
  15. void drawHeader();
  16. void drawBoard(char board[ROW][COL]);
  17. void initialiseBoard(char board[ROW][COL]);
  18. int playerMove(char board[ROW][COL], char, int);
  19. int againstComputer(char board[ROW][COL], char, int);
  20. char chooseOption();
  21. char quitGame(char);
  22. int computerMove(int);
  23.  
  24. int main()
  25. {
  26. int status=1; // 1 for start or restart, 2 for play, 3 for quit
  27. char board[ROW][COL];
  28. char option, playerChar, startAgain = 'y';
  29. int move, playerNum;
  30.  
  31.  
  32. drawHeader();
  33.  
  34. do
  35. {
  36. if (status==1) //Just start or Restart
  37. {
  38. playerNum=1;
  39. option = chooseOption();
  40.  
  41. if (option=='a')
  42. {
  43. status = 3;
  44. // play against computer
  45. }
  46. else if (option=='b')
  47. {
  48.  
  49. status = 2;
  50. //game play
  51. }
  52. else if (option=='c')//option is 'q'
  53. {
  54. status=4;
  55. }
  56. else
  57. cout << "Invalid Choice!";
  58.  
  59. }
  60. else if (status==2) //playing
  61. {
  62.  
  63. do
  64. {
  65. if (startAgain == 'y')
  66. {
  67. initialiseBoard(board);
  68. playerNum = 1;
  69. startAgain = 'n';
  70. }
  71.  
  72. if (playerNum == 1)
  73. playerChar = player1;
  74. else
  75. playerChar = player2;
  76.  
  77. drawBoard(board);
  78.  
  79. move = playerMove(board, playerChar, playerNum);
  80. if (move==-1)
  81. {
  82. // no switch
  83. }
  84. else (move != 0)
  85. {
  86. if (playerNum == 0)
  87. playerNum = 1;
  88. else
  89. playerNum = 0;
  90. }
  91. else
  92. {
  93. startAgain = quitGame('y');
  94. if (startAgain == 'n')
  95. status=4;
  96. else if (startAgain == 'y')
  97. status=2;
  98. }
  99.  
  100. }while (status == 2);
  101. }
  102. else if (status == 3)//this is the new section that doesnt like working
  103. {
  104. do
  105. {
  106. if (startAgain == 'y')
  107. {
  108. initialiseBoard(board);
  109. playerNum = 1;
  110. startAgain = 'n';
  111. }
  112.  
  113. if (playerNum == 1)
  114. playerChar = player1;
  115. else
  116. playerChar = player2;
  117.  
  118. drawBoard(board);
  119.  
  120. move = againstComputer(board, playerChar, playerNum);
  121. if (move==-1)
  122. {
  123. }
  124. else (move != 0)
  125. {
  126. if (playerNum == 0)
  127. playerNum = 1;
  128. else
  129. playerNum = 0;
  130. }
  131. else
  132. {
  133. startAgain = quitGame('y');
  134. if (startAgain == 'n')
  135. status=4;
  136. else if (startAgain == 'y')
  137. status=3;
  138. }
  139.  
  140. }while (status ==3);
  141. }
  142.  
  143. }while (status!=4);
  144.  
  145. cout << "\nThanks for playing Connect Four, the game of the clever people!" << endl;
  146.  
  147. return 0;
  148. }
  149.  
  150. void drawHeader()
  151. {
  152. cout << "*********************************************************" << endl;
  153. cout << "* Connect Four *" << endl;
  154. cout << "*********************************************************" << endl;
  155. cout << "Welcome to the Connect Four computer game. You can choose" << endl;
  156. cout << "to play against another player or against the computer " << endl;
  157. cout << "(not implemented in this version)." << endl;
  158. cout << "In this game the first player to position four of his/her" << endl;
  159. cout << "pieces on a line will win. The line can be horizontal," << endl;
  160. cout << "vertical or at an angle, but the 4 pieces must be next to" << endl;
  161. cout << "each other." << endl;
  162. cout << "*********************************************************" << endl;
  163.  
  164. return;
  165. }
  166.  
  167. char chooseOption()
  168. {
  169. char option;
  170.  
  171. cout << "\n*************************************" << endl;
  172. cout << "* Choose an Option *" << endl;
  173. cout << "*************************************" << endl;
  174. cout << "* a) Play against the computer *" << endl;
  175. cout << "* b) Play against another Player *" << endl;
  176. cout << "* c) Quit *" << endl;
  177. cout << "*************************************" << endl;
  178.  
  179. cout << "Enter your choice: ";
  180. cin >> option;
  181.  
  182. return option;
  183. }
  184.  
  185.  
  186.  
  187. char quitGame(char quit)
  188. {
  189.  
  190. char cont, startAgain;
  191.  
  192. if (quit == 'y')
  193. {
  194. cout << "Do you want to start a new game (y/n)?: ";
  195.  
  196. cin >> cont;
  197.  
  198. cin.clear(); cin.ignore(300, '\n');
  199.  
  200. while ((cont != 'y') && (cont != 'n'))
  201. {
  202. cout << "Do you want to start a new game (y/n)?: ";
  203.  
  204. cin >> cont;
  205.  
  206. cin.clear(); cin.ignore(300, '\n');
  207. }
  208.  
  209. if (cont =='y')
  210. startAgain = 'y';
  211. else
  212. startAgain = 'n';
  213. }
  214.  
  215.  
  216. return startAgain;
  217. }
  218.  
  219. void initialiseBoard(char board[ROW][COL])
  220. {
  221. char rowComp = 0, colComp = 0;
  222.  
  223. while(rowComp < ROW)
  224. {
  225. while(colComp < COL)
  226. { board[rowComp][colComp] = '.';
  227. ++colComp;
  228. }
  229. colComp = 0;
  230. rowComp++;
  231. }
  232. return;
  233. }
  234.  
  235. void drawBoard(char board[ROW][COL])
  236. {
  237. char rowComp = 0, colComp = 0;
  238.  
  239. while(rowComp < ROW)
  240. {
  241. while(colComp < COL)
  242. { cout << board[rowComp][colComp];
  243. ++colComp;
  244. }
  245. cout << endl;
  246. colComp = 0;
  247. rowComp++;
  248. }
  249. cout << "12345678" << endl;
  250. return;
  251.  
  252. }
  253.  
  254. int playerMove(char board[ROW][COL], char playerChar, int playerNum)
  255. {
  256. int move, index = 5, position;
  257. bool cont = false;
  258.  
  259. cout << (playerNum == 1 ? "Player 1" : "Player 2") << " move (1-8, 0 to quit): ";
  260. cin >> move;
  261.  
  262. if ((move != 0) && ((move>=1) && (move<=8)))
  263. {
  264. do
  265. {
  266.  
  267. position = move - 1;
  268.  
  269. if (board[index][position] != '.')
  270. {
  271. if (--index == -1)
  272. {
  273. cout << "Sorry no more room in this column..." << endl;
  274. return -1;
  275. }
  276. }
  277. else
  278. cont = true;
  279.  
  280. } while (!cont);
  281.  
  282. board[index][position] = playerChar;
  283. }
  284. else if (move == 0)
  285. {
  286. cont = true;
  287. }
  288. else
  289. {
  290. cout << "This move is not allowed!" << endl;
  291. return -1;
  292. }
  293. return move;
  294. }
  295.  
  296.  
  297. int againstComputer(char board[ROW][COL], char playerChar, int playerNum)
  298. {
  299. int move, index = 5, position, cMove;
  300. bool cont = false;
  301.  
  302. cout << (playerNum == 1 ? "Player 1" : "Computer") << " move (1-8, 0 to quit): ";
  303.  
  304. if (playerNum != 1)
  305. computerMove(cMove);
  306. move = cMove;
  307. else
  308. cin >> move;
  309.  
  310. if ((move != 0) && ((move>=1) && (move<=8)))
  311. {
  312. do
  313. {
  314.  
  315. position = move - 1;
  316.  
  317. if (board[index][position] != '.')
  318. {
  319. if (--index == -1)
  320. {
  321. cout << "Sorry no more room in this column..." << endl;
  322. return -1;
  323. }
  324. }
  325. else
  326. cont = true;
  327.  
  328. } while (!cont);
  329.  
  330. board[index][position] = playerChar;
  331. }
  332. else if (move == 0)
  333. {
  334. cont = true;
  335. }
  336. else
  337. {
  338. cout << "This move is not allowed!" << endl;
  339. return -1;
  340. }
  341. return move;
  342. }
  343.  
  344. int computerMove(int cMove)
  345. {
  346. srand ( time(NULL) );
  347. cMove= rand() % 8 + 1;
  348.  
  349. return cMove;
  350. }

errors i am gettign are :

parse errors on lines 85,102,111,121,307
im also getting told that "IOS C++ forbids decleration of..." on lines 109, 110, 118, 120

there are also other errors withing that part of the code, what is weird to me however, is that it works perfectly fine in the other place that the almost identical code is placed (status 2)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Slavrix is offline Offline
67 posts
since Oct 2005
Jun 1st, 2007
0

Re: help if possible (asap)

In againstComputer, the first if statement needs braces. In main, when you test move, you used else instead of else if twice. And you use cMove before giving it a value in againstComputer.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 1st, 2007
0

Re: help if possible (asap)

with the part abotu the computer move that u said, how would i go abotu fixing that, i had a try and the several things i tried didnt work, any suggestions /
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Slavrix is offline Offline
67 posts
since Oct 2005
Jun 1st, 2007
0

Re: help if possible (asap)

dw i got it, thnx again mate
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Slavrix is offline Offline
67 posts
since Oct 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: different C++ versions. suspected dll incompattibility
Next Thread in C++ Forum Timeline: Hash Table display problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC