package cpsc2150.extendedConnectX.models;

/**
 * Class that implements the IGameBoard interface and contains an overridden
 * implementation of {@link Object#toString()}.
 *
 * @author Owen Sullivan
 * @version 1.0
 */
public abstract class AbsGameBoard implements IGameBoard {

    /**
     * Overloaded toString method for GameBoard.
     * <p><b>NOTE:</b> The string will change depending on the values of the object
     * that implements IGameBoard which calls the toString method, and thus the post-condition
     * for this method is partially informal.</p>
     *
     * @return A string that shows the entire game board
     *
     * @post
     *      [toString shows the entire game board] AND self = #self
     */
    @Override
    public String toString() {

        // Create a string to be added on to, initialize with border character
        String boardView = "|";

        // Loop through number of columns, adding column numbers and border characters to the string
        for (int i = 0; i < getNumColumns(); i++) {

            // If the current value of i is single-digit, add extra space
            if (Integer.toString(i).trim().length() == 1) {

                boardView += (" " + i + "|");

                // Otherwise, add no extra space
            } else {

                boardView += (i + "|");

            }

        }

        // Move to the next line and add a border character to the string
        boardView += "\n|";
        // Loop through rows (starting from top)
        for (int i = getNumRows() - 1; i >= 0; i--) {

            // Loop through columns (starting from left)
            for (int j = 0; j < getNumColumns(); j++) {

                // Create a BoardPosition object with the current row and column
                BoardPosition pos = new BoardPosition(i, j);

                // Add the token at the current position and a space and a border character to the string
                boardView += (whatsAtPos(pos) + " |");

            }

            // If we're not on the last row
            if (i - 1 >= 0) {

                // Move to the next line and add a border character to the string
                boardView += "\n|";

            }

        }

        // Add an extra new line at the end
        boardView += "\n";

        return boardView;

    }

}
