-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect_cli.c
More file actions
189 lines (163 loc) · 6.04 KB
/
connect_cli.c
File metadata and controls
189 lines (163 loc) · 6.04 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
184
185
186
187
188
189
//***************************************************
// Filename: connect_cli.c
//
// Author: YOUR NAME(S)
// Jag Nandigam
//***************************************************
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>
#include "connect_game.h"
// function declarations
void getGameInfo(int *board_size, int *connect_length, int *start_player, char *popout);
void displayGameInfo(int board_size, int connect_length, int start_player, char popout);
void playGame(int board_size, int connect_length, int start_player, char popout);
int main(int argc, char *argv[])
{
int board_size, connect_length, start_player;
char popout;
// validate command-line arguments, if any provided
bool args_valid = true;
if (argc == 5) {
board_size = atoi(argv[1]);
connect_length = atoi(argv[2]);
start_player = atoi(argv[3]);
popout = tolower(argv[4][0]);
if (board_size < 8 || board_size > 10 ||
connect_length < 4 || connect_length > 6 ||
start_player < 1 || start_player > 2 ||
(popout != 'y' && popout != 'n')) {
printf("Command-line arguments missing or invalid. Getting game info interactively...\n");
args_valid = false;
}
} else {
printf("Command-line arguments missing or invalid. Getting game info interactively...\n");
args_valid = false;
}
printf("Welcome to Connect Four Game!!!\n");
if (!args_valid) {
getGameInfo(&board_size, &connect_length, &start_player, &popout);
}
displayGameInfo(board_size, connect_length, start_player, popout);
playGame(board_size, connect_length, start_player, popout);
return 0;
}
void getGameInfo(int *board_size, int *connect_length, int *start_player, char *popout)
{
do {
printf("Enter the board size (8 - 10): ");
scanf("%d", board_size);
if (*board_size < 8 || *board_size > 10) {
printf("Invalid board size. Try again...\n");
}
} while (*board_size < 8 || *board_size > 10);
do {
printf("Enter the connect length (4 - 6): ");
scanf("%d", connect_length);
if (*connect_length < 4 || *connect_length > 6) {
printf("Invalid connect length. Try again...\n");
}
} while (*connect_length < 4 || *connect_length > 6);
do {
printf("Player to start the game (1 or 2)? ");
scanf("%d", start_player);
if (*start_player < 1 || *start_player > 2) {
printf("Invalid player number. Try again...\n");
}
} while (*start_player < 1 || *start_player > 2);
do {
printf("Enable PopOut option (y/n)? ");
scanf(" %c", popout);
*popout = tolower(*popout);
if (*popout != 'y' && *popout != 'n') {
printf("Invalid input. Try again...\n");
}
} while (*popout != 'y' && *popout != 'n');
}
void displayGameInfo(int board_size, int connect_length, int start_player, char popout)
{
printf("\nBoard Size: %d x %d\n", board_size, board_size);
printf("Connect Length: %d\n", connect_length);
printf("PopOut Option: %s\n", popout == 'y' ? "ON" : "OFF");
printf("Start Player: %d\n\n", start_player);
}
void nextPlayer(int currentPlayer){
if(currentPlayer == 1){
currentPlayer = 2;
}else{
currentPlayer = 1;
}
}
void playGame(int board_size, int connect_length, int start_player, char popout)
{
int currentPlayer = start_player;
int turn = 0;
char poppin;
int dropCol;
int board[board_size][board_size];
initializeBoard(board_size, board_size, board);
printBoard(board_size, board_size, board);
while(isWinByPlayer(board_size, board_size, board, connect_length, currentPlayer) == false && isBoardFull(board_size, board_size, board) == false){
if(turn > 0 && popout == 'y'){
printf("Player %i would you like to pop?\n" , currentPlayer);
scanf(" %c", &poppin);
poppin = tolower(poppin);
if (poppin != 'y' && poppin != 'n') {
printf("Invalid input. Try again...\n");
}else if(poppin == 'y'){
printf("Which column would you like to pop?\n");
scanf(" %d", &dropCol);
while(dropCol > board_size-1 || dropCol < 1){
printf("please specify a number between 1 and %i: \n", (board_size-1));
scanf(" %d", &dropCol);
}//end while
popDiscAtColumn(board_size, board_size, board, dropCol, currentPlayer);
if(currentPlayer == 1){
currentPlayer = 2;
}else{
currentPlayer =1;
}
turn++;
printBoard(board_size, board_size, board);
} else{
printf("Player %i which column would you like to drop?\n" , currentPlayer);
scanf(" %d", &dropCol);
printf("debug statement 1\n");
while(dropCol > board_size-1 || dropCol < 1){
printf("please specify a number between 1 and %i: \n", (board_size-1));
scanf(" %d", &dropCol);
}//end while
dropDiscAtColumn(board_size, board_size, board, dropCol, currentPlayer);
if(currentPlayer == 1){
currentPlayer = 2;
}else{
currentPlayer =1;
}
turn++;
printBoard(board_size, board_size, board);
}
}else{
printf("else \n");
printf("Player %i which column would you like to drop?" , currentPlayer);
scanf(" %d", &dropCol);
printf("debug statement 1\n");
while(*&dropCol > board_size-1 || *&dropCol < 1){
printf("please specify a number between 1 and %i: \n", (board_size-1));
scanf(" %d", &dropCol);
}//end while
printf("debug statement 2\n");
dropDiscAtColumn(board_size, board_size, board, dropCol, currentPlayer);
if(currentPlayer == 1){
currentPlayer = 2;
}else{
currentPlayer =1;
}
turn++;
printBoard(board_size, board_size, board);
}
}//close while.
}
//
// YOU ARE FREE TO ADD OTHER FUNCTIONS IN THIS FILE ONLY!!!
//