I code a game but for defining the win or not userdefine part is not working pla
ID: 3600648 • Letter: I
Question
I code a game but for defining the win or not userdefine part is not working plase help me fix it. The other part is all correct. The picture is sample
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
void printbroad(int arr[][7]) { //this function is used to print the board
int i;
int j;
char x[6][7]; //make all elements ' '
for (i = 0; i < 6; ++i) {
for (j = 0; j < 7; ++j) {
x[i][j] = ' ';
}
}
printf(" A B C D E F G ");
printf("|---|---|---|---|---|---|---| ");
for (i = 0; i < 6; ++i) {
for (j = 0; j < 7; ++j) { //replace ' ' with X or O
if (arr[i][j] == 1) {
x[i][j] = 'X';
}
if (arr[i][j] == 0) {
x[i][j] = 'O';
}
printf("| %c ", x[i][j]);
}
printf("|");
printf(" ");
printf("|---|---|---|---|---|---|---|");
printf(" ");
}
}
int playchoice(int player) { //this function reads and outputs the colunm to put X or O
char choice = 'h';
char symble = 'A';
if (player == 1) {
symble = 'X';
}
if (player == 2) {
symble = 'O';
}
printf("Player %d: which column would you like to add an '%c'?", player, symble);
scanf(" %c", &choice);
while ((choice != 'a') && (choice != 'b') && (choice != 'c') && (choice != 'd') && (choice != 'e') && (choice != 'f') && (choice != 'g')) { //when column is valid
printf("Invalid Entry ");
printf("Player %d: which column would you like to add an '%c'?", player, symble);
scanf(" %c", &choice);
}
if (choice == 'a') { //return column
return 0;
}
if (choice == 'b') {
return 1;
}
if (choice == 'c') {
return 2;
}
if (choice == 'd') {
return 3;
}
if (choice == 'e') {
return 4;
}
if (choice == 'f') {
return 5;
}
if (choice == 'g') {
return 6;
}
}
int playgame1(int arr[][7], int col) { //this function is used determine if the column is full or not and put X and O in right place
int i;
while (arr[0][col] == 3) { //check full
if (arr[5][col]==3) {
arr[5][col] = 1;
return 1; //return not full
}
if (arr[4][col] == 3) { // put O and X in right place
arr[4][col] = 1;
return 1;
}
if (arr[3][col] == 3) {
arr[3][col] = 1;
return 1;
}
if (arr[2][col] == 3) {
arr[2][col] = 1;
return 1;
}
if (arr[1][col] == 3) {
arr[1][col] = 1;
return 1;
}
if (arr[0][col] == 3) {
arr[0][col] = 1;
return 1;
}
}
return 4; //return full
}
int Winornot(int x[][7], int player) {
int i, j;
int sum = 0;
for (i = 0; i < 3; ++i) { //check column
for (j = 0; j < 7; ++j) {
if (x[i][j] == player) {
sum = sum + x[i][j] + x[i+1][j] + x[i+2][j] + x[i+3][j];
if (sum == 4) {
return 1;
}
if (sum == 0) {
return 0;
}
}
}
}
for (i = 0; i < 6; ++i) { //check row
for (j = 0; j < 4; ++j) {
if (x[i][j] == player) {
sum = sum + x[i][j] + x[i][j + 1] + x[i][j + 2] + x[i][j + 3];
if (sum == 4) {
return 1;
}
if (sum == 0) {
return 0;
}
}
}
}
for (i = 0; i < 3; ++i) { //check diagonals
for (j = 0; j <4 ; ++j) {
if (x[i][j] == player) {
sum = sum + x[i][j] + x[i + 1][j + 1] + x[i + 2][j + 2] + x[i + 3][j + 3];
if (sum == 4) {
return 1;
}
if (sum == 0) {
return 0;
}
}
}
}
return 4;
}
int playgame2(int arr[][7], int col) { //same as playgame1
int i;
while (arr[0][col] == 3) {
if (arr[5][col] == 3) {
arr[5][col] = 0;
return 1;
}
if (arr[4][col] == 3) {
arr[4][col] = 0;
return 1;
}
if (arr[3][col] == 3) {
arr[3][col] = 0;
return 1;
}
if (arr[2][col] == 3) {
arr[2][col] = 0;
return 1;
}
if (arr[1][col] == 3) {
arr[1][col] = 0;
return 1;
}
if (arr[0][col] == 3) {
arr[0][col] = 0;
return 1;
}
}
return 4;
}
int checktie(int x[][7]) {
int i, j;
for (i = 0; i < 6; ++i) { // make all elements equal to 3
for (j = 0; j < 7; ++j) {
if (x[i][j] == 3);
return 4; //return not full
}
}
return 0;
}
int main() {
int i;
int j;
int arr[6][7];
int check = 4;
int col;
int full = 1;
char conti = 'y';
int tiegame = 4;
printf("Let's play Connect Four in a line ");
while (conti == 'y') { //check conti
for (i = 0; i < 6; ++i) { // make all elements equal to 3
for (j = 0; j < 7; ++j) {
arr[i][j] = 3; //start a new game
}
}
tiegame = 4; //start a new game
full = 1;
check = 4;
if (tiegame == 0){
printbroad(arr);
printf("TIE GAME. You should try again ");
}
else {
while (tiegame == 4) {
if (check == 1) {
printbroad(arr);
printf("Player 1 is a WINNER ");
break;
}
else {
while (check == 4) { //check win for player 1
printbroad(arr); //palyer 1 begins to play
col = playchoice(1);
full = playgame1(arr, col);
while (full == 4) {
printf("That Column is full ");
col = playchoice(1);
full = playgame1(arr, col);
}
printf("Let's play Connect Four in a line ");
check = Winornot(arr, 1);
break;
}
}
if (check == 0) {
printbroad(arr);
printf("Player 2 is a WINNER ");
}
else {
while (check == 4) { //check win for player 1
printbroad(arr); //palyer 1 begins to play
col = playchoice(2);
full = playgame2(arr, col);
while (full == 4) {
printf("That Column is full ");
col = playchoice(2);
full = playgame2(arr, col);
}
printf("Let's play Connect Four in a line ");
check = Winornot(arr, 2);
break;
}
}
}
}
printf("Would you like to play again? (y/n) ");
scanf(" %c", &conti);
while (conti != 'y' && conti != 'n') {
printf("Enter a valid choice ");
scanf(" %c", &conti);
}
}
Explanation / Answer
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
void printbroad(int arr[][7]) { //this function is used to print the board
int i;
int j;
char x[6][7]; //make all elements ' '
for (i = 0; i < 6; ++i) {
for (j = 0; j < 7; ++j) {
x[i][j] = ' ';
}
}
printf(" A B C D E F G ");
printf("|---|---|---|---|---|---|---| ");
for (i = 0; i < 6; ++i) {
for (j = 0; j < 7; ++j) { //replace ' ' with X or O
if (arr[i][j] == 1) {
x[i][j] = 'X';
}
if (arr[i][j] == 0) {
x[i][j] = 'O';
}
printf("| %c ", x[i][j]);
}
printf("|");
printf(" ");
printf("|---|---|---|---|---|---|---|");
printf(" ");
}
}
int playchoice(int player) { //this function reads and outputs the colunm to put X or O
char choice = 'h';
char symble = 'A';
if (player == 1) {
symble = 'X';
}
if (player == 2) {
symble = 'O';
}
printf("Player %d: which column would you like to add an '%c'?", player, symble);
scanf(" %c", &choice);
while ((choice != 'a') && (choice != 'b') && (choice != 'c') && (choice != 'd') && (choice != 'e') && (choice != 'f') && (choice != 'g')) { //when column is valid
printf("Invalid Entry ");
printf("Player %d: which column would you like to add an '%c'?", player, symble);
scanf(" %c", &choice);
}
if (choice == 'a') { //return column
return 0;
}
if (choice == 'b') {
return 1;
}
if (choice == 'c') {
return 2;
}
if (choice == 'd') {
return 3;
}
if (choice == 'e') {
return 4;
}
if (choice == 'f') {
return 5;
}
if (choice == 'g') {
return 6;
}
}
int playgame1(int arr[][7], int col) { //this function is used determine if the column is full or not and put X and O in right place
int i;
while (arr[0][col] == 3) { //check full
if (arr[5][col]==3) {
arr[5][col] = 1;
return 1; //return not full
}
if (arr[4][col] == 3) { // put O and X in right place
arr[4][col] = 1;
return 1;
}
if (arr[3][col] == 3) {
arr[3][col] = 1;
return 1;
}
if (arr[2][col] == 3) {
arr[2][col] = 1;
return 1;
}
if (arr[1][col] == 3) {
arr[1][col] = 1;
return 1;
}
if (arr[0][col] == 3) {
arr[0][col] = 1;
return 1;
}
}
return 4; //return full
}
int Winornot(int x[][7], int player) {
int i, j;
int sum = 0;
for (i = 0; i < 3; ++i) { //check column
for (j = 0; j < 7; ++j) {
if (x[i][j] == player) {
sum = sum + x[i][j] + x[i+1][j] + x[i+2][j] + x[i+3][j];
if (sum == 4) {
return 1;
}
if (sum == 0) {
return 0;
}
}
}
}
for (i = 0; i < 6; ++i) { //check row
for (j = 0; j < 4; ++j) {
if (x[i][j] == player) {
sum = sum + x[i][j] + x[i][j + 1] + x[i][j + 2] + x[i][j + 3];
if (sum == 4) {
return 1;
}
if (sum == 0) {
return 0;
}
}
}
}
for (i = 0; i < 3; ++i) { //check diagonals
for (j = 0; j <4 ; ++j) {
if (x[i][j] == player) {
sum = sum + x[i][j] + x[i + 1][j + 1] + x[i + 2][j + 2] + x[i + 3][j + 3];
if (sum == 4) {
return 1;
}
if (sum == 0) {
return 0;
}
}
}
}
return 4;
}
int playgame2(int arr[][7], int col) { //same as playgame1
int i;
while (arr[0][col] == 3) {
if (arr[5][col] == 3) {
arr[5][col] = 0;
return 1;
}
if (arr[4][col] == 3) {
arr[4][col] = 0;
return 1;
}
if (arr[3][col] == 3) {
arr[3][col] = 0;
return 1;
}
if (arr[2][col] == 3) {
arr[2][col] = 0;
return 1;
}
if (arr[1][col] == 3) {
arr[1][col] = 0;
return 1;
}
if (arr[0][col] == 3) {
arr[0][col] = 0;
return 1;
}
}
return 4;
}
int checktie(int x[][7]) {
int i, j;
for (i = 0; i < 6; ++i) { // make all elements equal to 3
for (j = 0; j < 7; ++j) {
if (x[i][j] == 3);
return 4; //return not full
}
}
return 0;
}
int main() {
int i;
int j;
int arr[6][7];
int check = 4;
int col;
int full = 1;
char conti = 'y';
int tiegame = 4;
printf("Let's play Connect Four in a line ");
while (conti == 'y') { //check conti
for (i = 0; i < 6; ++i) { // make all elements equal to 3
for (j = 0; j < 7; ++j) {
arr[i][j] = 3; //start a new game
}
}
tiegame = 4; //start a new game
full = 1;
check = 4;
if (tiegame == 0){
printbroad(arr);
printf("TIE GAME. You should try again ");
}
else {
while (tiegame == 4) {
if (check == 1) {
printbroad(arr);
printf("Player 1 is a WINNER ");
break;
}
else {
while (check == 4) { //check win for player 1
printbroad(arr); //palyer 1 begins to play
col = playchoice(1);
full = playgame1(arr, col);
while (full == 4) {
printf("That Column is full ");
col = playchoice(1);
full = playgame1(arr, col);
}
printf("Let's play Connect Four in a line ");
check = Winornot(arr, 1);
break;
}
}
if (check == 0) {
printbroad(arr);
printf("Player 2 is a WINNER ");
}
else {
while (check == 4) { //check win for player 1
printbroad(arr); //palyer 1 begins to play
col = playchoice(2);
full = playgame2(arr, col);
while (full == 4) {
printf("That Column is full ");
col = playchoice(2);
full = playgame2(arr, col);
}
printf("Let's play Connect Four in a line ");
check = Winornot(arr, 2);
break;
}
}
}
}
printf("Would you like to play again? (y/n) ");
scanf(" %c", &conti);
while (conti != 'y' && conti != 'n') {
printf("Enter a valid choice ");
scanf(" %c", &conti);
}
}
printf("Goodbye ");
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.