Code your first game!
Let's try and code a simple game where the user plays against the computer. The user picks, and the computer randomizes one of the three values which are compared and the result is displayed on the console (that's how we sometimes call the screen) and written to an array.
Your job is to complete the code responsible for the game logic.
Some parts of the code were already written: they are necessary for the code to work correctly but we have not learned all of them yet: these are more advanced topics. For now you will understand how they work in general to know what is going on in the code.
Note: the code as is now, does not compile.
Let's go through the code:
- At the beginning you can see two import instructions: one of them you probably remember from the previous lesson when we discussed receiving data from the user. The second one lets us use the functionalities the computer needs to randomize a value.
- Under the 'main' method declaration you can see the Scanner class object declaration, and a similar one for the Random class object.
You can start writing the game!
1. Declare an array to store the result from three rounds. We're assuming that it will have String type elements (based on the result they will be: win, lose, draw).
2. The next element is a 'for' loop: we will use it to display the question about the user's choice three times. In the marked place complete the 'for' loop to make it work like this.
3. Now, in the loop, using the System.out.println() instruction to display the message asking the user to make a choice. For example: "Enter 1 (paper), 2 (rock) or 3 (scissors)."
Note: It is easier to validate the entered data in our game when they are numbers rather than text. That's why we use number 1 to mean paper, 2 for stone, and 3 for scissors.
4. Declare a String type variable: in it we will store the value of the user's keyboard input.
5. To the variable you just declared, assign the value from the keyboard. The next two lines of code may not be clear for you: we did not discuss this in our lessons because they refer to more advanced topics. But let's explain what they do.
int liczba = Integer.parseInt(choice);
The code above is used to convert the retrieved text value into a number. As we know from the previous lesson, the message read from the console always has the String type: that's why we need to convert it into a number to be able to compare it easily later. As you can see, after the appropriate type conversion the variable that was String can now be assigned to the int type.
The next, large part of code includes conditional statements. The first big condition is going to check whether the values entered by the user are within the right range. If so, we will write next conditions; if not - we are going to print the information that the entered value is outside the range and end the program (in the future rather than ending the program we can add a functionality that keeps asking the user as long as they give an acceptable answer).
The main condition was written before: we need to complete it.
6. First, write a conditional statement that checks the user's input and based on the chosen value displays an appropriate message (e.g., "You picked rock"). Remember that we hide the values under the numbers 1, 2 and 3 so you need to check the numeric variables, not text ones.
Important: array indexes start from 0.
Now we need to write the next conditions to decide who won the round. We can write a formula of available options:
|Computer randomized:||User chose:||Who won:|
Now we need to code it. The starting point will be the the user's choice, we are going to compare the computer's random value with it.
7. Write a condition, like the one in task 6 but for the computer's random choices.
8. Now, for each of the conditions from task 7 write the result based on each of the possible choices of the user. Inside the conditions put an instruction to display the message to the user: about winning, losing or drawing the round. Don't forget to assign each value to a String type variable storing the result that you are later going to write to the array of results.
9. Store the result variable under an appropriate index in the array. Do it in a loop with the 'i' variable that changes its value with every round.
10. Here in the code we have an already filled array with results. Your job is to use a "for" loop to print the values from the entire array.