Download presentation
Presentation is loading. Please wait.
Published byFelix Townsend Modified over 9 years ago
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; for(int i=0;i<12;i++) { 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 http://www.nku.edu/~foxr/Camp08/Programs/BoardGame2.java http://www.nku.edu/~foxr/Camp08/Programs/BoardGame2.java
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]); } We use 4 arrays x, y for the 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 For our game, we can either use questions whose answers will always be something simple like “yes/no”, “true/false” or 1-word answers, as in – String[ ] questions = {“Who was the first president of the US?”, “Can ducks fly?”, “In which season does Labor Day fall?”}; or we can also add our answers to the questions as in String[ ] questions = {“Which US president served the longest term, A. Washington, B. Franklin, C. Roosevelt, D. Kennedy?”, “Which of the following birds cannot fly? A. Robin, B. Goose, C. Duck, D. Ostrich”};
7
Continued The code might look something like this: – Random g = new Random( ); – String[ ] questions = {“question1”, “question2”, …}; – char[ ] answers = {‘D’, ‘A’, …}; – int j; – char userGuess; – while(!done) { j=g.nextInt(questions.length); System.out.println(“Your next question is:\n” + questions[j]); userGuess = JOptionPane.showInputDialog(“What is your answer?”).toUpperCase( ).charAt(0); if(userGuess==answers[j]) {…// user is right} else { done=true; … //user is wrong} }
8
Continued What do we do if they are right? – Increment the level – Add to their winnings – Output that they are right What do we do if they are wrong? – Set winnings to 0 – Set done to true so that we can exit the while loop – Output that they are wrong After the while loop, we need to – Output how much they have won, or if they lost, output some message to indicate that they lost and have no money We can also let the user exit the game by “walking away” – for this, they need to be able to input the walk away choice, for a multiple choice question, ‘E’ for exit, for a String based answer, “exit” or “quit” You will implement parts of this as an activity and the remainder as today’s project
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.