/**
 * This class takes a gamestate and evaluates the its value based on a
 * weighted sum of the scores for the pieces, corner pieces, and side pieces
 * according to color.
 * @version 1 of April 8 2000
 * @authors Mark Nordheim, Paul Bailey, Andrew Gorski
 */
public class Scorer {

    /**
     * finds the total score by adding up the scores from the sides, corners
     * and total number of pieces
     */
    public int scoreThis(GameState gs) {
        int score = 0;
        score = score + 10*getCorner(gs);
        score = score + 3*getSide(gs);
        score = score + 1*numPieces(gs);
        return score;
    }
    /**
     * This method finds the total value of the corners. What you say?
     * the value of the corners is the sum of the number of corners the
current
     * player holds minus the sum of the number of corners any other players
     * hold. Thus positive values indicate that the current player holds more
     * corners than the opposing player does.
     * The value of the corners is returned as an int.
     * The preconditions are that that GameState exists and that it has a
     * method, .board.lookupPiece(row,col) that returns what board would
     * return if given the same call.
     * problems? oh, we got problems. What if the board isn't 8X8. Also, none
     * of these Objects are even a twinkel in any programers eye yet. So this
     * really won't compile.
     */

    protected int getCorner(GameState gs) {
        //initalize the score that is to be returned.
        int score = 0;

        //find the number of rows and columns on the board
        // !!!unfortunately there is no method for getting these fields
        // from the board!!!
        //stub like event:
        int rows = gs.board.numRows();
        int columns = gs.board.numColumns();

        //look at all the corners to see what color pieces are there (if at
all)
        //!!! what will a call to Board's lookupPiece method look like?!!!

        //first bring in the corners
        Piece corner1 = gs.board.lookupPiece(1, 1);
        Piece corner2 = gs.board.lookupPiece(1, columns);
        Piece corner3 = gs.board.lookupPiece(rows, 1);
        Piece corner4 = gs.board.lookupPiece(rows, columns);
        //now look at them in order and unless they are null, either add or
        //subtract from their value depending on if their color is that of
        //the present player or the opposite
        if (corner1 != null) {
            if (gs.getColor == corner1.getColor()) {score = score + 1}
            else {score = score -1}
        }
        if (corner2 != null) {
            if (gs.getColor == corner2.getColor()) {score = score + 1}
            else {score = score -1}
        }
        if (corner3 != null) {
            if (gs.getColor == corner3.getColor()) {score = score + 1}
            else {score = score -1}
        }
        if (corner4 != null) {
            if (gs.getColor == corner4.getColor()) {score = score + 1}
            else {score = score -1}
        }
        return score;
    }

    protected int getSide(GameState gs) {
        //initalize the score that is to be returned.
        int score = 0;

        //find the number of rows and columns on the board
        // !!!again unfortunately there is no method for getting these fields
        // from the board!!!
        //stub like event:
        int rows = gs.board.numRows();
        int columns = gs.board.numColumns();

        //however, we don't want to look all the way to the corners so we need
to
        //decrease the value of rows and columns by one.
        rows2 = rows -1;
        columns2 = columns -1;

        //step through the bottom and top side and find their value:
        pieceTmp = gs.board.lookupPiece;
        for (i=2; i <= columns2 ; i = i +1) {
            pieceTmp = gs.board.lookupPiece(1,i);
            if (pieceTmp != null) {
                if (gs.getColor == pieceTmp) {score = score + 1;}
                else {score = score -1;}
            }
            pieceTmp = gs.board.lookupPiece(columns,i);
            if (pieceTmp != null) {
                if (gs.getColor == pieceTmp) {score = score + 1;}
                else {score = score -1;}
            }
        }//for(i=2...)
        //step through the right and left sides
        for (i=2; i <= rows2; i = i +1) {
            pieceTmp = gs.board.lookupPiece(i,1);
            if (pieceTmp != null) {
                if (gs.getColor == pieceTmp) {score = score + 1;}
                else {score = score -1;}
            }
            pieceTmp = gs.board.lookupPiece(i,rows);
            if (pieceTmp != null) {
                if (gs.getColor == pieceTmp) {score = score + 1;}
                else {score = score -1;}
            }
        }
        return score;
    }

    /**
     * Purpose: This method determines a score based on the number of pieces
of
     * each color on the board
     * Parameters: gs is the state of the game for which a score is needed
     * Preconditions: gs contains an board with pieces and the color of the
     * player
     * looking to maximize score
     * Postconditions: nothing changes
     * Produces: A score (base 0, +1 for each of the current player's pieces,
     * -1 for each other piece)
     * Problems: A non-rectangular board might not work correctly
     */
    protected int numPieces (GameState gs)
    {
        int i; // Indices for the positions on the board
        int j;
        int score = 0; // Total score

        // Look at each position on the board
        for (i = 1; i <= gs.board.numRows(); i++)
            {
                for (j = 1; j <= gs.board.numColumns; j++)
                    {
                        // If there is a piece here look at its color
                        Piece temp = gs.board.lookupPiece(i,j);

                        if (temp != null)
                            {
                                // Add one for current player's piece
                                if (temp.getColor() == gs.getColor())
                                    {
                                        score++;
                                    } // if(color)
                                // Subtract one for the other player's piece
                                else
                                    {
                                        score--;
                                    } // else
                            } // if(null)
                    } // for(j)
            } // for(i)

        return score;
    } // numPieces(gamestate)
}

