Results 1 to 7 of 7
  1. #1
    highschool is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default 4x4 2D array card game,need solving

    can any1 help me solve my assignment?please
    i only studied java for a month and the lecture throw this kinda question to us
    i really got no idea how to do

    A common memory matching game played by young children is to start with a deck of cards that contain identical pairs. For example, given a six cards in the deck, two might be labeled 1, two labeled 2, and two labeled 3. The cards are shuffled and placed face down on the table. A player then selects two cards that are face down, turns the face up, and if the cards match they are left face up. If the two cards do not match, they are returned to their original face down position. The game continues until all the cards are face up.

    Write a program that plays the memory matching game. Use sixteen card that are laid out in a 4x4 square that are labeled with pairs of numbers from 1 to 8. Your program should allow the player to specify the cards that she would like to select through a coordinate system.

    1 2 3 4

    1 8 * * *
    2 * * * *
    3 * 8 * *
    4 * * * *

    All of the cards that are face down are indicated by a *. The pairs of 8 which are face up are at coordinates (1,1) and (2,3). To hide the cards that have been temporarily places face up, output a large number of newlines to force the old board off the screen.

    Hint: Use a 2D array for the arrangement of cards and another 2D array that indicates if a card is face up or down. Or, a more elegant solution is to create a single 2D array where each element is an object that stores both the card’s value and face. Write a function that “shuffles” the cards in the array.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It looks like a large daunting project, and this can be intimidating. The way to solve it is to break this large beast down into smaller problems and then solve each smaller problem on their own, then combine the solutions. So my main recommendation to you is to break the problem down, then start solving. Much luck!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Screw that two dimensional array, you can use a simple one dimensional List for that purpose. The Collections class has a utility method that can scramble the content of a List so for starters, all you have to do is enter the numbers 1, 1, 2, 2, 3, 3 ... 8, 8 in that list and have it scrambled by that method.

    For the two dimensional view: if i and j are the row and column coordinates respectively in the range [1 ... 4] Mapping those two values i and j to a one dimensional index value in the range [0 ... 15] the following formula will do: 4*i+j-5. Try to understand it and you're halfway there in the setup of that simple game.

    kind regards,

    Jos

  4. #4
    highschool is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    what's scramble?? i'm seriously very bad with it n i just got bout 2 hours of lecture per week.
    i was hoping any1 out there would actually help me do it
    i wont be posting it here if i aint desperate
    i need pass up on friday but since yesterday i've been busy preparing for my mid term non stop at least until saturday,at least one papaer per day.
    so if anyone of u out there that could help me do,your help is deeply appreciated

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by highschool View Post
    what's scramble?? i'm seriously very bad with it n i just got bout 2 hours of lecture per week.
    i was hoping any1 out there would actually help me do it
    i wont be posting it here if i aint desperate
    i need pass up on friday but since yesterday i've been busy preparing for my mid term non stop at least until saturday,at least one papaer per day.
    so if anyone of u out there that could help me do,your help is deeply appreciated
    rentacoder.

    kind regards,

    Jos

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by highschool View Post
    what's scramble?? i'm seriously very bad with it n i just got bout 2 hours of lecture per week.
    i was hoping any1 out there would actually help me do it
    i wont be posting it here if i aint desperate
    i need pass up on friday but since yesterday i've been busy preparing for my mid term non stop at least until saturday,at least one papaer per day.
    so if anyone of u out there that could help me do,your help is deeply appreciated
    We will be glad to help you with problems in your code, but no one of course will do it for you. If you are still having problems, please feel free to post your code with as much detail about the current problem, and let's see what we can do. Much luck with your project and your time management skills.

  7. #7
    highschool is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    this is wat we came up wif but the problem is we cant make it go back to entering code if the input is not in between 1 to 4
    do i use if else? or do while? n should i add it in?
    Java Code:
    import java.util.*;
    import java.lang.*;
    public class cardg2{
    	public static void main(String[] args){
    		String pl="";
    		Scanner kb=new Scanner(System.in);
    		System.out.println("Enter the card coordinate for each card when requested");
    		System.out.println("e.g '12' for first and '21' for second. range must be between 1-4 only");
    		System.out.println();
    		do
    		{
    			brd();
    			System.out.println("you've won");
    			System.out.println("Play again? Yes/No(Y/N)");
    			pl=kb.next();
    		}while (pl.equals("y")||pl.equals("Y"));
    	System.out.println("Thank You for playing");
    	}
    
    
    //print the board
    	public static void brd(){
    		int cards[][]=new int[4][4];
    		boolean cs[][]=new boolean[4][4];
    
    		cards=shuf();
    		System.out.println("   | 1 2 3 4 ");
    		System.out.println("---+---------");
    		for(int i=0;i<4;++i)
    		{
    			System.out.print(" " +(i+1) +" | ");
    			for(int j=0;j<4;++j)
    			{
    				System.out.print("* ");
    				cs[i][j]=false;
    			}
    			System.out.println();
    		}
    		System.out.println();
    		str(cs,cards);
    }
    
    
    //game starts here
    	public static void str(boolean[][] cs,int[][] cards){
    		boolean ov=false;
    		Scanner kb=new Scanner(System.in);
    
    		char rw0,rw1,cl0,cl1;
    		int r1,c1;
    		int r2=0,c2=0,tmr=5000;
    		//Timer timer=new Timer(tmr);
    		do
    		{
    			do
    			{
    				System.out.println("Please insert the 1st card:");
    				String rw=new String(kb.next());
    				rw0=rw.charAt(0);cl0=rw.charAt(1);
    				r1=Character.digit(rw0,5);c1=Character.digit(cl0,5);
    				if(cs[r1-1][c1-1] == true)
    				{
    					System.out.println("This card is already flipped! Select another card.");
    				}
    			}while(cs[r1-1][c1-1]!= false);
    			do
    			{
    				System.out.println("Please insert the 2nd card:");
    				String rw11=new String(kb.next());
    				rw1=rw11.charAt(0);cl1=rw11.charAt(1);
    				r2=Character.digit(rw1,5);c2=Character.digit(cl1,5);
    				if(cs[r2-1][c2-1] == true)
    				{
    					System.out.println("This card is already flipped! Select another card.");
    				}
    				if((r1==r2)&&(c1==c2)){
    					System.out.println("This card is already chosen! Select another card.");
    				}
    			}while((cs[r2-1][c2-1]!= false)||((r1==r2)&&(c1==c2)));
    			r1--;
    			c1--;
    			r2--;
    			c2--;
    			System.out.println();
    			System.out.println("   | 1 2 3 4 ");
    			System.out.println("---+---------");
    			for (int r=0; r<4; r++)
    			{
    				System.out.print(" " +(r+1) +" | ");
    				for (int c=0; c<4; c++)
    				{
    					if ((r==r1)&&(c==c1))
    					{
    						System.out.print(cards[r][c] +" ");
    					}
    					else if((r==r2)&&(c==c2))
    					{
    						System.out.print(cards[r][c] +" ");
    					}
    					else if (cs[r][c] == true)
    					{
    						System.out.print(cards[r][c] +" ");
    					}
    					else
    					{
    						System.out.print("* ");
    					}
    				}
    				System.out.println();
    			}
    			System.out.println();
    			if (cards[r1][c1]==cards[r2][c2]) // to keep the card flipped
    			{
    				System.out.println("Cards Matched!");
    
    				cs[r1][c1] = true;
    				cs[r2][c2] = true;
    			}
    			//should have timer delay here
    			//System.out.println("Insert any key to continue.");
    			//String asd=kb.next();
    			try
    			{
    				Thread.sleep(2000);
    
    			}catch (InterruptedException ie)
    			{
    				System.out.println(ie.getMessage());
                }
    			for (int b=0; b<=20; b++){
    				System.out.println();
    			}
    			System.out.println("   | 1 2 3 4 ");
    			System.out.println("---+---------");
    
    			for (int r=0; r<4; r++) // reprint the board,for user to make new guess
    			{
    				System.out.print(" " +(r+1) +" | ");
    				for (int c=0; c<4; c++)
    				{
    					if (cs[r][c] == true)
    					{
    						System.out.print(cards[r][c] +" ");
    					}
    					else
    					{
    						System.out.print("* ");
    					}
    				}
    				System.out.println();
    			}
    			System.out.println();
    			ov = true;
    			for (int r=0; r<4; r++) // check all card status, they all should be true/flipped to end the game.
    			{
    				for (int c=0; c<4; c++)
    				{
    					if(cs[r][c]==false)
    					{
    					  ov = false;
    					  c=5;
    					}
    				}
    				if(ov == false)
    				{
    					r=5;
    				}
    			}
    		} while(ov != true); // loop, the only way to get out is to finish the game!
    	}
    
    
    
    //shuffle the cards
    	public static int[][] shuf(){
    		int start[]={1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};
    		int cards[][]=new int[4][4];
    		Random ran=new Random();
    		int tmp,i;
    		for (int s=0; s<=20; s++)
    		{
    			for (int x=0; x<16; x++) //randomize the card placements
    			{
    				i=ran.nextInt(100000)%15;
    				tmp=start[x];
    				start[x]=start[i];
    				start[i]=tmp;
    			}
    		}
    		i=0;
    
    		for (int r=0; r<4; r++) // put values in cards here
    		{
    			for (int c=0; c<4; c++)
    			{
    				cards[r][c]=start[i];
    				i=i+1;
    			}
    		}
    		return cards;
    	}
    }

Similar Threads

  1. Replies: 44
    Last Post: 10-19-2009, 08:07 PM
  2. Java-programmed card game pairs
    By Galore in forum Java Applets
    Replies: 0
    Last Post: 08-30-2009, 07:34 AM
  3. Creating a Card Game in Java
    By Natrix in forum New To Java
    Replies: 1
    Last Post: 05-05-2009, 05:55 PM
  4. card game Rummy
    By javafox in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 03:53 PM
  5. A Online Card Game
    By GonzaloP in forum Networking
    Replies: 0
    Last Post: 12-28-2008, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •