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.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Word List A.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
Introduction to Computing Science and Programming I
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Complexity (Running Time)
Fractions and Decimals
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Shorter of two objects and changing color Functions, events and setting the color Susan Rodger, Duke University June 2008.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
SPELLING GAME PROJECT SAMPLE ASSESSMENT MATERIAL GCSE Computing.
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
Visual Basic Games: Prepare for Hangman
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Shorter of two objects and changing color V2 Functions, events and setting the color in sequence and randomly This is a modification of the Changing Color.
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
What are the challenges? To know how to translate word problems into a workable mathematical equation. To know where to start and how to go about figuring.
Implementation of the Hangman Game in C++
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
Subtracting With Place Value. Place Value to Subtract ,000 How many ones make a ten?
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
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.
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
GCSE Computing: Programming GCSE Programming Remembering Python.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
HANGMAN- Software/ Hardware Integration Project Idea was to design a working C++ program to play a Hangman word guessing game Idea was to design a working.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Loops. Review Control Structure – making decisions if-statement Program Design Understand what you want to do Design your solution Write and test your.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
World of Wokcraft The very best in Single pan cooking themed fantasy gaming!
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
World of Wokcraft The very best in Single pan cooking themed fantasy gaming!
Shlomo Hershkop Basics overview. Shlomo Hershkop Basic Review - Overview Practice coding Practice coding finger guessing game finger guessing.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Engineering Innovation Center
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Module 4 Loops.
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.
While Loops in Python.
Presentation transcript:

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

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

Story Teller Program Let’s write a program that will generate the plot of a random story – Each story consists of Main character Character met Location left Location traveled to Event that took place Situation or action that happened to main character Moral learned – Use 7 String arrays and randomly select one element from each array

Example String[ ] main={“Frank Zappa”, “President Obama”, “Mickey Mouse”, “Joey Votto”, “Bill Gates”, “Renee”, “The invisible man who lives in my closet”}; Selecting a random string is done by using main[g.nextInt(main.length)] – main.length is the length of the array – we ask the random number generator to provide a random number from 0 to length-1 and use it as the array index to withdraw 1 String from main We would similarly do this for each of our arrays We use one (or more) output statements to output the story, filling in the gaps between the array elements so that it might read like this: – Frank Zappa left Cincinnati, OH to travel to the moon where he met King Kong. Together, they went to a movie but on the way, Frank Zappa broke his leg and learned that crime never pays – the elements underlined are those withdrawn from the arrays

Question Answering Game Let’s write a simple game that requires that the user correctly answer a number of questions – The user can get points for each correct answer and win money (or prizes) based on the number of points – Questions might be worth different points based on the difficulty of the question – We might allow a user to keep answering until they miss a question, or we might give the user several “lives” – Alternatively, we can give the user some “cheats” or “lifelines” – We can put questions into different categories and let the user pick a category – For now, we’ll stick to an easy game

Basic Logic Create two arrays, an array of questions and an array of answers – both will be Strings – String[ ] questions = {“question 1”, “question 2”, … “question n”}; – String[ ] answers = {“answer 1”, “answer 2”, … “answer n”}; – int amount = 0, round=1; – boolean correct=true; – while(correct&&round<LIMIT) { generate a random question and ask the user get the user’s answer if user’s answer is equal to the answer in the answer array, add to amount else set correct to false } – Output the result of the game

Generating a Random Question & Answer Make sure that your arrays are set up so that the questions and their respective answers are in the same order – questions[i]’s answer is in answer[i] To generate a random question – num=g.nextInt(questions.length); – print the question out and get the user’s answer, let’s store that in guess If(guess.equalsIgnoreCase(answer[num]) … We either need to equip our game with hundreds (or thousands) of questions, or use a do loop to ensure that no question is asked twice during a game, can you figure out how to do that?

Not Repeating Random Numbers Consider the task of generating 6 lotto numbers – seems easy, 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? – we store each number in an array and for each new number, we will check to see if we have generated it yet or not 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; // assume #s are from 1-48 found = false; k = 0; while(!found && k < j) if(numbers[k]= = temp) found = true; else k++; } while(found); numbers[j] = temp; }

Millionaire Game Who Wants to be a Millionaire is a similar game to the one we just discussed but there are a few differences Each question has 4 answers labeled A, B, C, D this simplifies checking the user’s answer to the real answer as we can just compare characters using == we have to alter our questions array to include the multiple choice answers, for instance, one entry might be “Who was the first president of the US?\nA. Abraham Lincoln\nB. Bill Clinton\nC. George Washington\nD. George Jefferson” In the game, the contestant’s dollar amount goes up in levels, $100, $200, $300, $500, $1000, $2500, etc We can store these in an array, or more simply, just double the amount after each answer

Continued We can also allow the user to enter 4 more letters other than A-D – E – walk away (do not try to answer the current question, just leave with the money you have) – F – use a lifeline (50/50) – G – use a lifeline (call a friend) – H – use a lifeline (poll the audience) How do we implement the lifelines? – This is challenging, but what we could do is create 3 more arrays – For instance, the callAFriend array would store for element i what a friend might advice for question i I think its “C”, I’m about 75% sure, but not positive I have no idea It is definitely “A” – But to make the game fun, make sure that lifelines are not always correct!