Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool haswon(int64_t board)
- {
- int64_t y = board & (board >> 7);
- if (y & (y >> 2 * 7)) // check diagonal
- return true;
- y = board & (board >> 8);
- if (y & (y >> 2 * 8)) // check horizontal -
- return true;
- y = board & (board >> 9);
- if (y & (y >> 2 * 9)) // check / diagonal
- return true;
- y = board & (board >> 1);
- if (y & (y >> 2)) // check vertical |
- return true;
- return false;
- }
- #include <iostream>
- #include <bitset>
- namespace tictactoe {
- constexpr std::size_t N = 3;
- using board = std::bitset<N*N>;
- constexpr char NOUGHT = 'O';
- constexpr char CROSS = 'X';
- bool curr_move_noughts = true;
- board combined() {
- return noughts | crosses; // combined bit board for both players
- }
- }
- 10000001
- 00000010
- 00000100
- 00000000
- 00000000
- 00000000
- 00000000
- 00000000
- +---+---+---+
- | 0 | 1 | 2 |
- +---+---+---+
- | 3 | 4 | 5 |
- +---+---+---+
- | 6 | 7 | 8 |
- +---+---+---+
- bool line_is_full (Board* p_board, char p_player, int p1, int p2, int p3)
- {
- return ( (p_board [p1] == p_player)
- && (p_board [p2] == p_player)
- && (p_board [p3] == p_player));
- }
- bool board_has_win (Board* p_board, char p_player)
- {
- return ( line_is_full (p_board, p_player, 0, 1, 2)
- || line_is_full (p_board, p_player, 3, 4, 5)
- || line_is_full (p_board, p_player, 6, 7, 8)
- || line_is_full (p_board, p_player, 0, 3, 6)
- || line_is_full (p_board, p_player, 1, 4, 7)
- || line_is_full (p_board, p_player, 2, 5, 8)
- || line_is_full (p_board, p_player, 0, 4, 8)
- || line_is_full (p_board, p_player, 2, 4, 6));
- }
- 0 .... All three cells empty
- 1 .... One O, two empty cells
- 2 .... Two Os, one empty cell
- 3 .... Three Os - O wins
- 4 .... One X, two empty cells
- 5 .... One X, one O, one empty cell
- 6 .... One X, two Os
- 7 .... Impossible
- 8 .... Two Xs, one empty cell
- 9 .... Two Xs, one O
- 10 .... Impossible
- 11 .... Impossible
- 12 .... Three Xs - X wins
- 13+ ... Impossible
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement