-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIST163_Moran_T_FinalExam_ticTacToe.cpp
More file actions
183 lines (155 loc) · 4.49 KB
/
IST163_Moran_T_FinalExam_ticTacToe.cpp
File metadata and controls
183 lines (155 loc) · 4.49 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************
Author: Tucker Moran
Course: IST 163 - Programming Practicum
Date: 7/29/2016 - 7/31/2016
*******************************************
Files for Assignment: IST163_Moran_T_FinalExam_ticTacToe.cpp, ticTacToe.h, ticTacToe_Implementation.cpp
Purpose of File: Allow two players to play a tic-tac-toe game
Assistance Recieved: Notes from chapters 7-10
*******************************************/
#include<iostream>
#include<iomanip>
#include<string>
#include"ticTacToe.h"
using namespace std;
void showMenu();
ticTacToe game;
int main()
{
// variables
bool playersPlaying = true;
char playAgain;
bool playAgainMenu = false;
int row;
int col;
// Start the game with large while loop, loops until players specify that they are done playing
while (playersPlaying)
{
// Show menu
showMenu();
// Nested while loop for players moves, loop ends when the game is won or tied
while (!game.endGameCheck)
{
// Player input variables
bool playerXInputValid = true;
bool playerOInputValid = true;
// print board
game.printBoard();
cout << "To win the game, place three of your pieces ('X' or 'O') in a row \nbefore the other player. Good Luck!" << endl << endl;
// Loop for player X input
while (playerXInputValid == true)
{
// Player X input, checkMove, checkWinner if move is valid, and then allow Player O to select his move if game is not won
cout << "Player X, select the position of your move by entering the row number and \ncolumn number, separated by a space. (i.e. '0 1'): ";
cin >> row >> col;
cout << endl << endl;
if (game.checkMove(row, col, 'X'))
{
game.getMove(row, col, 'X');
game.printBoard();
cout << endl;
playerXInputValid = false;
}
else
{
cout << "Invalid entry. Try again." << endl << endl;
game.printBoard();
}
}
// check game winner
if (game.checkWinner())
game.endGameCheck = true;
else
{
// Loop for player O input
while (playerOInputValid == true)
{
// Player O input, checkMove, checkWinner if move is valid, move goes back to player X
cout << "Player O, select the position of your move by entering the row number and \ncolumn number, separated by a space. (i.e. '0 1'): ";
cin >> row >> col;
cout << endl << endl;
if (game.checkMove(row, col, 'O'))
{
game.getMove(row, col, 'O');
game.printBoard();
cout << endl;
playerOInputValid = false;
}
else
{
cout << "Invalid entry. Try again." << endl << endl;
game.printBoard();
}
// check game winner
if (game.checkWinner())
game.endGameCheck = true;
}
}
}
// print board upon a win or tie and then clear it
game.printBoard();
game.clearBoard();
cout << endl << endl;
// call completedGames
game.completedGames();
cout << endl << endl;
// Play again? if no: playersPlaying = false
while (!playAgainMenu)
{
cout << "Would you like to play again? Enter 'Y' for yes or 'N' for no. " << endl;
cin >> playAgain;
if (playAgain == 'Y' || playAgain == 'y')
{
cout << "Playing again..." << endl << endl;
playAgainMenu = true;
}
else if (playAgain == 'N' || playAgain == 'n')
{
cout << "Ending game..." << endl;
playAgainMenu = true;
playersPlaying = false;
}
else
{
cout << "Invalid input. Enter 'Y' or 'N'.";
cout << endl << endl;
}
}
}
return 0;
}
/***************************
Function: showMenu
Precondition: no parameters
Postcondition: outputs a menu for the user. user can choose to play or quit game
Dates of Coding: 7/30/2016
Modifications:
***************************/
void showMenu()
{
char menuOption;
bool menu = false;
while (!menu)
{
// program/game menu
cout << endl;
cout << "-------------------------TIC-TAC-TOE-------------------------" << endl;
cout << "Options: " << endl;
cout << "1. Enter '1' to play a game of tic-tac-toe." << endl;
cout << "2. Enter '2' to exit the game" << endl << endl;
cout << "Option: ";
cin >> menuOption;
cout << endl << endl;
// check user input
if (menuOption == '1')
menu = true;
else if (menuOption == '2')
{
game.endGameCheck = true;
menu = true;
}
else
cout << "Invalid response. Please enter a '1' or a '2'.";
cout << endl << endl;
}
}