dumbo
A fun little game engine.
 All Classes
board.h
1 /*
2  * Copyright (c) 2019. David Fridovich-Keil.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Author: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 //
39 // Tic tac toe board.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #ifndef DUMBO_TIC_TAC_TOE_BOARD_H
44 #define DUMBO_TIC_TAC_TOE_BOARD_H
45 
46 #include <dumbo/core/game_state.h>
47 #include <dumbo/tic_tac_toe/square.h>
48 
49 #include <tuple>
50 #include <unordered_set>
51 #include <vector>
52 
53 namespace dumbo {
54 namespace tic {
55 
56 class Board : public core::GameState<Square> {
57  public:
58  ~Board() {}
59  Board(bool my_turn = true) : Board({}, my_turn) {}
60  Board(const std::unordered_set<Square, Square::Hasher>& occupied_squares,
61  bool my_turn = true);
62 
63  // Enumerate all legal moves from this game state.
64  std::vector<Square> LegalMoves() const { return empty_squares_; }
65 
66  // Choose a random move from this game state.
67  Square RandomMove() const;
68 
69  // Populate the GameState that occurs when the current player plays
70  // the given move. Returns whether or not the move was legal.
71  bool NextState(const Square& move, GameState* next_state) const;
72 
73  // Is this a terminal state? If so, return whether it is a win/loss/draw
74  // (encoded as 1.0/0.0/0.5).
75  bool IsTerminal(double* win) const;
76 
77  // Check if it is the AI's turn or not.
78  bool IsMyTurn() const { return my_turn_; }
79 
80  // Check if same as another game state.
81  bool operator==(const GameState<Square>& rhs) const;
82  // Render.
83  void Render() const;
84 
85  private:
86  // Check to make sure this is a valid board configuration.
87  void Check() const;
88 
89  // Set of occupied and empty squares on the board. Occupied squares stored as
90  // unordered set but empty squares stored as vector for efficiency of
91  // legality and random selection operations.
92  std::unordered_set<Square, Square::Hasher> occupied_squares_;
93  std::vector<Square> empty_squares_;
94 
95  // Set of possible winning and losing three-in-a-rows.
96  typedef std::tuple<Square, Square, Square> SquareTuple;
97  static const std::vector<SquareTuple> winning_sets_;
98  static const std::vector<SquareTuple> losing_sets_;
99 }; //\class Board
100 
101 } // namespace tic
102 } // namespace dumbo
103 
104 #endif