Last active
December 5, 2022 17:47
-
-
Save lostsh/43dced0685111ae602370517ac03dba5 to your computer and use it in GitHub Desktop.
ChatGPT generate a connect 4 game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#define ROWS 6 | |
#define COLUMNS 7 | |
// Global variables to keep track of the game state | |
int board[ROWS][COLUMNS]; | |
bool game_over = false; | |
int current_player = 1; | |
char *current_piece = "X"; | |
// Prints the current game board to the console | |
void print_board() { | |
printf("\n"); | |
for (int row = 0; row < ROWS; row++) { | |
for (int col = 0; col < COLUMNS; col++) { | |
if (board[row][col] == 0) { | |
printf("| "); | |
} else if (board[row][col] == 1) { | |
printf("| X "); | |
} else { | |
printf("| O "); | |
} | |
} | |
printf("|\n"); | |
} | |
printf("\n"); | |
} | |
// Places a piece for the current player at the specified column | |
void place_piece(int column) { | |
// Check if the column is full | |
if (board[ROWS - 1][column] != 0) { | |
printf("Column is full!\n"); | |
return; | |
} | |
// Place the piece in the first empty spot in the column | |
for (int row = 0; row < ROWS; row++) { | |
if (board[row][column] == 0) { | |
board[row][column] = current_player; | |
break; | |
} | |
} | |
} | |
// Changes the current player and piece | |
/* | |
void change_player() { | |
current_player = (current_player == 1) ? 2 : 1; | |
current_piece = (current_player == 1) ? "X" : "O"; | |
}*/ | |
// Checks if the current player has won the game | |
bool check_for_win(int column) { | |
// Check if the piece was placed in the bottom row | |
if (board[ROWS - 1][column] == current_player) { | |
// Check for horizontal wins | |
for (int col = 0; col < COLUMNS - 3; col++) { | |
if (board[ROWS - 1][col] == current_player && | |
board[ROWS - 1][col + 1] == current_player && | |
board[ROWS - 1][col + 2] == current_player && | |
board[ROWS - 1][col + 3] == current_player) { | |
return true; | |
} | |
} | |
} | |
// Check if the piece was placed in the left-most or right-most column | |
if (column == 0 || column == COLUMNS - 1) { | |
// Check for vertical wins | |
for (int row = 0; row < ROWS - 3; row++) { | |
if (board[row][column] == current_player && | |
board[row + 1][column] == current_player && | |
board[row + 2][column] == current_player && | |
board[row + 3][column] == current_player) { | |
return true; | |
} | |
} | |
} else { | |
// Check for vertical and diagonal wins | |
for (int row = 0; row < ROWS; row++) { | |
// Check for vertical wins | |
if (row <= ROWS - 4) { | |
if (board[row][column] == current_player && | |
board[row + 1][column] == current_player && | |
board[row + 2][column] == current_player && | |
board[row + 3][column] == current_player) { | |
return true; | |
} | |
} | |
// Check for diagonal wins | |
if (row <= ROWS - 4 && column <= COLUMNS - 4) { | |
if (board[row][column] == current_player && | |
board[row + 1][column + 1] == current_player && | |
board[row + 2][column + 2] == current_player && | |
board[row + 3][column + 3] == current_player) { | |
return true; | |
} | |
} | |
if (row >= 3 && column <= COLUMNS - 4) { | |
if (board[row][column] == current_player && | |
board[row - 1][column + 1] == current_player && | |
board[row - 2][column + 2] == current_player && | |
board[row - 3][column + 3] == current_player) { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
// Changes the current player and piece | |
void change_player() { | |
current_player = (current_player == 1) ? 2 : 1; | |
current_piece = (current_player == 1) ? "X" : "O"; | |
} | |
// Main function to run the game | |
int main() { | |
// Initialize the game board to empty | |
for (int row = 0; row < ROWS; row++) { | |
for (int col = 0; col < COLUMNS; col++) { | |
board[row][col] = 0; | |
} | |
} | |
// Game loop | |
while (!game_over) { | |
// Print the current board | |
print_board(); | |
// Ask the current player to enter a column to place their piece | |
printf("Player %d (%s), enter a column to place your piece: ", current_player, current_piece); | |
int column; | |
scanf("%d", &column); | |
// Place the piece in the selected column | |
place_piece(column); | |
// Check if the current player has won | |
if (check_for_win(column)) { | |
printf("Player %d (%s) has won!\n", current_player, current_piece); | |
game_over = true; | |
} else { | |
// If the game is not over, change the current player | |
change_player(); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment