While writing a rock-paper-scissors RPG in Java, writing `` `System.out.println``` every time was quite troublesome, so I tried to find out how to make it easier.
I think it will be easier with extensions, but this time I will try from a different direction.
** The general flow is **
--Create a method for console output
--Replace the previously described System.out.println
with the method name.
In the commentary, I will try to adapt to only one of the three classes.
Code so far
Judge.java
public class Judge {
public void startJanken(Player player1, Player player2) {
System.out.println("[Start rock-paper-scissors]\n");
for (int cnt = 0;cnt < 3; cnt++) {
System.out.println("["+(cnt+1)+"Round]");
//Determine which one won
Player winner = judgeJanken(player1,player2);
if(winner != null) {
System.out.println("\n"+winner.getName()+"Won");
winner.notifyResult(true);
}else {
System.out.println("It's a draw");
}
}
System.out.println("Rock-paper-scissors finished");
Player finalWinner = judgeFinalWinner(player1,player2);
System.out.print(player1.getWinCount()+"versus"+player2.getWinCount()+"so");
if (finalWinner != null) {
System.out.println(finalWinner.getName()+"Is the winner");
}else {
System.out.println("It's a draw");
}
}
private Player judgeJanken(Player player1,Player player2) {
Player winner = null;
int player1hand = player1.showHand();
int player2hand = player2.showHand();
printHand(player1hand);
System.out.print("vs.");
printHand(player2hand);
System.out.print("\n");
if((player1hand == Player.stone && player2hand == Player.scissors)||
(player1hand == Player.scissors && player2hand == Player.paper)||
(player1hand == Player.paper && player2hand == Player.stone)) {
winner = player1;
}else if((player1hand == Player.stone && player2hand == Player.paper)||
(player1hand == Player.scissors && player2hand == Player.stone)||
(player1hand == Player.paper && player2hand == Player.scissors)) {
winner = player2;
}
return winner;
}
private Player judgeFinalWinner(Player player1,Player player2) {
Player winner = null;
int player1WinCount = player1.getWinCount();
int player2WinCount = player2.getWinCount();
if(player1WinCount > player2WinCount) {
winner = player1;
}else if(player1WinCount < player2WinCount) {
winner = player2;
}
return winner;
}
private void printHand(int hand) {
switch(hand) {
case Player.stone:
System.out.print("Goo");
break;
case Player.scissors:
System.out.print("Choki");
break;
case Player.paper:
System.out.print("Par");
break;
default:
break;
}
}
}
Try adding a method like the one below
//here! !! !!
public static void put(String str) {
System.out.println(str);
}
Then, instead of typing System.out.println
, I could only do
put```
Judge.java
public class Judge {
public void startJanken(Player player1, Player player2) {
put("[Start rock-paper-scissors]\n");
for (int cnt = 0;cnt < 3; cnt++) {
put("["+(cnt+1)+"Round]");
//Determine which one won
Player winner = judgeJanken(player1,player2);
if(winner != null) {
put("\n"+winner.getName()+"Won");
winner.notifyResult(true);
}else {
put("It's a draw");
}
}
put("Rock-paper-scissors finished");
Player finalWinner = judgeFinalWinner(player1,player2);
System.out.print(player1.getWinCount()+"versus"+player2.getWinCount()+"so");
if (finalWinner != null) {
put(finalWinner.getName()+"Is the winner");
}else {
put("It's a draw");
}
}
private Player judgeJanken(Player player1,Player player2) {
Player winner = null;
int player1hand = player1.showHand();
int player2hand = player2.showHand();
printHand(player1hand);
System.out.print("vs.");
printHand(player2hand);
System.out.print("\n");
if((player1hand == Player.stone && player2hand == Player.scissors)||
(player1hand == Player.scissors && player2hand == Player.paper)||
(player1hand == Player.paper && player2hand == Player.stone)) {
winner = player1;
}else if((player1hand == Player.stone && player2hand == Player.paper)||
(player1hand == Player.scissors && player2hand == Player.stone)||
(player1hand == Player.paper && player2hand == Player.scissors)) {
winner = player2;
}
return winner;
}
private Player judgeFinalWinner(Player player1,Player player2) {
Player winner = null;
int player1WinCount = player1.getWinCount();
int player2WinCount = player2.getWinCount();
if(player1WinCount > player2WinCount) {
winner = player1;
}else if(player1WinCount < player2WinCount) {
winner = player2;
}
return winner;
}
private void printHand(int hand) {
switch(hand) {
case Player.stone:
System.out.print("Goo");
break;
case Player.scissors:
System.out.print("Choki");
break;
case Player.paper:
System.out.print("Par");
break;
default:
break;
}
}
//here! !! !!
public static void put(String str) {
System.out.println(str);
}
}
It's a method, so it may be a little troublesome when crossing classes, but it's a lot easier lol
In my case, I knew it on the way, so I had to rewrite everything I had written so far to `` `put```.
I was just studying Linux, so try using the `sed command`
~$ sed -e s/Character before replacement/Character after replacement/g [file name]
―― “-e” may or may not be present --If g is not entered, replace only the first character
There are various things such as, but I will omit them here.
Now, replace it at the terminal!
tarminal.
~$ sed -e s/System.out.println/put/g Judge.java
public class Judge {
public void startJanken(Player player1, Player player2) {
put("[Start rock-paper-scissors]\n");
for (int cnt = 0;cnt < 3; cnt++) {
put("["+(cnt+1)+"Round]");
//Determine which one won
Player winner = judgeJanken(player1,player2);
if(winner != null) {
put("\n"+winner.getName()+"Won");
winner.notifyResult(true);
}else {
put("It's a draw");
}
}
put("Rock-paper-scissors finished");
Player finalWinner = judgeFinalWinner(player1,player2);
:
:
:
:
public static void put(String str) {
put(str);
}
}
Check the file
tarminal.
~$ cat Judge.java
public class Judge {
public void startJanken(Player player1, Player player2) {
System.out.println("[Start rock-paper-scissors]\n");
for (int cnt = 0;cnt < 3; cnt++) {
System.out.println("["+(cnt+1)+"Round]");
//Determine which one won
Player winner = judgeJanken(player1,player2);
if(winner != null) {
System.out.println("\n"+winner.getName()+"Won");
winner.notifyResult(true);
}else {
System.out.println("It's a draw");
}
}
System.out.println("Rock-paper-scissors finished");
Player finalWinner = judgeFinalWinner(player1,player2);
:
:
:
:
public static void put(String str) {
System.out.println(str);
}
}
I thought that the replacement was completed here, but when I checked it with an editor, it was not replaced. ..
When I looked it up here, the sed command was only seen in the preview and was not actually reflected.
I want you to actually reflect it, so if you look it up Optional `` `-i``` (reflected directly in the file)
So I will try again
~$ sed -i -e s/System.out.println/put/g Judge.java
#### **`tarminal.`**
~$ cat Judge.java public class Judge {
public void startJanken(Player player1, Player player2) {
put("[Start rock-paper-scissors]\n");
for (int cnt = 0;cnt < 3; cnt++) {
put("["+(cnt+1)+"Round]");
//Determine which one won
Player winner = judgeJanken(player1,player2);
if(winner != null) {
put("\n"+winner.getName()+"Won");
winner.notifyResult(true);
}else {
put("It's a draw");
}
}
put("Rock-paper-scissors finished");
Player finalWinner = judgeFinalWinner(player1,player2);
:
:
:
:
public static void put(String str) {
put(str);
}
}
It is reflected firmly.
After that, the method has also been replaced, so I wonder if I should fix it.
### result
It became easier (^ ∀ ^)
Recommended Posts