-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetravex.cc
98 lines (82 loc) · 1.85 KB
/
tetravex.cc
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
#include "tetravex.hh"
#include <fstream>
#include <iostream>
#include <sstream>
int get_after_slash_last_position(std::string str)
{
int pos = str.find_last_of('/');
if (pos == std::string::npos)
return 0;
return pos + 1;
}
Tetravex::Tetravex(std::string filename)
{
int slash_pos = get_after_slash_last_position(filename);
int size = filename[slash_pos + 1] - '0';
if (size < 2 || size > 6)
{
std::cout << "Invalid size : " << size << " for file : " << filename << std::endl;
return;
}
this->size = size;
std::ifstream file(filename);
if (!file.is_open())
{
std::cout << "Unable to open file" << std::endl;
return;
}
tiles = new Tile[size * size];
std::string line;
int i = 0;
while (std::getline(file, line))
{
Tile& tile = tiles[i++];
if (line.empty())
continue;
tile.is_locked = line.back() == '@';
for (int i = 0; i < 4; ++i)
tile.value[i] = line[i] - '0';
}
file.close();
if (i != size * size)
{
delete[] tiles;
std::cout << "Invalid file" << std::endl;
}
}
Tetravex::Tetravex(const Tetravex& other)
{
size = other.size;
tiles = new Tile[size * size];
for (int i = 0; i < size * size; ++i)
{
for (int j = 0; j < 4; ++j)
tiles[i].value[j] = other.tiles[i].value[j];
tiles[i].is_locked = other.tiles[i].is_locked;
}
}
void Tetravex::write(std::string file_out) const
{
std::ofstream file(file_out);
if (!file.is_open())
{
std::cout << "Unable to open file" << std::endl;
return;
}
for (int i = 0; i < size * size; ++i)
{
Tile& tile = tiles[i];
for (int j = 0; j < 4; ++j)
file << tile.value[j];
file << std::endl;
}
file.close();
}
Tile& Tetravex::get_tile(int x, int y) const
{
return tiles[x + y * size];
}
void Tetravex::swap_tiles(int a, int b)
{
std::swap(tiles[a], tiles[b]);
}