-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHistory.cpp
78 lines (67 loc) · 1.51 KB
/
History.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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
#include "History.h"
#include "Game.h"
#include "Arena.h"
#include "Player.h"
#include "Robot.h"
#include "globals.h"
History::History(int nRows, int nCols)
: m_rows(nRows), m_cols(nCols), m_numOfMoves(0)
{}
bool History::record(int r, int c)
{
if (r < 1 || c < 1 || r > MAXROWS || c > MAXCOLS)
return false;
m_numOfMoves++;
historyR[m_numOfMoves-1] = r;
historyC[m_numOfMoves-1] = c;
return true;
}
int History::rows() const
{
return m_rows;
}
int History::cols() const
{
return m_cols;
}
void History::display() const
{
// Position (row,col) in the arena coordinate system is represented in
// the array element grid[row-1][col-1]
char grid[MAXROWS][MAXCOLS];
int r, c;
// Fill the grid with dots
for (r = 0; r < rows(); r++)
for (c = 0; c < cols(); c++)
grid[r][c] = '.';
// Indicate the players move history
for (int x = 0; x < m_numOfMoves; x++)
{
char& gridChar = grid[historyR[x]-1][historyC[x]-1];
switch (gridChar)
{
case '.':
gridChar = '1';
break;
case '9':
break;
default:
gridChar++;
break; // '2' through '8'
}
}
// Draw the grid
clearScreen();
for (r = 0; r < rows(); r++)
{
for (c = 0; c < cols(); c++)
cout << grid[r][c];
cout << endl;
}
cout << endl;
}