-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
113 lines (106 loc) · 2.78 KB
/
Game.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
//
// Game.cpp
// Project1
//
// Created by Atibhav Mittal on 1/6/16.
// Copyright © 2016 ati. All rights reserved.
//
#include "Game.h"
#include "Arena.h"
#include "Player.h"
#include "Robot.h"
#include "History.h"
#include "globals.h"
#include <iostream>
using namespace std;
///////////////////////////////////////////////////////////////////////////
// Game implementations
///////////////////////////////////////////////////////////////////////////
Game::Game(int rows, int cols, int nRobots)
{
if (nRobots < 0)
{
cout << "***** Cannot create Game with negative number of robots!" << endl;
exit(1);
}
if (nRobots > MAXROBOTS)
{
cout << "***** Trying to create Game with " << nRobots
<< " robots; only " << MAXROBOTS << " are allowed!" << endl;
exit(1);
}
if (rows == 1 && cols == 1 && nRobots > 0)
{
cout << "***** Cannot create Game with nowhere to place the robots!" << endl;
exit(1);
}
// Create arena
m_arena = new Arena(rows, cols);
// Add player
int rPlayer = randInt(1, rows);
int cPlayer = randInt(1, cols);
m_arena->addPlayer(rPlayer, cPlayer);
// Populate with robots
while (nRobots > 0)
{
int r = randInt(1, rows);
int c = randInt(1, cols);
// Don't put a robot where the player is
if (r == rPlayer && c == cPlayer)
continue;
m_arena->addRobot(r, c);
nRobots--;
}
}
Game::~Game()
{
delete m_arena;
}
void Game::play()
{
Player* p = m_arena->player();
if (p == nullptr)
{
m_arena->display();
return;
}
do
{
bool moveRobots = true;
m_arena->display();
cout << endl;
cout << "Move (u/d/l/r//h/q): ";
string action;
getline(cin,action);
if (action.size() == 0) // player stands
p->stand();
else
{
switch (action[0])
{
default: // if bad move, nobody moves
cout << '\a' << endl; // beep
continue;
case 'q':
return;
case 'u':
case 'd':
case 'l':
case 'r':
p->moveOrAttack(decodeDirection(action[0]));
break;
case 'h':
m_arena->history().display();
cout<<"Press enter to continue.";
cin.ignore(10000,'\n');
moveRobots = false;
break;
}
}
if(moveRobots == true)
{
m_arena->moveRobots();
}
} while ( ! m_arena->player()->isDead() && m_arena->robotCount() > 0);
m_arena->display();
}