-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
60 lines (52 loc) · 1.71 KB
/
main.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
// File Name: GameState.h
//
// Author: Amy Sidon
// Date: November 29, 2023
// Assignment Number: 4
// CS 2308
// Manages a tic tac toe game that allows players to move or undo the most
// recent of their opponents moves when it is their turn.
#include <iostream>
using namespace std;
#include "GameState.hpp"
int main() {
GameState game; // manages the board and undo stack
string command; // input from user
int x; // x coordinate of move
int y; // y coordinate of move
bool gameOver = false; // manages the game loop
game.displayBoardState(cout); // output initial board
while (!gameOver) {
cout << "Player " << game.getCurrentPlayer() << " make a turn." << endl;
cin >> command;
if (command == "move") {
cin >> x >> y;
Move move(x,y);
int result = game.addMove(move); // 1, 0, or -1
if (result == -1)
cout << "Incorrect move. Please try again." << endl;
else
{
game.displayBoardState(cout);
if (game.checkLastPlayerWin()) {
cout << "Player " << !game.getCurrentPlayer() << " won!\n";
gameOver = true;
}
else if (result == 0) {
cout << "It's a draw!" << endl;
gameOver = true;
}
}
}
else if (command == "undo") {
bool undid = game.undoLast();
if (!undid)
cout << "No moves to undo." << endl;
else {
game.displayBoardState(cout);
}
}
else
cout << "Invalid command" << endl;
}
}