Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3. #include <algorithm>
  4. #include <functional>
  5. #include <array>
  6.  
  7. class TicTacToe
  8. {
  9. public:
  10. bool isFull() const;
  11. void draw() const;
  12. void turn(char player);
  13. bool check(char player) const;
  14.  
  15. private:
  16. bool fill(char player, int position);
  17.  
  18. private:
  19. static const std::size_t mDim = 3;
  20. std::array<char, mDim * mDim> grid
  21. {
  22. {
  23. '-', '-', '-',
  24. '-', '-', '-',
  25. '-', '-', '-'
  26. }
  27. };
  28. };
  29.  
  30. template<std::size_t dim>
  31. struct column : public std::unary_function<int, bool>
  32. {
  33. column(int i) : colNum(i){}
  34. bool operator() (int number) { return (number % dim == colNum); }
  35. int colNum;
  36. };
  37.  
  38. bool TicTacToe::fill(char player, int position)
  39. {
  40. if (grid[position] != '-')
  41. return false;
  42. grid[position] = player;
  43. return true;
  44. }
  45.  
  46. bool TicTacToe::isFull() const
  47. {
  48. return 0 == std::count_if(grid.begin(), grid.end(),
  49. [](const char& i)
  50. {
  51. return '-';
  52. });
  53. }
  54.  
  55. bool TicTacToe::check(char player) const
  56. {
  57. column<mDim>::argument_type input;
  58.  
  59. // check for row or column wins
  60. bool row1 = true, row2 = true, row3 = true,
  61. col1 = true, col2 = true, col3 = true,
  62. diag1 = true, diag2 = true;
  63. int j = 0;
  64.  
  65. // columns
  66. std::for_each(grid.begin(), grid.end(),
  67. [&](char i)
  68. {
  69. int x = j++;
  70.  
  71. if (column<mDim>(0)(input = x))
  72. col1 &= i == player;
  73.  
  74. if (column<mDim>(1)(input = x))
  75. col2 &= i == player;
  76.  
  77. if (column<mDim>(2)( input = x))
  78. col3 &= i == player;
  79.  
  80.  
  81. });
  82.  
  83. // diagonals
  84. j = 0;
  85. for (const auto& m : grid)
  86. {
  87. int x = j++;
  88. if (x == 0 || x == 4 || x == 8)
  89. diag1 &= m == player;
  90. if (x == 2 || x == 4 || x == 6)
  91. diag2 &= m == player;
  92. }
  93.  
  94. if (col1 || col2 || col3 || diag1 || diag1)
  95. return true;
  96.  
  97. // rows
  98. return std::search_n(grid.begin(), grid.end(), 3, player) != grid.end();
  99. }
  100.  
  101. void TicTacToe::draw() const
  102. {
  103. //Creating a onscreen grid
  104. std::cout << ' ';
  105. for (std::size_t i = 1; i <= mDim; ++i)
  106. std::cout << " " << i;
  107.  
  108. int j = 0;
  109. char A = 'A';
  110. column<mDim>::argument_type input;
  111.  
  112. for (auto& i : grid)
  113. {
  114. int x = j++;
  115. if (column<mDim>(0)(input = x ))
  116. std::cout << "n " << A++;
  117.  
  118. std::cout << ' ' << i << ' ';
  119. }
  120. std::cout << "nn";
  121. }
  122.  
  123. void TicTacToe::turn(char player)
  124. {
  125. char row = 0;
  126. char column = 0;
  127. std::size_t position = 0;
  128. bool applied = false;
  129.  
  130. std::cout << "n" << player << ": Please play. n";
  131.  
  132. while (!applied)
  133. {
  134. std::cout << "Row(1,2,3,...): ";
  135. std::cin >> row;
  136. std::cout << player << ": Column(A,B,C,...): ";
  137. std::cin >> column;
  138.  
  139. position = mDim * (std::toupper(column) - 'A') + (row - '1');
  140.  
  141. if (position < grid.size())
  142. {
  143. applied = fill(player, position);
  144.  
  145. if (!applied)
  146. std::cout << "Already Used. Try Again. n";
  147. }
  148. else
  149. {
  150. std::cout << "Invalid position. Try again.n";
  151. }
  152. }
  153.  
  154. std::cout << "nn";
  155. }
  156.  
  157. class Game
  158. {
  159. public:
  160. Game();
  161. void run();
  162.  
  163. private:
  164. TicTacToe ttt;
  165. std::array<char, 2> players{ { 'X', 'O' } };
  166. int player = 0;
  167.  
  168. std::function<void()> display = std::bind(&TicTacToe::draw, &ttt);
  169. std::function<void(char)> turn = std::bind(&TicTacToe::turn, &ttt, std::placeholders::_1);
  170. std::function<bool(char)> win = std::bind(&TicTacToe::check, &ttt, std::placeholders::_1);
  171. std::function<bool()> full = std::bind(&TicTacToe::isFull, &ttt);
  172. };
  173.  
  174. Game::Game()
  175. :ttt()
  176. {
  177.  
  178. }
  179.  
  180. void Game::run()
  181. {
  182. while (!win(players[player]) && !full())
  183. {
  184. player ^= 1;
  185. display();
  186. turn(players[player]);
  187. }
  188.  
  189. display();
  190.  
  191. if (win)
  192. {
  193. std::cout << "n" << players[player] << " is the Winner!n";
  194. }
  195. else
  196. {
  197. std::cout << "nTie game!n";
  198. }
  199. }
  200.  
  201. int main()
  202. {
  203. Game game;
  204. game.run();
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement