-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
117 lines (100 loc) · 2.37 KB
/
player.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
#include"player.h"
#include<iomanip>
#include<fstream>
#include<string.h>
#include "soldier.h"
#include "boom_flag.h"
#include "spy_miner.h"
#include "scout.h"
/* name: constructor */
/* desc: sets the team number, */
/* allocates an array of piece pointers, */
/* and then allocates each of the player's */
/* pieces. flag first, then set up in order from
1's to 10's. */
/* then bombs, then flag. */
/* params: int team */
/* return: n/a */
/* input file will consist of 40 lines....
each line is a type of piece to be found in the game...
soldier 1
soldier 2
soldier 3
soldier 3
soldier 4
soldier 4
soldier 4
..
..
miner
scout
spy
bomb
flag
*/
player::player(int team, char * filename, image * h, image **r)
{
_team = team;
_pieces = new piece*[40];
assert(_pieces);
ifstream in(filename, ios::in);
if ( ! in )
{
cerr<<"Could not open "<<filename<<endl;
exit(1);
}
int i, rank;
char type[1024];
for ( i = 0; i < 40; i ++ )
{
in>>type;
if ( strcmp(type, "soldier") == 0 )
{
in>>rank;
_pieces[i] = new soldier(team, rank, h, r[rank-1]);
}
else if ( strcmp(type, "miner") == 0 )
_pieces[i] = new miner(team,h, r[MINER_R]);
else if ( strcmp(type, "bomb") == 0 )
_pieces[i] = new bomb(team,h, r[BOMB_R]);
else if ( strcmp(type, "spy") == 0 )
_pieces[i] = new spy(team,h, r[SPY_R]);
else if ( strcmp(type, "flag") == 0 )
_pieces[i] = new flag(team,h, r[FLAG_R]);
else if ( strcmp(type, "scout") == 0 )
_pieces[i] = new scout(team,h, r[SCOUT_R]);
assert(_pieces[i]);
}
in.close();
}
/* name: destructor */
/* desc: deletes the pieces, then the array. */
/* params: none */
/* return: n/a */
player::~player()
{
int i;
for ( i = 0; i < 40; i ++ )
delete _pieces[i];
delete [] _pieces;
}
/* name: set up */
/* desc: places the player's peices on the board. */
/* places each piece one at a time. gets the x,y of a */
/* peice, then places it on the board. */
/* params: board * b */
/* return: void */
/* 1) place all pieces on the board in order */
/* 2) randomly swap two pieces 1000 times. */
void player::set_up(board *b, stratego * g)
{
int i, r, c;
for ( i = 0; i < 40; )
{
r = rand() % 4 + _team * 6;
c = rand() % COLS;
_pieces[i]->reset();
if ( b->put(c, r, _pieces[i]) )
i++;
}
}