Home > Back-end >  Tic Tac Toe in C
Tic Tac Toe in C

Time:01-11

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!"

CodePudding user response:

I guess that the check is weird. No matter where you place your token in the very first round, you will still have two rows which remain empty. Since you initialize your board with space chars, your row check

board[i][0] == board[i][1] && board[i][1] == board[i][2]

will evaluate to

' ' == ' ' && ' ' == ' '

which is true. You should check additionally if the row/column/diagonal actually has a token in it.

CodePudding user response:

The board is initialized with spaces:

 // 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] = ' ';
        }
    }

The variable player is initialised with 'X' because X is playing first:

char player = 'X';

But when the check_whowon(board) function is called the first time, it checks if the horizontal, vertical or diagonal lines have the same character (not if they specifically have 'X' or 'O'). So it always returns true because of the initialised space characters.

To fix this, edit checkwhowon() function to ignore the space character or specifically check for 'X' and 'O'.

  •  Tags:  
  • c
  • Related