简体   繁体   中英

Coding Tic Tac Toe in C with only If statements

So the past week I've been attempting to learn some C, I was giving an exercise to code a really simplistic version of tic tac toe.

I have been asked:

  1. Prompt two users to enter a 'naught' or a 'cross' respectively into one of the nine positions on the tic, tac, toe grid
  2. After all 9 inputs have been made display the grid
  3. To make it simpler: only when all 9 grid positions have been entered figure out if there is a winner (you can do this with a few 'if' statements) .

I'm told this can be done with a few if statements, so far I have only learnt up to if 's, including the basic int , char , float , double , ect.

So, what I'm not truly grasping in how to only use if statements to check if:

  1. the position is already taken, if it has prompt the user to try again or place the current persons naught or cross in that position.

  2. keep track of the positions the two users enter, so I know which position each of the two users they have entered to check those positions if its empty or not.

I feel like I am somewhat over thinking this but I am unsure, if anyone has got any help that'd be great.

This is what I've written so far:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char board[9]; 
    int num;
    printf("Today we are playing a game of Tic Tac Toe\n");
    printf("To play the game you need to line up 3 of the same type 'x's or o's' in a line to win\n");
    printf("Before we start, whoever wants to be 'X' starts first, and whoever wants to be 'O' starts second\n");
    printf("The board looks like this\n");
    printf("%d%c%d%c%d\n", 1, 124, 2, 124, 3);
    printf("%d%c%d%c%d\n", 4, 124, 5, 124, 6);
    printf("%d%c%d%c%d\n", 7, 124, 8, 124, 9);
    printf("Lets begin");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'O';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'O';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'O';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'O';
    printf("Please enter a number between 1 - 9 ");
    scanf("%d", &num);
    board[num] = 'X';
    return 0;
}

Note: Could I also ask that all answers be kept to only using if statements as that is as far as I am up too, and it is how the exercise is meant to be completed, although probably this is ten times easier with for/while and arrays. Thank you!

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
 #define ROWS 3
 #define COLUMNS 3
 #define PLAYER_ONE 'X'
 #define PLAYER_TWO 'O'

void board();//calling class required by C
int checkForWinner();//calling class required by C
char tic_tac_toe[ROWS][COLUMNS] =
    {
        { '1', '2', '3'},
        { '4', '5', '6'},
        { '7', '8', '9'}
    };
int main()
{

    int isGameOn=1, currentPlayer=1, pick;
    char checked;

    do
    {
        board();
        currentPlayer = (currentPlayer % 2) ? 1 : 2;
        printf("Player %d, pick a number: ",  currentPlayer);
        scanf("%d", &pick);
        checked = (currentPlayer == 1) ? 'X' : 'O';
        isGameOn = checkForWinner();
        if(pick == 1 && tic_tac_toe[0][0] == '1'){
            tic_tac_toe[0][0] = checked;
            if(isGameOn == 1)break;
        }else if(pick == 2 && tic_tac_toe[0][1] == '2'){
            tic_tac_toe[0][1] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 3 && tic_tac_toe[0][2] == '3'){
            tic_tac_toe[0][2] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 4 && tic_tac_toe[1][0] == '4'){
            tic_tac_toe[1][0] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 5 && tic_tac_toe[1][1] == '5'){
            tic_tac_toe[1][1] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 6 && tic_tac_toe[1][2] == '6'){
            tic_tac_toe[1][2] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 7 && tic_tac_toe[2][0] == '7'){
            tic_tac_toe[2][0] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 8 && tic_tac_toe[2][1] == '8'){
            tic_tac_toe[2][1] = checked;
            if(isGameOn == 1){break;}
        }else if(pick == 9 && tic_tac_toe[2][2] == '9'){
            tic_tac_toe[2][2] = checked;
            if(isGameOn == 1){break;}
        }
        else{
            printf("Invalid moved");
        }
        isGameOn = checkForWinner();
        if(isGameOn == 1){
            printf("***************************************\n");
            printf("The winner is player %d\n", currentPlayer);
            printf("***************************************");
            break;
         }
        currentPlayer++;
    }while(isGameOn == -1);
    board();

    return(0);
}
/*Functions*/

    /*Board display*/
    void board()
    {
        printf("\n");
        printf("%c\t %c\t %c\n", tic_tac_toe[0][0], tic_tac_toe[0][1], tic_tac_toe[0][2]);
        printf("%c\t %c\t %c\n", tic_tac_toe[1][0], tic_tac_toe[1][1], tic_tac_toe[1][2]);
        printf("%c\t %c\t %c\n", tic_tac_toe[2][0], tic_tac_toe[2][1], tic_tac_toe[2][2]);

    }
    /*Checking for winner*/
    int checkForWinner()
    {
        /*checkign vertical line*/
        for(int col=0; col<COLUMNS; col++){
            int row=0;
            if(tic_tac_toe[row][col] == tic_tac_toe[row+1][col] && tic_tac_toe[row+1][col] == tic_tac_toe[row+2][col]){
                return 1;
            }
        }
        /*checking horizontal line*/
        if(tic_tac_toe[0][0] == tic_tac_toe[0][1] && tic_tac_toe[0][1] == tic_tac_toe[0][2]){
            return 1;
        }
        else if(tic_tac_toe[1][0] == tic_tac_toe [1][1] && tic_tac_toe[1][1] == tic_tac_toe[1][2])
        {
            return 1;
        }
        else if(tic_tac_toe[2][0] == tic_tac_toe [2][1] && tic_tac_toe[2][1] == tic_tac_toe[2][2])
        {
            return 1;
        }
        else if(tic_tac_toe[0][0] == tic_tac_toe[1][1] && tic_tac_toe[1][1] == tic_tac_toe[2][2])/*diagonal*/
        {
            return 1;
        }
        else {
            return -1;
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM