简体   繁体   中英

Tic Tac Toe - Turns C

I've been turning around and around trying to figure out how to give my players turns. The questions is for there to be two players playing tic tac toe but I can't figure how to do that. Here's my code:

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

void displayBoard(char [3][3]);enter code here
int playerType (int player, char boardArray[3][3]);
int selectLocation(char [3][3], int , int );
char setTurn(char [3][3], int , int , char );


int main()
{
    int player,location;
    char position;
    char boardArray[3][3]={{'1','2','3'},
            {'4','5','6'},
            {'7','8','9'}};

    player= playerType (player, boardArray);

    int i;

    for(i=0;i<9;i++)
    {

        if (player==3)
            break;
        else{

            location=selectLocation(boardArray, player, location);

            position=setTurn(boardArray, location, player, position);
        }

    }


    return 0;
}

void displayBoard(char boardArray [3][3]) //This displays the tic tac toe board
{
    printf("\t%c|%c|%c\n", boardArray[0][0], boardArray[0][1], boardArray[0][2]);
    printf("\t%c|%c|%c\n", boardArray[1][0], boardArray[1][1], boardArray[1][2]);
    printf("\t%c|%c|%c\n", boardArray[2][0], boardArray[2][1], boardArray[2][2]);

}
int playerType (int player, char boardArray [3][3]) //This decides who plays first
{

    player=0;

    printf("Enter 1 for Player X.\n");
    printf("Enter 2 for Player O.\n");
    printf("Enter 3 to Quit. \n");
    scanf("%d", &player);


    if (player == 1)
    {
        printf("You're player X.\n");
        displayBoard(boardArray);
    }
    else if (player == 2)
    {
        printf("You're player O.\n");
        displayBoard(boardArray);
    }
    else if(player == 3)
        printf("You Quit.\n");

    else
        printf("Invalid Entry.\n");




    return player;
}


int selectLocation(char boardArray[3][3], int player, int location) //This takes in the location
{
    printf("Pick a location from 1-9.\n");
    scanf("%d", &location);

    return location;
}

char setTurn(char boardArray[3][3], int location, int player, char position) //This outputs the location
{

    if (player == 1)
    {
        switch(location)
        {
        case 1:
        {
            boardArray[0][0]='x';
            break;
        }
        case 2:
        {
            boardArray[0][1]='x';
            break;
        }
        case 3:
        {
            boardArray[0][2]='x';
            break;
        }
        case 4:
        {
            boardArray[1][0]='x';
            break;
        }
        case 5:
        {
            boardArray[1][1]='x';
            break;
        }
        case 6:
        {
            boardArray[1][2]='x';
            break;
        }
        case 7:
        {
            boardArray[2][0]='x';
            break;
        }
        case 8:
        {
            boardArray[2][1]='x';
            break;
        }
        case 9:
        {
            boardArray[2][2]='x';
            break;
        }
        default:
            printf("invalid");
        }

    }
    else if (player == 2)
    {
        switch(location)
        {
        case 1:
        {
            boardArray[0][0]='O';
            break;
        }
        case 2:
        {
            boardArray[0][1]='O';
            break;
        }
        case 3:
        {
            boardArray[0][2]='O';
            break;
        }
        case 4:
        {
            boardArray[1][0]='O';
            break;
        }
        case 5:
        {
            boardArray[1][1]='O';
            break;
        }
        case 6:
        {
            boardArray[1][2]='O';
            break;
        }
        case 7:
        {
            boardArray[2][0]='O';
            break;
        }
        case 8:
        {
            boardArray[2][1]='O';
            break;
        }
        case 9:
        {
            boardArray[2][2]='O';
            break;
        }
        default:
            printf("Invalid");
        }
    }

    printf("\t%c|%c|%c\n", boardArray[0][0], boardArray[0][1], boardArray[0][2]);
    printf("\t%c|%c|%c\n", boardArray[1][0], boardArray[1][1], boardArray[1][2]);
    printf("\t%c|%c|%c\n", boardArray[2][0], boardArray[2][1], boardArray[2][2]);


    return position;

}

Change player selection part player= playerType (player, boardArray); inside for loop like this -

int player,location;
char position;
char boardArray[3][3]={{'1','2','3'},
                       {'4','5','6'},
                       {'7','8','9'}};
int i;
for(i=0;i<9;i++)
{
    player= playerType (player, boardArray);
    if (player==3)
        break;
    else
    {
        location=selectLocation(boardArray, player, location);
        position=setTurn(boardArray, location, player, position);
    }
}

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