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.
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();
}
}
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;
}
}
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;
}
}
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