The following is my code for a connectn program. When I run my code in the tester, I failed two
out 10 tests. I was hoping for some help in finding what causes these errors. I have included the
homework prompt and test failed results. Thank you so much!
#include
#include
#include
void read_args(int argc, char *argv[], int* num_rows, int* num_cols, int *num_pieces_to_win);
void create_board(int num_rows, int num_cols, char*** board, int* turn);
void print_board(int num_rows, int num_cols, char** board);
void destroy_board(int num_rows, char*** board);
void play_game(int num_rows, int num_cols, int num_pieces_to_win, int turn, char** board);
void check_move();
void get_play(int num_rows, int num_cols, char** board, int* user_move);
void make_move(int num_rows, char**board, int user_move, char pieces);
bool play_is_valid(int num_cols, int num_rows, char** board, int user_move_num, int
user_move);
bool game_over(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool game_won(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool game_tied(int num_rows, int num_cols, int num_pieces_to_win, char**board);
bool row_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool col_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool right_diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool left_diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
void read_args(int argc, char *argv[], int* num_rows, int* num_cols, int *num_pieces_to_win) {
//reads command input and ensures there are exactly three numbers
if (argc > 4) {
printf(\"Too many arguments entered\ \");
printf(\"Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win\");
exit(0);
}
else if (argc < 4) {
printf(\"Not enough arguments entered\ \");
printf(\"Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win\");
exit(0);
}
else { //puts user input into variables
sscanf(argv[1], \"%d\", &*num_rows);
sscanf(argv[2], \"%d\", &*num_cols);
sscanf(argv[3], \"%d\", &*num_pieces_to_win);
}
return;
}
void create_board (int num_rows, int num_cols, char*** board, int* turn) {
//set up the connect-n board
const char BLANK_SPACE = \'*\';
int i, j;
*turn = 0; //player 1 always goes first
*board = (char**)malloc(num_rows * sizeof(char*)); //creates pointers to the rows
for (i = 0; i < num_rows; ++i){ //for each row
(*board)[i] = (char*)malloc(num_cols * sizeof(char)); //create it
for (j = 0; j < num_cols; j++) { //fill in the row with blanks
(*board)[i][j] = BLANK_SPACE;
}
}
}
void print_board (int num_rows, int num_cols, char** board) {
//displays connect-n board
int i, j;
int k = 0;
for(i = 0; i < num_rows; i++){
printf(\"%d \", ((num_rows- i) - 1));
for(j = 0; j < num_cols; ++j){
printf(\"%c \", board[i][j]);
}
printf(\"\ \");
}
printf(\" %d \", k);
for(j = 1; j <.
The following is my code for a connectn program. When I run my code .pdf
1. The following is my code for a connectn program. When I run my code in the tester, I failed two
out 10 tests. I was hoping for some help in finding what causes these errors. I have included the
homework prompt and test failed results. Thank you so much!
#include
#include
#include
void read_args(int argc, char *argv[], int* num_rows, int* num_cols, int *num_pieces_to_win);
void create_board(int num_rows, int num_cols, char*** board, int* turn);
void print_board(int num_rows, int num_cols, char** board);
void destroy_board(int num_rows, char*** board);
void play_game(int num_rows, int num_cols, int num_pieces_to_win, int turn, char** board);
void check_move();
void get_play(int num_rows, int num_cols, char** board, int* user_move);
void make_move(int num_rows, char**board, int user_move, char pieces);
bool play_is_valid(int num_cols, int num_rows, char** board, int user_move_num, int
user_move);
bool game_over(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool game_won(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool game_tied(int num_rows, int num_cols, int num_pieces_to_win, char**board);
bool row_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool col_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool right_diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool left_diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
void read_args(int argc, char *argv[], int* num_rows, int* num_cols, int *num_pieces_to_win) {
//reads command input and ensures there are exactly three numbers
if (argc > 4) {
2. printf("Too many arguments entered ");
printf("Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win");
exit(0);
}
else if (argc < 4) {
printf("Not enough arguments entered ");
printf("Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win");
exit(0);
}
else { //puts user input into variables
sscanf(argv[1], "%d", &*num_rows);
sscanf(argv[2], "%d", &*num_cols);
sscanf(argv[3], "%d", &*num_pieces_to_win);
}
return;
}
void create_board (int num_rows, int num_cols, char*** board, int* turn) {
//set up the connect-n board
const char BLANK_SPACE = '*';
int i, j;
*turn = 0; //player 1 always goes first
*board = (char**)malloc(num_rows * sizeof(char*)); //creates pointers to the rows
for (i = 0; i < num_rows; ++i){ //for each row
(*board)[i] = (char*)malloc(num_cols * sizeof(char)); //create it
for (j = 0; j < num_cols; j++) { //fill in the row with blanks
(*board)[i][j] = BLANK_SPACE;
}
}
}
3. void print_board (int num_rows, int num_cols, char** board) {
//displays connect-n board
int i, j;
int k = 0;
for(i = 0; i < num_rows; i++){
printf("%d ", ((num_rows- i) - 1));
for(j = 0; j < num_cols; ++j){
printf("%c ", board[i][j]);
}
printf(" ");
}
printf(" %d ", k);
for(j = 1; j < num_cols; j++){
printf("%d ", (j));
}
printf(" ");
}
void destroy_board(int num_rows, char*** board){
//destroys connect-n board
int i;
for(i=0; i < num_rows; ++i){ //delete each row
free((*board)[i]);
}
free(*board); //delete pointers to row
*board = NULL; //set board to the null pointers so we know it isn't valid anymore
}
void play_game(int num_rows, int num_cols, int num_pieces_to_win, int turn, char** board){
//play a game of connectn
char pieces[] = "XO"; //player 1 is 'X' and player 2 is 'O'
int user_move;
4. while(game_over(num_rows, num_cols, num_pieces_to_win, board) == false){ //each turn
print_board(num_rows, num_cols, board);
get_play(num_rows, num_cols, board, &user_move);
make_move(num_rows, board, user_move, pieces[turn]);
turn = (turn + 1) % 2; //change the turn
}
//game is now over
print_board(num_rows, num_cols, board); //display final board
if(game_won(num_rows, num_cols, num_pieces_to_win, board) == true){ //if someone won
if(turn == 1){
printf("Player 1 Won! ");
}
else{
printf("Player 2 Won! ");
}
}
else{
printf("Tie game! ");
}
}
void check_move(){ //checks the input
char c = getchar();
while((c != ' ') && (c != EOF)){
c = getchar();
}
}
void get_play(int num_rows, int num_cols, char** board, int* user_move){ //gets move from
user
int user_move_num;
printf("Enter a column between 0 and %d to play in: ", num_cols - 1);
user_move_num = scanf("%d", user_move);
while(play_is_valid(num_rows, num_cols, board, user_move_num, *user_move) == false){
5. printf("Enter a column between 0 and %d to play in: ", num_cols - 1);
user_move_num = scanf("%d", user_move);
check_move();
}
}
void make_move(int num_rows, char**board, int user_move, char pieces){ //changes the board
based on the move
int i = (num_rows - 1);
const char BLANK_SPACE = '*';
while(i >= 0) {
if(board[i][user_move] == BLANK_SPACE){
board[i][user_move] = pieces;
break;
}
else{
i = (i - 1);
}
}
}
bool play_is_valid(int num_cols, int num_rows, char** board, int user_move_num, int
user_move) {
//ensures user move is valid
const char BLANK_SPACE = '*';
if (user_move_num != 1){
return false;
}
else if (user_move >= num_cols){ //outside grid
return false;
}
else if (board[0][user_move] != BLANK_SPACE){ //occupied space
6. return false;
}
else {
return true;
}
}
bool game_over(int num_rows, int num_cols, int num_pieces_to_win, char** board){
//checks if the game is over
if (game_won(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else if (game_tied(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else {
return false;
}
}
bool game_won(int num_rows, int num_cols, int num_pieces_to_win, char** board) {
//checks to see if someone has won
if (row_win(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else if (col_win(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else if (diag_win(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else {
return false;
}
7. }
bool game_tied(int num_rows, int num_cols, int num_pieces_to_win, char**board) {
//checks to see if someone has tied
const char BLANK_SPACE = '*';
int i, j;
if (game_won(num_rows, num_cols, num_pieces_to_win, board) == true) {
return false;
}
else {
for(i = 0; i < num_rows; i++){
for(j = 0;j < num_cols; j++){
if(board[i][j] == BLANK_SPACE){
return false;
}
}
}
return true;
}
}
bool row_win(int num_rows, int num_cols, int num_pieces_to_win, char** board) {
//checks if there is a win horizontally
int i, j;
bool o_wins = false;
bool x_wins = false;
int o_cnt;
int x_cnt;
for (i=0; i=0; j--) {
if (k == num_rows) {
break;
}
if ((x_wins || o_wins) == true) {
8. break;
}
if (board[k][j] == 'O') {
o_cnt++;
x_cnt = 0;
k = k + 1;
}
else if (board[k][j] == 'X') {
x_cnt++;
o_cnt = 0;
k = k + 1;
}
else {
x_cnt = 0;
o_cnt = 0;
k = k + 1;
}
if (x_cnt == num_pieces_to_win) {
x_wins = true;
}
if (o_cnt == num_pieces_to_win) {
o_wins = true;
}
}
k = l;
o_cnt = 0;
x_cnt = 0;
}
}
if (x_wins == true) {
return true;
}
else if (o_wins == true) {
return true;
9. }
else {
return false;
}
}
#include
#include
#include
void read_args(int argc, char *argv[], int* num_rows, int* num_cols, int *num_pieces_to_win);
void create_board(int num_rows, int num_cols, char*** board, int* turn);
void print_board(int num_rows, int num_cols, char** board);
void destroy_board(int num_rows, char*** board);
void play_game(int num_rows, int num_cols, int num_pieces_to_win, int turn, char** board);
void check_move();
void get_play(int num_rows, int num_cols, char** board, int* user_move);
void make_move(int num_rows, char**board, int user_move, char pieces);
bool play_is_valid(int num_cols, int num_rows, char** board, int user_move_num, int
user_move);
bool game_over(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool game_won(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool game_tied(int num_rows, int num_cols, int num_pieces_to_win, char**board);
bool row_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool col_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool right_diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool left_diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
void read_args(int argc, char *argv[], int* num_rows, int* num_cols, int *num_pieces_to_win) {
//reads command input and ensures there are exactly three numbers
if (argc > 4) {
printf("Too many arguments entered ");
printf("Usage connectn.out num_rows num_columns
10. number_of_pieces_in_a_row_needed_to_win");
exit(0);
}
else if (argc < 4) {
printf("Not enough arguments entered ");
printf("Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win");
exit(0);
}
else { //puts user input into variables
sscanf(argv[1], "%d", &*num_rows);
sscanf(argv[2], "%d", &*num_cols);
sscanf(argv[3], "%d", &*num_pieces_to_win);
}
return;
}
void create_board (int num_rows, int num_cols, char*** board, int* turn) {
//set up the connect-n board
const char BLANK_SPACE = '*';
int i, j;
*turn = 0; //player 1 always goes first
*board = (char**)malloc(num_rows * sizeof(char*)); //creates pointers to the rows
for (i = 0; i < num_rows; ++i){ //for each row
(*board)[i] = (char*)malloc(num_cols * sizeof(char)); //create it
for (j = 0; j < num_cols; j++) { //fill in the row with blanks
(*board)[i][j] = BLANK_SPACE;
}
}
}
11. void print_board (int num_rows, int num_cols, char** board) {
//displays connect-n board
int i, j;
int k = 0;
for(i = 0; i < num_rows; i++){
printf("%d ", ((num_rows- i) - 1));
for(j = 0; j < num_cols; ++j){
printf("%c ", board[i][j]);
}
printf(" ");
}
printf(" %d ", k);
for(j = 1; j < num_cols; j++){
printf("%d ", (j));
}
printf(" ");
}
void destroy_board(int num_rows, char*** board){
//destroys connect-n board
int i;
for(i=0; i < num_rows; ++i){ //delete each row
free((*board)[i]);
}
free(*board); //delete pointers to row
*board = NULL; //set board to the null pointers so we know it isn't valid anymore
}
void play_game(int num_rows, int num_cols, int num_pieces_to_win, int turn, char** board){
//play a game of connectn
char pieces[] = "XO"; //player 1 is 'X' and player 2 is 'O'
int user_move;
while(game_over(num_rows, num_cols, num_pieces_to_win, board) == false){ //each turn
print_board(num_rows, num_cols, board);
12. get_play(num_rows, num_cols, board, &user_move);
make_move(num_rows, board, user_move, pieces[turn]);
turn = (turn + 1) % 2; //change the turn
}
//game is now over
print_board(num_rows, num_cols, board); //display final board
if(game_won(num_rows, num_cols, num_pieces_to_win, board) == true){ //if someone won
if(turn == 1){
printf("Player 1 Won! ");
}
else{
printf("Player 2 Won! ");
}
}
else{
printf("Tie game! ");
}
}
void check_move(){ //checks the input
char c = getchar();
while((c != ' ') && (c != EOF)){
c = getchar();
}
}
void get_play(int num_rows, int num_cols, char** board, int* user_move){ //gets move from
user
int user_move_num;
printf("Enter a column between 0 and %d to play in: ", num_cols - 1);
user_move_num = scanf("%d", user_move);
while(play_is_valid(num_rows, num_cols, board, user_move_num, *user_move) == false){
printf("Enter a column between 0 and %d to play in: ", num_cols - 1);
user_move_num = scanf("%d", user_move);
13. check_move();
}
}
void make_move(int num_rows, char**board, int user_move, char pieces){ //changes the board
based on the move
int i = (num_rows - 1);
const char BLANK_SPACE = '*';
while(i >= 0) {
if(board[i][user_move] == BLANK_SPACE){
board[i][user_move] = pieces;
break;
}
else{
i = (i - 1);
}
}
}
bool play_is_valid(int num_cols, int num_rows, char** board, int user_move_num, int
user_move) {
//ensures user move is valid
const char BLANK_SPACE = '*';
if (user_move_num != 1){
return false;
}
else if (user_move >= num_cols){ //outside grid
return false;
}
else if (board[0][user_move] != BLANK_SPACE){ //occupied space
return false;
}
14. else {
return true;
}
}
bool game_over(int num_rows, int num_cols, int num_pieces_to_win, char** board){
//checks if the game is over
if (game_won(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else if (game_tied(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else {
return false;
}
}
bool game_won(int num_rows, int num_cols, int num_pieces_to_win, char** board) {
//checks to see if someone has won
if (row_win(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else if (col_win(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else if (diag_win(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else {
return false;
}
}
15. bool game_tied(int num_rows, int num_cols, int num_pieces_to_win, char**board) {
//checks to see if someone has tied
const char BLANK_SPACE = '*';
int i, j;
if (game_won(num_rows, num_cols, num_pieces_to_win, board) == true) {
return false;
}
else {
for(i = 0; i < num_rows; i++){
for(j = 0;j < num_cols; j++){
if(board[i][j] == BLANK_SPACE){
return false;
}
}
}
return true;
}
}
bool row_win(int num_rows, int num_cols, int num_pieces_to_win, char** board) {
//checks if there is a win horizontally
int i, j;
bool o_wins = false;
bool x_wins = false;
int o_cnt;
int x_cnt;
for (i=0; i=0; j--) {
if (k == num_rows) {
break;
}
if ((x_wins || o_wins) == true) {
break;
}
16. if (board[k][j] == 'O') {
o_cnt++;
x_cnt = 0;
k = k + 1;
}
else if (board[k][j] == 'X') {
x_cnt++;
o_cnt = 0;
k = k + 1;
}
else {
x_cnt = 0;
o_cnt = 0;
k = k + 1;
}
if (x_cnt == num_pieces_to_win) {
x_wins = true;
}
if (o_cnt == num_pieces_to_win) {
o_wins = true;
}
}
k = l;
o_cnt = 0;
x_cnt = 0;
}
}
if (x_wins == true) {
return true;
}
else if (o_wins == true) {
return true;
}
else {
17. return false;
}
}
Test FAILED.
Reason: Your output did not match the solution.
Way program was called: ./connectn.out 6 7 4
Input provided: 5
5
4
6
5
3
4
4
3
2
3
3
Position of first mismatch: 261
Your word: Enter
Correct word: 5
Context around YOUR answer: Enter a column between 0 and 6 to play in: ****Enter**** a
column between 0 and 6 to play in:
Context around SOLUTION: Enter a column between 0 and 6 to play in: ****5**** * * * * * *
18. * 4 *
YOUR complete output: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * * *
1 * * * * * * *
0 * * * * * * *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * * *
1 * * * * * * *
0 * * * * * X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * * *
1 * * * * * O *
0 * * * * * X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * * *
1 * * * * * O *
0 * * * * X X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: Enter a column between 0 and 6 to play in: 5 * * * *
* * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * O *
1 * * * * * O *
0 * * * * X X *
19. 0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * O *
1 * * * * * O *
0 * * * X X X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * O *
1 * * * * O O *
0 * * * X X X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * X O *
1 * * * * O O *
0 * * * X X X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * X O *
1 * * * O O O *
0 * * * X X X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * X O *
1 * * * O O O *
0 * * X X X X *
0 1 2 3 4 5 6
20. Player 1 Won!
Complete SOLUTION: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * * *
1 * * * * * * *
0 * * * * * * *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * * *
1 * * * * * * *
0 * * * * * X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * * *
1 * * * * * O *
0 * * * * * X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * * *
1 * * * * * O *
0 * * * * X X *
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
21. 2 * * * * * * *
1 * * * * * O *
0 * * * * X X O
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * X *
1 * * * * * O *
0 * * * * X X O
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * X *
1 * * * * * O *
0 * * * O X X O
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * * X *
1 * * * * X O *
0 * * * O X X O
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * O X *
1 * * * * X O *
0 * * * O X X O
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * O X *
22. 1 * * * X X O *
0 * * * O X X O
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * * O X *
1 * * * X X O *
0 * * O O X X O
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * * * * *
2 * * * X O X *
1 * * * X X O *
0 * * O O X X O
0 1 2 3 4 5 6
Enter a column between 0 and 6 to play in: 5 * * * * * * *
4 * * * * * * *
3 * * * O * * *
2 * * * X O X *
1 * * * X X O *
0 * * O O X X O
0 1 2 3 4 5 6
Player 2 Won!
Test FAILED.
Reason: Your program failed to complete within 5 seconds.
This likely means you have an infinite loop.
Note your output will have been truncated to be no larger than
the larger of the length of correct solution or 1000 characters.
Way program was called: ./connectn.out 1 2 4
Input provided: 0
1
23. YOUR output: 0 * *
0 1
Enter a column between 0 and 1 to play in: 0 X *
0 1
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column
between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0
and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to
play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in:
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column
between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0
and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to
play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in:
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column
between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0
and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to
play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in:
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column
between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0
and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to
play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in:
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Ent
YOUR error:
Test FAILED.
Reason: Your program failed to complete within 5 seconds.
This likely means you have an infinite loop.
24. Note your output will have been truncated to be no larger than
the larger of the length of correct solution or 1000 characters.
Way program was called: ./connectn.out 1 2 4
Input provided: 0
1
YOUR output: 0 * *
0 1
Enter a column between 0 and 1 to play in: 0 X *
0 1
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column
between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0
and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to
play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in:
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column
between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0
and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to
play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in:
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column
between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0
and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to
play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in:
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column
between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0
and 1 to play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to
play in: Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in:
Enter a column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Enter a
column between 0 and 1 to play in: Enter a column between 0 and 1 to play in: Ent
25. YOUR error: AT&T; LTE 5:13 PM smartsite.ucdavis.edu 17 63% 2 of 4 1. Write a program to
implement the game connect-n. Connect-n is like Connect4 except instead of having the board be
a constant 6 X 7 we will allow the user to enter the size of the board they would like to play on.
In addition we will also allow the user to choose how many pieces in a row are necessary to win.
The game is played as follows. Two players take turns dropping their pieces into a column until
either player gets N of their pieces in a row either horizontally vertically, or diagonally, or until
their or no more spaces to play 1. Your program must accept 3 command line parameters in this
order: number of rows, number of columns, number of pieces in a row to win 1. If the user does
not enter enough arguments or enters too many arguments your program should tell them the
proper usage of your program and terminate . You may find the exit function helpful 2. The user
should be allowed to create an unwinable game 1 For example a board that is 3 X 3 but requires
4 pieces in a row to 2. Your program should not allow the user to make an impossible play but
should continue to ask the user for a play until a valid play is entered Invalid plays consist of
plays made outside the board or in to columns that are already full I. 3. The token used to
represent Player I is X 4. The token used to represent Player 2 is O, a capitol oh and not a zero 5.
After the game is over the winner should be declared or if there is no winner a 6. You must split
your code up into at least 2 files. 7. You must submit a make file named Makefile that when run
compiles your 8. The executable created by your make file should be named connectn.out tie
should be declared 1. I personally had 4 separate c files program 9. Hints This is your first
"large" program. It took me around 300 lines of code to complete. You will want to break your
problem down into many small manageable functions to make the problem easier to deal with 1.
I also recommend testing each function your write as you go along to 1. help you locate your
errors early 2. I had the following functions defined in my solution and they give a pretty good
ordering of how you should write you should solve this problem 1. read args 2. create board,
print board, destroy board 3. play_game, get play, play is_valid 4. game over, game _won, row
win, col win, diag_ win, right diag win, left diag win
Solution
#include
#include
#include
void read_args(int argc, char *argv[], int* num_rows, int* num_cols, int *num_pieces_to_win);
void create_board(int num_rows, int num_cols, char*** board, int* turn);
void print_board(int num_rows, int num_cols, char** board);
26. void destroy_board(int num_rows, char*** board);
void play_game(int num_rows, int num_cols, int num_pieces_to_win, int turn, char** board);
void check_move();
void get_play(int num_rows, int num_cols, char** board, int* user_move);
void make_move(int num_rows, char**board, int user_move, char pieces);
bool play_is_valid(int num_cols, int num_rows, char** board, int user_move_num, int
user_move);
bool game_over(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool game_won(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool game_tied(int num_rows, int num_cols, int num_pieces_to_win, char**board);
bool row_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool col_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool right_diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
bool left_diag_win(int num_rows, int num_cols, int num_pieces_to_win, char** board);
void read_args(int argc, char *argv[], int* num_rows, int* num_cols, int *num_pieces_to_win) {
//reads command input and ensures there are exactly three numbers
if (argc > 4) {
printf("Too many arguments entered ");
printf("Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win");
exit(0);
}
else if (argc < 4) {
printf("Not enough arguments entered ");
printf("Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win");
exit(0);
}
else { //puts user input into variables
sscanf(argv[1], "%d", &*num_rows);
sscanf(argv[2], "%d", &*num_cols);
sscanf(argv[3], "%d", &*num_pieces_to_win);
}
return;
27. }
void create_board (int num_rows, int num_cols, char*** board, int* turn) {
//set up the connect-n board
const char BLANK_SPACE = '*';
int i, j;
*turn = 0; //player 1 always goes first
*board = (char**)malloc(num_rows * sizeof(char*)); //creates pointers to the rows
for (i = 0; i < num_rows; ++i){ //for each row
(*board)[i] = (char*)malloc(num_cols * sizeof(char)); //create it
for (j = 0; j < num_cols; j++) { //fill in the row with blanks
(*board)[i][j] = BLANK_SPACE;
}
}
}
void print_board (int num_rows, int num_cols, char** board) {
//displays connect-n board
int i, j;
int k = 0;
for(i = 0; i < num_rows; i++){
printf("%d ", ((num_rows- i) - 1));
for(j = 0; j < num_cols; ++j){
printf("%c ", board[i][j]);
}
printf(" ");
}
printf(" %d ", k);
for(j = 1; j < num_cols; j++){
printf("%d ", (j));
28. }
printf(" ");
}
void destroy_board(int num_rows, char*** board){
//destroys connect-n board
int i;
for(i=0; i < num_rows; ++i){ //delete each row
free((*board)[i]);
}
free(*board); //delete pointers to row
*board = NULL; //set board to the null pointers so we know it isn't valid anymore
}
void play_game(int num_rows, int num_cols, int num_pieces_to_win, int turn, char** board){
//play a game of connectn
char pieces[] = "XO"; //player 1 is 'X' and player 2 is 'O'
int user_move;
while(game_over(num_rows, num_cols, num_pieces_to_win, board) == false){ //each turn
print_board(num_rows, num_cols, board);
get_play(num_rows, num_cols, board, &user_move);
make_move(num_rows, board, user_move, pieces[turn]);
turn = (turn + 1) % 2; //change the turn
}
//game is now over
print_board(num_rows, num_cols, board); //display final board
if(game_won(num_rows, num_cols, num_pieces_to_win, board) == true){ //if someone won
if(turn == 1){
printf("Player 1 Won! ");
}
else{
29. printf("Player 2 Won! ");
}
}
else{
printf("Tie game! ");
}
}
void check_move(){ //checks the input
char c = getchar();
while((c != ' ') && (c != EOF)){
c = getchar();
}
}
void get_play(int num_rows, int num_cols, char** board, int* user_move){ //gets move from
user
int user_move_num;
printf("Enter a column between 0 and %d to play in: ", num_cols - 1);
user_move_num = scanf("%d", user_move);
while(play_is_valid(num_rows, num_cols, board, user_move_num, *user_move) == false){
printf("Enter a column between 0 and %d to play in: ", num_cols - 1);
user_move_num = scanf("%d", user_move);
check_move();
}
}
void make_move(int num_rows, char**board, int user_move, char pieces){ //changes the board
based on the move
int i = (num_rows - 1);
const char BLANK_SPACE = '*';
while(i >= 0) {
if(board[i][user_move] == BLANK_SPACE){
board[i][user_move] = pieces;
break;
30. }
else{
i = (i - 1);
}
}
}
bool play_is_valid(int num_cols, int num_rows, char** board, int user_move_num, int
user_move) {
//ensures user move is valid
const char BLANK_SPACE = '*';
if (user_move_num != 1){
return false;
}
else if (user_move >= num_cols){ //outside grid
return false;
}
else if (board[0][user_move] != BLANK_SPACE){ //occupied space
return false;
}
else {
return true;
}
}
bool game_over(int num_rows, int num_cols, int num_pieces_to_win, char** board){
//checks if the game is over
if (game_won(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else if (game_tied(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else {
return false;
}
}
31. bool game_won(int num_rows, int num_cols, int num_pieces_to_win, char** board) {
//checks to see if someone has won
if (row_win(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else if (col_win(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else if (diag_win(num_rows, num_cols, num_pieces_to_win, board)) {
return true;
}
else {
return false;
}
}
bool game_tied(int num_rows, int num_cols, int num_pieces_to_win, char**board) {
//checks to see if someone has tied
const char BLANK_SPACE = '*';
int i, j;
if (game_won(num_rows, num_cols, num_pieces_to_win, board) == true) {
return false;
}
else {
for(i = 0; i < num_rows; i++){
for(j = 0;j < num_cols; j++){
if(board[i][j] == BLANK_SPACE){
return false;
}
}
}
return true;
}
}
bool row_win(int num_rows, int num_cols, int num_pieces_to_win, char** board) {
32. //checks if there is a win horizontally
int i, j;
bool o_wins = false;
bool x_wins = false;
int o_cnt;
int x_cnt;
for (i=0; i=0; j--) {
if (k == num_rows) {
break;
}
if ((x_wins || o_wins) == true) {
break;
}
if (board[k][j] == 'O') {
o_cnt++;
x_cnt = 0;
k = k + 1;
}
else if (board[k][j] == 'X') {
x_cnt++;
o_cnt = 0;
k = k + 1;
}
else {
x_cnt = 0;
o_cnt = 0;
k = k + 1;
}
if (x_cnt == num_pieces_to_win) {