Thread: Tic tac toe, final piece...need the check winner to return values but don't know how.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    Tic tac toe, final piece...need the check winner to return values but don't know how.

    So this is what the c program is doing right now... it is checking for a winner...

    I need it to heck for a winner each time a move is made and since this function is called by the int main...I don't really know how I am going to do it.

    could someone show me what to put in this function and then the intmain so that the game will check for a winner or draw??

    Code:
    int checkWinner(char cell[])
    {
    int i;
    
    
    // Diagonals
    if (cell[4]!=0 && (cell[4]==cell[0] && cell[4]==cell[8] || cell[4]==cell[2] && cell[4]==cell[6]))
    return cell[4];
    
    
    // Horizontals
    for (i=0; i<9; i+=3)
    if (cell[i]>' ' && cell[i]==cell[i+1] && cell[i]==cell[i+2])
    return cell[i];
    
    
    // Verticals
    for (i=0; i<3; i++)
    if (cell[i]>' ' && cell[i]==cell[i+3] && cell[i]==cell[i+6])
    return cell[i];
    
    
    // No match
    return ' ';
    
    
    
    
    }
    my int main funt right now....(ask if you would like to see whole code..

    Code:
    int main()
    {
        char cell[9];
        int cont = 1;
        int ccode = 0;
    
    
        info();
        init(cell);
        gameBoard(cell);
        while(cont)
        {
    userMove(cell);
    gameBoard(cell);
    
    
    
    
    //check if user wins
    //if user wins, print user won
    //and cont-- to exit loop
    computerMove(cell);
    gameBoard(cell);
    
    
    checkWinner(cell);
    
    
    //check if computer wins, print computer won
    //and cont-- to exit loop
    //if nobody won get another user input
          }
    
    
        return 0;
    }

    basically the checkwinner is not doing its job right now....its just there but the game works....

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well in main, you would do something like

    Code:
    winner = checkWinner(cell);
    if ( winner != ' ' ) {
      print the winner
      flag that the game is over
    }
    But your checkWinner() seems to be in some doubt as to whether 0 or ' ' represents an empty cell.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    wow thanks

    so i put in the main

    Code:
    int winner;
    
    winner = checkWinner(cell);
    if ( winner != ' ' ) {
      printf( " you win!!!!\n", winner);
      break;

    and this works perfect!

    shall I do the same for the computermove??

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    heres what I was thinking of doing, however...when the gameboard fills up....it still says I win....:/

    can someone help me fix? this is in main

    Code:
    int winner;
    
    winner = checkWinner(cell);
    if ( winner != ' ' ) {
      printf( " you win!!!!\n", winner);
      break;
    
    
      {
          if( winner='o')
            printf( "you lose to a machine!??????!!?!?!?!\n", winner);
    
    
            else("you lose", winner);
      }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Checking for a win om TTT is best done using a lookup table. This has been discussed various other times on this board, you can probably find them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    Thanks

    BUT I want to stick with this one...

    it will be too much work to even start a new way which I've no clue on executing yet......:\

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    TTT: declares draw after computer move, annoying...how to get it only if there is a..

    get it to display only if it actually is a draw

    Code:
    winner = checkWinner(cell);
    if ( winner != ' ' ) {
      printf( " you totally won!!!!\n", winner);
      break;
    
    
      {
          if( winner='o' )
            printf( "you lost!?!!?!?!?!\n", winner);
    
    
    
    
      }
      }else{
          printf("DRAW\n", winner);
    
    
      }
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Dom View Post
    Code:
          if( winner='o' )
    = assigns a value
    == compares a value


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe check winner
    By dhardin in forum C++ Programming
    Replies: 15
    Last Post: 12-20-2009, 07:57 PM
  2. Check tic tac toe winner
    By cjmdjm in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2005, 12:41 PM
  3. Replies: 12
    Last Post: 06-06-2005, 05:45 AM
  4. Replies: 7
    Last Post: 05-20-2005, 02:48 PM
  5. My first attempt at buying a computer piece by piece
    By the dead tree in forum Tech Board
    Replies: 11
    Last Post: 12-22-2004, 03:11 PM