plz help wid this tic tac toe code

This is a discussion on plz help wid this tic tac toe code within the C Programming forums, part of the General Programming Boards category; im writin a tic tac toe code..bt theres is a problem my code cannot decide who the winner is ....i ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    Angry plz help wid this tic tac toe code

    im writin a tic tac toe code..bt theres is a problem my code cannot decide who the winner is ....i have checked the code and there seems to be nutthing wrong wid it any help is welcome
    i am posting the code below:
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main ()
    {
    int go=0;
    int i =0;
    int player;
    int row=0;
    int column=0;
    int winner=0;
    int line=0;
    char board[3][3]={
    		   {'1','2','3'},
    		   {'4','5','6'},
    		   {'7','8','9'}
    		 };
    
    
     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][1],board[1][2],board[1][3]);
     printf("---|---|--- |\n");
     printf("%c| %c| %c| \n", board[2][1],board[2][2],board[2][3]);
    
    
    
    
     for(i=0;i<9;i++)
     {
    
    player=(i%2)+1;
    
     printf("hi player %d please indicate position where u want 2 place %c \n", player,(player==1)?'x':'o');
     scanf("%d",&go);
     row=--go/3;
     column=go%3;
    
    
    
    
     board[row][column]=(player==1)? 'x':'o';
    
    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]);
      printf("---|---|---| \n");
        scanf("%d", board[row++][column++]);
    }
    
    
    if(i<9);
    {
    goto chk;
    chk:
    {
    //this is the part where winner has to be decided and where the problem is its not working//
    
     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;
    
     }
    
    {
       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;
    
    }
    if((board[0][0]==board[1][0]&&board[0][0]==board[2][0])||
       (board[0][1]==board[1][1]&&board[0][1]==board[2][1])||
       (board[0][2]==board[1][2]&&board[0][2]==board[2][2]))
       winner=player;
    }
    
    
     printf("player%dwins\n",winner);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if(i<9);
    {
    goto chk;
    chk:
    {
    That probably takes the prize for todays most pointless piece of code. What do you actually expect this to do? [What it actually does is: It checks if i is less than 9, if so do nothing (as there is a semicolon immediately after the i), then start a new block, which contains a goto to the immediately following label]. Removing that (and the corresponding end brace) will change NOTHING of what the code actually does.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right now you'll say that the player wins, even if the computer wins, since that's what player always is. Once you find three-in-a-row, you'll have to figure out whether it's three X or three O.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  2. Please look at my tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 10
    Last Post: 12-11-2001, 07:50 AM
  3. Tic Tac Toe
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2001, 12:33 PM
  4. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 11:52 AM
  5. Tic Tac Toe -- Can you guys rate this please?
    By Estauns in forum Game Programming
    Replies: 2
    Last Post: 09-15-2001, 10:22 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21