-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTicTacToeFunctions.cpp
127 lines (112 loc) · 2.91 KB
/
TicTacToeFunctions.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
/*Goal: Practice creating classes and functions.
**Create a program that allows two users to
**play a 4x4 tic-tac-toe game.
*/
#include <fstream>
void getUserNames(string &, string &);
void printBlankBoard(string);
void printTheBoard(Board, string);
void printUserPrompt(string, char);
void printGameWinner(Board, string, string);
int getUserResponse();
void checkResponse(Board&, char);
void writeTheBoard(Board);
using namespace std;
void checkResponse(Board &boardIn, char input)
{//check that the position selected is not already
//selected
int position;
int userInput;
do
{
position = getUserResponse();
userInput = boardIn.setPosition(position, input);
if(userInput == -1)
{
cout<<"That postition is taken.";
}
}while(userInput == -1);
}
void getUserNames(string &userX, string &userY)
{//get the user names
std::cout<< "Name of user to be 'x' :";
std::cin >>userX;
std::cout<< "Name of user to be 'o' :";
std::cin >>userY;
}
void printBlankBoard()
{//print a blank board, with numbered squares
for(int i = 0; i <16; i++)
{
std::cout<<"|"<<i<<":";
if(i <= 9)
cout<<" ";
if(i%4 == 3)
{
std::cout<<"|\n";
}
}
cout<<"\n\n\n";
}
void printTheBoard(Board boardIn)
{//print the board with player moves
printBlankBoard();
for(int i = 0; i <16; i++)
{
std::cout<<"|"<<boardIn.getPositions()[i];
if(i%4 == 3)
{
std::cout<<"|\n";
}
}
cout<<"\n\n\n";
}
void writeTheBoard(Board boardIn)
{//print the board with player moves
cout<<"\n\n";
for(int i = 0; i <16; i++)
{
cout<<"|"<<boardIn.getPositions()[i];
if(i%4 == 3)
{
cout<<"|\n";
}
}
cout<<"\n\n\n";
}
void printUserPrompt(string nameIn, char letter)
{//prompt for user input
std::cout<<nameIn<<" where would you like to place an "<<letter<<"?: ";
cout<<"\n\n";
cout<<" where would you like to place an "<<letter<<"?: ";
}
void printGameWinner(Board boardIn, string nameX, string nameO)
{//print the winner statement
char winner;
winner = boardIn.determineWinner();
if(winner == 'x')
{
cout<<"Congrats "<<nameX<<" you won!\n\n";
}
if(winner == 'o')
{
cout<<"Congrats "<<nameO<<" you won!\n\n";
}
}
int getUserResponse()
{//get the chosen user position, check that it is an integer
//check that it is between 0 and 15 inclusive
int position = -1;
cout<<"Enter an integer between 0 and 15: ";
std::cin>>position;
while(position > 15 or position < 0 or !cin)
{
cin.clear();
//cin.ignore(numeric_limits<streamsize>::max(), '\n');
std::cout<<"That is not a valid position\n";
cout<<"Enter an integer between 0 and 15: ";
cin.clear();
cin>>position;
}
return position;
}