-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileIO.cpp
152 lines (125 loc) · 4.17 KB
/
FileIO.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 <string>
#include <map>
#include <vector>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <algorithm>
#include <ctime>
#include "FileIO.h"
using namespace std;
// this function saves the game state in a text file
// this includes the board , player counter, mode of the game, difficulty of the game, and if the bot is player 1 or 2
// singleplayer and multiplayer is saved in different text files
void save_game_state(int counter, string mode, int difficulty, int bot_pos, map<char, vector<char>> tables)
{
ofstream game_file;
if (mode == "1")
game_file.open("GameStateSingle.txt");
else
game_file.open("GameStateMulti.txt");
game_file << counter - 1;
game_file << endl;
game_file << difficulty;
game_file << endl;
game_file << bot_pos;
game_file << endl;
map<char, vector<char>>::iterator itr;
for (itr = tables.begin();itr != tables.end();itr++)
{
game_file << itr->first << endl;
vector<char> vect_temp = itr->second;
vector<char>::iterator itr2;
for (itr2 = vect_temp.begin();itr2 != vect_temp.end();itr2++)
{
game_file << *itr2 << endl;
}
}
game_file.close();
return;
}
// this function loads the game state from a text file
// this includes the board , player counter, mode of the game, difficulty of the game, and if the bot is player 1 or 2
void load_game_state(int& counter, string mode, int& difficulty, int& bot_pos, map<char, vector<char>>& tables)
{
ifstream game_file;
cout << "... Loading File ...\n";
if (mode == "1")
game_file.open("GameStateSingle.txt");
else
game_file.open("GameStateMulti.txt");
map<char, vector<char>>::iterator itr;
vector<char> vect_temp;
char input;
char board;
game_file >> counter;
game_file >> difficulty;
game_file >> bot_pos;
while (game_file >> board)
{
for (int i = 0;i < 9;i++)
{
game_file >> input;
vect_temp.push_back(input);
}
tables[board] = vect_temp;
vect_temp.clear();
}
// checks if the grids are empty
if (tables.empty())
{
cout << "No Previous Saves\n\n";
cout << "Starting New Game ...\n\n";
vector<char> vect = { '0', '1', '2', '3', '4', '5', '6', '7', '8' };
string diff;
if (mode == "2")
cout << "Please select number of boards:\n[3]: Enter 1\t[4]: Enter 2\t[5]: Enter 3\nNumber of boards: ";
else
cout << "Please select difficulty level\n[Easy]: Enter 1 [Medium]: Enter 2 [Difficult]: Enter 3\n\nDifficulty: ";
cin >> diff;
cout << "\n";
// validate difficulty
while (true)
{
if (diff == "1" || diff == "2" || diff == "3")
break;
if (mode == "2")
cout << "Please select number of boards:\n[3]: Enter 1\t[4]: Enter 2\t[5]: Enter 3\nNumber of boards: ";
else
cout << "Please select difficulty level\n[Easy]: Enter 1 [Medium]: Enter 2 [Difficult]: Enter 3\n\nDifficulty: ";
cin >> diff;
}
difficulty = stoi(diff);
if (mode == "1")
{
string input;
cout << "Would you like to go first?\n[yes/no]: ";
cin >> input;
while (true)
{
if (input == "yes")
{
bot_pos = 2;
counter = 0;
break;
}
else if (input == "no")
{
bot_pos = 1;
counter = 1;
break;
}
cout << "Would you like to go first?\n[yes/no]: ";
cin >> input;
}
}
cout << endl;
for (int i = 0; i < 2 + difficulty; i++) {
tables[(char)('A' + i)] = vect;
}
}
game_file.close();
return;
}