Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. // what is it
  2. // Michael Freeland 4/25/17
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. // This allows you to input and output data and us string data class.
  7. const int PLAYER_ROWS = 3,
  8. PLAYER_COLS = 3,
  9. DISPLAY_ROWS = 9,
  10. DISPLAY_COLS = 11;
  11. // this makes a constant intager that will be used in the program.
  12. void displayBoard(char[][11]);
  13. void makeMove(char[][11], char[][PLAYER_COLS], bool);
  14. bool boardFull(char[][PLAYER_COLS]);
  15. bool isaWinner(char[][PLAYER_COLS], char);
  16. // It sets up the board and the bolian statments.
  17. int main()
  18. {
  19. bool xTurn = true;
  20. bool xWins = false,
  21. oWins = false;
  22. // This is setting up what will be determand as true of false.
  23. char gameBoard[DISPLAY_ROWS][DISPLAY_COLS] =
  24. { { ' ', ' ', ' ', '|', ' ', ' ', ' ', '|', ' ', ' ', ' ' },
  25. { ' ', '*', ' ', '|', ' ', '*', ' ', '|', ' ', '*', ' ' },
  26. { '_', '_', '_', '|', '_', '_', '_', '|', '_', '_', '_' },
  27. { ' ', ' ', ' ', '|', ' ', ' ', ' ', '|', ' ', ' ', ' ' },
  28. { ' ', '*', ' ', '|', ' ', '*', ' ', '|', ' ', '*', ' ' },
  29. { '_', '_', '_', '|', '_', '_', '_', '|', '_', '_', '_' },
  30. { ' ', ' ', ' ', '|', ' ', ' ', ' ', '|', ' ', ' ', ' ' },
  31. { ' ', '*', ' ', '|', ' ', '*', ' ', '|', ' ', '*', ' ' },
  32. { ' ', ' ', ' ', '|', ' ', ' ', ' ', '|', ' ', ' ', ' ' } };
  33. // This is making the gameboard.
  34. char moves[PLAYER_ROWS][PLAYER_COLS] = { '*', '*', '*', '*', '*', '*', '*', '*', '*', };
  35. // this makes the moves.
  36. while (!(xWins || oWins || boardFull(moves)))
  37. {
  38. displayBoard(gameBoard);
  39.  
  40. makeMove(gameBoard, moves, xTurn);
  41.  
  42. if (xTurn && isaWinner(moves, 'X'))
  43. xWins = true;
  44. else if (!xTurn && isaWinner(moves, 'O'))
  45. oWins = true;
  46.  
  47. xTurn = !xTurn;
  48. }
  49. // This is setting up the gameboard and makeing the moves.
  50. if (xWins)
  51. cout << "\n**** GAME OVER -- X WINS! ****\n";
  52. else if (oWins)
  53. cout << "\n**** GAME OVER -- O WINS! ****\n";
  54. else
  55. cout << "\n**** GAME OVER -- It's a TIE. ****\n";
  56. displayBoard(gameBoard);
  57. return 0;
  58. }
  59. // This show who wins and also is the end of the game.
  60. /***************************************************
  61. * displayBoard *
  62. * *
  63. ***************************************************/
  64. void displayBoard(char gameBoard[][DISPLAY_COLS])
  65. {
  66. string spaces = " ";
  67.  
  68. for (int row = 0; row < DISPLAY_ROWS; row++)
  69. {
  70. cout << endl << spaces;
  71. for (int col = 0; col < 11; col++)
  72. cout << gameBoard[row][col];
  73. }
  74. cout << endl << endl;
  75. }
  76. // This is how the the board gets displayed.
  77. /*********************************************
  78. * boardFull *
  79. * *
  80. *********************************************/
  81. bool boardFull(char moves[][PLAYER_COLS])
  82. {
  83. for (int row = 0; row < PLAYER_ROWS; row++)
  84. {
  85. for (int col = 0; col < PLAYER_COLS; col++)
  86. {
  87. if (moves[row][col] == '*')
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. // It test whether the board is full or not.
  94. /**************************************
  95. * makeMove *
  96. * *
  97. **************************************/
  98. void makeMOve(char gameBoard[][DISPLAY_COLS], char moves[][PLAYER_COLS], bool xTurn)
  99. {
  100. int row,
  101. col,
  102. moveRow,
  103. moveCol;
  104. char sign;
  105.  
  106. if (xTurn)
  107. {
  108. cout << "\nX's turn -Enter your move\n";
  109. sign = 'X';
  110. }
  111. else
  112. {
  113. cout << "\nO,s turn - Enter your move \n";
  114. sign = 'O';
  115. }
  116. cout << "Row (1-3) and colum (1-3) seperated by a space: ";
  117. cin >> row >> col;
  118.  
  119. while (row <1 || col < 1 || row > PLAYER_ROWS || col > PLAYER_COLS || moves[row - 1][col - 1] != '*')
  120. {
  121. cout << "\nRow and column must be a \"free\" space between 1 and 3.\n";
  122. cout << "re-enter row (1-3) and column (1-3) seperted by a space: ";
  123. cin >> row >> col;
  124. }
  125. moveRow = row - 1;
  126. moveCol = col - 1;
  127. moves[moveRow][moveCol] = sign;
  128.  
  129. moveRow = moveRow * 3 + 1;
  130. moveCol = moveCol * 4 + 1;
  131. gameBoard[moveRow][moveCol] = sign;
  132. }
  133. // This sets up how moving will work on the board and how the turns will go.
  134. /************************************
  135. * isaWinner *
  136. * *
  137. ************************************/
  138. bool isaWinner(char moves[][PLAYER_COLS], char sign)
  139. {
  140. for (int row = 0; row < PLAYER_ROWS; row++)
  141. {
  142. if (moves[row][0] == sign && moves[row][1] == sign && moves[row][2] == sign)
  143. return true;
  144. }
  145. for (int col = 0; col < PLAYER_COLS; col++)
  146. {
  147. if (moves[0][col] == sign && moves[1][col] == sign && moves[2][col] == sign)
  148. return true;
  149. }
  150. if (moves[0][0] == sign && moves[1][1] == sign && moves[2][2] == sign)
  151. return true;
  152. if (moves[0][2] == sign && moves[1][1] == sign && moves[2][2] == sign)
  153. return true;
  154.  
  155. return false;
  156. }
  157. // This show how depeding on the placment of the moves if you win or lose.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement