Problem_all_the_time

ai.cc

Feb 15th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <random>
  2. #include <string>
  3. #include <vector>
  4. #include "ai.h"
  5.  
  6. int FirstOpenAgent(int board[6][7], int ai_player) {
  7. // To keep the interface for AIs consistent.
  8. (void)ai_player;
  9.  
  10. // Loop over board and find the first place that is not occupied.
  11. for (int row = 0; row < 5; ++row) {
  12. for (int col = 0; col < 6; ++col) {
  13. if (board[row][col] == 0) {
  14. return col;
  15. }
  16. }
  17. }
  18. // This can only happen if game is broken.
  19. throw;
  20. //return board[6][7];
  21. }
  22.  
  23. int RandomAgent(int board[6][7], int ai_player) {
  24. // To keep the interface for AIs consistent.
  25. (void)ai_player;
  26.  
  27. std::vector<int> open_columns;
  28.  
  29. // Create list of columns that has an open space
  30. for (int row = 0; row < 6; ++row) {
  31. for (int col = 0; col < 7; ++col) {
  32. if (board[row][col] == 0) {
  33. open_columns.push_back(col);
  34. }
  35. }
  36. }
  37.  
  38. // Randomly select from this list. An empty list implies the board is full.
  39. // This should not happen.
  40. //.empty()
  41. for (size_t choice : open_columns) {
  42. choice = rand() % open_columns.size();
  43. srand(choice);
  44. board[6][7] = {open_columns[choice]};
  45. // This is working. Ai is choosing from random columns. Feb 15
  46. return board[6][7];
  47. }
  48. /*if (open_columns.empty()) {
  49. throw; // IT was complaining about this! (Line 49)
  50. }*/
  51. return 0;
  52. }
  53.  
  54. PlayerType GetPlayerType(const std::string &arg) {
  55. if (arg == "human") {
  56. return PlayerType::kHuman;
  57. } else if (arg == "random") {
  58. return PlayerType::kRandom;
  59. } else if (arg == "first") {
  60. return PlayerType::kFirst;
  61. } else {
  62. return PlayerType::kInvalid;
  63. }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment