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.
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]
==================================
Main.java
file.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;
}
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.
https://www.youtube.com/watch?v=FsRHdL_r0pE Why you can't do object-oriented development
Recommended Posts