Problem_all_the_time

connect4.cc

Feb 15th, 2022 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include <ncurses.h>
  2. #include <iostream>
  3. #include <random>
  4. #include <utility>
  5. #include "ai.h"
  6. #include "game.h"
  7. #include <cstring>
  8.  
  9. // The main game loop for Connect 4.
  10. // *** Utility functions only used in this file. ***
  11.  
  12. // Switches from player 1 to player 2 and vice-versa.
  13. int SwitchPlayer(int player);
  14.  
  15. // Get a move from the human. Updates UI to show column selection.
  16. bool GetPlayerMove(int board[6][7], int &move);
  17.  
  18. // Plays a single game.
  19. int PlayGame(std::pair<PlayerType, PlayerType> players, bool silent);
  20.  
  21. int main(int argc, char **argv) {
  22. srand(time(0));
  23.  
  24. std::pair<PlayerType, PlayerType> players;
  25. if (argc == 3) {
  26. players = std::make_pair(GetPlayerType(argv[1]), GetPlayerType(argv[2])); // Keeping track of the amount of wins and loses
  27. } else if (argc == 1) {
  28. players = std::make_pair(PlayerType::kHuman, PlayerType::kRandom); // kHuman is one ai the other ai is kRandom
  29. } else {
  30. std::cerr << "Usage: ./connect4 [player1] [player2]" << std::endl;
  31. return 1;
  32. }
  33.  
  34. // Show the screen if either player is a human.
  35. bool silent = true;
  36. if (players.first == PlayerType::kHuman || players.second == PlayerType::kHuman) {
  37. silent = false;
  38. initscr();
  39. cbreak();
  40. noecho();
  41. keypad(stdscr, TRUE);
  42. }
  43.  
  44. if (silent) {
  45. int player1_wins = 0; int player2_wins = 0; int draws = 0;
  46. int winner = PlayGame(players, silent);
  47.  
  48. if (winner == 1) { player1_wins += 1; }
  49. else if (winner == 2) { player2_wins += 1; }
  50. else if (winner == -1) { draws += 1; }
  51.  
  52. std::cout << "Player 1 wins: " << player1_wins << std::endl;
  53. std::cout << "Player 2 wins: " << player2_wins << std::endl;
  54. std::cout << "Draws: " << draws << std::endl;
  55. } else {
  56. // Play a single game Human vs. AI.
  57. PlayGame(players, silent); // Inner
  58. }
  59.  
  60. // Wait for any key press.
  61. if (!silent) {
  62. getch();
  63. endwin();
  64. }
  65.  
  66. return 0;
  67. }
  68.  
  69. // Returns false if the user wants to exit.
  70. bool GetPlayerMove(int board[6][7], int &move) {
  71. int ch;
  72. // This function goes inside DrawBoard
  73. while (true) {
  74. // User Move
  75. ch = getch();
  76. // To play the game, move the right arrow key and the left arrow key around. If you're done, press q
  77. // Press on the SPACE_BAR to place a piece
  78. if (ch == KEY_RIGHT && move <= 5) {
  79. move += 1;
  80. } else if (ch == KEY_LEFT && move > 0) {
  81. move -= 1;
  82. } else if (ch == ' ') {
  83. return true;
  84. } else if (ch == 'q') {
  85. // this is how you quit the game
  86. return false;
  87. }
  88. DrawBoard(board, move); // Inline function.
  89. }
  90. }
  91.  
  92. int SwitchPlayer(int player) {
  93. if (player == 1) {return 2;}
  94. else if (player == 2) {return 1;}
  95. else {throw;}
  96. }
  97.  
  98. int PlayGame(std::pair<PlayerType, PlayerType> players, bool silent) {
  99. int board[6][7];
  100. int current_player = 1;
  101. InitializeBoard(board);
  102. int player_move = 0;
  103.  
  104. // Loop ends if the game ends or GetPlayerMove returns false.
  105. bool result;
  106. while (silent == false) {
  107. DrawBoard(board, player_move);
  108.  
  109. PlayerType player_type; // This is calling the struct
  110.  
  111. if (current_player == 1) {player_type = players.first;}
  112. else if (current_player == 2) {player_type = players.second;}
  113.  
  114. // avoid updating the GUI
  115. int move_to_make;
  116.  
  117. switch (player_type) {
  118. case PlayerType::kHuman:
  119. if (!GetPlayerMove(board, player_move)) {
  120. printw("Game over.");
  121. return Winner(board);
  122. }
  123. move_to_make = player_move;
  124. break;
  125. case PlayerType::kRandom:
  126. move_to_make = RandomAgent(board, current_player);
  127. break;
  128. case PlayerType::kFirst:
  129. move_to_make = FirstOpenAgent(board, current_player);
  130. break;
  131. case PlayerType::kInvalid:
  132. default:
  133. throw;
  134. }
  135. result = Drop(board, current_player, move_to_make); // Might have to add true or false to Drop.
  136. if (result == true) {current_player = SwitchPlayer(current_player);}
  137. else if (result == false) {current_player = SwitchPlayer(current_player);}
  138. if (Winner(board) == 1) { silent = true;}
  139. else if (Winner(board) == 2) {silent = true;}
  140. else if (Winner(board) == -1) {silent = true;}
  141. }
  142. int winner = Winner(board);
  143. if (silent != false) {
  144. if (winner == 1) {
  145. printw("Player 1 wins!");
  146. } else if (winner == 2) {
  147. printw("Player 2 wins!");
  148. } else if (winner == -1) {
  149. printw("Draw.");
  150. } else {
  151. printw("Game over.");
  152. }
  153. }
  154. return winner;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment