Repository navigation
Expand file tree
/
Copy pathgame.cpp
More file actions
111 lines (96 loc) · 2.21 KB
/
Copy pathgame.cpp
File metadata and controls
111 lines (96 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "game.hpp"
ostream &operator << ( ostream &os, const Game &g )
{
int i, n = g.theGame_.size();
list<Board>::const_iterator p = g.theGame_.begin();
#ifdef DEBUG
cout << "trying to print a game." << endl;
#endif
cout << "=====================================Game Number: " << g.getNumWins() << endl;
for ( i = 0; (i < n) && (p != g.theGame_.end()); i++, ++p )
{
os << *p;
}
return os;
}
Game::Game(): numMoves_(0), numGames_(0), numWins_(0)
{
int i;
for ( i = 0; i < numHoles(); i++ )
stats[i] = 0;
theGame_.push_back ( Board() );
}
Game::~Game()
{
}
void Game::play()
{
int i, j, k;
int numPegsLeft = 0;
int numMoves = 0;
Board q, p;
#ifdef DEBUG
cout << "in Game.play" << endl;
#endif
if ( theGame_.size() == 0 )
{
cout << "No back!" << endl;
exit(0);
}
p = theGame_.back();
for ( i = 0; i < Board::numRows; i++ )
{
for ( j = 0; j < i+1; j++ )
{
if ( p.holes_[i][j].contents_ == Empty )
continue;
else if ( p.holes_[i][j].contents_ == Full )
numPegsLeft++;
else
continue;
for ( k = 0; k < Hole::numDirs; k++ )
{
#ifdef DEBUG
cout << "checking " << k << ". i:" << i << " j:" << j << endl;
#endif
if ( p.holes_[i][j].adj_[k] != NULL )
{
if ( p.holes_[i][j].adj_[k]->adj_[k] != NULL )
{
// make copy of the board
q = p;
if ( q.jump( q.holes_[i][j], q.holes_[i][j].adj_[k],
q.holes_[i][j].adj_[k]->adj_[k] ) )
{
// add it to the list
theGame_.push_back( q );
play();
numMoves_++;
numMoves++;
// remove that last board position
theGame_.pop_back();
}
}
}
}
}
}
#ifdef DEBUG
cout << "finished all directions" << endl;
#endif
if ( numMoves == 0 ) // end of game
{
numGames_++;
stats[ numPegsLeft ]++;
//if ( numPegsLeft > maxNumLeft_ )
//maxNumLeft_ = numPegsLeft;
}
if ( numPegsLeft == 1 ) // we win!
{
numWins_++;
cout << *this;
}
#ifdef DEBUG
cout << "Exiting Game.play" << endl;
#endif
}