#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    TicTacToe game - evaluation of win


    Greetings,

    I have to do a TicTacToe game, but in 3D (you are not playing in a square, but in a cube). I have fields like this: char [X][X][X]. (X is an optional number)
    This program should work for 2 players, and when one player has number of X fields in a row, column or in each optional diagonal, program should end.

    For example, if the X=3 (so i will play on 3x3x3), alghorytm for detection one of the win situation will be like this:
    (first coordinate is row, second column, and the third depth)

    for (a=0; a<3; a++)
    {
    for (b=0;b<3;b++){
    if(pole[0][a][b]==pole[1][a][b]&&pole[0][a][b]==pole[2][a][b])
    {
    ... bla bla bla, somebody wins, bla bla bla....
    }

    Problem is, that i have no idea how to make alghorytm when X is bigger then 3. Yes, I can manually write it for x=4, x=5, but it would be very long, so I am looking for an universal alghorytm depending on X. Any ideas? Thanx.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Something like this
    Code:
    int check_row ( int board[3][3], int row ) {
        int c = board[row][0];
        int win = 1;
        for ( i = 1 ; win && i < 3 ; i++ ) {
            win = win && c == board[row][i] );
        }
        return win;
    }
    int check ( int board[3][3] ) {
        int win = 0;
        for ( i = 0 ; !win && i < 3 ; i++ ) {
            win = check_row(board,i);
        }
        for ( i = 0 ; !win && i < 3 ; i++ ) {
            win = check_col(board,i);
        }
        if ( !win ) win = check_lr_diag(board);
        if ( !win ) win = check_rl_diag(board);
    }
    You need to:
    - implement check_col, check_lr_diag and check_rl_diag
    - change board[][] into board **
    - add a 'boardsize' parameter to all functions, replacing all the hard-coded 3's.
    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

IMN logo majestic logo threadwatch logo seochat tools logo