dumbo
A fun little game engine.
 All Classes
game_state.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 // Base class for all game states.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #ifndef DUMBO_CORE_GAME_STATE_H
44 #define DUMBO_CORE_GAME_STATE_H
45 
46 #include <dumbo/core/move.h>
47 
48 #include <memory>
49 #include <random>
50 
51 namespace dumbo {
52 namespace core {
53 
54 template <typename M>
55 class GameState {
56  public:
57  virtual ~GameState() {}
58 
59  // Enumerate all legal moves from this game state.
60  virtual std::vector<M> LegalMoves() const = 0;
61 
62  // Choose a random move from this game state.
63  virtual M RandomMove() const = 0;
64 
65  // Populate the GameState that occurs when the current player plays
66  // the given move. Returns whether or not the move was legal.
67  virtual bool NextState(const M& move, GameState* next_state) const = 0;
68 
69  // Is this a terminal state? If so, return whether it is a win/loss/draw
70  // (encoded as 1.0/0.0/0.5).
71  virtual bool IsTerminal(double* win) const = 0;
72 
73  // Check if it is the AI's turn or not.
74  bool IsMyTurn() const { return my_turn_; }
75 
76  // Check if same as another game state.
77  virtual bool operator==(const GameState<M>& rhs) const = 0;
78 
79  // Render.
80  virtual void Render() const = 0;
81 
82  protected:
83  GameState(bool my_turn = true) : my_turn_(my_turn) {}
84 
85  // Is it my turn?
86  bool my_turn_;
87 
88  // Random number generation.
89  static std::random_device rd_;
90  static std::default_random_engine rng_;
91 }; //\class GameState
92 
93 // Define static variables.
94 template <typename M>
95 std::random_device GameState<M>::rd_;
96 
97 template <typename M>
98 std::default_random_engine GameState<M>::rng_(GameState<M>::rd_());
99 
100 } // namespace core
101 } // namespace dumbo
102 
103 #endif