/**
 *A stub class for Player
 *
 * @author Matthew Charnetski
 * @author Anne Feltovich
 * @author Elizabeth Ottesen
 * @author Austin Wells
 * @version 1.0 of March 2000
 */

public class CompPlayer
    implements Player
{
    //Fields
    
    // stores number of rows on board.
    protected int numberofrows;
    // stores number of columns on board.
    protected int numberofcolumns;
    // double array of all positions on board in the form int[row][col].  Zero 
    // is in that location if move is illegal, 1 if move is legal.
    protected int[][] legalmoves;
    // stores color.
    protected String color;
    
    // makes a move.
    public void makeMove(Board board, Rules rules, String color) {
        this.numberofrows = board.getRowNumber();
        // assumes board has a method getROwNumber that tells us
        // how many rows are in the board.
	this.numberofcolumns = board.getColumnNumber(); 
        // assumes board has getColumnNumber
	this.legalmoves = rules.allMoves();
        this.color = color;
        // creates new double array for storing the number pieces captured from
        // a move at position.
     	int[][] captured = new int[rownumber-1][columnnumber-1];
        fillCapturedArray(captured);
        // chooses best move according to the first of the moves that capture
        // the most pieces.
        int bestrow = 0;
        int bestcol = 0;
        int maxsofar = 0;
        for (int i = 0; i < numberofrows; i++) {
            for (int j = 0; j < numberofcolumns; j++) {
                if (captured[i][j] > maxsofar) {
                    bestrow = i;
                    bestcol = j;
                    maxsofar = captured[i][j];
                } 
            }
        }
        // make the move specified above.
        Piece piece = new Piece(bestrow, bestcol, color);
        board.makeMove(piece);
    }
    
    
    // this fills in the captured array define above.
    public void fillCapturedArray(int[][] captured){  
        for (int row = 0; row<rownumber; row++) {
            for (int col = 0; col<columnnumber; col++) {
                if (legalmoves[row][col] == 0) {
                    captured[row][col] = 0
                        } // if that square is not a legal move
                else {
                    captured[row][col] = numCapturedAtPosition(row, col);
                }//else
            }//for
        }//for
    }//fillCapturedArray


    /**
     *This method takes a row and column number and sees how many
     *pieces would be captured if the computer moved there.  
     */
    public int numCapturedAtPosition(int row, int col) {
        
        int rowinc;
        int colinc;
        int numcaptured = 0;
        // looks in each of the 8 directions and adds up the total number of 
        // captured pieces.
        for(rowinc = -1, rowinc <= 1, rowinc++){
            for(colinc = -1, colinc <= 1, colinc++){
                if((rowinc == 0) && (colinc == 0)) {
                }//if
                else{
                    numcaptured = (numcaptured +
                                   numCapturedinDirection(row, col,
                                                          row, col,
                                                          rowinc, colinc));
                }//else
            }//for
        }//for
} //numCaptured

    //this method returns the number of pieces captured in a direction
    public int numCapturedinDirection(occupiedrow, occupiedcol, 
                                      currentrow, currentcol, 
                                      rowincrement, columincrement) {

        //if the cursor has moved off the end of the board without closing
        //off the capture bracket, no captures are made
        if(currentrow<0 || currentrow>(numberofrows - 1)
           || currentcolumn<0 || currentcolumn>(numberofcolumns - 1)) {
            return 0:
        }//if

        //if the cursor finds an unoccupied space before closing off the 
        //capture bracket, no captures are made
        else if(board.isOccupied(currentrow, currentcol)== false) {
            return 0;
        }//if
        
        //asks board for the piece occupying the current space
        Piece currentPiece = board.lookUpPiece(currentrow, currentcol);
        
        //if the piece is the player's color, then it finds how many pieces are
        //enclosed in the capture bracket
        if(currentPiece.getColor == this.color) {
            if (rowincrement == 0) {
                return (currentcol-occupiedcol-1);
            }//if
            else {
                return (currentrow-occupiedrow-1);
            }//else
        }//if

        //otherwise recursively call the method, moving the cursor in the 
        //specified direction
        else{
            numCapturedinDirection(occupiedrow, occupiedcol,
                                   currentrow + rowincrement, 
                                   currentcol + rowincrement, 
                                   rowincrement, columnincrement);
        }//else
    }//numCapturedinDirection

}//class compPlayer
        

