This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2014/5/9

Boolean Tic Tac Toe Method

JasonZhu JasonZhu

2014/5/9

#
I'm writing a Boolean Tic Tac Toe method that checks for a winner through diagonals and crosses. I've got it working, but it seemed really inefficient and unnecessarily redundant; I can't think up of a way to simplify it. Can you guys see a way? Thanks!
    public boolean winner(int player)
    {
        int count = 0;
        for(int r=0;r<3;r++){ // top left to bottom right
            if(board[r][r].getPlayer()==player){
                count++;
            }
        }
        if(count==3){
            return true;
        }else{
            count = 0;
            int c = 2;
            for(int r=0;r<3;r++){ // top right to bottom left
                if(board[r][c--].getPlayer()==player){
                    count++;   
                }
            }
        }
        if(count==3){
            return true;
        }else{
            count = 0;
            for(int r=0;r<3;r++){ // top to bottom from col 0
                if(board[r][0].getPlayer()==player){
                    count++;
                }
            }  
        }
        if(count==3){
            return true;
        }else{
            count = 0;
            for(int r=0;r<3;r++){ // top to bottom from col 1
                if(board[r][1].getPlayer()==player){
                    count++;
                }
            }
        }
        if(count==3){
            return true;
        }else{
            count = 0;
            for(int r=0;r<3;r++){ // top to bottom from col 2
                if(board[r][2].getPlayer()==player){
                    count++;
                }
            }
        }
        if(count==3){
            return true;
        }else{
            count = 0;
            for(int c=0;c<3;c++){ // left to right from row 0
                if(board[0][c].getPlayer()==player){
                    count++;
                }
            }  
        }      
        if(count==3){
            return true;
        }else{
            count = 0;
            for(int c=0;c<3;c++){ // left to right from row 1
                if(board[1][c].getPlayer()==player){
                    count++;
                }
            }
        }
        if(count==3){
            return true;
        }else{
            count = 0;
            for(int c=0;c<3;c++){ // left to right from row 2
                if(board[2][c].getPlayer()==player){
                    count++;
                }
            }
        }
        if(count==3){
            return true;
        }
        return false;
    }
danpost danpost

2014/5/9

#
private boolean winner(int player)
{
    for (int i=0; i<3; i++)
    {  // the horizontals and verticals
        if (board[0][i] == player && board[1][i] == player && board[2][i] == player) return true;
        if (board[i][0] == player && board[i][1] == player && board[i][2] == player) return true;
    }
    // the diagonals
    if (board[0][0] == player && board[1][1] == player && board[2][2] == player) return true;
    if (board[0][2] == player && board[1][1] == player && board[2][0] == player) return true;
    return false;
}
JasonZhu JasonZhu

2014/5/9

#
Wow, that saves so much more room, thanks.
You need to login to post a reply.