Пример #1
0
    private int calculateRating()
    {
        int rating          = 0;
        int playerBefore    = GameManager.NONE;      //the last player is saved here. Will never be GameManager.NONE excpet at the beginning
        int sameStonesCount = 0;
        int currentPlayer;

        //count the same stones in a row
        for (int row = 0; row < board.boardRows; row++)
        {
            for (int column = 0; column < board.boardColumns; column++)
            {
                currentPlayer = board.getPlayerAt(row, column);

                if (currentPlayer == GameManager.NONE)
                {
                    //do nothing. Rows wiht a gap between the stones are counted like rows without a gap
                }
                else if (playerBefore == GameManager.NONE)
                {
                    //first stone in the row
                    playerBefore    = currentPlayer;
                    sameStonesCount = 1;
                }
                else if (playerBefore == currentPlayer)
                {
                    //same stone as before
                    sameStonesCount++;
                }
                else
                {
                    //stone changed -> rate
                    rating         += rate(sameStonesCount, playerBefore);
                    playerBefore    = currentPlayer;
                    sameStonesCount = 1;
                }

                //if its the last column count the stones to the rating
                if (column == board.boardColumns - 1)
                {
                    rating += rate(sameStonesCount, currentPlayer);
                }
            }
        }

        playerBefore    = GameManager.NONE;      //the last player is saved here. Will never be GameManager.NONE excpet at the beginning
        sameStonesCount = 0;

        //count the same stones in a column
        for (int column = 0; column < board.boardColumns; column++)
        {
            for (int row = 0; row < board.boardRows; row++)
            {
                currentPlayer = board.getPlayerAt(row, column);

                if (currentPlayer == GameManager.NONE)
                {
                    //do nothing. Columns with a gap between the stones are counted like columns without a gap
                }
                else if (playerBefore == GameManager.NONE)
                {
                    //first stone in the column
                    playerBefore    = currentPlayer;
                    sameStonesCount = 1;
                }
                else if (playerBefore == currentPlayer)
                {
                    //same stone as before
                    sameStonesCount++;
                }
                else
                {
                    //stone changed -> rate
                    rating         += rate(sameStonesCount, playerBefore);
                    playerBefore    = currentPlayer;
                    sameStonesCount = 1;
                }

                //if its the last row count the stones to the rating
                if (row == board.boardRows - 1)
                {
                    rating += rate(sameStonesCount, currentPlayer);
                }
            }
        }

        //count the same stones diagonal

        /*
         * for (int row = 0; row < board.boardRows; row++) {
         *      for (int column = 0; column < board.boardColumns; column++) {
         *              currentPlayer = board.getPlayerAt (row, column);
         *
         *              if (currentPlayer == GameManager.NONE) {
         *                      //do nothing. Rows wiht a gap between the stones are counted like rows without a gap
         *              } else if (playerBefore == GameManager.NONE) {
         *                      //first stone in the row
         *                      playerBefore = currentPlayer;
         *                      sameStonesCount = 1;
         *              } else if (playerBefore == currentPlayer) {
         *                      //same stone as before
         *                      sameStonesCount++;
         *              } else {
         *                      //stone changed -> rate
         *                      rating += rate(sameStonesCount, playerBefore);
         *                      playerBefore = currentPlayer;
         *                      sameStonesCount = 1;
         *              }
         *
         *              //if its the last column count the stones to the rating
         *              if (column == board.boardColumns - 1) {
         *                      rating += rate (sameStonesCount, currentPlayer);
         *              }
         *      }
         * }*/


        return(rating);
    }