For an Assignment I need to code the game Tic Tac Toe in C in Putty. I cant find whats wrong with the program. No matter what I put as input the program returns "Player X Won!"
can anyone spot the issue?
heres the code for the functions
#include <stdio.h>
#include "tictac.h"
#define BOARD_SIZE 3 // size of the board
void print_theboard(char board[BOARD_SIZE][BOARD_SIZE]) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
printf(" %c ", board[i][j]);
if (j < BOARD_SIZE - 1) {
printf("|");
}
}
printf("\n");
if (i < BOARD_SIZE - 1) {
printf("---+---+---\n");
}
}
}
int check_whowon(char board[BOARD_SIZE][BOARD_SIZE]) {
//Gewinn in den Reihen
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
return 1;
}
//Gewinn in den Spalten
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[0][j] == board[1][j] && board[1][j] == board[2][j])
return 1;
}
//Gewinn in den Diagonalen
if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
return 0;
if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
return 1;
return 0;
}
~
heres the code for the .h file
void print_theboard();
int check_whowon();
int check_draw();
heres the code for the main
#include <stdio.h>
#include "tictac.h"
#define BOARD_SIZE 3 // size of the boad
int main() {
char board[BOARD_SIZE][BOARD_SIZE];
int row, col, game_over = 0;
char player = 'X';
// initialize the board with empty spaces
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = ' ';
}
}
while (!game_over) {
// display the current status of the board
print_theboard(board);
printf("Player %c, enter the row and column (e.g. 0 2): ", player);
scanf("%d %d", &row, &col);
// validate the input
if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE) {
board[row][col] = player;
// check if the game is won or drawn
if (check_whowon(board)) {
printf("Player %c wins!\n", player);
game_over = 1;
}
else {
printf("Invalid input. Please try again.\n");
} if(player='X')
player='O';
else
player='X';
}
return 0;
}
}
No matter what I put as input the program returns "Player X Won!"