Download week8,9

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming w/ Java – Week 8, 9
Week 8, 9 Agenda:
Our first Game – Rock, Paper, Scissors!
Today we will continue with our knowledge of how to interact with users – asking them for input and then
using that input.
First we will click on File and choose New Project and Java Application.
For our project name we will type “Week8”, and we will allow Java to create a Main method for us.
Feel free to delete all the standard comments that come up, and then type the program that follows. But
before we do, let’s learn a new Netbeans trick:
REFACTORING!
Refactoring is a fancy way to renaming a class – by using this Netbeans can do more than just rename your
class – it can rename the file, and check to see where else the name is used (comments, dependencies from
other classes in your package which call this class, etc.) and make sure we don’t break our program when
we rename. So right click in the Projects Window on the Week8.java file and choose “Refactor->Rename”
– then type “RockPaperScissors” for the new class name. Now, onto the program:
Step 1 – As always, with good programming we will break our game down into tasks. First, we will get
the user’s guess. Next, we will generate the computer’s guess using a random number generator. To do
these things we will use some pre-written classes from the Java Utilities package – but importing them.
Finally, we will compare the guesses and display the results. It is a good idea to write your program as an
outline first, using comments to mark placeholders for the methods that need to be written.
----------------package week8;
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
System.out.println("Let's play Rock, Paper, Scissors!");
// Get userGuess();
// Get computerGuess();
// compareAndDisplayResults();
}
}
-----------------------Make sure you compile/run this to get rid of any errors/typos/mistakes. If it works, great, let’s move on.
Step 2 – Now let’s write the method which gets the user’s guess. Note that we call the variable in the main
method userGuess, and we call the method that get’s this getUserGuess.
----------------package week8;
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
static String getUserGuess() {
System.out.println("What is your guess?");
Scanner readText = new Scanner(System.in);
return readText.next();
}
public static void main(String[] args) {
String userGuess, computerGuess;
System.out.println("Let's play rock, paper, scissors!");
userGuess = getUserGuess();
System.out.println("You have chosen "+userGuess);
// getComputerGuess();
// compareAndDisplayResults();
}
}
-----------------------Once this works you are on your way!
Step 3 – Now let’s write the method which gets the computer’s guess. Note that we call the variable in the
main method computerGuess, and we call the method that gets this getComputerGuess. Notice here that
we use the Netbeans feature where we can close methods – by clicking on the +/- box on the left of the first
line of the method. I have closed the getUserGuess method so it is easier to read what we are doing.
Again, notice that the new code is in bold.
----------------package week8;
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
static String getUserGuess() {
static String getComputerGuess() {
int randomInt;
Random randomGenerator = new Random();
randomInt = randomGenerator.nextInt(3);
if (randomInt == 0) {
return "rock";
} else if (randomInt ==1) {
return "paper";
} else {
return "scissors";
}
}
public static void main(String[] args) {
String userGuess, computerGuess;
System.out.println("Let's play rock, paper, scissors!");
userGuess = getUserGuess();
System.out.println("You have chosen "+userGuess);
computerGuess = getComputerGuess();
System.out.println("The Computer chose "+computerGuess);
//compareAndDisplayResults(userGuess, computerGuess);
}
}
-----------------------Step 4 – Finally, let’s write the method which compares and displays the results. Again, below we show
the new Method and the changes to the Main method. Notice the new operations here:
1. We use the || symbol to indicate the logical “OR” operation. Any of the three options can set the
userWins flag to true.
2. We use the userChoice.equals() and computerChoice.equals() to compare Strings, instead of the “==”
operation. This is because the “==” doesn’t work the same with strings as it does with variables. While
“==” asks the question “are these equivalent?” with variables, with Strings it literally means “do these point
to the same location in memory?”. This answer would be “no” for strings”, so instead we use the “.equals”
method of Strings.
----------------static void compareAndDisplayResults (String userChoice, String computerChoice) {
boolean userWins = false;
if ( (userChoice.equals("rock") && computerChoice.equals("scissors")) ||
(userChoice.equals("paper") && computerChoice.equals("rock")) ||
(userChoice.equals("scissors") && computerChoice.equals("paper")) ) {
userWins = true;
} else {
userWins = false;
}
if (userChoice.equals(computerChoice) ) {
System.out.println("Its a tie! \n");
} else if (userWins == true) {
System.out.println("You win - you have defeated the computer! \n");
} else {
System.out.println("So sorry, you lose! \n");
}
}
public static void main(String[] args) {
String userGuess, computerGuess;
System.out.println("Let's play rock, paper, scissors!");
userGuess = getUserGuess();
System.out.println("You have chosen "+userGuess);
computerGuess = getComputerGuess();
System.out.println("The Computer chose "+userGuess);
compareAndDisplayResults(userGuess, computerGuess);
}
}
-----------------------Does this work? Congratulations!
Next, try some improvements – but first, open a New Java Project called “Week9”. Then, using the Project
Window, try copying/pasting your RockPaperScissors file from the Week8 project (and package) to the
Week9 project (and package). Then use the “Refactor->Rename” command to change the name to
“RockPaperScissorsFinal”. Now proceed with improving via steps 1 and 2 below:
1. First, if the user enters anything other than “rock”, “paper”, or “scissors”, given them a warning message.
Do not proceed on until you have a valid input.
2. Ask them how many times they want to play – and then try to keep a running count of the score which
you display after each round….
Final Challenge – create a new class – I suggest right clicking on the Week9 package – again in the project
Window, and selecting “New, Java Main Class”. Call this class randomTester. Then write a program to
see if the computer is really generating a random number. Do this by having the computer generate a
random number 1000 times, then show how many times each number (between 0 and 2) was chosen. Are
they pretty equal? What if you run it 10,000 times?