I tried to make a rock-paper-scissors program somehow.
janken.java
import java.util.Random;
public class janken {
//Goo Choki Paa constant definition
int GU = 0;
int CHOKI = 1;
int PA = 2;
//player;
int player1;
int player2;
//hand
String te1;
String te2;
//Random numbers determine the move of player 1
public void player1() {
Random random = new Random();
int hand1 = random.nextInt(3);
switch (hand1) {
case 0:
player1 = GU;
te1 = "Goo";
break;
case 1:
player1 = CHOKI;
te1 = "Choki";
break;
case 2:
player1 = PA;
te1 = "Par";
break;
}
}
//Random numbers determine the move of player 2
public void player2() {
Random random = new Random();
int hand2 = random.nextInt(3);
switch (hand2) {
case 0:
player2 = GU;
te2 = "Goo";
break;
case 1:
player2 = CHOKI;
te2 = "Choki";
break;
case 2:
player2 = PA;
te2 = "Par";
break;
}
}
public static void main(String[] args) {
janken janken = new janken();
janken.player1();
janken.player2();
System.out.println("Play rock-paper-scissors with player1 and player2.");
System.out.println("hand of player1: " + janken.te1);
System.out.println("hand of player2: " + janken.te2);
//Judgment
if (janken.player1 == 0 && janken.player2 == 1) {
System.out.println("Preiya-1 Kachi");
} else if (janken.player1 == 1 && janken.player2 == 2) {
System.out.println("Preiya-1 Kachi");
} else if (janken.player1 == 2 && janken.player2 == 0) {
System.out.println("Preiya-1 Kachi");
} else if (janken.player1 == janken.player2) {
System.out.println("Divide");
} else {
System.out.println("Preyya 2 Kachi");
}
}
}
Recommended Posts