[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.

Introduction

In 2018, I touched various languages halfway and felt emptiness that I could understand what I wanted to do but could not make something that works by myself, so I focused on java / kotlin and studying. ..

I tried my best to understand object orientation, but it's subtle. ..

By the way, it's a spaghetti code.

What I made

As with ordinary rock-paper-scissors, the one with the most points in five games wins.

The disadvantage is that it is inconvenient because you do not know what the PC has released. ..

**I feel like this. ** **

console.


Give your Player a name
Beginner
Fight as a beginner
[Starting rock-paper-scissors]

[1st round]
===============
stone    :   0
scissors :   1
paper    :   2
===============
Please enter here
1
----------------
Choki (beginner) vs Goo
----------------
Beginner lost
:
:
:
:
[5th round]
===============
stone    :   0
scissors :   1
paper    :   2
===============
Please enter here
2
----------------
Par(Beginner)  vs Par
----------------
It's a draw

[End of rock-paper-scissors]
==================================
Result: [Draw]
==================================

What I was conscious of (my own object-oriented programming)

  1. First, write all the processing in the Main.java file.
  2. The parts that can be made into parts are put together into a method. (It is better to reduce the number in Main as much as possible)
  3. Where you can divide the class, divide it into classes

Main.java



import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

import org.ietf.jgss.Oid;
import org.omg.CORBA.PUBLIC_MEMBER;

public class Main implements jankeninterface{

	
	public static void main(String[] args) throws IOException {
		
		//Player settings
		Player p = new Player();
	    
		//Rock-paper-scissors processing
		Player.jankenbuttle();
	
		//Result display
		result();


	}
	
	

	public static void result() {
		System.out.println("");
		System.out.println("[End of rock-paper-scissors]");
		System.out.println("==================================");
		if(Player.pcWinCount > Player.playerWinCount) {
			System.out.println("Result: [PC has won]");
		}else if(Player.pcWinCount < Player.playerWinCount) {
			System.out.println("result:【"+Player.playername+"Has won]");
		}else {
			System.out.println("Result: [Draw]");
		}
		System.out.println("==================================");
	}
}

Player.java



import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class Player {
	public static int playerWinCount = 0;
	public static String playername;
	public static int pcWinCount = 0;
	
	public Player() {
		// TODO Auto-generated constructor stub
		createName();
	}
	
	public static void createName() {
		System.out.println("Give your Player a name");
		Scanner name = new Scanner(System.in);
	    playername = name.nextLine();
	    
	    System.out.println(playername+"Fight as");
	}
	
	public static void jankenbuttle() throws IOException {
		System.out.println("[Starting rock-paper-scissors]");

//Start rock-paper-scissors
		for(int i = 1;i <= 5; i++) {
		System.out.println("");	
		System.out.println("【"+i+"Round]");
		
		System.out.println("===============");
		System.out.println("stone    :   0");
		System.out.println("scissors :   1");
		System.out.println("paper    :   2");
		System.out.println("===============");
		
//Rock-paper-scissors hand of PC
		Random r = new Random();
		int pcNum = r.nextInt(3);
		
		System.out.println("Please enter here");
		
//player's rock-paper-scissors hand
		Scanner s = new Scanner(System.in);
		int playerNum = s.nextInt();
		
//If the player wins
		if(pcNum == 0 && playerNum == 2 || 
		   pcNum == 1 && playerNum == 0 || 
		   pcNum == 2 && playerNum == 1 ) {
			playerWinCount++;
			if(pcNum == 0) {
				System.out.println("");
				System.out.println("Par("+playername+"Mr.)vs goo");
			}else if(pcNum == 1) {
				System.out.println("");
				System.out.println("Goo("+playername+"Mr.)vs choki");
			}else {
				System.out.println("");
				System.out.println("Choki("+playername+"Mr.)vs park");
			}
			System.out.println(playername+"Won");
				
			
//When player loses
		}else if(pcNum == 0 && playerNum == 1||
				 pcNum == 1 && playerNum == 2||
				 pcNum == 2 && playerNum == 0) {
			if(pcNum == 0) {
				System.out.println("----------------");
				System.out.println("Choki ("+playername+"San) vs Goo");
				System.out.println("----------------");
			}else if(pcNum == 1) {
				System.out.println("----------------");
				System.out.println("Par("+playername+"Mr.)vs choki");
				System.out.println("----------------");
			}else {
				System.out.println("----------------");
				System.out.println("Goo("+playername+"Mr.)vs park");
				System.out.println("----------------");
			}
			pcWinCount++;
			System.out.println(playername+"Lost");
		}else {
			if(pcNum == 0) {
				System.out.println("----------------");
				System.out.println("Goo("+playername+"Mr.)  vs Goo");
				System.out.println("----------------");
			}else if(pcNum == 1) {
				System.out.println("----------------");
				System.out.println("Choki("+playername+"Mr.)  vs Choki");
				System.out.println("----------------");
			}else {
				System.out.println("----------------");
				System.out.println("Par("+playername+"Mr.)  vs Par");
				System.out.println("----------------");
			}
			System.out.println("It's a draw");
			
		}
		
		
		}
	}
	
}

javainterface.java


public interface jankeninterface {
	public static final int stone = 0;
	public static final int scissors = 1;
	public static final int paper = 2;
}

Ingenuity

The point that you can set the player name every time the rock-paper-scissors starts

This is the part.

console.


Give your Player a name
->Beginner
Fight as a beginner

I tried to write a name in the argument of the construct, but I could give it a name, but it wouldn't change unless I wrote it in the source code, so I tried my best.

I have to work harder.

reference

https://www.youtube.com/watch?v=FsRHdL_r0pE Why you can't do object-oriented development

Recommended Posts

[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
[Java] I tried to make a maze by the digging method ♪
I tried to display the calendar on the Eclipse console using Java.
I tried learning Java with a series that beginners can understand clearly
I tried to make a login function in Java
[Small story] I tried to make the java ArrayList a little more convenient
I tried to make a program that searches for the target class from the process that is overloaded with Java
I tried to make a client of RESAS-API in Java
I tried to make a Web API that connects to DB with Quarkus
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I tried to make a talk application in Java using AI "A3RT"
I tried to make Basic authentication with Java
java I tried to break a simple block
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I made a rock-paper-scissors game in Java (CLI)
I tried to set tomcat to run the Servlet.
I tried to break a block with java (1)
I tried running Java on a Mac terminal
I want to play a GIF image on the Andorid app (Java, Kotlin)
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
I tried to decorate the simple calendar a little
I tried using Log4j2 on a Java EE server
I tried to make FizzBuzz that is uselessly flexible
I tried to implement the Euclidean algorithm in Java
[Ruby] I want to make a program that displays today's day of the week!
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
The story that did not disappear when I tried to delete mysql on ubuntu
A story I was addicted to when getting a key that was automatically tried on MyBatis
I tried to make a product price comparison tool of Amazon around the world with Java, Amazon Product Advertising API, Currency API (2017/01/29)
I tried to create a java8 development environment with Chocolatey
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I tried adding a separator line to TabLayout on Android
I tried to modernize a Java EE application with OpenShift.
[JDBC] I tried to access the SQLite3 database from Java.
I tried to summarize the basics of kotlin and java
[Beginner] Try to make a simple RPG game with Java ①
Links that I referred to before running Elasticsearch 7.7 on Java 11
Four-in-a-row with gravity that can be played on the console
I want to make a list with kotlin and java!
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
I just wanted to make a Reactive Property in Java
I want to make a function with kotlin and java!
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried to make Java Optional and guard clause coexist
I have summarized the articles that programming beginners referred to.
I tried to convert a string to a LocalDate type in Java
I tried a puzzle that can only be solved by the bottom 10% of bad engineers
Input to the Java console
How to make a key pair of ecdsa in a format that can be read by Java
How to deal with the type that I thought about writing a Java program for 2 years
I made the "Sunshine Ikezaki game" I saw on Twitter in Java.
Mechanism for converting to a language that the browser can recognize
I tried to summarize the words that I often see in docker-compose.yml
I want to use swipeback on a screen that uses XLPagerTabStrip
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
I tried to create a Spring MVC development environment on Mac
[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
Logic to draw a circle with ASCII art on the console
I tried to translate the error message when executing Eclipse (Java)
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to make an Android application with MVC now (Java)