Advertisement
advictoriam

Untitled

Jan 25th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SquareBoard
  4. {
  5.    /**
  6.       Constructs an empty square game board.
  7.       @param sideLength the length of each side
  8.    */
  9.    public SquareBoard(int sideLength)
  10.    {
  11.       length = sideLength;
  12.       board = new String[length][length];
  13.       // Fill with spaces
  14.       for (int i = 0; i < length; i++)
  15.          for (int j = 0; j < length; j++)
  16.             board[i][j] = " ";
  17.    }
  18.  
  19.    /**
  20.       Creates a string representation of the board, such as
  21.       |x o|
  22.       | x |
  23.       | o |.
  24.       @return the string representation
  25.    */
  26.    public String toString()
  27.    {
  28.       String r = "";
  29.       for (int i = 0; i < length; i++)
  30.       {
  31.          r = r + "|";
  32.          for (int j = 0; j < length; j++)
  33.             r = r + board[i][j];
  34.          r = r + "|\n";
  35.       }
  36.       return r;
  37.    }  
  38.  
  39.    /**
  40.       Fills the diagonals of this square board with "*" strings.
  41.    */
  42.    public void fillDiagonals()
  43.    {
  44.       for(int i = 0; i < length; i++)
  45.       {
  46.          for(int j = 0; j < length; j++)
  47.          {
  48.             if(j == i || length - i - 1 == j)
  49.             {
  50.                board[i][j] = "*";
  51.             }
  52.             else
  53.             {
  54.                board[i][j] = " ";
  55.             }
  56.          }
  57.       }
  58.    }
  59.  
  60.    private String[][] board;
  61.    private int length;
  62.  
  63.  
  64.    public static void main(String[] args)
  65.    {
  66.       Scanner in = new Scanner(System.in);
  67.       int n = in.nextInt();
  68.       SquareBoard chessBoard = new SquareBoard(n);
  69.       chessBoard.fillDiagonals();
  70.       System.out.println(chessBoard.toString());
  71.    }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement