I am creating a memory card game. I want to display my board so that it looks like a 6 x 5 grid. Right now, my output has one card per line and some numbers are doubled. I figured that this has something to do with the way I am storing the card. Can somebody please help? Thanks!
Layout Class
import java.util.*; public class Layout{ private int matches; private int attempts; Card[][] board = new Card[6][5]; int Count = 1; public void addCard(int[] count, int row, int col){ String [] alpha = new String[15]; alpha[0] = "AA"; alpha[1] = "BB"; alpha[2] = "CC"; alpha[3] = "DD"; alpha[4] = "EE"; alpha[5] = "FF"; alpha[6] = "GG"; alpha[7] = "HH"; alpha[8] = "II"; alpha[9] = "JJ"; alpha[10] = "KK"; alpha[11] = "LL"; alpha[12] = "MM"; alpha[13] = "NN"; alpha[14] = "OO"; String tempSymbol = ""; int tempRand = 0; int tempCount = 0; Random r = new Random(); tempRand = r.nextInt(15); tempSymbol = alpha[tempRand]; tempCount = count[tempRand]; Card card = new Card(tempSymbol); card.setNum(Count); board[row][col] = card; } public void genBoard(int[] count){ for(int row=0; row<6; row++){ for(int column=0; column<5; column++){ addCard(count, row, column); Count++; } } } public void displayBoard(){ Card tempCard = new Card(); for(int i=0; i<6; i++){ tempCard = board[i][0]; System.out.print("[" + tempCard.getNum() + "] "); for(int j=0; j<5; j++){ tempCard = board[i][j]; System.out.println("[" + tempCard.getNum() + "]"); } } } public int getMatches(){ return matches; } public void setMatches(int matches){ this.matches = matches; } public int getAttempts(){ return attempts; } public void setAttempts(int attempts){ this.attempts = attempts; } }
Card Class
public class Card{ private String symbol = ""; private int num; private boolean face = false; private int row; private int column; public Card(String symbol){ this.symbol = symbol; } public Card(){ } public String getSymbol(){ return symbol; } public void setSymbol(String text){ this.symbol = text; } public int getNum(){ return num; } public void setNum(int number){ this.num = number; } public boolean getFace(){ return face; } public void setFace(boolean f){ this.face = f; } public int getRow(){ return row; } public void setRow(int row){ this.row = row; } public int getColumn(){ return column; } public void setColumn(int column){ this.column = column; } }
Main Class
public class Memory{ /** * @param args */ public static void main(String[] args){ int [] count = new int[15]; for(int i=0; i<15; i++){ count[i] = 0; } Layout test = new Layout(); test.genBoard(count); test.displayBoard(); } }