What Will I Learn?
- You will learn using new methods on java
- You will learn the usage of loops on java.
- You will learn how to make a basic game on java language.
Requirements
- Eclipse
- Basic knowledge of coding on java
- Basic knowledge on using Eclipse
Difficulty
- Basic
Tutorial Contents
Hello,in this course i am going to show you guys how to make a rock,paper,scissors game,also you will learn the usage of "SuppressWarnings" on Java.Let's begin.
First of all i am defining libraries of java,naming my program and starting the coding.I'm going to use "java.util.scanner" .This library is a simple text scanner which can parse primitive types and strings using regular expressions.
package Course2;
import java.util.Scanner;
public class RPC
public static void main(String args[])
Now i am going to use "suppresswarnings" method.This method Indicates that the named compiler warnings should be suppressed in the annotated element,so basically this method blocks warnings.
Then in the below line i am going to use "scanner" and name it as "Rpc". I defined "java.util.Scanner" library for this line.Now i defined a new scanner.
In the final step on this line we acquaint our scanner with using "new" constructs a new scanner that produces values scanned from the specified input stream and using "system.in" is used for to input the stream.
@SuppressWarnings("resource")
Scanner Rpc = new Scanner(System.in);
In this step i am defining two integers names as "computerwon" and "userwon" and i equalize them to zero.
int ComputerWon = 0;
int UserWon = 0;
Now i am going to use "while" loop for these lines.For using while loop i need a condition.I will end the game who ever hits the 3 points first.So for this step i will make while loop continue as long as my integers "computerwon" and "userwon" is 3.
In the below line we make system to print if they want to choose rock,paper or scissors.Then i define two new integers named as "user" and "computer" and equalize "user" to "Rpc.nextInt". This is used for to scan what user selected and "nextInt" scans the next token of the input as an integer. We do same thing in below for the Computer with using "math.random" method.This will make computer generate a random number from 0-2 which will make computer to play against the user.
while(ComputerWon<3 && UserWon<3){
System.out.println("Rock,Paper,Scissors ?");
int User = Rpc.nextInt();
int Computer = (int)(Math.random()*3);

In this step we will make user and computer to select rock,paper or scissors.I'm going to use switch-case for this.With using "System.out.print" i will say whose turn is that and after that we will use switch "switch (Computer)" and define the cases from 0 to 2.Then i am going to do the same thing for the User.
System.out.print("Computer :");
switch (Computer)
{
case 0: System.out.println("Rock."); break;
case 1: System.out.println("Paper."); break;
case 2: System.out.println("Scissors.");
}
System.out.print("User :");
switch (User)
{
case 0: System.out.println("Rock"); break;
case 1: System.out.println("Paper"); break;
case 2: System.out.println("Scissors ");
}

Now we will evaluate the possibilities in first step if user and computer selects same thing,none of them will get the point.So we will use "if" loop for this lines.
if(User==Computer){
System.out.println("Draw");
Then if the result is not draw,we will evaluate all the possibilities again.What we do here is basically
determine the rules of the game.If user selects rock and computer goes for scissors the user will win the point,so basically we evaluate all posibilites of users chance of winning in this lines.We will use "else" to evaluate computers chance of winning.
In the final step of code we use our integers that we defined in the begging of the code "userwon" and "computerwon" depending on which one ever won we add them the point by using "++" which adds one point.
Note : "&&" means "and" and || means "or"
else{
if((User==0 && Computer ==2) || (User==1 && Computer ==0) || (User==2 && Computer ==1)){
System.out.println("You won this hand");
UserWon++;
else{
System.out.println("Computer won this hand");
ComputerWon++;

In the final step we will calculate the total score and print it to the screen,then show the final message if "Userwon" is equal to 3 message will say "you won" if not message will say "Computer won try again".The qestion mark is used on same purpose as "if" loop in there and ":" is same purpose as "or".
System.out.println("User : "+UserWon+" - "+ "Computer : "+ComputerWon);
}
System.out.println(UserWon==3 ? "You won!":"Computer won try again!");

Whole Code


Outputs


Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors