Advertisement
Guest User

vcpnnrcy

a guest
Oct 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. /*
  2. randomize who goes first
  3. initialize board
  4. print the board
  5. while game is not over
  6. ask the player to make a move
  7. take input of player
  8. drop puck in board
  9. printBoard
  10. check if won
  11. if won => set game to over
  12. if not won => switch player
  13. print winner
  14. */
  15.  
  16. #include <iostream>
  17. #include <cstdlib>
  18. #include <ctime>
  19. #include <random>
  20.  
  21. using namespace std;
  22.  
  23. const int HORIZONTAL = 7;
  24. const int VERTICAL = 6;
  25. int board[HORIZONTAL][VERTICAL];
  26. //empty = 0
  27. //PUCK O = 1
  28. //PUCK X = -1
  29.  
  30. void initializeBoard() {
  31. for (int i = 0; i < 7; i++) {
  32. for (int j = 0; j < 6; j++) {
  33. board[i][j] = 0;
  34. }
  35. }
  36. }
  37.  
  38. void printBoard() {
  39. for (int i = 0; i < 7; i++) {
  40. for (int j = 0; j < 6; j++) {
  41. if (board[i][j] == 0) {
  42. cout << "| ";
  43. }
  44. else if (board[i][j] == 1) {
  45. cout << "|O";
  46. }
  47. else if (board[i][j] == -1) {
  48. cout << "|X";
  49. }
  50. }
  51. cout << "|" << endl;
  52. }
  53. cout << endl;
  54. }
  55.  
  56. //function: dropPuck()
  57. //input: HORIZONTAL number (int), between 0 and 6, player (1 or -1)
  58. //output: boolean; true if success, false if not.
  59. //purpose: Stores 1 (O) or -1 (X) into board[HORIZONTAL][VERTICAL] where the VERTICAL is the top of the pile in that HORIZONTAL row
  60. bool dropPuck(int vert, int player) {
  61. int i = 0;
  62. while ((board[i][vert] == 0) && (i < HORIZONTAL)) {
  63. i++;
  64. }
  65.  
  66. if (i == 0) {
  67. return false;
  68. }
  69.  
  70. board[i - 1][vert] = player;
  71. return true;
  72. }
  73.  
  74. //function: checkWin()
  75. //input: N/A
  76. //output: true if current player wins, false if else
  77. /*bool checkWin() {
  78. for (int j = 0; i < VERTICAL; j++)
  79. for (int i = 0; i < HORIZONTAL; i++) {
  80.  
  81. }
  82. return false;
  83. }
  84. */
  85.  
  86. int main() {
  87. bool gameover = false;
  88.  
  89. srand(time(0));
  90. int random = rand();
  91. int player = 1-2*(random%2);
  92. cout << "Player " << player << " is starting." << endl;
  93. cout << endl;
  94.  
  95. initializeBoard();
  96. printBoard();
  97.  
  98. while(!gameover) {
  99. cout << "Input a number from 0 to 5 to drop the puck." << endl;
  100. int drop_pos = 0;
  101. cin >> drop_pos;
  102. while ((drop_pos > 5) || (drop_pos < 0) || (!dropPuck(drop_pos, player))) {
  103. cout << endl;
  104. cout << "INVALID MOVE; PUCK IS OUT OF BOUNDS" << endl;
  105. cout << endl;
  106. cout << endl;
  107. cout << "Input a number from 0 to 5 to drop the puck." << endl;
  108. cin >> drop_pos;
  109. }
  110. printBoard();
  111. //gameover = checkWin();
  112. player = player *-1;
  113. }
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement