I recently started studying Java, so I wanted to make something object-oriented.
① Enter your name. (The name of the other party is decided randomly.) ② Choose a hand to put out. (The opponent's hand is randomly determined.) ③ Output the result. ④ The end. (This is a one-time match.)
①Janken.java This is the main function.
Janken.java
public class Janken {
    public static void main(String[] args) {
        Player you   = new You();
        Player enemy = new Enemy();
        System.out.print("Please enter your name. :> ");
        you.setName();
        enemy.setName();
        String yourName  = you.getName();
        String enemyName = enemy.getName();
        System.out.println("Your name is" + yourName);
        System.out.println("The name of the other party is" + enemyName);
        Hand yourHand  = you.nextHand();
        Hand enemyHand = enemy.nextHand();
        System.out.println(yourName   + "Is" + yourHand  +  "Was issued.");
        System.out.println(enemyName  + "Is" + enemyHand +  "Was issued.");
        if (yourHand.winTo(enemyHand)) {
            System.out.println(yourName + "Nokachi!");
        } else if (yourHand.loseTo(enemyHand)) {
            System.out.println(yourName + "Bonus ...");
        } else {
            System.out.println("This is Aiko.");
        }
    }
}
②Player.java An abstract class with rough information about the player. Inherit this class and create a class for you and your opponent.
Player.java
public abstract class Player {
    protected String name;
    public String getName(){
        return this.name;
    }
    public abstract void setName();
    public abstract Hand nextHand();
}
③you.java
You.java
import java.util.*;
public class You extends Player {
    @Override
    public void setName() {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        this.name = name;
    }
    @Override
    public Hand nextHand() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("What to put out?Goo:0 choki:1 par:2  > ");
            try {
                int hand_number = Integer.parseInt(scanner.nextLine());
                if (0 <= hand_number && hand_number <= 2) {
                    return Hand.fromInt(hand_number);
                } else {
                    System.err.println("A number outside the range has been entered.");
                }
            } catch (NumberFormatException e) {
                System.err.println("Other than numbers have been entered");
            }
        }
    }
}
④Enemy.java
Enemy.java
public class Enemy extends Player {
    private final String[] names = {"Abe", "Koizumi", "Aso", "Suga"};
    @Override
    public void setName() {
        String enemyName = names[(int) (Math.random() * names.length)];
        this.name = enemyName;
    }
    @Override
    public Hand nextHand() {
        return Hand.fromInt((int) (Math.random() * 3));
    }
}
⑤Hand.java I used an enum class.
Hand.java
public enum Hand {
    Rock,
    Scissors,
    Paper;
    @Override
    public String toString() {
        switch (this) {
            case Rock     : return "Goo";
            case Scissors : return "Choki";
            case Paper    : return "Par";
        }
        throw new IllegalStateException();
    }
    public static Hand fromInt(int n) {
        switch (n) {
            case 0 : return Rock;
            case 1 : return Scissors;
            case 2 : return Paper;
        }
        throw new IllegalArgumentException(Integer.toString(n));
    }
    public boolean winTo(Hand hand) {
        switch (this) {
            case Rock     : return hand == Scissors;
            case Scissors : return hand == Paper;
            case Paper    : return hand == Rock;
        }
        throw new IllegalStateException();
    }
    public boolean loseTo(Hand hand) {
        return this != hand && !this.winTo(hand); 
    }
    
}
that's all. It seems interesting to make a lot of enemies and play tournament battles, or make a strategy class. I would appreciate any improvements.
Recommended Posts