dimanche 20 septembre 2015

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 to prompt two users to enter a "naught" or a "cross" into one of the nine positions on a tic tact grid, then display them on the grid and say who is the winner or if there is a winner, it does however mention to make things simpler I'm only to only display the grid completed at the end.

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 some what over thinking this but I am unsure, if anyone has got any help that'd be great. 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.

This is what I've written so far.

Thank you,

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

int main()
{
    int spot0, spot1, spot2, spot3, spot4, spot5, spot6, spot7, spot8, temp;
    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 which spot you would like to choose between 1 and 9 ");
    scanf("%d\n",&temp);
    if((temp = 1)){
        spot0 == "X";
    }
    printf("Please pick another number ");
    scanf("%d\n", &temp);
    if((temp = 2) && (temp!= 1) %% (spot1 != "X")){
        spot1 = "O";
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire