-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
166 lines (132 loc) · 4.3 KB
/
Copy pathmain.cpp
File metadata and controls
166 lines (132 loc) · 4.3 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <system_error>
#include "GameState.h"
#include "Graph.h"
#include "Vertex.h"
#include <Queue.h>
using namespace std;
Vec validMove(GameState game){
for (int i = 0; i < game.size; i++){
for (int j = 0; j < game.size; j++){
if (game.grid[i][j] == -1){
return Vec(i, j);
}
}
}
return Vec(0,0);
}
int getReward(Vertex<GameState>& successor, int depth, int player){
//takes in a vertex and returns the score using minmax algorithm
if (depth == 0 || successor.data.done == true){
//evaluate the final score
if (successor.data.hasWon(0)){
//x has won
return 100;
}
if (successor.data.hasWon(1)){
//o has won
return -100;
}
else{
return 0;
}
}
if(player == 0){
//player X is the maximizing player
int max = -101;
// for each child of vertex
for(int i = 0; i < successor.neighbors.size(); i++){
int x = getReward(*(successor.neighbors[i]->location), depth - 1 , 1);
max = (max > x) ? max : x;
}
return max;
}
else{
//player O is the minimizing player
int min = 101;
//for each child of vertex
for(int i = 0; i < successor.neighbors.size(); i++){
int x = getReward(*(successor.neighbors[i]->location), depth - 1 , 0);
min = (min < x) ? min : x;
}
return min;
}
}
Graph<GameState> findGraph(GameState& game){
Graph<GameState> stateSpace;
Vertex<GameState>* start = new Vertex<GameState>(game);
stateSpace.addVertex(start);
Queue<Vertex<GameState>*> frontier;
frontier.enqueue(start);
while (!frontier.isEmpty()){
Vertex<GameState>* curr = frontier.dequeue();
if (!curr->data.done){
for(int i = 0; i < game.size; i++){
for (int j = 0; j < game.size; j++){
if(curr->data.grid[i][j] == -1){
GameState next = curr->data;
next.play(i, j);
Vertex<GameState>* successor = new Vertex<GameState>(next);
stateSpace.addVertex(successor);
stateSpace.addDirectedEdge(curr, successor);
if (!successor->data.done){
frontier.enqueue(successor);
}
}
}
}
}
}
return stateSpace;
// for every successor of start, call getReward(successor, player1)
//return Vec(0,0);
}
Vec findBestMove(GameState& game){
auto data = findGraph(game);
auto successor = *(data.vertices[0]);
Vec bestMove = data.vertices[0]->neighbors[0]->location->data.lastMove;
auto bestReward = getReward(*(successor.neighbors[0])->location, 9 - game.turnCount, !game.currentTurn);
for(int i = (successor.neighbors.size()) - 1; i >= 0; i--){
auto reward = getReward(*(successor.neighbors[i])->location, 9 - game.turnCount, !game.currentTurn);
std::cout << reward << " : " << data.vertices[0]->neighbors[i]->location->data.lastMove << std:: endl;
if(reward > bestReward){
bestReward = reward;
bestMove = data.vertices[0]->neighbors[i]->location->data.lastMove;
}
}
return bestMove;
}
int main(){
GameState game;
while(!game.done){
//system("clear");
cout << game << endl;
int x, y;
if (!game.currentTurn){
Vec move = findBestMove(game);
x = move.x;
y = move.y;
}
else{
cout << endl;
//auto jelly = findGraph(game);
//std::cout << getReward(*(jelly.vertices[0]), 9 - game.turnCount, game.currentTurn) << std::endl;
cout << "Enter move for (" << (!game.currentTurn ? "X" : "O") << "): ";
cin >> x >> y;
}
game.play(x, y);
}
system("clear");
cout << game << endl;
cout << endl;
if (game.hasWon(0)){
cout << "Player X has won" << endl;
}
else if (game.hasWon(1)){
cout << "Player O has won" << endl;
}
else {
cout << "It's a tie" << endl;
}
return 0;
}