-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadstuff.cpp
More file actions
115 lines (92 loc) · 2.49 KB
/
Copy pathreadstuff.cpp
File metadata and controls
115 lines (92 loc) · 2.49 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
107
108
109
110
111
112
113
114
115
/*
* Read pieces from text files, and do some operations on them.
*/
#include <algorithm>
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include "Board.hpp"
#include "colors.hpp"
#include "Piece.hpp"
using namespace std;
Piece readpiecefromfile(string filename);
vector<vector<Piece> > readpieces()
{
int N_pieces = 12;
string piecenames("ABCDEFGHIJKL");
//int colors[] = {208, 196, 17, 212, 22, 15, 14, 127, 226, 53, 46, 240};
vector<vector<Piece> > pieces(N_pieces, vector<Piece>());
for (int ii = 0; ii < N_pieces; ii++)
{
// Read piece from files
ostringstream filename;
filename << "gamedata/" << piecenames.at(ii);
Piece pbuf = readpiecefromfile(filename.str());
pbuf.name = piecenames.at(ii);
pbuf.color = COLORS[ii];
pbuf.number = ii;
// Find all rotations, flips etc.
// This relies on std::set, Piece::operator<() and operator==().
set<Piece> pset;
pset.insert(pbuf);
for (int kk = 0; kk < 3; kk++)
{
pbuf.rot90();
pset.insert(pbuf);
}
pbuf.flip();
pset.insert(pbuf);
for (int kk = 0; kk < 3; kk++)
{
pbuf.rot90();
pset.insert(pbuf);
}
pieces[ii].assign(pset.begin(), pset.end());
}
return pieces;
}
Piece readpiecefromfile(string filename)
{
ifstream piecefile(filename.c_str());
Piece pbuf;
char linebuf[100];
// Read shape of the piece from file
int row = 0;
while(piecefile.getline(linebuf, 100) && row < 9)
{
string line(linebuf);
for(int col = 0; col < min(5, (int)line.length()); col++)
{
if (line.at(col) == ' ')
pbuf.shape(row, col) = 0;
else
pbuf.shape(row, col) = 1;
}
row++;
}
piecefile.close();
return pbuf;
}
Board readboard(string filename)
{
Board board;
vector<vector<Piece> > pieces = readpieces();
ifstream infile(filename.c_str());
char linebuf[100];
while(infile.getline(linebuf, 100))
{
int pieceid, piecevar, crow, ccol;
string line(linebuf);
istringstream linestream(line);
linestream >> pieceid;
linestream >> piecevar;
linestream >> crow;
linestream >> ccol;
board.placePiece(pieces[pieceid][piecevar], crow, ccol);
}
infile.close();
return board;
}