Results 1 to 2 of 2

Thread: tic tac toe game code in turbo c needed???

  1. #1
    asdz's Avatar
    asdz is offline Advance Member
    Last Online
    3rd April 2022 @ 10:51 AM
    Join Date
    29 Apr 2010
    Age
    34
    Posts
    695
    Threads
    173
    Credits
    331
    Thanked
    10

    Question tic tac toe game code in turbo c needed???

    sm1 plz help...i need simple tic tac toe game code in turbo c....sm1 help....

  2. #2
    KHANZZzzz's Avatar
    KHANZZzzz is offline Senior Member+
    Last Online
    23rd December 2014 @ 10:39 PM
    Join Date
    14 Dec 2008
    Location
    Athens, Greece
    Age
    34
    Gender
    Male
    Posts
    2,652
    Threads
    187
    Credits
    127
    Thanked
    446

    Default

    #include<stdio.h>

    #include<conio.h>

    #include<stdlib.h>

    int board[10] = {2,2,2,2,2,2,2,2,2,2};

    int turn = 1,flag = 0;

    int player,comp;

    void menu();

    void go(int n);

    void start_game();

    void check_draw();

    void draw_board();

    void player_first();

    void put_X_O(char ch,int pos);



    main()

    {

    clrscr();

    _setcursortype(_NOCURSOR);

    menu();

    getch();

    return(0);

    }

    void menu()

    {

    int choice;

    printf("\n--------MENU--------");

    printf("\n1 : Play with X");

    printf("\n2 : Play with O");

    printf("\n3 : Exit");

    printf("\nEnter your choice:>");

    scanf("%d",&choice);

    turn = 1;

    switch (choice)

    {

    case 1:

    player = 1;

    comp = 0;

    player_first();

    break;

    case 2:

    player = 0;

    comp = 1;

    start_game();

    break;

    case 3:

    exit(0);

    default:

    menu();

    }

    }

    int make2()

    {

    if(board[5] == 2)

    return 5;

    if(board[2] == 2)

    return 2;

    if(board[4] == 2)

    return 4;

    if(board[6] == 2)

    return 6;

    if(board[8] == 2)

    return 8;

    return 0;

    }

    int make4()

    {

    if(board[1] == 2)

    return 1;

    if(board[3] == 2)

    return 3;

    if(board[7] == 2)

    return 7;

    if(board[9] == 2)

    return 9;

    return 0;

    }

    int posswin(int p)

    {

    // p==1 then X p==0 then O

    int i;

    int check_val,pos;

    if(p == 1)

    check_val = 18;

    else

    check_val = 50;

    i = 1;

    while(i<=9)//row check

    {

    if(board[i] * board[i+1] * board[i+2] == check_val)

    {

    if(board[i] == 2)

    return i;

    if(board[i+1] == 2)

    return i+1;

    if(board[i+2] == 2)

    return i+2;

    }

    i+=3;

    }

    i = 1;

    while(i<=3)//column check

    {

    if(board[i] * board[i+3] * board[i+6] == check_val)

    {

    if(board[i] == 2)

    return i;

    if(board[i+3] == 2)

    return i+3;

    if(board[i+6] == 2)

    return i+6;

    }

    i++;

    }

    if(board[1] * board[5] * board[9] == check_val)

    {

    if(board[1] == 2)

    return 1;

    if(board[5] == 2)

    return 5;

    if(board[9] == 2)

    return 9;

    }

    if(board[3] * board[5] * board[7] == check_val)

    {

    if(board[3] == 2)

    return 3;

    if(board[5] == 2)

    return 5;

    if(board[7] == 2)

    return 7;

    }

    return 0;

    }

    void go(int n)

    {

    if(turn % 2)

    board[n] = 3;

    else

    board[n] = 5;

    turn++;

    }

    void player_first()

    {

    int pos;

    check_draw();

    draw_board();

    gotoxy(30,18);

    printf("Your Turn :> ");

    scanf("%d",&pos);

    if(board[pos] != 2)

    player_first();

    if(pos == posswin(player))

    {

    go(pos);

    draw_board();

    gotoxy(30,20);

    textcolor(128+RED);

    cprintf("Player Wins");

    getch();

    exit(0);

    }

    go(pos);

    draw_board();

    start_game();

    }

    void start_game()

    {

    // p==1 then X p==0 then O

    if(posswin(comp))

    {

    go(posswin(comp));

    flag = 1;

    }

    else

    if(posswin(player))

    go(posswin(player));

    else

    if(make2())

    go(make2());

    else

    go(make4());

    draw_board();

    if(flag)

    {

    gotoxy(30,20);

    textcolor(128+RED);

    cprintf("Computer wins");

    getch();

    }

    else

    player_first();

    }

    void check_draw()

    {

    if(turn > 9)

    {

    gotoxy(30,20);

    textcolor(128+RED);

    cprintf("Game Draw");

    getch();

    exit(0);

    }

    }

    void draw_board()

    {

    int j;

    for(j=9;j<17;j++)

    {

    gotoxy(35,j);

    printf("| |");

    }

    gotoxy(28,11);

    printf("-----------------------");

    gotoxy(28,14);

    printf("-----------------------");

    for(j=1;j<10;j++)

    {

    if(board[j] == 3)

    put_X_O('X',j);

    else

    if(board[j] == 5)

    put_X_O('O',j);

    }

    }

    void put_X_O(char ch,int pos)

    {

    int m;

    int x = 31, y = 10;

    m = pos;

    if(m > 3)

    {

    while(m > 3)

    {

    y += 3;

    m -= 3;

    }

    }

    if(pos % 3 == 0)

    x += 16;

    else

    {

    pos %= 3;

    pos--;

    while(pos)

    {

    x+=8;

    pos--;

    }

    }

    gotoxy(x,y);

    printf("%c",ch);

    }

Similar Threads

  1. Nokia Secret Codes And Tricks!!!!!!
    By zaibshah in forum General Mobile Discussion
    Replies: 17
    Last Post: 25th June 2012, 09:05 PM
  2. Nokia Mobile Sets Secret Codes
    By majid786 in forum General Mobile Discussion
    Replies: 16
    Last Post: 24th March 2012, 03:48 PM
  3. all format and unlock codes
    By IT_DOCTOR in forum General Mobile Discussion
    Replies: 30
    Last Post: 29th December 2011, 08:19 PM
  4. Nokia 2170 Secret Codes
    By IT_DOCTOR in forum General Mobile Discussion
    Replies: 3
    Last Post: 24th March 2010, 09:32 AM
  5. all gsm secret code
    By tahseenrajput in forum General Mobile Discussion
    Replies: 13
    Last Post: 15th March 2010, 05:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •