-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
262 lines (219 loc) · 8.27 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//main.cpp
//Hossein Mohebbi
//
//multiplayer connect 4 game
//libraries
#include <iostream>
using namespace std;
struct PlayerInformation
{
char Name[100];
char piece;
};
//functions
bool isFour (char board[][10], PlayerInformation activePlayer); //function that gets values from array and struct and returns bool
int restart (char board[][10]); //function that gets values from array and returns bool
int columnChoice(char board[][10], PlayerInformation activePlayer); //function that gets values from array and struct and returns int
int maxFilled(char board[][10]);//function that gets values from array and returns int
void checkBelow (char board[][10], PlayerInformation activePlayer, int dropChoice); //function that gets values from array and struct and an int and returns nothing
void displayBoard (char board[][10]);//function that gets values from array and returns nothing
void PlayerWin ( PlayerInformation activePlayer );//function that gets from struct and returns nothing
int main()
{
//variables
PlayerInformation playerOne, playerTwo; //make two variables, playerOne and playerTwo from struct
char board[9][10]; //create a 2D array
int dropChoice = 0; //make a variable called dropChoice of type int
int full = 0; //make a variable called full of type int
int again = 1; //make a variable called again of type int
bool win; //make a variable called win of type bool
cout << "This program is a multiplayer connect 4 game!" << endl;//intro statement
cout << "Player 1, please type in your name: "; //get name from playerOne
cin >> playerOne.Name;
cout<<endl;
playerOne.piece = 'X'; //set playerOne's piece to Y
cout << "Player 2, please type in your name: "; //get name from playerOne
cin >> playerTwo.Name;
cout<<endl;
playerTwo.piece = 'O';//set playerTwo's piece to O
displayBoard(board);//function that prints the base board
cout<<endl;
cout<<endl;
do
{
cout<<endl;
cout<<endl;
//moves for playerOne
dropChoice = columnChoice(board, playerOne);//function that gets column choice from playerOne
checkBelow(board, playerOne, dropChoice);//checks pieces under choice to ensure its empty
displayBoard(board);//reprints base board
cout<<endl;
cout<<endl;
win = isFour(board, playerOne); //use function to check if connect 4 has been made
if (win) //if so...
{
PlayerWin(playerOne);//playerOne has won
again = restart(board);//ask to restart
if (again != 1)//if no
{
break;//exit loop and game ends
}
}
//moves for playerTwo
dropChoice = columnChoice(board, playerTwo);//function that gets column choice from playerOne
checkBelow(board, playerTwo, dropChoice);//checks pieces under choice to ensure its empty
displayBoard(board);//print board
win = isFour(board, playerTwo);//function to see if playerTwo won
if (win)//if so...
{
PlayerWin(playerTwo);//playerTwo is winner
again = restart(board);//ask to reset board
if (again != 1)//if no
{
break;//exit loop and end game
}
}
full = maxFilled(board);//check to see if board is filled
if (full == 7)//if maximum is reached at 7
{
cout << "It's a draw!" << endl;
again = restart(board);//ask to restart game
if (again != 1)//if no
{
break;//exit loop and end game
}
}
}
while (again == 1);//condition for loop to keep looping, user must always want again
return 0;
}
int columnChoice(char board[][10], PlayerInformation activePlayer)//function to get user's choice
{
int dropChoice = 0;
while (dropChoice < 1 || dropChoice > 7)//while loop to ensure column choice is between 1-7
{
cout << activePlayer.Name << ", it's your turn, ";
cout << "Choose one of the labelled columns (1-7): ";
cin >> dropChoice;//get choice
while (board[1][dropChoice] == 'X' || board[1][dropChoice] == 'O')//while loop to ensure that when column is filled user chooses other one
{
cout << "That column is full, please choose another column: ";
cin >> dropChoice;//get choice
}
}
return dropChoice;
}
void checkBelow (char board[][10], PlayerInformation activePlayer, int dropChoice)//checking below piece
{
int rowLength = 6;//make variable called rowLength, reps the 6 rows
int turn = 0;
do//create do loop that continues until turn is 1
{
if (board[rowLength][dropChoice] != 'X' && board[rowLength][dropChoice] != 'O')//condition for when row and drop choice are empty position
{
board[rowLength][dropChoice] = activePlayer.piece;//piece can take that position
turn = 1;//set turn to 1 since possible position found and exits loop
}
else//otherwise
{
rowLength--;//take one away from rowLength
}
}
while (turn != 1);
}
void displayBoard (char board[][10])//printing board
{
int rows = 6; //variable for rows set to 6
int columns = 7; //variable for columns set to 7
cout << " 1234567 " <<endl;//identifying positions
for(int i = 1; i <= rows; i++)//for loop counting the rows
{
cout << "|";//sidebar
for(int j = 1; j <= columns; j++)//for loop counting the columns
{
if(board[i][j] != 'X' && board[i][j] != 'O')//condition for spots with no pieces
{
board[i][j] = '~';//print symbol representing empty space
}
cout << board[i][j];//print
}
cout << "|" << endl;//sidebar
}
}
bool isFour (char board[][10], PlayerInformation activePlayer)//function to see if connect 4
{
char XorO;
bool win = false;
XorO = activePlayer.piece;//the current players piece
//nested loops
for(int i = 8; i >= 1; i--)//count down from 8 to 1
{
for(int j = 9; j >= 1; j--)//count down from 9 to 1
{
//diagonal connect 4 of identical pieces
if(board[i][j] == XorO && board[i-1][j-1] == XorO && board[i-2][j-2] == XorO && board[i-3][j-3] == XorO)
{
win = true;//yes to win
}
//second diagonal connect 4 of identical pieces
if( board[i][j] == XorO && board[i][j-1] == XorO && board[i][j-2] == XorO && board[i][j-3] == XorO)
{
win = true;//yes to win
}
//vrtical connect 4 of identical pieces
if( board[i][j] == XorO && board[i-1][j] == XorO && board[i-2][j] == XorO && board[i-3][j] == XorO)
{
win = true;//yes to win
}
//third diagonal connect 4 of identical pieces
if( board[i][j] == XorO && board[i-1][j+1] == XorO && board[i-2][j+2] == XorO && board[i-3][j+3] == XorO)
{
win = true;//yes to win
}
//horizontal connect 4 of identical pieces
if ( board[i][j] == XorO && board[i][j+1] == XorO && board[i][j+2] == XorO && board[i][j+3] == XorO)
{
win = true;//yes to win
}
}
}
return win;
}
int maxFilled(char board[][10])//function that determines if board is filled
{
int full = 0;
for (int i = 1; i <= 7; i++)//for loop from 1 - 7, reps columns
{
if (board[1][i] != '~')//if top row column is not empty
{
full++;//add to full by 1
}
}
return full;
}
void PlayerWin (PlayerInformation activePlayer)//unction that gives winning announcement
{
cout << activePlayer.Name << ", YOU JUST WON!" << endl;
}
int restart (char board[][10])//function for restarting game
{
int restart;
cout << "Would you like to play again? Enter 1 for yes and 2 for No: ";
cin >> restart;
if (restart == 1)//if yes
{
//reprinting empty board
for(int i = 1; i <= 6; i++)
{
for(int j = 1; j <= 7; j++)
{
board[i][j] = '~';
}
}
}
else//otherwise say game ended
{
cout << "Game Ended" << endl;
}
return restart;
}