-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
152 lines (117 loc) · 3.65 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
147
148
149
150
151
152
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <stdio.h>
#include <fstream>
#include <streambuf>
#include "includes/json.hpp"
using json = nlohmann::json;
#include "Game.h"
#include "Unit.h"
#include "Monster.h"
#include "ScriptIO.h"
#define SLEEP_SECS 0
#define SLEEP_NSECS 200000000
#define SLEEPS_PER_TURN 100
#define CONFINE_TURN_NUMBER 300
static int startup_sleep = 8;
using namespace std;
int main(int argc, char *argv[]) {
if (argc < 4) {
cerr << "not enough args. expected player1script, player2script, mapfile" << endl;
return 1;
} else if (argc == 5) {
startup_sleep = atoi(argv[4]);
}
// initialize the player scripts
start_scripts(argv[1], argv[2]);
struct timespec startupSleep;
startupSleep.tv_sec = startup_sleep;
startupSleep.tv_nsec = 0;
nanosleep(&startupSleep, NULL);
// get the map json string from the file (argv[3])
ifstream t(argv[3]);
string map_str;
t.seekg(0, ios::end);
map_str.reserve(t.tellg());
t.seekg(0, ios::beg);
map_str.assign((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
Game game = Game(map_str, "Player1", "Player2");
json message_map1 = {
{"type", "map"},
{"player_id", 1},
{"map", map_str}
};
json message_map2 = {
{"type", "map"},
{"player_id", 2},
{"map", map_str}
};
write_to_player(1, message_map1);
write_to_player(2, message_map2);
int turn_number = 0;
struct timespec sleepFor;
sleepFor.tv_sec = SLEEP_SECS;
sleepFor.tv_nsec = SLEEP_NSECS;
while (game.get_winner() == NO_WINNER) {
turn_number += 1;
json game_json = game.to_json();
cout << game_json << endl;
//cout << "-----Turn " << turn_number << "-----" << endl;
//game.print_game();
json message_turn = {
{"type", "turn"},
{"turn_number", turn_number},
{"game_data", game_json},
};
json p1_msg = message_turn;
p1_msg["game_data"][1]["Destination"] = -1;
write_to_player(1, p1_msg);
json p2_msg = message_turn;
p2_msg["game_data"][0]["Destination"] = -1;
write_to_player(2, p2_msg);
// get responses from players
Player::Decision p1_dec("");
Player::Decision p2_dec("");
string p1_reply = "";
string p2_reply = "";
int sleeps_done = 0;
bool got_valid_p1_decision = false;
bool got_valid_p2_decision = false;
while (sleeps_done < SLEEPS_PER_TURN && (!got_valid_p1_decision || !got_valid_p2_decision)) {
nanosleep(&sleepFor, NULL);
if (!got_valid_p1_decision) {
p1_reply += read_from_player(1);
p1_dec = Player::Decision(p1_reply);
got_valid_p1_decision = p1_dec.is_valid();
}
if (!got_valid_p2_decision) {
p2_reply += read_from_player(2);
p2_dec = Player::Decision(p2_reply);
got_valid_p2_decision = p2_dec.is_valid();
}
sleeps_done ++;
}
if (!got_valid_p1_decision) {
cerr << "Invalid response from Player1: " << p1_reply << endl;
}
if (!got_valid_p2_decision) {
cerr << "Invalid response from Player2: " << p2_reply << endl;
}
// run the game's turn based on the players' actions
game.do_player_decisions(p1_dec, p2_dec);
if (turn_number < CONFINE_TURN_NUMBER){
game.do_movement_tick();
} else if (turn_number == CONFINE_TURN_NUMBER){
game.move_players_to_start();
}
game.do_damage_tick();
game.do_monster_deaths();
game.do_player_deaths();
}
json j;
j["Winner"] = game.get_winner();
cout << j.dump() << endl;
terminate_scripts();
return 0;
}