Rock-paper-scissors game for beginners in Java

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.

code

Body

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

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

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

Rock-paper-scissors game for beginners in Java
[For beginners] Run Selenium in Java
Rock-paper-scissors in Java
Rock-paper-scissors game java practice
I made a rock-paper-scissors game in Java (CLI)
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Java for beginners, data hiding
Java application for beginners: stream
[For beginners] Minimum sample to display RecyclerView in Java
[For beginners] Explanation of classes, instances, and statics in Java
Java for beginners, expressions and operators 1
Java for beginners, expressions and operators 2
Rock-paper-scissors game
Age guessing game made in Java
Settings for SSL debugging in Java
[For Java beginners] About exception handling
java rock-paper-scissors
Classes and instances Java for beginners
A rock-paper-scissors game for two players who play against each other in threads with Java
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
First steps for deep learning in Java
Key points for introducing gRPC in Java
Learn Java with "So What" [For beginners]
[Java] for Each and sorted in Lambda
[For beginners] Difference between Java and Kotlin
[For beginners] How to debug in Eclipse
For JAVA learning (2018-03-16-01)
Partization in Java
Changes in Java 11
2017 IDE for Java
I wrote EX25 of AtCoder Programming Guide for beginners (APG4b) in java.
Pi in Java
[Java] [For beginners] How to insert elements directly in a 2D array
Java for statement
FizzBuzz in Java
[For beginners] I tried using DBUnit in Eclipse
ChatWork4j for using the ChatWork API in Java
Technology for reading Java source code in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
Solution for NetBeans 8.2 not working in Java 9 environment
Kantai Collection Java # 1 Classes and Objects [For Beginners]
A collection of simple questions for Java beginners
Set pop-up display for Java language in vim.
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
[Java beginner's anguish] Hard-to-test code implemented in Junit
Compare PDF output in Java for snapshot testing
Enable / disable SNI in Java for each communication
Things to watch out for in Java equals
[java] sort in list
[Java] for statement, while statement
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
[For beginners] DI ~ The basics of DI and DI in Spring ~