0

I am not able to correctly fill a 5X5 array and successfully print the array. Both are methods. a 4X4 works fine.

I have tried re writing the code in different ways but no success

// assume I have defined everything else
private int[][] board = new int[4][4];

//variables and other stuff
private String title = "Layout";
private String[] images = {"_", "D", "H", "C", "S"};

private final int BLANK = 0;
private final int DIAMONDS = 1;
private final int HEARTS = 2;
private final int CLUBS = 3;
private final int SPADES = 4;
//if it helps i found a error with calling the method. it prints correctly but only partially. 

This method works

  public void createBoardLayout1() 
  {
    for (int i = 0; i < board.length; i++)
    {
      for (int j = 0; j < board[i].length; j++)
      {
        if (j % 2 == 0)
        {
          board[i][j] = DIAMONDS;          
        }
        else
        {
          board[i][j] = BLANK;
        }
      }
    }
    System.out.println();
  }

This is the method that is giving me issue

  public void createBoardLayout4() 
  {
    for (int i = 0; i < board.length; i++)
    {
      for (int j = 0; j < board[i].length; j++)
      {
        if (i == 0)
        {
          board[i][j] = CLUBS;
        }
        else if (i == 4)
        {
          board[i][j] = CLUBS;
        }
        if (j == 0)
        {
          board[i][j] = CLUBS;
        }
        else if (j == 4)
        {
          board[i][j] = CLUBS;
        }
        else if (i == 1)
        {
          board[i][j] = DIAMONDS;
        }
        else if (i == 3)
        {
          board[i][j] = DIAMONDS;
        }
        else if (j == 1)
        {
          board[i][j] = DIAMONDS;
        }
        else if (j == 3)
        {
          board[i][j] = DIAMONDS;
        }
        else if (i == 2)
        {
          board[i][j] = SPADES;
        }
        else if (j == 2)
        {
          board[i][j] = SPADES;
        }
        else
        {
          board[i][j] = BLANK;
        }
      }
    }
    System.out.println();
  }

Method that prints array but not what I expect for createBoardLayout4()

  public void printBoard()
  {
    for (int i = 0; i < board.length; i++)
    {
      System.out.println();
      for (int j = 0; j < board[i].length; j++)
      {
        if (board[i][j] == 0)
        {
          board[i][j] = BLANK;
          System.out.print(images[BLANK] + " ");
        }
        else if (board[i][j] == 1)
        {
          board[i][j] = DIAMONDS;
          System.out.print(images[DIAMONDS] + " ");
        }
        else if (board[i][j] == 2)
        {
          board[i][j] = HEARTS;
          System.out.print(images[HEARTS] + " ");
        }
        else if (board[i][j] == 3)
        {
          board[i][j] = CLUBS;
          System.out.print(images[CLUBS] + " ");
        }
        else if (board[i][j] == 4)
        {
          board[i][j] = SPADES;
          System.out.print(images[SPADES] + " ");
        }
      }
    }
    System.out.println();
    System.out.println();
  }

createBoardLayout1 output is:

D _ D _ 
D _ D _ 
D _ D _
D _ D _

createBoardLayout4 output is:

C C C C C
C D _ _ C
C _ S _ C
C _ _ D C
C C C C C

createBoardLayout4 output is supposed to be:

C C C C C
C D D D C
C D S D C
C D D D C
C C C C C
  • for layout for you need multiple cases for example diamond : if i==1 && i != 0 &&i != 4 (for the second row) oO or maybe you can calculate it by the difference from the center – jonathan Heindl Apr 08 '19 at 16:49
  • you can start from the outside and do for 2...1...0 if Math.abs(2-j)< index&&Math.abs(2-i)< index set C ,D,S – jonathan Heindl Apr 08 '19 at 16:52

2 Answers2

0

I like this approach : you go from outside to inside and set everything to the type thats smaller than the current square around the center

for(int s=2;s>=0;s--)
{
   for (int i = 0; i < board.length; i++)
   {
      for (int j = 0; j < board[i].length; j++)
      { 
           if(Math.abs(2-j)<s&&Math.abs(2-i)<s){
               if(s==2){
                      board[i][j] = CLUBS;
               }else if(s==1){
                      board[i][j] = DIAMONDS;
               }else if(s==0){
                      board[i][j] = SPADES;
               }
           }
      }
   }
}
jonathan Heindl
  • 844
  • 7
  • 16
  • Thanks guy but I figured it out. here is the new code:public void for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { if (i == 0 || j == 0) { board[i][j] = CLUBS; } if (i == 4 || j == 4) { board[i][j] = CLUBS; } else if (i == 2 && j == 2) // work { board[i][j] = SPADES; } else if ((i > 0 && i < 4) && (j > 0 && j < 4)) { board[i][j] = DIAMONDS; } – Thomas McDonnell Apr 08 '19 at 17:00
  • oh it didmnt display at first ^^ – jonathan Heindl Apr 08 '19 at 17:18
0

Thanks guy but I figured it out. here is the new code:

 for (int i = 0; i < board.length; i++)
    {
      for (int j = 0; j < board[i].length; j++)
      {
        if (i == 0 || j == 0)
        {
          board[i][j] = CLUBS;
        }
        if (i == 4 || j == 4)
        {
          board[i][j] = CLUBS;
        }
        else if (i == 2 && j == 2) // work
        {
          board[i][j] = SPADES;
        }
        else if ((i > 0 && i < 4) && (j > 0 && j < 4))
        {
          board[i][j] = DIAMONDS;
        }