Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and Logic Let’s generate 6 lotto numbers using a loop and the random number generator but we don’t want to duplicate any numbers how do we know.

Similar presentations


Presentation on theme: "Arrays and Logic Let’s generate 6 lotto numbers using a loop and the random number generator but we don’t want to duplicate any numbers how do we know."— Presentation transcript:

1 Arrays and Logic Let’s generate 6 lotto numbers using a loop and the random number generator but we don’t want to duplicate any numbers how do we know if a number has already been generated? Random generator = new Random( ); int[ ] numbers = new int[6]; for(int j=0;j<6;j++) { // j keeps track of which number we are going to generate do { temp = Math.abs(generator.nextInt( ))%48 + 1; // found = false; k = 0; while(!found && k < j) if(numbers[k]==temp) found = true; else k++; } while(found); numbers[j] = temp; }

2 Multiple Images Here, we create an array of ImageIcons and place them all on the screen as thumbnails ImageIcon[] imageIcons = new ImageIcon[12]; Image[] images = new Image[12]; String filename; // assume all images are in images/pic1.jpg, pic2.jpg etc for(int i=0;i<12;i++) { filename = "images/pic" + i + ".jpg"; imageIcons[i] = new ImageIcon(filename); images[i] = imageIcons[i].getImage(); } int xPosition = 0, yPosition = 0; xPosition = (i%4) * 50; yPosition = (i/4) * 50; g.drawImage(images[i], xPosition, yPosition, 45, 45, Color.black, this);

3 Representing Variable Players
Now lets consider if we want to change our simple game to allow for between 2 and 5 players We can’t implement p1, p2, p3, p4, p5 because, while that seems simple, it would be awkward to determine whose turn it is next in the case of having 3 players, or 2 players, or 5 players So instead, we will use an array of players, p[i] storing the location of player i We will store the number of players in a variable, n, and use a for loop to control whose turn it is So, here we have a revised version of the BoardGame program at

4 Storing Graphics Data in an Array
Random generator = new Random(); int i, red, green, blue; int[] x, y, size; Color[] color; x = new int[25]; y = new int[25]; size = new int[25]; color = new Color[25]; for(i=0;i<25;i++) { x[i]=generator.nextInt(480) + 10; y[i]=generator.nextInt(480) + 10; size[i]=generator.nextInt(10) + 3; red=generator.nextInt(256); green=generator.nextInt(256); blue=generator.nextInt(256); color[i]=new Color(red, green, blue); } g.setColor(Color.black); g.fillRect(0,0,500,500); for(i=0;i<25;i++) { g.setColor(color[i]); g.fillOval(x[i], y[i], size[i], size[i]); Storing Graphics Data in an Array We use 4 arrays x, y for the <x, y> coordinate size for the size of the oval color to store the Color of the oval

5 Arrays for Random Storage
Another way to use an array is to create an array with dozens, hundreds or thousands of items and then randomly select from them As an example, imagine a hangman game You want to randomly select a word to use String[ ] words = {“word1”, “word2”, “word3”, “word4”, “word5”, “word6”, “word7”, “word8”}; Now you want to withdraw a word from words Use a random number generator to get a legal index from 0 to the length of the array String word = words[generator.nextInt(words.length)]; Here, word is one of the 8 words in the array

6 Millionaire Game The game of Who Wants to be a Millionaire uses hundreds of questions from which a few are randomly chosen String[ ] questions = {“question 1”, “question 2”, …}; We can randomly generate a question to ask the user, but in the game, the user is also given 4 possible answers, so we also need an array of answers that we will call selections In this case, each answer is really 4 different Strings String[ ][ ] selections = {{“answer 1 to question 1”, “answer 2 to question 1”, “answer 3 to question 1”, “answer 4 to question 1”}, {“answer 1 to question 2”, “answer 2 to question 2”, “answer 3 to question 2”, “answer 4 to question 2”}, …}; So we randomly select a question using the random number generator, that number also gives us the 4 possible selections

7 Continued The code might look something like this:
Random g = new Random( ); String[ ] questions = {…}; String[ ][ ] selections = {{…}, {…}, …, {…}}; int j; char userGuess; while(!done) { j=g.nextInt(questions.length); System.out.println(“Your next question is:\n” + questions[j]); System.out.println(“Your answers are:\n”); System.out.println(“A: ” + selections[j][0]); System.out.println(“B: ” + selections[j][1]); System.out.println(“C: ” + selections[j][2]); System.out.println(“D: ” + selections[j][3]); userGuess = JOptionPane.showInputDialog(“What is your answer?”).charAt(0);

8 Continued Assume they answer a C, now what?
We also need an array of answers This array will contain a letter, (A, B, C or D) for each question So, for every question, you have to put the question in one array You have to put 4 possible selections in a second array You have to put the answer (A, B, C or D) in a third array If you have 100 questions, you will need 400 selections and 100 answers! By generating one random number (j), you index into all 3 arrays


Download ppt "Arrays and Logic Let’s generate 6 lotto numbers using a loop and the random number generator but we don’t want to duplicate any numbers how do we know."

Similar presentations


Ads by Google