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
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;
}
}
}
}
}
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.