-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBlackHole.cpp
88 lines (79 loc) · 1.83 KB
/
BlackHole.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
#include "BlackHole.h"
#include "Galaxy.h"
BlackHole::BlackHole(QTextStream &stream, unsigned id):_id(id)
{
const static QMap<QString,int> bhOptions={
{"Star1Id",0},
{"Star2Id",1},
{"TurnsToClose",2}
};
int nesting=0;
QString line = stream.readLine().trimmed();
while (!line.isNull())
{
nesting+=line.contains('{');
nesting-=line.contains('}');
if(line.startsWith("HoleId"))
{
nesting-=1;
QStringRef idRef(&line,6,line.indexOf(' ')-6);
_id=idRef.toInt();
}
if(nesting<0)
{
break;
}
QString varname=line.section('=',0,0);
QString value=line.section('=',-1);
switch(bhOptions.value(varname,-1))
{
case 0://Star1Id
_star1Id=value.toInt();
break;
case 1://Star2Id
_star2Id=value.toInt();
break;
case 2://TurnsToClose
_turnsToClose=value.toInt();
break;
default:
//skip record
break;
}
line = stream.readLine().trimmed();
}
}
unsigned BlackHole::id() const
{
return _id;
}
unsigned BlackHole::star1Id() const
{
return _star1Id;
}
unsigned BlackHole::star2Id() const
{
return _star2Id;
}
int BlackHole::turnsToClose() const
{
return _turnsToClose;
}
void readBlackHoles(QTextStream &stream,Galaxy &galaxy)
{
QString line = stream.readLine().trimmed();;
while (!line.isNull())
{
if(line.contains("HoleId"))
{
QStringRef idRef(&line,6,line.indexOf(' ')-6);
unsigned id=idRef.toInt();
galaxy.addBlackHole(BlackHole(stream, id));
}
else if(line.contains('}'))
{
break;
}
line = stream.readLine().trimmed();
}
}