Play a game is very good and fun... but playing a game that you have created, is a pleasure that just a few have.
The Java programming language is known to be widely used for creating games. Most likely you've played a game, even online ones, are done in Java.
Based on the knowledge gained in our online course of Java, it is possible to create a game.
Although very simple, it is quite genious and cool.
The computer will randomly select a number between 1 and 1000 and you have to guess what number this is.
Each round you enter a number and the computer will give you the tips, saying the number you typed is greater or less than the number it choose.
Ends when you hit, and he keeps the number of attempts you made. Can you beat your own record?
You know what's the best strategy?
STEP 1: Computer generates a number between 1 and 1000
To generate a number, we need to import the Random class:
import java.util.Random;
We need to create an object of type Random, so it can generate the numbers, let's call 'randomGenerate':
randomGenerate Random = new Random();
Now we need you to provide us a random number in the range of 1000 numbers. Let armazenas this value into integer variable 'raffled':
raffled = randomGenerate.nextInt (1000);
But if so, it will generate a number between 0 and 999. To be between 1 and 1000, we add 1:
raffled = randomGenerate.nextInt (1000) + 1;
STEP 2: main program loop
Number generated!
Now the user will give guesses. As the user will guess the number at least once, looping needs to run at least once. Therefore, we use the do while looping.
This loop occurs while we try to guess the number, that is, as our guess is different from the number raffled:
do {
// Loop
// co game
} while(guess! = raffled);
STEP 3: attempt incremented
Every attempt to give the variable 'attempts', which accounts for our attempts is incremented by one:
attempts++;
STEP 4: analyzing the guess - tip method
We'll send 3 numbers to tip the method(): our guess, the number raffled by the computer and the number of attempts that we already tried.
For our game to give some hint, we will use if else conditionals.
The method tests whether our guess was greater than the number generated by the computer, if it is, gives this tip:
if (guess > number) {
System.out.println ("Your guess is higher than the number raffled.");
}
Case is not, i.e the guess is less than or equal to the number drawn, this case goes to else.
If the user's guess is less than the number drawn by the computer, this tip is displayed:
if (guess <number) {
System.out.println ("Your guess is less than the number raffled.");
Finally, if the guess is not bigger or smaller than the number drawn is because equals.
Whilst it is equal, you hit it and hit the message and the number of attempts made:
System.out.println ("Congratulations, you are right! The number was" + number);
System.out.println ("You tried" + attempts + "times before you get it right!");
When you hit, the tip method is no longer called as 'guess != raffled' returns a false value, since these numbers are equal (raffled = guess), then the loop ends, ending our game in Java.
Our Java game code is:
import java.util.Random;
import java.util.Scanner;
public class guessTheNumberGame {
public static void tip(int guess, int raffled, int attempts){
if(guess > raffled){
System.out.println("Your guess is bigger than the raffled number.");
} else {
if(guess < raffled){
System.out.println("Your guess is less than the raffled number..");
} else {
System.out.println("Congratulations, you won! The number was " + raffled);
System.out.println("You tried " + attempts + " times");
}
}
}
public static void main(String[] args) {
int guess=0,
raffled,
attempts=0;
Scanner input = new Scanner(System.in);
Random geradorDeAleatorios = new Random();
raffled = geradorDeAleatorios.nextInt(1000) + 1;
System.out.println("Number between 1 and 1000 raffled!");
do{
System.out.printf("\n\n\n\n-----------------\n");
System.out.println("Attempt number: " + attempts);
System.out.print("Type your guess: ");
guess = input.nextInt();
attempts++;
tip(guess,raffled, attempts);
}while (guess != raffled);
}
}
No comments:
Post a Comment