-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
146 lines (108 loc) · 3.83 KB
/
main.cpp
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
#include "game_engine/game.hpp"
#include "game_engine/piece.hpp"
#include "networkinterface.h"
#include "PlayerInterface.hpp"
#include <cstdlib>
std::vector<std::string> parseRequest(std::string request)
{
std::vector<std::string> str_list;
std::string tmp = "";
for (char c : request)
{
if (c == ',')
{
str_list.push_back(tmp);
tmp = "";
continue;
} else if (c == '\n')
{
continue;
}
else
{
tmp += c;
}
}
str_list.push_back(tmp);
return str_list;
}
void assignColors(PlayerSPtr p1, PlayerSPtr p2)
{
srand(time(NULL));
int rand_num = rand() % 2;
if (rand_num)
{
p1->isWhite = Color::white;
p2->isWhite = Color::black;
}
else
{
p1->isWhite = Color::black;
p2->isWhite = Color::white;
}
}
int main(int argc, char *argv[])
{
/* ############# SETUP #############
Setup necessary irrespective of player type (human or computer) or client type (GUI, terminal, browser)
*/
PlayerInterface p1(std::make_shared<Player>());
PlayerInterface p2(std::make_shared<Player>());
assignColors(p1.player, p2.player);
// Player 1 is always human
p1.player->isHuman = PlayerType::human;
p1.interface = std::make_shared<NetworkInterface>();
/* ############# CLIENT HANDSHAKE #############
Waits for connection from first player (always human). Upon connection, client will tell
the server it wants to play and its client type (GUI, terminal, browser). If the player is
the first to connect, the server will ask if the client wants to player single or multiplayer.
Otherwise, server will respond with 'occupied'.
*/
p1.interface->initSocket(12819);
p1.interface->waitForResponse();
std::vector<std::string> p1_msg = parseRequest(p1.interface->readRequest());
if (p1_msg[0] == "I want to play")
{
p1.interface->setMode(std::stoi(p1_msg[1])); // CHANGE TO SHORT
// Send message where client will choose b/w playing AI or PvP
p1.interface->sendResponse("p1 choice");
p1.interface->waitForResponse();
std::string request = p1.interface->readRequest();
if (request[0] == '0')
{
// Singleplayer
std::cout << "[Server] starting game!\n";
//throw std::exception(); // (Stockfish integration not yet supported)
}
else if (request[0] == '1')
{
// Wait for second player
std::cout << "[Server] waiting for second player...\n";
}
else
{
throw std::exception();
}
}
/* ############# GAME BEGINS #############
The game logic from this point forward should be identical for all gamemodes and client types,
regardless whether Player vs AI or Player vs Player, or GUI vs terminal, GUI vs browser, etc...
*/
GameEngine engine(p1.player, p2.player);
while (true)
{
p1.interface->sendResponse(engine.GAMEBOARD->print_board());
auto move = p1.interface->getUserMove(static_cast<bool>(p1.player->isWhite));
engine.GAMEBOARD->take_turn(p1.player, move);
// PLEASE NOTE THAT ONLY 1 CLIENT IS POLLED FOR DEBUGGING
p1.interface->sendResponse(engine.GAMEBOARD->print_board());
move = p1.interface->getUserMove(static_cast<bool>(p2.player->isWhite));
engine.GAMEBOARD->take_turn(p2.player, move);
// Synchronize game state with client(s)
// (BOTH) Handle WIN/STALEMATE/CHECK condition
//engine.GAMEBOARD->print_board();
//engine.GAMEBOARD->take_turn(engine.black_player);
// Synchronize game state with client(s)
// (BOTH) Handle WIN/STALEMATE/CHECK condition
}
}