・ Goo: 1, Choki: 2, Par: 3. ・ The number of players will be one to one. ・ The number of battles will be one.
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