-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
82 lines (73 loc) · 1.4 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
//
// Player.cpp
// Project1
//
// Created by Atibhav Mittal on 1/6/16.
// Copyright © 2016 ati. All rights reserved.
//
#include <iostream>
#include "Arena.h"
#include "Player.h"
using namespace std;
///////////////////////////////////////////////////////////////////////////
// Player implementations
///////////////////////////////////////////////////////////////////////////
Player::Player(Arena* ap, int r, int c)
{
if (ap == nullptr)
{
cout << "***** The player must be in some Arena!" << endl;
exit(1);
}
if (r < 1 || r > ap->rows() || c < 1 || c > ap->cols())
{
cout << "**** Player created with invalid coordinates (" << r
<< "," << c << ")!" << endl;
exit(1);
}
m_arena = ap;
m_row = r;
m_col = c;
m_age = 0;
m_dead = false;
}
int Player::row() const
{
return m_row;
}
int Player::col() const
{
return m_col;
}
int Player::age() const
{
return m_age;
}
void Player::stand()
{
m_age++;
}
void Player::moveOrAttack(int dir)
{
m_age++;
int r = m_row;
int c = m_col;
if (m_arena->determineNewPosition(r, c, dir))
{
if (m_arena->nRobotsAt(r, c) > 0)
m_arena->attackRobotAt(r, c, dir);
else
{
m_row = r;
m_col = c;
}
}
}
bool Player::isDead() const
{
return m_dead;
}
void Player::setDead()
{
m_dead = true;
}