dumbo
A fun little game engine.
 All Classes
player.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 // Generic game playing client. Manages game from initial board configuration
40 // to final result. Templated on move type M, game state type G.
41 //
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #ifndef DUMBO_CORE_PLAYER_H
45 #define DUMBO_CORE_PLAYER_H
46 
47 #include <dumbo/core/game_state.h>
48 #include <dumbo/core/move.h>
49 #include <dumbo/core/solver.h>
50 
51 #include <glog/logging.h>
52 #include <iostream>
53 #include <memory>
54 
55 namespace dumbo {
56 namespace core {
57 
58 template <typename M, typename G>
59 class Player {
60  public:
61  ~Player() {}
62  Player(const G& initial_state, std::unique_ptr<Solver<M, G>> solver)
63  : state_(initial_state), solver_(std::move(solver)) {
64  CHECK_NOTNULL(solver_.get());
65  }
66 
67  // Play out the rest of the game.
68  void Play();
69 
70  private:
71  // Current state of the game.
72  G state_;
73 
74  // Solver.
75  std::unique_ptr<Solver<M, G>> solver_;
76 }; //\class Player
77 
78 // ----------------------------- IMPLEMENTATION ---------------------------- //
79 
80 template <typename M, typename G>
81 void Player<M, G>::Play() {
82  double win = 0.0;
83 
84  // Play until the game is over.
85  while (!state_.IsTerminal(&win)) {
86  state_.Render();
87  G next_state;
88 
89  // If it's our move, then just play.
90  if (state_.IsMyTurn()) {
91  std::cout << "My turn." << std::endl;
92  const M move = solver_->Run(state_);
93  std::cout << move << std::endl;
94 
95  CHECK(state_.NextState(move, &next_state));
96  } else {
97  // Ask for a move from the human.
98  std::cout << "Please input a 0-indexed row [ENTER] and column [ENTER]."
99  << std::endl;
100 
101  M move;
102  std::cin >> move;
103  std::cout << move << std::endl;
104 
105  // Try to take this move. If not legal, then ask for another move.
106  while (!state_.NextState(move, &next_state)) {
107  std::cout << "Illegal move. Please try again." << std::endl;
108  std::cin >> move;
109  std::cout << move << std::endl;
110  }
111  }
112 
113  // Update current board state.
114  state_ = next_state;
115  }
116 
117  // Print result.
118  state_.Render();
119  if (win < 0.5)
120  std::cout << "Congratulations! You won the game." << std::endl;
121  else if (win > 0.5)
122  std::cout << "Sorry. You lost the game." << std::endl;
123  else
124  std::cout << "Tie game." << std::endl;
125 }
126 
127 } // namespace core
128 } // namespace dumbo
129 
130 #endif