Advertisement

NEEd help bad code dont understand min max

Started by November 06, 2005 08:58 PM
-1 comments, last by supernegro 19 years ago
before i read about min max algorithm i started doin my own "algorithm" i have to make a tic tac toe game for class. i seem to have a problem with the computer player here is my code i think its kinda brute force, the computer is able to win and block moves when a a player makes them but, I do not know what to do to set off the sequence, that the the initial play. here is my code it is in java.

public class ComputerPlayer
{
    private char computerSymbol;
    private int xPos;
    private int yPos;    
    
    public ComputerPlayer()
    {
        computerSymbol = ' ';
        xPos=0;
        yPos=2;
    }
    
    public void makeMove()
    {
        xPos = (int)(Math.random() *3);
        yPos = (int)(Math.random() *3);        
    }
    private boolean isValidMove(char board[][],int row,int col)
    {
        if(board[row][col]==' ')
            return true;
        return false;
    }
    
    private int[] nextVertMove(int index,char board[][])
    {
        int[] nextVertInfo = {-1,-1,-1};
        int opposingSide = 0;
        int computer=0;
        
        for(int curr=0; curr < 3 ;curr++)
        {
            if(board[curr][index]!=computerSymbol &&
            board[curr][index]!=' ')
            {                
                opposingSide++;
                if(opposingSide==2)
                {            
                    
                    if(curr==1)
                        nextVertInfo[0] = curr+1;
                    else if(curr==2)             
                    {
                        nextVertInfo[0] = curr-1;
                        if(!isValidMove(board,nextVertInfo[0],index))
                            nextVertInfo[0] = 0;
                    }
                    
                    
                    nextVertInfo[1] = opposingSide;
                }
                
            }
            
        }
        
        
        for(int curr=0; curr < 3 ;curr++)
            {
                if(board[curr][index]==computerSymbol)
                {                           
                    
                    computer++;                    
                    if(computer==2)
                    {
                        
                        if(curr==1)
                        {
                            nextVertInfo[0] = curr+1;                            
                        }
                        else if(curr==2)             
                        {                        
                            nextVertInfo[0] = curr-1;                            
                            if(!isValidMove(board,nextVertInfo[0],index))
                                nextVertInfo[0] = 0;
                            
                        }
                        
                        nextVertInfo[2] = computer;

                    }
                }
                            
            }
        
        return nextVertInfo;
    }
    
    
    private int[] nextHorzMove(int index,char board[][])
    {
        int[] nextHorzInfo = {-1,-1,-1};
        int opposingSide = 0;
        int computer=0;
        
        for(int curr=0; curr < 3 ;curr++)
        {
            
                if(board[index][curr]!=computerSymbol &&
                    board[index][curr]!=' ')
                 {
                    opposingSide++;
                    if(opposingSide==2)
                    {                
                    
                        if(curr==1)
                            nextHorzInfo[0] = curr+1;
                        else if(curr==2)             
                        {                        
                            nextHorzInfo[0] = curr-1;
                
                            if(!isValidMove(board,index,nextHorzInfo[0]))
                                nextHorzInfo[0] = 0;
                            
                        }
                        
                        nextHorzInfo[1] = opposingSide;
                    }
                    
                }

            
        }
                      
        for(int curr=0; curr < 3 ;curr++)
        {
            if(board[index][curr]==computerSymbol)
            {                           
                
                computer++;                    
                if(computer==2)
                {
                        
                    if(curr==1)
                    {
                        nextHorzInfo[0] = curr+1;                            
                    }
                    else if(curr==2)             
                    {                        
                        nextHorzInfo[0] = curr-1;                            
                        if(!isValidMove(board,index,nextHorzInfo[0]))
                            nextHorzInfo[0] = 0;
                          
                    }
                        
                    nextHorzInfo[2] = computer;

                }
            }
                            
        }
                        
        return nextHorzInfo;
    }
    
    
    private int[] nextFirstDiagMove(char board[][])
    {
        int[] nextDiagInfo = {-1,-1,-1};
        int opposingSide = 0;
        int computer=0;
                
        for(int curr=0; curr < 3 ;curr++)
        {
            
            if(board[curr][curr]!=computerSymbol &&
            board[curr][curr]!=' ')
            {
                opposingSide++;
                
                if(opposingSide==2)
                {               
                
                    if(curr==1)
                    nextDiagInfo[0] = curr+1;
                    else if(curr==2)
                    {                                 
                        nextDiagInfo[0] = curr-1;
                        if(!isValidMove(board,nextDiagInfo[0],nextDiagInfo[0]))
                            nextDiagInfo[0] = 0;
                    }
                                        
                    nextDiagInfo[1] = opposingSide;
                }
            }
            
        }
        
        
        for(int curr=0; curr < 3 ;curr++)
        {
            
            if(board[curr][curr]==computerSymbol)
            {
                computer++;
                
                if(computer==2)
                {               
                
                    if(curr==1)
                    nextDiagInfo[0] = curr+1;
                    else if(curr==2)
                    {                                 
                        nextDiagInfo[0] = curr-1;
                        if(!isValidMove(board,nextDiagInfo[0],nextDiagInfo[0]))
                            nextDiagInfo[0] = 0;
                    }
                                        
                    nextDiagInfo[2] = computer;
                }
            }
            
        }
        
        return nextDiagInfo;
    }
    
    private int[] nextSecondDiagMove(char board[][])
    {
        int[] nextDiagInfo = {-1,-1,-1,-1};
        int opposingSide = 0;
        int computer=0;
        
        for(int curr=0; curr < 3 ;curr++)
        {
            
            if(board[curr][(2-curr)]!=computerSymbol &&
                board[curr][(2-curr)]!=' ')
            {
                opposingSide++;
                
                if(opposingSide==2)
                {   
                        
                    
                    if(curr==1)
                    {
                        
                        nextDiagInfo[0] = curr - 1;
                        nextDiagInfo[2] = curr + 1;                        
                        
                        
                    }
                    
                    else if(curr==2)
                    {              
                    
                        nextDiagInfo[0] = curr-1;
                        nextDiagInfo[2] = curr-1;
                        
                        if(!isValidMove(board,curr-1,nextDiagInfo[0]))
                        {
                            nextDiagInfo[2] = 2-curr;
                            nextDiagInfo[0] = curr;
                        }
                    }
                    
                    
                    nextDiagInfo[1] = opposingSide;
                    
                }
            }
            
        }
        
        
        for(int curr=0; curr < 3 ;curr++)
        {
            
            if(board[curr][(2-curr)]==computerSymbol)
            {
                computer++;
                
                if(computer==2)
                {   
                        
                    
                    if(curr==1)
                    {
                        
                        nextDiagInfo[0] = curr - 1;
                        nextDiagInfo[2] = curr + 1;                        
                        
                        
                    }
                    
                    else if(curr==2)
                    {              
                    
                        nextDiagInfo[0] = curr-1;
                        nextDiagInfo[2] = curr-1;
                        
                        if(!isValidMove(board,curr-1,nextDiagInfo[0]))
                        {
                            nextDiagInfo[2] = 2-curr;
                            nextDiagInfo[0] = curr;
                        }
                    }
                    
                    
                    nextDiagInfo[3] = computer;
                    
                }
            }
            
        }
        
        return nextDiagInfo;
    }
    
    public void makeMove(char board[][])
    {
        
        int[] nextMovesRow = new int[2];
        int[] nextMovesCol = new int[2];
        int[] nextMovesFirstDiag = new int[2];
        int[] nextMovesSecondDiag = new int[2];
        
        
        for(int i =0; i < board.length; i++)
        {
            nextMovesRow = nextHorzMove(i,board);

            if(nextMovesRow[2] == 2)
            {                                          
                setRow(i);
                setCol(nextMovesRow[0]);                
                
            } 
            else if(!isValidMove(board,xPos,yPos))
            {
                if(nextMovesRow[1] == 2)
                {                       
                    setRow(i);
                    setCol(nextMovesRow[0]);
                }                                    
            

            }
         }
        
        if(!isValidMove(board,xPos,yPos))
        {
        
            for(int i =0; i < board.length; i++)
            {
                
                nextMovesCol = nextVertMove(i,board);
                
                if(nextMovesCol[2] == 2)
                {   
                                    
                    setRow(nextMovesCol[0]);                
                    setCol(i);                                        
                 
                }
                
                else if(!isValidMove(board,xPos,yPos))
                {
                    
                    if(nextMovesCol[1] == 2)
                    {                       
                        setRow(nextMovesCol[0]);                
                        setCol(i);
                        
                    }                                       
            
    
                }
           
            }
        
        }
        
        if(!isValidMove(board,xPos,yPos))
        {
            
            nextMovesSecondDiag = nextSecondDiagMove(board);
            
            if(nextMovesSecondDiag[1] == 2)
            {                                                                                    
                setRow(nextMovesSecondDiag[2]);
                setCol(nextMovesSecondDiag[0]);
            }
            else if(!isValidMove(board,xPos,yPos))
            {
                
                if(nextMovesSecondDiag[3] == 2)
                {                
                 
                    setRow(nextMovesSecondDiag[2]);
                    setCol(nextMovesSecondDiag[0]);
                    
                }   
            }
            
        }
        
        if(!isValidMove(board,xPos,yPos))
        {
            
            nextMovesFirstDiag = nextFirstDiagMove(board);
            System.out.println(nextMovesFirstDiag[2]);
            if(nextMovesFirstDiag[2] == 2)
            {
                                            
                setRow(nextMovesFirstDiag[0]);                
                setCol(nextMovesFirstDiag[0]);
                
            } 
            else if(!isValidMove(board,xPos,yPos))
            {
                
                if(nextMovesFirstDiag[1] == 2)
                {                
                 
                    setRow(nextMovesFirstDiag[0]);                
                    setCol(nextMovesFirstDiag[0]);
                    
                }   
            }
        }
                   
    }
    
    public void setSymbol(char symbol)
    {
        computerSymbol = symbol;    
    }
    
    private void setRow(int row)
    {
        xPos = row;
    }
    
    private void setCol(int col)
    {
        yPos = col;
    }
    
    public int getRow()
    {
        return xPos;
    }
    
    public int getCol()
    {
         return yPos;   
    }
    
    public char getSymbol()
    {
        return computerSymbol;
    }
    
}

This topic is closed to new replies.

Advertisement