-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexampleRobot.cpp
More file actions
107 lines (75 loc) · 1.72 KB
/
exampleRobot.cpp
File metadata and controls
107 lines (75 loc) · 1.72 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
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <filesystem>
using namespace std;
char map[200][200];
//example helper
void moveUp(std::ofstream &response)
{
response << "U ";
};
void mineUP(std::ofstream &response)
{
response << "M U";
};
int main()
{
int id = 0;
std::cout << "enter rover id: ";
std::cin >> id;
int round = 0;
while (true)
{
std::string serverFileName = "game/s" + std::to_string(id) + "_" + std::to_string(round) +
".txt";
std::ifstream input(serverFileName);
if (input)
{
//it is our turn to move
//read the file...
{
int mapSizeX, mapSizeY;
input >> mapSizeX;
input >> mapSizeY;
for (int y = 0; y < mapSizeY; y++)
for (int x = 0; x < mapSizeX; x++)
{
char b;
input >> b;
map[x][y] = b;
}
int roverPosX = 0;
int roverPosY = 0;
input >> roverPosX;
input >> roverPosY;
//read the rest
}
input.close();
//write the response back into a temporary file
std::string ourFileNameTemp = "game/c" + std::to_string(id) + "_" + std::to_string(round) + "temp.txt";
std::string ourFileName = "game/c" + std::to_string(id) + "_" + std::to_string(round) + ".txt";
std::ofstream response(ourFileNameTemp);
//..
//write your rover commands here
//
{
//move up move up mine up mine left
response << "U U M U M L\n";
}
//close the file
response.close();
//rename the temporary file to the actual final file name.
std::error_code errorCode;
std::filesystem::rename(ourFileNameTemp, ourFileName, errorCode);
//increment the round
round++;
}
else
{
//waiting for other players to make their moves
}
}
return 0;
}