-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChomp.cpp
More file actions
108 lines (93 loc) · 2.38 KB
/
Chomp.cpp
File metadata and controls
108 lines (93 loc) · 2.38 KB
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
// 0 1 2 3 4 5 6 7 8 9 101112131415
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
//1 < X O O O O O
//2 < O O O O
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
// 1 2 3 4 5 6 7 8 9 1011121314
#include "Chomp.h"
#include <iostream>
#include <cassert>
using std::cout;
using std::cin;
Chomp::Chomp() : curPlayer(false) {
int col;
cout << "Welcome to chomp! \n"
"Enter number of rows (max. " << MAX_ROW << "): ";
cin >> height;
cout << "Enter number of columns (max. " << MAX_COL << "): ";
cin >> col;
assert(height > 0 and height <= MAX_ROW and col > 0 and col <= MAX_COL);
// construct grid
arr = new int[height];
for (int i = 0; i < height; ++i) {
arr[i] = col; // when displaying, will show indices from 0 to col - 1
}
display();
while (play());
}
Chomp::~Chomp() {
delete arr;
}
bool Chomp::valid(int row, int col) const {
if (row < 0 or row >= height or col < 0)
return false;
if (row == 0 and col == 0)
return false;
return arr[row] >= col;
}
void Chomp::display() const {
// print the beginning indexes
cout << " ";
int width = arr[0];
for (int k = 0; k < width; ++k) {
cout << k;
if (k < 10)
cout << ' ';
}
cout << "\n ";
for (int k = 0; k < width; ++k)
cout << " ^";
cout << '\n';
// print the rest
int i = 0;
// height might be greater than number of rows left after chopping
while (i < height and arr[i] != 0) {
if (i < 10)
cout << " ";
cout << i << " < ";
cout << (i == 0 ? 'X' : 'O');
for (int j = 1; j < arr[i]; ++j)
cout << " O";
cout << '\n';
++i;
}
}
bool Chomp::play() {
int row, col;
cout << "\nPlayer " << (curPlayer + 1) << ", enter your move: ";
cin >> row >> col;
if (not valid(row, col)) {
cout << "Invalid input. Try again: ";
return true;
}
chop(row, col);
display();
if (lost()) {
cout << "\nPlayer " << (curPlayer + 1) << ", congrats--you won! \n";
return false;
}
curPlayer = not curPlayer;
return true;
}
void Chomp::chop(int row, int col) {
for (int i = row; i < height and arr[i] > col; ++i) {
arr[i] = col;
}
}
bool Chomp::lost() const {
if (height == 1) {
return arr[0] == 1;
} else {
return arr[0] == 1 and arr[1] == 0;
}
}