-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
149 lines (130 loc) · 3.84 KB
/
game.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "intro.h"
#include "game.h"
#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>
// displayBoard displays the board in the terminal
void displayBoard(char *row)
{
std::cout << '\n'
<< " | | " << '\n'
<< " " << row[0] << " | " << row[1] << " | " << row[2] << " " << '\n'
<< " | | " << '\n'
<< " ---------------------" << '\n'
<< " | | " << '\n'
<< " " << row[3] << " | " << row[4] << " | " << row[5] << " " << '\n'
<< " | | " << '\n'
<< " ---------------------" << '\n'
<< " | | " << '\n'
<< " " << row[6] << " | " << row[7] << " | " << row[8] << " " << '\n'
<< " | | " << '\n'
<< '\n';
}
// selectSquare prompts the player for a number
int selectSquare()
{
int square{};
std::cout << " Select a square (1-9): ";
std::cin >> square;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cerr << " Invalid input. Please try again: " << '\n';
selectSquare();
}
if (square < 1 || square > 9)
{
std::cerr << " Number not within 1-9 range. Please try again: " << '\n';
selectSquare();
}
return square;
}
// validateSelectedSquare validates square is available
int validateSelectedSquare(std::vector<int>& selected)
{
int marker{ selectSquare() };
if (std::find(selected.begin(), selected.end(), marker) != selected.end())
{
std::cout << " Square has already been selected. Please make another selection." << '\n';
marker = validateSelectedSquare(selected);
}
else
{
selected.push_back(marker);
}
return marker;
}
// calculateWinner returns true if it finds three characters in a row
bool calculateWinner(char *row, char m)
{
if ((row[0] == m && row[1] == m && row[2] == m) ||
(row[3] == m && row[4] == m && row[5] == m) ||
(row[6] == m && row[7] == m && row[8] == m) ||
(row[0] == m && row[4] == m && row[8] == m) ||
(row[0] == m && row[3] == m && row[6] == m) ||
(row[1] == m && row[4] == m && row[7] == m) ||
(row[2] == m && row[5] == m && row[8] == m) ||
(row[2] == m && row[4] == m && row[6] == m))
return true;
return false;
}
// playAgain prompts the player to play the game again
void playAgain()
{
char answer;
std::cout << " Would you like to play again (Y or N) ?: ";
std::cin >> answer;
if (toupper(answer) == 'Y')
{
clearScreen();
runGame();
}
return;
}
// runGame runs the game
void runGame()
{
char player1{'X'};
char player2{'O'};
char row[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
displayBoard(row);
int marker{0};
bool running{ true };
std::vector<int> selected;
while (running)
{
std::cout << player1Text << '\n';
marker = validateSelectedSquare(selected);
row[marker-1] = player1;
clearScreen();
displayBoard(row);
if (calculateWinner(row, player1))
{
std::cout << player1Win << '\n';
running = false;
playAgain();
return;
}
if (selected.size() == 9)
{
running = false;
std::cout << " Tie!" << '\n';
playAgain();
return;
}
std::cout << player2Text << '\n';
marker = validateSelectedSquare(selected);
row[marker-1] = player2;
clearScreen();
displayBoard(row);
if (calculateWinner(row, player2))
{
std::cout << player2Win << '\n';
running = false;
playAgain();
return;
}
}
}