2

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

Adriaan
  • 17,741
  • 7
  • 42
  • 75
wiwi weaam
  • 31
  • 4
  • Most likely, the culprit is scanf, as it leaves behind “\n” characters that interfere with subsequent scans. Use fget to consume the whole line and then process the buffer to determine what the player wants to play. Also, always make sure fscan works as intended by checking its output. – picchiolu Jan 10 '23 at 22:52
  • 1
    When the board is empty `board[0][0]`, `board[0][1]`, and `board[0][2]` will all be equal, so `check_whowon` will return 1. – user3386109 Jan 10 '23 at 22:57
  • 1
    This is "using PuTTY", not "in PuTTY" as it's just a terminal, not a compiler or even a programming language. – tadman Jan 10 '23 at 23:12
  • Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Jan 10 '23 at 23:33
  • Please note that [it is generally expected that you make a debugging attempt yourself before asking for help on Stack Overflow](https://idownvotedbecau.se/nodebugging/). Questions which do not demonstrate any debugging attempt and do not specify what you have learnt in the debugging attempt, are usually not well received. – Andreas Wenzel Jan 10 '23 at 23:33

2 Answers2

2

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.

Aseider
  • 5,875
  • 1
  • 14
  • 25
2

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'.