Tic tac toe (how do i check for winnner?)

This is a discussion on Tic tac toe (how do i check for winnner?) within the C++ Programming forums, part of the General Programming Boards category; Ok, i have my tic tac toe game almost complete, i just need a loop or if statement or both ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Tic tac toe (how do i check for winnner?)

    Ok, i have my tic tac toe game almost complete, i just need a loop or if statement or both that checks if somebody won. My board is board[3][3]. HOw do i do this? Thanks

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The easiest way would be lots of if statements.....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    check for each possible victory condition.
    there are eight.
    2 diagonal, 3 horizontal, 3 vertical
    I think this should do it but I don't have a compiler here.

    bool CheckForWin( char board[3][3], char ch )
    {
    // check for diagonal cases
    if( ch == board[0][0] && ch == board[1][1] && ch == board[2][2] )
    return( true );
    if( ch == board[0][2] && ch == board[1][1] && ch == board[2][0] )
    return( true );

    // check for horizontal cases
    if( ch == board[0][0] && ch == board[0][1] && ch == board[0][2] )
    return( true );
    if( ch == board[1][0] && ch == board[1][1] && ch == board[1][2] )
    return( true );
    if( ch == board[2][0] && ch == board[2][1] && ch == board[2][2] )
    return( true );

    // check for vertical cases
    if( ch == board[0][0] && ch == board[1][0] && ch == board[2][0] )
    return( true );
    if( ch == board[0][1] && ch == board[1][1] && ch == board[2][1] )
    return( true );
    if( ch == board[0][2] && ch == board[1][2] && ch == board[2][2] )
    return( true );

    // not a win yet
    return( false );
    }

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, checking all 8 cases is a viable option if you're dealing with a tic-tac-toe game, but one thing to consider is you could ALSO just write a function that checks if the last move made is in a line of 3 (i.e. is game-winning).

    For this game, it might be MORE work to do it like this, but if you wanted to program something like connect-4, you MUST check like this (see if each piece wins the game) because to try to check for every possible victory in a larger board wouldn't be viable.

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, 10:59 AM
  2. Bizarre Tic Tac Toe!
    By Lynux-Penguin in forum C Programming
    Replies: 5
    Last Post: 05-14-2002, 06:10 PM
  3. Check my Tic Tac Toe game for bugs please
    By aresashura in forum Game Programming
    Replies: 5
    Last Post: 12-19-2001, 08:47 AM
  4. Replies: 22
    Last Post: 11-08-2001, 11:01 PM
  5. My tic tac toe game, please check this out.
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 11:14 PM

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