I made a rock-paper-scissors game in Java (CLI)

specification

・ Goo: 1, Choki: 2, Par: 3. ・ The number of players will be one to one. ・ The number of battles will be one.

Operation screen

janken.gif

Source code

Main.java Main class

package janken;

public class Main {
	public static void main(String[] args) {
		GameController gc = new GameController();
		gc.start();	//game start
		gc.end();	//Game over
	}
}

GameController.java Rock-paper-scissors game controller class

package janken;

/**
 * GameController Class
 *Control the game
 * @author user
 *
 */
public class GameController {
	private Hand myHand;;
	private Hand opponentHand;
	private Rule rule;

	/**
	 *constructor
	 */
	public GameController() {
		this.rule = new Rule();
		this.myHand = new Hand();
		this.opponentHand = new Hand();
	}

	/**
	 * start method
	 *Start the game
	 */
	public void start() {
		System.out.println("Start rock-paper-scissors.");

		do {
			myHand.setHand();
			opponentHand.setRandomHand();
			rule.showHands(myHand, opponentHand);
		}while(rule.isDraw(myHand, opponentHand));

		rule.showResult(myHand, opponentHand);
	}

	/**
	 * end method
	 *End the game
	 */
	public void end() {
		System.out.println("Finish the rock-paper-scissors.");

	}
}

Hand.java Rock-paper-scissors hand object class

package janken;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

/**
 * Hand Class
 *Rock-paper-scissors hand object class
 * @author user
 *
 */
public class Hand {
	private int hand;

	/**
	 * setHand method
	 *Set hands with standard input
	 */
	public void setHand() {
		System.out.println("Please enter your hand.");
		System.out.println("Goo: 1, Choki: 2, Par: 3");

		while(true) {
			try {
				Scanner scan = new Scanner(System.in);
				this.hand = scan.nextInt();
				break;
			}catch(InputMismatchException e){
				System.out.println("Please enter a numerical value.");
			}
		}
	}

	/**
	 * setRandomHand method
	 *Randomly set hands
	 */
	public void setRandomHand() {
		Random random = new Random();
		this.hand = random.nextInt(3)+1;
	}

	/**
	 * handName method
	 *Returns the name of the hand
	 * @return
	 */
	public String handName() {
		String handName = null;

		if(this.hand == 1) handName = "Goo";
		if(this.hand == 2) handName = "Choki";
		if(this.hand == 3) handName = "Par";

		return handName;
	}

	/**
	 * hand method
	 *Returns the value of the hand
	 * @return
	 */
	public int hand() {
		return this.hand;
	}
}

Rule.java Class that defines the rules of rock-paper-scissors

package janken;

/**
 * Rule Class
 *Class that defines the rule
 * @author user
 *
 */
public class Rule {
	/**
	 * showHands method
	 *Display your own hand and the other's hand.
	 * @param myHand
	 * @param opponetHand
	 */
	public void showHands(Hand myHand, Hand opponetHand) {
		System.out.print("My hand is"+ myHand.handName() +"。");
		System.out.print("The other party's hand"+ opponetHand.handName() +"。");
		System.out.println("");
	}

	/**
	 * isDraw method
	 *Judge Aiko
	 * @param myHand
	 * @param opponetHand
	 * @return
	 */
	public boolean isDraw(Hand myHand, Hand opponetHand) {
		if(myHand.hand() == opponetHand.hand()) {
			System.out.println("Aiko!");
			return true;
		}

		return false;
	}

	/**
	 * showResult method
	 *Display the result of victory or defeat
	 * @param myHand
	 * @param opponetHand
	 */
	public void showResult(Hand myHand, Hand opponetHand) {
		String result;
		// 1:Goo, 2: Choki, 3:Par
		if((myHand.hand() == 1 && opponetHand.hand() == 2) ||
		    (myHand.hand() == 2 && opponetHand.hand() == 3) ||
			(myHand.hand() == 3 && opponetHand.hand() == 1)) {
			result = "win";
		}else {
			result = "Lose";
		}
		System.out.println("your" + result + "is.");
	}

}

Github https://github.com/TakumiKondo/janken

Recommended Posts

I made a rock-paper-scissors game in Java (CLI)
I made a simple calculation problem game in Java
I made a primality test program in Java
I made roulette in Java.
Rock-paper-scissors game for beginners in Java
I created a PDF in Java.
I made a shopify app @java
I made an annotation in Java.
Age guessing game made in Java
[Beginner] I made a program to sell cakes in Java
Rock-paper-scissors in Java
I made a rock-paper-scissors app with kotlin
I made a new Java deployment tool
I made a rock-paper-scissors app with android
I made the "Sunshine Ikezaki game" I saw on Twitter in Java.
I made a Diff tool for Java files
Rock-paper-scissors app in Java
Rock-paper-scissors game java practice
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
I made a Ruby extension library in C
I wrote a prime factorization program in Java
Learn Java with Progate → I will explain because I made a basic game myself
I tried to create a Clova skill in Java
I tried to make a login function in Java
I made a Restful server and client in Spring.
What I learned when building a server in Java
I made a Wrapper that calls KNP from Java
I made a chat app.
Find a subset in Java
I tried metaprogramming in Java
I made a server side of an online card game ⑤
I made a server side of an online card game ③
I made a server side of an online card game ⑥
I just wanted to make a Reactive Property in Java
I made a server side of an online card game ④
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I made a server side of an online card game ②
I tried to make a client of RESAS-API in Java
I made a Dockerfile to start Glassfish 5 using Oracle Java
A rock-paper-scissors game for two players who play against each other in threads with Java
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I made a program in Java that solves the traveling salesman problem with a genetic algorithm
Ruby: I made a FizzBuzz program!
3 Implement a simple interpreter in Java
I made a GUI with Swing
I wrote Goldbach's theorem in java
A simple sample callback in Java
I made a simple recommendation function.
I tried using JWT in Java
Get stuck in a Java primer
I made a matching app (Android app)
Sample vending machine made in Java
I made a package.xml generation tool.
[Android] I made a pedometer app.
I tried to illuminate the Christmas tree in a life game
I can't create a Java class with a specific name in IntelliJ
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
I recently made a js app in the rumored Dart language