It's been about a month since I started studying, so I wrote a rock-paper-scissors game while reviewing. I was able to write a program with only the main method relatively quickly, I had a hard time trying to make something that looked like object-oriented. Since I am self-taught, I think there are many things to do.
Main.java
package janken;
public class Main {
@SuppressWarnings("resource")
public static void main(String[] args) {
final String errorException = "Please enter the correct number.";
final String errorMessage = "Please enter a number from 0 to 2 or 99.";
final String exitMessage = "Finish";
final String menuMessage = "0:Goo 1:Choki 2:Par 99:End";
Player you = new Player();
Player enemy = new Player();
for(;;) {
System.out.println(menuMessage);
try {
you.jankenNum = new java.util.Scanner(System.in).nextInt();
enemy.jankenNum = new java.util.Random().nextInt(3);
if(you.jankenNum == 99) {
//End
System.out.println("");
System.out.println(exitMessage);
System.out.println("");
break;
} else if(you.jankenNum < 0 | you.jankenNum > 2) {
//If an incorrect number is entered ...
System.out.println("");
System.out.println(errorMessage);
System.out.println("");
} else {
//Battle method call.
Battle result = new Battle();
result.battle(you, enemy);
}
} catch(java.util.InputMismatchException error) {
//If a non-numeric value is entered ...
System.out.println("");
System.out.println(errorException);
System.out.println("");
}
}
}
}
Player.java
package janken;
public class Player {
//Holds the number of wins and losses of the player. Defines rock-paper-scissors hands, wins and losses, etc.
int jankenNum;
final String goo = "Goo";
final String choki = "Choki";
final String par = "Par";
int win; int lose; int draw;
String Hand() {
//Convert player numbers into rock-paper-scissors hands.
switch(this.jankenNum) {
case 0:
return this.goo;
case 1:
return this.choki;
case 2:
return this.par;
default:
return null;
}
}
void board(int win, int lose, int draw) {
//Display the number of wins and losses.
System.out.println("Win:" + this.win + " Lose:" + this.lose + " Draw:" + this.draw);
}
}
Battle.java
package janken;
public class Battle {
//Decide the victory or defeat.
final String aiko = "Aiko";
final String kachi = "win";
final String make = "Lose";
void battle(Player pc1, Player pc2) {
//Decide whether to win or lose between players.
int result = (pc1.jankenNum - pc2.jankenNum +3) % 3;
switch(result) {
//Win or lose is decided from each other's hands and stored as a score. View results.
case 0:
System.out.println("");
System.out.println(pc1.Hand() + " VS " + pc2.Hand());
System.out.println(aiko);
pc1.draw += 1; pc2.draw += 1;
pc1.board(pc1.win, pc1.lose, pc1.draw);
System.out.println("");
break;
case 1:
System.out.println("");
System.out.println(pc1.Hand() + " VS " + pc2.Hand());
System.out.println(make);
pc1.lose += 1; pc2.win += 1;
pc1.board(pc1.win, pc1.lose, pc1.draw);
System.out.println("");
break;
case 2:
System.out.println("");
System.out.println(pc1.Hand() + " VS " + pc2.Hand());
System.out.println(kachi);
pc1.win += 1; pc2.lose += 1;
pc1.board(pc1.win, pc1.lose, pc1.draw);
System.out.println("");
break;
}
}
}
I'm satisfied because it worked like that for the time being. It's quite difficult to think about how to divide it.
Recommended Posts