Rock-paper-scissors app in Java

This is the first post. I created a rock-paper-scissors app that is written in various ways.

Creation period: 2 days Language: Java Environment: MacOS, Terminal Text editor: Atom

It is a game operated by commands. The rules are as follows.

――Three games --Janken's hands are managed by an array --The CPU creates a random number and decides the hand --The player decides his hand by changing the entered characters into numerical values. ――Judging victory or defeat by numerical value --Finally, display the result of the game

The actual screen looks like this.

スクリーンショット 2018-08-23 2.15.35.png

Janken class

It is a battle class.

Janken.java


package jankenapp;

public class Janken {
  static String[] handList = {"Goo","Choki","Par"};

  static void startMessage() {
    System.out.println("--------------------");
    System.out.println("Let's play rock-paper-scissors! It ’s three games!");
  }

  static void endMessage() {
    System.out.println("Thank you very much! Let's play again!");
    System.out.println("--------------------");
  }

  public static void main(String[] args) {
    Player player = new Player();
    CPU cpu = new CPU();
    Judge judge = new Judge();

    //Opening greeting
    startMessage();

    for (int i = 1; i <= 3; i++) {
     System.out.println("【" + i + "Round]");

     //CPU randomly creates hands
     cpu.setHand();
     //Player inputs hand
     player.setHand();
     // Player,Show CPU hand
     System.out.println("・ Your hand:" + handList[player.getHand()]);
     System.out.println("・ My hand:" + handList[cpu.getHand()]);
     //Judgment
     judge.judgement(cpu.getHand(), player.getHand());
    } //End of repetition
    //View results
    judge.result();
    //Closing remarks
    endMessage();
  }
}

Player class

I use Scanner to get the hand as a string and digitize it with a switch statement.

Player.java


package jankenapp;
import java.util.*;

public class Player {
  int hand;

  public void setHand() {
    System.out.print("Please enter one of "Goo", "Choki", and "Par". >");
    Scanner sc = new Scanner(System.in);
    String inputHand = sc.nextLine();

    switch(inputHand) {
      case "Goo":
        hand = 0;
        break;
      case "Choki":
        hand = 1;
        break;
      case "Par":
        hand = 2;
        break;
      default:
        System.out.println("I made a mistake, so I'm done ...");
    }
  }

  public int getHand() {
    return hand;
  }
}

CPU class

The hand is decided randomly by the random method.

CPU.java


package jankenapp;
import java.util.*;

public class CPU {
    int hand;

    public void setHand() {
     double rand = Math.random() * 3;
     hand = (int)rand;
    }

    public int getHand() {
      return this.hand;
    }
}

Judge class

It is a class of the judgment of victory or defeat and the result of the game. The judgment method is to quantify each hand and calculate it by (CPU hand-player's hand +3)% 3. The number of wins, losses and draws of the player is counted, and the result is displayed in an if statement.

Judge.java


Package jankenapp;

public class Judge {
  int judge;
  int win;
  int lose;
  int even;

  public void judgement(int cpuHand, int playerHand) {
    judge = (cpuHand - playerHand + 3 ) % 3;

    switch(judge) {
      case 0:
        System.out.println("This is Aiko!");
        even++;
        break;
      case 1:
        System.out.println("You win!");
        win++;
        break;
      case 2:
        System.out.println("You lose!");
        lose++;
        break;
      default:
    }
    System.out.println("--------------------");
  }

  public void result() {
    System.out.println("[Result of victory or defeat]");
    System.out.println("win:" + win + "Times");
    System.out.println("Losing:" + lose + "Times");
    System.out.println("Aiko:" + even + "Times");
    System.out.println("");
    if (win > lose) {
      System.out.println("This game is yours! Congrats!");
    } else if (win < lose) {
      System.out.println("This game is your loss! Do not mind!");
    } else {
      System.out.println("This game is a draw!");
    }
  }
}

I'm self-taught, so I'm sorry if there are any unsightly parts. I would be grateful if you could give me any advice.

Thank you for browsing to the end.

【reference】 A little application of the rock-paper-scissors algorithm (I referred to the judgment formula.)

Postscript (8/23) I noticed that even if I make a mistake in the input, the default value will give a goo. I will take measures.

Recommended Posts

Rock-paper-scissors app in Java
Rock-paper-scissors in Java
java rock-paper-scissors
Rock-paper-scissors game for beginners in Java
Partization in Java
Changes in Java 11
Pi in Java
FizzBuzz in Java
Create a TODO app in Java 7 Create Header
Try making a calculator app in Java
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Rock-paper-scissors game java practice
Format XML in Java
Java app performance tuning
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
I made a rock-paper-scissors game in Java (CLI)
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Change Java heap size in Tomcat in Azure App Service
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.
Create hyperlinks in Java PowerPoint