Please forgive it as it is a self-sufficient memorandum.
When I was studying Java, I found an article about RPG games, so I would like to arrange it in my own way.
Enemy appears → Rock-paper-scissors → Win: Give large damage, Aiko: Give small damage, Loss: Take damage → Defeat It is a flow. Well, if you get stuck, you may change it. Lol
For the time being, let's make a rock-paper-scissors game first.
Janken.java
import java.util.Scanner;
class Janken {
public static void main(String[] args) {
System.out.println("Goo at first! Rock-paper-scissors!");
String[] cpuhands = {"Goo", "Choki", "Par"};
System.out.println("Please select the number you want to issue\n[0]Goo\n[1]Choki\n[2]Par");
Integer number = new Scanner(System.in).nextInt();
System.out.println("Your hand:" +cpuhands[number]);
}
}
Terminal
Goo at first! Rock-paper-scissors!
Please select the number you want to issue
[0]Goo
[1]Choki
[2]Par
1
Your hand: Choki
First of all, you can now select the hand you want to put out.
Janken2.java
class Janken2 {
public static void main(String[] args) {
double d = Math.random();
System.out.println(d);
}
}
Terminal
0.1831838611927178
0.762839659494738
It is said that Math.random (); can generate random numbers from 0.0 to less than 1.0. Also, when dealing with decimal points, use the double type.
Janken3.java
class Janken3 {
public static void main(String[] args) {
double d = Math.random();
System.out.println(d);
int i = (int)(Math.random() * 3);
System.out.println(i);
}
}
Terminal
0.3986020715932931
0
0.9918706705068715
2
0.4195932748905167
1
By multiplying the value of Math.random () by 3 and converting to int type, an integer from 0 to 2 could be generated. By the way, this conversion is called a cast.
Janken4.java
class Janken4 {
public static void main(String[] args) {
System.out.println("Uncle Janken has appeared! !!");
System.out.println("(^_^)v");
System.out.println("Goo at first! Rock-paper-scissors!");
String[] cpuhands = {"Goo", "Choki", "Par"};
double d = Math.random();
int i = (int)(Math.random() * 3);
System.out.println("Uncle" + cpuhands[i] + "Has been issued.");
}
}
Terminal
Uncle Janken has appeared! !!
(^_^)v
Goo at first! Rock-paper-scissors!
The uncle has put out a goo.
Uncle Janken has appeared! !!
(^_^)v
Goo at first! Rock-paper-scissors!
The uncle has put out a par.
Random goo choki par is now available.
Janken5.java
import java.util.Scanner;
class Janken5 {
public static void main(String[] args) {
System.out.println("Uncle Janken has appeared! !!");
System.out.println("(^_^)v");
System.out.println("Goo at first! Rock-paper-scissors!");
String[] cpuhands = {"Goo", "Choki", "Par"};
System.out.println("Please select the number you want to issue.[0]Goo,[1]Choki,[2]Par");
Integer number = new Scanner(System.in).nextInt();
System.out.println("Your hand:" +cpuhands[number]);
double d = Math.random();
int i = (int)(Math.random() * 3);
System.out.println("Uncle[" + cpuhands[i] + "]Has been issued.");
switch (number) {
case 0:
if (i == 0) {
System.out.println("[Aiko]is");
} else if (i == 1) {
System.out.println("[win]Was");
} else if (i == 2) {
System.out.println("[Lose]Was");
}
break;
case 1:
if (i == 0) {
System.out.println("[Lose]Was");
} else if (i == 1) {
System.out.println("[Aiko]is");
} else if (i == 2) {
System.out.println("[win]Was");
}
break;
case 2:
if (i == 0) {
System.out.println("[win]Was");
} else if (i == 1) {
System.out.println("[Lose]Was");
} else if (i == 2) {
System.out.println("[Aiko]is");
}
break;
}
}
}
Terminal
Uncle Janken has appeared! !!
(^_^)v
Goo at first! Rock-paper-scissors!
Please select the number you want to issue.[0]Goo,[1]Choki,[2]Par
2
Your hand: Par
Uncle[Choki]Has been issued.
[Lose]Was
Uncle Janken has appeared! !!
(^_^)v
Goo at first! Rock-paper-scissors!
Please select the number you want to issue.[0]Goo,[1]Choki,[2]Par
0
Your hand: Goo
Uncle[Choki]Has been issued.
[win]Was
You have won or lost.
I would like to end this article here and continue with another article.
http://pbsb.hatenablog.com/entry/2018/10/09/151551 https://note.com/ganga_1/n/na2959e604fed https://qiita.com/dk_masu/items/d7bb7a81cbc6e16fbb49
It was good. Thank you very much.
Recommended Posts