-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (63 loc) · 2.11 KB
/
Copy pathmain.cpp
File metadata and controls
76 lines (63 loc) · 2.11 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
#include <iostream>
#include "connectM.hpp"
int main(int argc, char* argv[]) {
if(argc != 4) {
std::cout << "Usage: ./connectM N M H\n";
std::cout << "Where N is the board size NxN\n";
std::cout << ", M is the winning length\n";
std::cout << ", and H is 0 if computer goes first\n";
std::cout << ", else H is 1 if player goes first\n";
exit(1);
}
int N = std::stoi(argv[1]);
int M = std::stoi(argv[2]);
int H = std::stoi(argv[3]);
ConnectM game(N,M,H);
char keepGoing = ' ';
int playerMove = 0;
do {
game.reset();
std::cout << game.showBoard() << std::endl;
while(true) {
do {
std::cout << "Enter column> ";
std::cin >> playerMove;
}while((playerMove < 0 || playerMove >= N) || !game.move(playerMove,(H)? 1:0));
std::cout << game.showBoard() << std::endl;
if(game.check4Win((H)? 1:0)) {
if(H == 1) {
std::cout << "Human won.\n";
}
else {
std::cout << "Computer won.\n";
}
break;
}
else if(game.isBoardFull()) {
std::cout << "It's a draw!\n";
break;
}
do {
std::cout << "Enter column> ";
std::cin >> playerMove;
}while((playerMove < 0 || playerMove >= N) || !game.move(playerMove,(H)? 0:1));
std::cout << game.showBoard() << std::endl;
if(game.check4Win((H)? 0:1)) {
if(H == 0) {
std::cout << "Human won.\n";
}
else {
std::cout << "Computer won.\n";
}
break;
}
else if(game.isBoardFull()) {
std::cout << "It's a draw!\n";
break;
}
}
std::cout << "Do you want to continue (y/n)? ";
std::cin >> keepGoing;
}while(keepGoing == 'y' || keepGoing == 'Y');
return 0;
}