Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Problem with Memory Card Game

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Memory Card Game

    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();
    	}
    }


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Memory Card Game

    I forgot, here is the output that I get...

    [1] [1]
    [2]
    [3]
    [4]
    [5]
    [6] [6]
    [7]
    [8]
    [9]
    [10]
    [11] [11]
    [12]
    [13]
    [14]
    [15]
    [16] [16]
    [17]
    [18]
    [19]
    [20]
    [21] [21]
    [22]
    [23]
    [24]
    [25]
    [26] [26]
    [27]
    [28]
    [29]
    [30]

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Memory Card Game

    What does that output represent?
    Can you add some comments that describe what those numbers mean.

    my output has one card per line and some numbers are doubled.
    Is that what you want or is there a problem with what is printed?
    How do you want the numbers displayed?

    Did you notice the relationship between the numbers that are doubled? Does that relationship say anything about the code that is printing the numbers.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Memory Card Game

    I would like my output to look like this...

    [1][2][3][4][5]
    [6][7][8][9][10]
    [11] ...

    I need 30 cards to be displayed. I've tried reworking some of the code but each time I always get some numbers that are doubled by either +5 or +6 increments. The only thing I can think of is that when I create a new instance of card, it's somehow saving these items twice? Maybe one of my for loops is wrong?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Memory Card Game

    To keep the printed output on one line, use the print() method without any "\n" (new line) characters.
    When you want the next output to go to a new line, use the println() method or add a "\n" at the end of the last thing you want to print on the current line.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Memory Card Game

    Ok that part is fixed, here is the new output.

    [1]
    [1][2][3][4][5][6]
    [6][7][8][9][10][11]
    [11][12][13][14][15][16]
    [16][17][18][19][20][21]
    [21][22][23][24][25][26]
    [26][27][28][29][30]

    My issue is how do I get rid of these duplicate numbers?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Memory Card Game

    how do I get rid of these duplicate numbers?
    Check your logic and see where it is printing the number two times.
    Print out the values of i and j as you go through the loops to make sure they are incrementing properly.
    Play computer and step through the logic
    or trace it in a debugger.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Memory Card Game

    I'm just a beginner and I'm not sure how to use the debugger. I don't know what variable I am supposed to be focusing on. My first step, I displayed just the numbers and they all displayed correctly. Now I'm trying to do it this way and there are duplicates.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Memory Card Game

    Look at your program and try to think how it would print those numbers 2 times. Why does it happen every 5 numbers? Where in the loops is there a 5 used?
    Look at the values of i and j in the loops.

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Memory Card Game

    OK thanks, I just got it to work!! I had too many unnecessary lines in displayBoard and I only needed 2 print statements.

Similar Threads

  1. A Card Game
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2012, 07:30 AM
  2. Card Game Problem.
    By d'fitz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 07:30 AM
  3. Card Game help....
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:55 AM
  4. Card Game Problem....Need Help
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2011, 07:30 AM
  5. help on war card game project
    By sc0field1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 04:51 AM