SlideShare a Scribd company logo
1 of 33
Download to read offline
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 < 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);
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);
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;
}
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;
}
}
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;
}
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 {
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
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 < 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);
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);
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;
}
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;
}
}
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;
}
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 {
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**** * * * * * *
* 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 *
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
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 * * * * * * *
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 *
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
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.
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
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);
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 < 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);
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);
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;
}
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;
}
}
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;
}
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 {
return false;
}
}

More Related Content

Similar to The following is my code for a connectn program. When I run my code .pdf

Objectives Create a Java program using programming fundamentals (fi.docx
Objectives Create a Java program using programming fundamentals (fi.docxObjectives Create a Java program using programming fundamentals (fi.docx
Objectives Create a Java program using programming fundamentals (fi.docxamit657720
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTsKevlin Henney
 
Write a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdfWrite a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdfmckenziecast21211
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquacareser
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquapariwar
 
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docxNewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docxcurwenmichaela
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...GkhanGirgin3
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 

Similar to The following is my code for a connectn program. When I run my code .pdf (12)

Lập trình C
Lập trình CLập trình C
Lập trình C
 
Objectives Create a Java program using programming fundamentals (fi.docx
Objectives Create a Java program using programming fundamentals (fi.docxObjectives Create a Java program using programming fundamentals (fi.docx
Objectives Create a Java program using programming fundamentals (fi.docx
 
Code em Poker
Code em PokerCode em Poker
Code em Poker
 
AI For Texam Hold'em poker
AI For Texam Hold'em pokerAI For Texam Hold'em poker
AI For Texam Hold'em poker
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
Write a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdfWrite a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdf
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docxNewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
Lab Question
Lab QuestionLab Question
Lab Question
 

More from eyelineoptics

Many primate females form dominance hierarchies becausethey compete .pdf
Many primate females form dominance hierarchies becausethey compete .pdfMany primate females form dominance hierarchies becausethey compete .pdf
Many primate females form dominance hierarchies becausethey compete .pdfeyelineoptics
 
MATLAB CODE Write a function with the header which calculates the p.pdf
MATLAB CODE Write a function with the header which calculates the p.pdfMATLAB CODE Write a function with the header which calculates the p.pdf
MATLAB CODE Write a function with the header which calculates the p.pdfeyelineoptics
 
Methods in Behavioral Research 12th EditionIdentify the independen.pdf
Methods in Behavioral Research 12th EditionIdentify the independen.pdfMethods in Behavioral Research 12th EditionIdentify the independen.pdf
Methods in Behavioral Research 12th EditionIdentify the independen.pdfeyelineoptics
 
look to determine what is the earliest era that fossils have been fo.pdf
look to determine what is the earliest era that fossils have been fo.pdflook to determine what is the earliest era that fossils have been fo.pdf
look to determine what is the earliest era that fossils have been fo.pdfeyelineoptics
 
Javathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdfJavathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdfeyelineoptics
 
java as a beginnerSolutionimport statements import java.io.pdf
java as a beginnerSolutionimport statements import java.io.pdfjava as a beginnerSolutionimport statements import java.io.pdf
java as a beginnerSolutionimport statements import java.io.pdfeyelineoptics
 
Implementing list using linked list Implementing list using linked l.pdf
Implementing list using linked list Implementing list using linked l.pdfImplementing list using linked list Implementing list using linked l.pdf
Implementing list using linked list Implementing list using linked l.pdfeyelineoptics
 
In corn, kernels are usually smooth due to the Su allele. The recess.pdf
In corn, kernels are usually smooth due to the Su allele. The recess.pdfIn corn, kernels are usually smooth due to the Su allele. The recess.pdf
In corn, kernels are usually smooth due to the Su allele. The recess.pdfeyelineoptics
 
If a concentration gradient has been established across a membrane, t.pdf
If a concentration gradient has been established across a membrane, t.pdfIf a concentration gradient has been established across a membrane, t.pdf
If a concentration gradient has been established across a membrane, t.pdfeyelineoptics
 
I need help to define each one. thank you. Define Acute enterocoli.pdf
I need help to define each one. thank you. Define  Acute enterocoli.pdfI need help to define each one. thank you. Define  Acute enterocoli.pdf
I need help to define each one. thank you. Define Acute enterocoli.pdfeyelineoptics
 
how to estimate the fermi temperature using any value of mSolut.pdf
how to estimate the fermi temperature using any value of mSolut.pdfhow to estimate the fermi temperature using any value of mSolut.pdf
how to estimate the fermi temperature using any value of mSolut.pdfeyelineoptics
 
How do I find the repeated ration number 0.415, where 15 is repeat.pdf
How do I find the repeated ration number 0.415, where 15 is repeat.pdfHow do I find the repeated ration number 0.415, where 15 is repeat.pdf
How do I find the repeated ration number 0.415, where 15 is repeat.pdfeyelineoptics
 
Describe three early signs of shock and the rationale for each.So.pdf
Describe three early signs of shock and the rationale for each.So.pdfDescribe three early signs of shock and the rationale for each.So.pdf
Describe three early signs of shock and the rationale for each.So.pdfeyelineoptics
 
Describe a real world application for right angle trigonometry. Expl.pdf
Describe a real world application for right angle trigonometry. Expl.pdfDescribe a real world application for right angle trigonometry. Expl.pdf
Describe a real world application for right angle trigonometry. Expl.pdfeyelineoptics
 
Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfeyelineoptics
 
Consider a collection of identical boards, each with mass m, length L.pdf
Consider a collection of identical boards, each with mass m, length L.pdfConsider a collection of identical boards, each with mass m, length L.pdf
Consider a collection of identical boards, each with mass m, length L.pdfeyelineoptics
 
Based on the given abstract and introduction to this journal article.pdf
Based on the given abstract and introduction to this journal article.pdfBased on the given abstract and introduction to this journal article.pdf
Based on the given abstract and introduction to this journal article.pdfeyelineoptics
 
9-42 The hospital administrator at St. Charles General must appoin.pdf
9-42 The hospital administrator at St. Charles General must appoin.pdf9-42 The hospital administrator at St. Charles General must appoin.pdf
9-42 The hospital administrator at St. Charles General must appoin.pdfeyelineoptics
 
624 Chapter Twenty-Two The Candiovarcular system. Vessels and Cimalat.pdf
624 Chapter Twenty-Two The Candiovarcular system. Vessels and Cimalat.pdf624 Chapter Twenty-Two The Candiovarcular system. Vessels and Cimalat.pdf
624 Chapter Twenty-Two The Candiovarcular system. Vessels and Cimalat.pdfeyelineoptics
 

More from eyelineoptics (20)

Many primate females form dominance hierarchies becausethey compete .pdf
Many primate females form dominance hierarchies becausethey compete .pdfMany primate females form dominance hierarchies becausethey compete .pdf
Many primate females form dominance hierarchies becausethey compete .pdf
 
MATLAB CODE Write a function with the header which calculates the p.pdf
MATLAB CODE Write a function with the header which calculates the p.pdfMATLAB CODE Write a function with the header which calculates the p.pdf
MATLAB CODE Write a function with the header which calculates the p.pdf
 
Methods in Behavioral Research 12th EditionIdentify the independen.pdf
Methods in Behavioral Research 12th EditionIdentify the independen.pdfMethods in Behavioral Research 12th EditionIdentify the independen.pdf
Methods in Behavioral Research 12th EditionIdentify the independen.pdf
 
look to determine what is the earliest era that fossils have been fo.pdf
look to determine what is the earliest era that fossils have been fo.pdflook to determine what is the earliest era that fossils have been fo.pdf
look to determine what is the earliest era that fossils have been fo.pdf
 
Javathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdfJavathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdf
 
java as a beginnerSolutionimport statements import java.io.pdf
java as a beginnerSolutionimport statements import java.io.pdfjava as a beginnerSolutionimport statements import java.io.pdf
java as a beginnerSolutionimport statements import java.io.pdf
 
Implementing list using linked list Implementing list using linked l.pdf
Implementing list using linked list Implementing list using linked l.pdfImplementing list using linked list Implementing list using linked l.pdf
Implementing list using linked list Implementing list using linked l.pdf
 
In corn, kernels are usually smooth due to the Su allele. The recess.pdf
In corn, kernels are usually smooth due to the Su allele. The recess.pdfIn corn, kernels are usually smooth due to the Su allele. The recess.pdf
In corn, kernels are usually smooth due to the Su allele. The recess.pdf
 
If a concentration gradient has been established across a membrane, t.pdf
If a concentration gradient has been established across a membrane, t.pdfIf a concentration gradient has been established across a membrane, t.pdf
If a concentration gradient has been established across a membrane, t.pdf
 
I need help to define each one. thank you. Define Acute enterocoli.pdf
I need help to define each one. thank you. Define  Acute enterocoli.pdfI need help to define each one. thank you. Define  Acute enterocoli.pdf
I need help to define each one. thank you. Define Acute enterocoli.pdf
 
how to estimate the fermi temperature using any value of mSolut.pdf
how to estimate the fermi temperature using any value of mSolut.pdfhow to estimate the fermi temperature using any value of mSolut.pdf
how to estimate the fermi temperature using any value of mSolut.pdf
 
How do I find the repeated ration number 0.415, where 15 is repeat.pdf
How do I find the repeated ration number 0.415, where 15 is repeat.pdfHow do I find the repeated ration number 0.415, where 15 is repeat.pdf
How do I find the repeated ration number 0.415, where 15 is repeat.pdf
 
Describe three early signs of shock and the rationale for each.So.pdf
Describe three early signs of shock and the rationale for each.So.pdfDescribe three early signs of shock and the rationale for each.So.pdf
Describe three early signs of shock and the rationale for each.So.pdf
 
Describe a real world application for right angle trigonometry. Expl.pdf
Describe a real world application for right angle trigonometry. Expl.pdfDescribe a real world application for right angle trigonometry. Expl.pdf
Describe a real world application for right angle trigonometry. Expl.pdf
 
Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdf
 
Consider a collection of identical boards, each with mass m, length L.pdf
Consider a collection of identical boards, each with mass m, length L.pdfConsider a collection of identical boards, each with mass m, length L.pdf
Consider a collection of identical boards, each with mass m, length L.pdf
 
CH27Quiz aect.pdf
CH27Quiz aect.pdfCH27Quiz aect.pdf
CH27Quiz aect.pdf
 
Based on the given abstract and introduction to this journal article.pdf
Based on the given abstract and introduction to this journal article.pdfBased on the given abstract and introduction to this journal article.pdf
Based on the given abstract and introduction to this journal article.pdf
 
9-42 The hospital administrator at St. Charles General must appoin.pdf
9-42 The hospital administrator at St. Charles General must appoin.pdf9-42 The hospital administrator at St. Charles General must appoin.pdf
9-42 The hospital administrator at St. Charles General must appoin.pdf
 
624 Chapter Twenty-Two The Candiovarcular system. Vessels and Cimalat.pdf
624 Chapter Twenty-Two The Candiovarcular system. Vessels and Cimalat.pdf624 Chapter Twenty-Two The Candiovarcular system. Vessels and Cimalat.pdf
624 Chapter Twenty-Two The Candiovarcular system. Vessels and Cimalat.pdf
 

Recently uploaded

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Recently uploaded (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

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) {
  • 33. 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 { return false; } }