Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. ttt.h
  2. __
  3. // ttt.h
  4.  
  5. #ifndef TTT_H
  6. #define TTT_H
  7.  
  8. #include <tuple>
  9. #include <array>
  10. #include <vector>
  11. #include <ctime>
  12. #include <random>
  13. #include <iterator>
  14. #include <iostream>
  15. #include <utility>
  16.  
  17. enum class Player { X, O, None };
  18. using Move = int;
  19. using State = std::array<Player,9>;
  20. using MoveEval = std::pair<Move,int>;
  21.  
  22. // used to get a random element from a container
  23. template<typename Iter, typename RandomGenerator>
  24. Iter select_randomly(Iter start, Iter end, RandomGenerator& g) {
  25. std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
  26. std::advance(start, dis(g));
  27. return start;
  28. }
  29.  
  30. template<typename Iter>
  31. Iter select_randomly(Iter start, Iter end) {
  32. static std::random_device rd;
  33. static std::mt19937 gen(rd());
  34. return select_randomly(start, end, gen);
  35. }
  36.  
  37. std::ostream &operator<<(std::ostream &os, const State &state);
  38. std::ostream &operator<<(std::ostream &os, const Player &player);
  39.  
  40. Player getCurrentPlayer(const State &state);
  41. State doMove(const State &state, const Move &m);
  42. Player getWinner(const State &state);
  43. std::vector<Move> getMoves(const State &state);
  44.  
  45. #endif // TTT_H
  46. ___
  47. main.cpp
  48. ___
  49. // ttt_ab.cpp
  50. // Compile with: g++ -std=c++11 -o ttt_ab ttt_ab.cpp ttt.cpp
  51.  
  52. #include <iostream>
  53. #include <algorithm>
  54. #include <map>
  55. #include <limits>
  56. #include "ttt.h"
  57.  
  58. enum class PlayerType { Human, Computer };
  59. Move choice;
  60.  
  61. int eval(const State &board, const Player &player, int ply)
  62. {
  63. if(getWinner(board) == player){
  64. return 500 + 10 * ply;
  65. }
  66. if(getWinner(board) != player && getWinner(board) != Player::None){
  67. return -500 - 10 * ply;
  68. }
  69. if(getWinner(board) == Player::None){
  70. return 0;
  71. }
  72. }
  73.  
  74. MoveEval alphaBeta(State &board, int ply, Player player, Player opponent, int alpha, int beta)
  75. {
  76. State trialboard = board;
  77. if (ply == 0)
  78. return std::make_pair(Move(), eval(trialboard, player, ply));
  79.  
  80. std::vector<Move> moves = getMoves(trialboard);
  81. if (moves.size() == 0)
  82. return std::make_pair(Move(), eval(trialboard, player, ply));
  83.  
  84. MoveEval best = std::make_pair(Move(),alpha);
  85. for (Move &move: moves) {
  86. trialboard = doMove(trialboard, move);
  87. MoveEval me = alphaBeta(trialboard, ply - 1, opponent, player, -beta, -alpha);
  88. trialboard = board;
  89. if (-me.second > alpha) {
  90. alpha = -me.second;
  91. best = std::make_pair(move,alpha);
  92. }
  93. if (alpha >= beta)
  94. return best;
  95. }
  96. return best;
  97. }
  98.  
  99. Move alphaBeta(const State &b, int ply)
  100. {
  101. State board = b;
  102. Player player = Player::O;
  103. Player opponent = Player::X;
  104.  
  105. return alphaBeta(board, ply, player, opponent, std::numeric_limits<int>::min() + 1, std::numeric_limits<int>::max()).first;
  106. }
  107.  
  108.  
  109.  
  110. int main()
  111. {
  112. std::srand(std::time(0));
  113.  
  114. std::map<Player,PlayerType> playerType;
  115. playerType[Player::X] = PlayerType::Human;
  116. playerType[Player::O] = PlayerType::Computer;
  117.  
  118. State board = {
  119. Player::None, Player::None, Player::None,
  120. Player::None, Player::None, Player::None,
  121. Player::None, Player::None, Player::None };
  122. std::cout << board << std::endl;
  123.  
  124. std::vector<Move> moves = getMoves(board);
  125. while (moves.size() > 0) {
  126. if (playerType[getCurrentPlayer(board)] == PlayerType::Human) {
  127. std::cout << "+-+-+-+" << std::endl <<
  128. "|0|1|2|" << std::endl <<
  129. "+-+-+-+" << std::endl <<
  130. "|3|4|5|" << std::endl <<
  131. "+-+-+-+" << std::endl <<
  132. "|6|7|8|" << std::endl <<
  133. "+-+-+-+" << std::endl << std::endl;
  134. std::cout << "Enter a move ( ";
  135. for (Move m: moves) std::cout << m << " ";
  136. std::cout << "): ";
  137. Move m;
  138. std::cin >> m;
  139. board = doMove(board, m);
  140. } else {
  141. board = doMove(board, alphaBeta(board, 9));
  142. }
  143. std::cout << board << std::endl;
  144. moves = getMoves(board);
  145. }
  146.  
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement