Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. bool haswon(int64_t board)
  2. {
  3. int64_t y = board & (board >> 7);
  4. if (y & (y >> 2 * 7)) // check diagonal
  5. return true;
  6. y = board & (board >> 8);
  7. if (y & (y >> 2 * 8)) // check horizontal -
  8. return true;
  9. y = board & (board >> 9);
  10. if (y & (y >> 2 * 9)) // check / diagonal
  11. return true;
  12. y = board & (board >> 1);
  13. if (y & (y >> 2)) // check vertical |
  14. return true;
  15. return false;
  16. }
  17.  
  18. #include <iostream>
  19. #include <bitset>
  20.  
  21. namespace tictactoe {
  22.  
  23. constexpr std::size_t N = 3;
  24.  
  25. using board = std::bitset<N*N>;
  26.  
  27. constexpr char NOUGHT = 'O';
  28. constexpr char CROSS = 'X';
  29.  
  30. bool curr_move_noughts = true;
  31.  
  32. board combined() {
  33. return noughts | crosses; // combined bit board for both players
  34. }
  35.  
  36. }
  37.  
  38. 10000001
  39. 00000010
  40. 00000100
  41. 00000000
  42. 00000000
  43. 00000000
  44. 00000000
  45. 00000000
  46.  
  47. +---+---+---+
  48. | 0 | 1 | 2 |
  49. +---+---+---+
  50. | 3 | 4 | 5 |
  51. +---+---+---+
  52. | 6 | 7 | 8 |
  53. +---+---+---+
  54.  
  55. bool line_is_full (Board* p_board, char p_player, int p1, int p2, int p3)
  56. {
  57. return ( (p_board [p1] == p_player)
  58. && (p_board [p2] == p_player)
  59. && (p_board [p3] == p_player));
  60. }
  61.  
  62. bool board_has_win (Board* p_board, char p_player)
  63. {
  64. return ( line_is_full (p_board, p_player, 0, 1, 2)
  65. || line_is_full (p_board, p_player, 3, 4, 5)
  66. || line_is_full (p_board, p_player, 6, 7, 8)
  67.  
  68. || line_is_full (p_board, p_player, 0, 3, 6)
  69. || line_is_full (p_board, p_player, 1, 4, 7)
  70. || line_is_full (p_board, p_player, 2, 5, 8)
  71.  
  72. || line_is_full (p_board, p_player, 0, 4, 8)
  73.  
  74. || line_is_full (p_board, p_player, 2, 4, 6));
  75. }
  76.  
  77. 0 .... All three cells empty
  78. 1 .... One O, two empty cells
  79. 2 .... Two Os, one empty cell
  80. 3 .... Three Os - O wins
  81.  
  82. 4 .... One X, two empty cells
  83. 5 .... One X, one O, one empty cell
  84. 6 .... One X, two Os
  85. 7 .... Impossible
  86.  
  87. 8 .... Two Xs, one empty cell
  88. 9 .... Two Xs, one O
  89. 10 .... Impossible
  90. 11 .... Impossible
  91.  
  92. 12 .... Three Xs - X wins
  93. 13+ ... Impossible
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement