Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   tic tac toe (http://www.go4expert.com/forums/tic-tac-toe-t14324/)

plike922 3Oct2008 06:19

tic tac toe
 
Please help
Code:

#include <stdio.h>
#include "strlib.h"
#include "simpio.h"
#include "genlib.h"
#include "string.h"

#define size 9

void main()
{
        int rand, x = 0, i, player = 0, turn = 0, row = 0, column = 0, line = 0, winner = 0, used[size], check;
        bool space;
        char board[size][size] = { {'1','2','3'}, {'4','5','6'},{'7','8','9'}};

        printf("Welcome to Tic Tac Toe: Player 1 = you-Player 2 = computer\n");

        for(check=0; check<10; check++)
                        {
                                used[check] = 1;
                        }
        for( i = 1; i<size && winner==0; i++)
        {
 
        //-----------------------------Display board--------------------------------//
        printf("\n\n");
        printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
        printf("-----------\n");
        printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
        printf("-----------\n");
        printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
        //--------------------------------------------------------------------------//

        player = i%2 + 1;
 
        if(player == 1)
        {
        printf("\nPlayer 1, place your X: ");
        turn = GetInteger();
        used[x] = turn;
        x++;
        }
        else
        {
                if(x==0)
                {
                        turn = 1;
                        used[x] = 1;
                        x++;
                }
                else
                {
                printf("\nPlayer 2, place your O: ");
                space = true;
                        for(rand=9; rand>1 && space != true; rand--)
                        {
                                space = true;
                                for(check=0; check<10; check++)
                                {
                                        if(used[check] == rand)
                                        {
                                                space = false;
                                        }
                                        else;
                                }
                                if(space == true)
                                {
                                        rand = -1;
                                }
                                else;
                        }
                turn = rand;
                used[x] = turn;
                x++;
                }
                printf("%d", turn);
        }

        row = --turn/3;
        column = turn%3;
        while(turn<0 || turn>size || board[row][column]>'9');
        board[row][column] = (player == 1) ? 'X' : 'O';


        if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2] == board[2][0]))
                {
                        winner = player;
                }
        else
                {
                        for(line = 0; line <= 2; line ++) if((board[line][0] == board[line][1] && board[line][0] == board[line][2]) || (board[0][line] == board[1][line] && board[0][line] == board[2][line]))winner = player;
                }
        }
        //-----------------------------Display board--------------------------------//
        printf("\n\n");
        printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
        //--------------------------------------------------------------------------//

    if(winner == 0)
        {
      printf("\nIt is a draw\n");
        }
        else
        {
      printf("\nCongratulations, player %d!\n", winner);
        }

  system("pause");
}


xpi0t0s 5Oct2008 04:25

Re: tic tac toe
 
What help do you want?

sun_kangane 24Nov2008 22:06

Re: tic tac toe
 
what problem u have..................

i have done it by some other way...................

search for TIC TAC TO.CPP

sun_kangane 24Nov2008 22:08

Re: tic tac toe
 
[QUOTE=sun_kangane;39387]what problem u have..................

i have done it by some other way...................

search for TIC TAC TO.CPP http://www.go4expert.com/showthread.php?t=15237


All times are GMT +5.5. The time now is 10:00.