I am trying to learn C after starting with assembly language and I am making a 6x6 connect 4 game using arrays and I am just getting started and testing the functions as I go to make sure they work. Any guidance would be appreciated. Also, I'm trying to do this using only the stdio.h
Here is a sample
Player 1, choose a column (1-6): j
You're in main now! 106
Player 2, choose a column (1-6):
You're in main now! 10
Invalid move. Try again.
Choose a column (1-6): ^C
I have no idea why it goes through the second if statement and prints the player 2 prompt before calling the valid_input function during the loop. Furthermore, why does it not ask the user for a second input if its going thought that if statement? Why is col equal to 10 after I give it a value that isn't 10?
#include <stdio.h>
#define true 1
#define false 0
//////////////////////////
// check if move is valid
//////////////////////////
int valid_move(int row_0[], int col)
{
while ((col != 49 || col != 50 || col != 51 || col != 52 || col != 53 || col != 54) && row_0[col] == 0)
{
printf("\nInvalid move. Try again.");
printf("\nChoose a column (1-6): ");
char col;
scanf("%c", &col);
col = (int)col;
printf("\nYoure in valid move now! %i", col); //a peek at if it saves variabel properly
}
return 0;
}
////////////////////
// get next open row
////////////////////
int open(int row_0[], int row_1[], int row_2[], int row_3[], int row_4[], int row_5[], int col)
{
int r;
while (true)
{
if (row_5[col] == 0)
{
r = 5;
}
if (row_4[col] == 0)
{
r = 4;
}
if (row_3[col] == 0)
{
r = 3;
}
if (row_2[col] == 0)
{
r = 2;
}
if (row_1[col] == 0)
{
r = 1;
}
if (row_0[col] == 0)
{
r = 0;
}
}
return r;
}
int main(void)
{
// Initialize Gameboard
int row_0[] = {0, 0, 0, 0, 0, 0};
int row_1[] = {0, 0, 0, 0, 0, 0};
int row_2[] = {0, 0, 0, 0, 0, 0};
int row_3[] = {0, 0, 0, 0, 0, 0};
int row_4[] = {0, 0, 0, 0, 0, 0};
int row_5[] = {0, 0, 0, 0, 0, 0};
// Welcome Message
printf("===================== Connect 4 =====================\n");
int game_over = false;
int turn = 0;
int rows = 6;
char col;
while (game_over == false)
{
if (turn == 0) // Ask for Player 1 input
{
printf("\nPlayer 1, choose a column (1-6): ");
scanf("%c", &col);
col = (int)col;
printf("\nYou're in main now! %i", col);
valid_move(row_0, col);
}
if (turn == 1) // Ask for Player 2 input
{
printf("\nPlayer 2, choose a column (1-6): ");
scanf("%c", &col);
col = (int)col;
printf("\nYou're in main now! %i", col);
valid_move(row_0, col);
}
turn++;
turn = turn % 2;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire