4 Replies - 534 Views - Last Post: 26 May 2012 - 10:23 AM Rate Topic: -----

#1 amy_rules  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-May 12

Generating random pairs of numbers in a 2d array (1-8)

Posted 25 May 2012 - 06:26 AM

Hi guys. I'm trying to create a so called matching pairs game for my assignment. And so far I've managed to to create a 4X4 array and filled it with pairs of 1-8. What I need help with is how do it randomize the positioning of the pairs in the array.

Any help would be greatful.

import java.util.Scanner;


public class Memory 
{    
    
    int[][] initialValue = {{1, 2, 3, 4}, {5, 6 ,7 , 8},{1, 2, 3, 4}, {5, 6 ,7 , 8}};
    
    boolean[][] displayed = new boolean[4][4];
    Scanner scan;

    
    
    Memory() 
    {
        boolean gameIsOver = false;
        scan = new Scanner(System.in);
        while(!gameIsOver) 
        {
            System.out.println("X represents the row which is downwards. Y represents the columns which is across. Example the" +
                                " coordinates for the top left is X = 0, Y = 0");
            System.out.println();
            
            
            
            displayCard();
            
            System.out.print("Enter coordinates X to show: ");
            int x = scan.nextInt();
            
            
            System.out.print("Enter coordinates Y to show: ");
            int y = scan.nextInt();
            displayed[x][y] = true;
            System.out.println();
            
        }
            
    }
    
    void displayCard() 
    {
        Random randomCards = new Random();
        System.out.println("The Cards:");
        
   
        for(int j = 0; j < 4; j++) 
        {
            for(int i = 0; i < 4; i++) 
            {
                if(displayed[i][j])

                    System.out.print(" " + initialValue[i][j] + " ");
                else
                    System.out.print(" * ");
            }
            System.out.println();
        }
    }
    
    public static void main(String[]args)
    {
        new Memory();
    }
    
}




Is This A Good Question/Topic? 0
  • +

Replies To: Generating random pairs of numbers in a 2d array (1-8)

#2 amy_rules  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-May 12

Re: Generating random pairs of numbers in a 2d array (1-8)

Posted 25 May 2012 - 06:43 AM

The output would be something like:

* * * *
* * * *
* * * *
* * * *
Was This Post Helpful? 0
  • +
  • -

#3 amy_rules  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-May 12

Re: Generating random pairs of numbers in a 2d array (1-8)

Posted 25 May 2012 - 06:49 AM

The user would then be asked to enter the coordinate for "X" which is the row and "Y" which is the columns.

Once the user has entered the coordinates, the "cards" would be so called face up. What i'm trying to figure out is how do I randomize the positioning of the number pairs. So far the output once the "cards" have been revealed it would be like this:

1 5 1 5
2 6 2 6
3 7 3 7
4 8 4 8
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2148
  • View blog
  • Posts: 8,925
  • Joined: 20-September 08

Re: Generating random pairs of numbers in a 2d array (1-8)

Posted 25 May 2012 - 08:16 AM

http://technojeeves....e-array-in-java

You can also use List<Integer> instead and call Collections.shuffle
Was This Post Helpful? 0
  • +
  • -

#5 gattigen  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 4
  • Joined: 29-March 12

Re: Generating random pairs of numbers in a 2d array (1-8)

Posted 26 May 2012 - 10:23 AM

Dont you need to import the random class (import java.util.Random)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1