-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (48 loc) · 1.74 KB
/
main.cpp
File metadata and controls
65 lines (48 loc) · 1.74 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
#include <iostream>
#include "TicTacToeBoard.hpp"
char getPlayerPiece() {
char playerPiece;
while (true) {
std::cout << "Would you like to be X or O? (X goes first)" << std::endl;
std::cout << " [x] [o] " << std::endl;
std::cin >> playerPiece;
if (playerPiece == 'x' || playerPiece == 'X' || playerPiece == 'o' || playerPiece == 'O') {
break;
} else {
std::cout << "Error: Invalid Selection, Please Enter Again" << std::endl;
}
}
return playerPiece;
}
char getPlayAgain() {
char playAgain;
while (true) {
std::cout << "Would you like to play again?" << std::endl;
std::cout << " [y] [n] " << std::endl;
std::cin >> playAgain;
if (playAgain == 'y' || playAgain == 'Y' || playAgain == 'n' || playAgain == 'N') {
break;
} else {
std::cout << "Error: Invalid Selection, Please Enter Again" << std::endl;
}
}
return playAgain;
}
int main() {
std::cout << "\033[2JWelcome to TicTacToe\n" << std::endl;
while (true) {
char playerPiece = getPlayerPiece();
TicTacToe game((playerPiece == 'O' || playerPiece == 'o'));
while (!game.isGameOver()) {
game.getNextMove();
}
game.printBoard();
std::cout << "Game Over!" << std::endl;
if (game.getWinner() == game.getAIPiece()) std::cout << "You Lost!" << std::endl;
else if (game.getWinner() == game.getConsolePiece()) std::cout << "You Won!" << std::endl;
else std::cout << "It was a Tie!\n" << std::endl;
char playAgain = getPlayAgain();
if (playAgain == 'n' || playAgain == 'N') break;
}
return 0;
}