-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_from_file.cpp
More file actions
46 lines (43 loc) · 1.15 KB
/
load_from_file.cpp
File metadata and controls
46 lines (43 loc) · 1.15 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
#pragma once
#include "table.hpp"
// board has SIZE x SIZE elements
// 4 of them are initialized
// it makes SIZE * SIZE - 4
// moves are saved row.column, format
// so it is multiplied by 4
#define LENGTH (SIZE * SIZE - 4) * 4
/**
* @brief get moves from file and return them as a vector
*
* @param none
*
* @return vector of moves from file
*/
vector<coor> Table::loadFromFile(string fileName)
{
vector<coor> moves;
coor c;
ifstream file;
char text[LENGTH];
// copy the moves from the file to string
file.open(fileName);
file.getline(text, LENGTH);
file.close();
char *ptr;
// the delimiter is a dot (.)
// because rows end with a dot
// use strtok() function to separate string using delimiter
ptr = strtok(text, ".");
while (ptr != NULL)
{
// use stoi() function to convert string to integer
c.row = stoi(ptr);
// the delimiter is a comma (,)
// because columns end with a comma
ptr = strtok(NULL, ",");
c.col = stoi(ptr);
moves.push_back(c);
ptr = strtok(NULL, ".");
}
return moves;
}