Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SquareBoard
- {
- private String[][] board;
- private int length;
- /**
- Constructs an empty square game board.
- @param sideLength the length of each side
- */
- public SquareBoard(int sideLength)
- {
- length = sideLength;
- board = new String[length][length];
- // Fill with spaces
- for (int i = 0; i < length; i++)
- for (int j = 0; j < length; j++)
- board[i][j] = " ";
- }
- /**
- Creates a string representation of the board, such as
- |x o|
- | x |
- | o |.
- @return the string representation
- */
- public String toString()
- {
- String r = "";
- for (int i = 0; i < length; i++)
- {
- r = r + "|";
- for (int j = 0; j < length; j++)
- r = r + board[i][j];
- r = r + "|\n";
- }
- return r;
- }
- /**
- Fills the borders of this board with "*" strings.
- */
- public void fillBorders()
- {
- for(int i = 0; i < length; i++)
- {
- board[0][i] = "*";
- board[length-1][i] = "*";
- }
- for(int i = 0; i < length; i++)
- {
- board[i][0] = "*";
- board[i][length-1] = "*";
- }
- }
- // This method is used to check your work.
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- SquareBoard board = new SquareBoard(n);
- board.fillBorders();
- System.out.println(board.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement