Ok, we need the computer to generate random numbers. Just add this code inside your main method so you have this:

Similar documents
Part II: Number Guessing Game Part 2. Lab Guessing Game version 2.0

Begin this assignment by first creating a new Java Project called Assignment 5.There is only one part to this assignment.

MITOCW R7. Comparison Sort, Counting and Radix Sort

Bagels, Pico, Fermi. Bob Albrecht & George Firedrake Copyright (c) 2004 by Bob Albrecht

Unit 5: What s in a List

Project 1: A Game of Greed

Memory. Introduction. Scratch. In this project, you will create a memory game where you have to memorise and repeat a sequence of random colours!

Beginning Game Programming, COMI 2040 Lab 6

INVENTION LOG FOR CODE KIT

In this project, you will create a memory game where you have to memorise and repeat a sequence of random colours!

CPSC 217 Assignment 3 Due Date: Friday March 30, 2018 at 11:59pm

In this project you will learn how to write a Python program telling people all about you. Type the following into the window that appears:

CS61B Lecture #33. Today: Backtracking searches, game trees (DSIJ, Section 6.5)

HW4: The Game of Pig Due date: Tuesday, Mar 15 th at 9pm. Late turn-in deadline is Thursday, Mar 17th at 9pm.

MITOCW mit-6-00-f08-lec06_300k

MITOCW ocw lec11

MANIFESTATION REVEALED - THE LAWS OF MIND SYSTEM

Blink. EE 285 Arduino 1

Mint Tin Mini Skulduggery

Circuit Playground Quick Draw

Project 2 - Blackjack Due 7/1/12 by Midnight

2: Turning the Tables

Description: PUP Math World Series Location: David Brearley High School Kenilworth, NJ Researcher: Professor Carolyn Maher

Girls Programming Network. Scissors Paper Rock!

OCR Statistics 1. Probability. Section 2: Permutations and combinations. Factorials

Trial version. Resistor Production. How can the outcomes be analysed to optimise the process? Student. Contents. Resistor Production page: 1 of 15

Writing a Scholarship Essay From Fastweb.com

For slightly more detailed instructions on how to play, visit:

Planning a Campaign Strategy

I: Can you tell me more about how AIDS is passed on from one person to the other? I: Ok. Does it matter a how often a person gets a blood transfusion?

Cumulative Distribution Function (CDF) - Analyzing the Roll of Dice with TSQL

MITOCW MITCMS_608S14_ses05

Ancestry Hints What to Do With All Those Little Green Leaves

Probability Paradoxes

Lesson 8 Tic-Tac-Toe (Noughts and Crosses)

Transcript of the podcasted interview: How to negotiate with your boss by W.P. Carey School of Business

Heuristics: Rules of Thumb

Summer 2006 I2T2 Number Sense Page 24. N3 - Fractions. Work in Pairs. Find three different models to represent each situation.

For this assignment, your job is to create a program that plays (a simplified version of) blackjack. Name your program blackjack.py.

CSE231 Spring Updated 04/09/2019 Project 10: Basra - A Fishing Card Game

Proven Performance Inventory

Let's Race! Typing on the Home Row

UNDERSTANDING LAYER MASKS IN PHOTOSHOP

Candidate Instructions

Take one! Rules: Two players take turns taking away 1 chip at a time from a pile of chips. The player who takes the last chip wins.

MITOCW watch?v=6fyk-3vt4fe

GeoPlunge Combo 1 Overview

(a) Left Right (b) Left Right. Up Up 5-4. Row Down 0-5 Row Down 1 2. (c) B1 B2 (d) B1 B2 A1 4, 2-5, 6 A1 3, 2 0, 1

D - Robot break time - make a game!

MITOCW R9. Rolling Hashes, Amortized Analysis

Autodesk University Laser-Scanning Workflow Process for Chemical Plant Using ReCap and AutoCAD Plant 3D

Rock, Paper, Scissors

Once this function is called, it repeatedly does several things over and over, several times per second:

Animal Poker Rulebook

Game A. Auction Block

Multimedia and Arts Integration in ELA

INTRODUCTION my world

Grade 7/8 Math Circles Game Theory October 27/28, 2015

~ 1 ~ WELCOME TO:

SPLIT ODDS. No. But win the majority of the 1089 hands you play in this next year? Yes. That s why Split Odds are so basic, like Counting.

"The Lottery Shotgun Method:

MITOCW MITCMS_608S14_ses03_2

MITOCW ocw f08-lec36_300k

MITOCW watch?v=fp7usgx_cvm

Environmental Stochasticity: Roc Flu Macro

MITOCW mit-6-00-f08-lec03_300k

A - Debris on the Track

6.00 Introduction to Computer Science and Programming, Fall 2008

Fred: Wow, that's really nice to hear. So yeah, so when something like this happens, you always have people around you to help you.

We're excited to announce that the next JAFX Trading Competition will soon be live!

Rubik's Cube Solution

Writing to the Editor

HW4: The Game of Pig Due date: Thursday, Oct. 29 th at 9pm. Late turn-in deadline is Tuesday, Nov. 3 rd at 9pm.

MITOCW R3. Document Distance, Insertion and Merge Sort

HOW TO WIN LOTTO WITH KEYS AND FORECAST MAKE YOUR WAY WITH BABA IJEBU

Circle Link 1 Online Rules of the Road

LEVEL A: SCOPE AND SEQUENCE

the gamedesigninitiative at cornell university Lecture 6 Uncertainty & Risk

DAZ Studio. Camera Control. Quick Tutorial

Things I DON'T Like. Things I DO Like. Skill Quizzes. The Agenda

Composition Allsop Research Paper Checklist NOTECARDS

BIEB 143 Spring 2018 Weeks 8-10 Game Theory Lab

Write an Opinion Essay

C# Tutorial Fighter Jet Shooting Game

Formulas: Index, Match, and Indirect

Block Sanding Primer Dos and Don ts Transcript

PHASE 10 CARD GAME Copyright 1982 by Kenneth R. Johnson

INTRODUCTION TO COMPUTER SCIENCE I PROJECT 6 Sudoku! Revision 2 [2010-May-04] 1

Project #1 Report for Color Match Game

Statistical Analysis of Nuel Tournaments Department of Statistics University of California, Berkeley

SET-UP QUALIFYING. x7 x4 x2. x1 x3

Photoshop Techniques Digital Enhancement

CS61B Lecture #22. Today: Backtracking searches, game trees (DSIJ, Section 6.5) Last modified: Mon Oct 17 20:55: CS61B: Lecture #22 1

class TicTacToe: def init (self): # board is a list of 10 strings representing the board(ignore index 0) self.board = [" "]*10 self.

CS 371M. Homework 2: Risk. All submissions should be done via git. Refer to the git setup, and submission documents for the correct procedure.

STAT 311 (Spring 2016) Worksheet: W3W: Independence due: Mon. 2/1

A NIGHT AT THE OPERA

MITOCW Recitation 9b: DNA Sequence Matching

The Beauty and Joy of Computing Lab Exercise 10: Shall we play a game? Objectives. Background (Pre-Lab Reading)

The notes are C, G, and E.

Transcription:

Java Guessing Game In this guessing game, you will create a program in which the computer will come up with a random number between 1 and 1000. The player must then continue to guess numbers until the player guesses the correct number. For every guess, the computer will either say "Too high" or "Too low", and then ask for another input. At the end of the game, the number is revealed along with the number of guesses it took to get the correct number. First create a new Java Project called Games, Next, we're going to start by creating a new class, or Java file. Call your new program GuessingGame, keeping the capitalization the same. If you're using Eclipse (and I strongly urge you to!) then make sure to checkmark the box to have it put in your main method for you. You should start out like this: Ok, we need the computer to generate random numbers. Just add this code inside your main method so you have this: The variable numbertoguess holds the integer that the player has to guess. Now, we need to figure out exactly what we need our game to do and how we're going to accomplish this goal. Let's start by listing what the guessing game needs to do, also known as the requirements of the program. Needs of the guessing game: Creates a random number to guess

Keeps track of number of guesses Asks us to guess a number Let user input a number Tells us whether we're guessing too high or too low Keeps playing until we guess the correct number Tells us the correct number and the number of tries We already took care of the first need, which was to create a random number. So, let's move on to the next requirement, keeping track of the number of guesses. To keep track of anything, you need a variable. In this case, since we're keeping track of guesses, a simple integer variable will do. Add an int variable to your code, and start it off at 0, since at the beginning the player has made no guesses. So far we have the variable, but it still does not keep track of the number of guesses because the user isn't being asked to make any guesses yet. Let's have the computer ask us to guess a number.

Now, we need the player to be able to input the number. This means we're going to need a Scanner. I suggest you try to define all variables as high up in the code as possible. It helps you keep track of all the different variables you have and makes sure that you don't accidentally use the same variable twice. So, create a Scanner at right under your variable that keeps track of the number of guesses. You should know how to create a Scanner Now that we have a scanner to use, we need to actually have a variable that stores the input from the user. You can create this variable at the top too.

Have your new variable store the input from the scanner. Remember, the player will be guessing integers, so having the variable be an integer is a must. Remember to use nextint() with Scanner probably. The computer then needs to tell us if this guess was too high or too low. Notice how in that sentence the word if is emphasized. That's a big clue as to what you need to use to accomplish this. Let's break down the if statement. If the number guessed is higher than the real number, tell us it s too high. If the number guessed is smaller than the real number, tell us it s too low. If the number guessed is the same as the real number, tell us that we won. You could do this in three different if statements, but it's best to use else if in this case to tie them all together. It helps to show that all those if's are related to each other, and that only one of those if's will ever be true at one time (the guessed number can never be too high and too low at the same time, for example). This is what your code should look like at this point: Keeps track of number of guesses (remember we only finished part of it)

Keeps playing until we guess the correct number Tells us the correct number and the number of tries We should track the number of guesses Right after the player guesses the number. Run your program now and notice what happens. The program would go one time, and then stop. This is because we need it to keep going until the user wins. We will have to think about this a little bit before we code it. First of all, what ways do we know to make Java do something over and over again? In the class we went over two ways, the for loop and the while loop. If you remember the difference, then you know that the for loop loops for a certain number of times. Unfortunately the number of times this program could loop depends on the player! So, a for loop is probably not the best way to handle this.

A while loop is the perfect choice. It keeps going until a condition is no longer true. So, while the player hasn't won yet, keep going. But how do we keep track of whether or not the player has won? We use a boolean variable. We can create a boolean variable called win near the top of our code with all the other variables. If we set win to false, then it means the player hasn't won yet. When win is true, then the player won the game. The last thing we need to figure out is which code to put inside of this while loop. I recommend everything starting from the computer asking the player to guess a number all the way down to the if statements. See how inside the while loop parenthesis the condition is when win is equal to false? This means it will continue to loop until something sets the win variable to true.

But what will set the win variable to true? The third part of the if statement seems like a good choice. The part that asks if the player guessed the correct number. Here is what this looks like: Now we need the game statistics when the game is over. Ok, after your while loop, we can add the code in. It should just be a series of println that tell us everything we need to know, such as number of guesses made. Your guessing game program is now complete! Go ahead and play it. I wouldn't try guessing letters or anything like that, as your program will crash.