[Beginner] Try to make a simple RPG game with Java ①

Please forgive it as it is a self-sufficient memorandum.

Try making an RPG game in Java

When I was studying Java, I found an article about RPG games, so I would like to arrange it in my own way.

Concept

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

Practice

Creating a rock-paper-scissors game

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.

Learn about random

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.

Allows you to generate integers from 0 to 2

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.

Randomly put out goo choki par

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.

Let's win or lose

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.

Reference 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

[Beginner] Try to make a simple RPG game with Java ①
Try to make a simple callback
Java beginner tried to make a simple web application using Spring Boot
[Beginner] Create a competitive game with basic Java knowledge
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
How to make a Java container
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Try to make a peepable iterator
How to make a Java array
Make a typing game with ruby
Try to make a cross-platform application with JRuby (jar file generation)
Interface Try to make Java problem TypeScript 7-3
How to make a Java calendar Summary
How to make a Discord bot (Java)
[docker] [nginx] Make a simple ALB with nginx
How to make an app with a plugin mechanism [C # and Java]
I tried to make Basic authentication with Java
java I tried to break a simple block
I did Java to make (a == 1 && a == 2 && a == 3) always true
Try to create a bulletin board in Java
Try to link Ruby and Java with Dapr
Increment behavior Try to make Java problem TypeScript 3-4
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
String operation Try to make Java problem TypeScript 9-3
Try to make a music player using Basic Player
Try to implement TCP / IP + NIO with JAVA
A simple rock-paper-scissors game with JavaFX and SceneBuilder
Easy to make LINE BOT with Java Servlet
Try debugging a Java program with VS Code
I tried to break a block with java (1)
I made a simple calculation problem game in Java
How to make a factory with a model with polymorphic association
Submit a job to AWS Batch with Java (Eclipse)
Initialization of for Try to make Java problem TypeScript 5-4
[Java basics] Let's make a triangle with a for statement
[Personal memo] Make a simple deep copy in Java
Let's make a robot! "A simple demo of Java AWT Robot"
I tried to make a login function in Java
Try connecting to AzureCosmosDB Emulator for Docker with Java
Try to solve a restricted FizzBuzz problem in Java
Try to build a Java development environment using Docker
Try building Java into a native module with GraalVM
I want to get along with Map [Java beginner]
I used to make nc (netcat) with JAVA normally
[Java] How to start a new line with StringBuilder
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
I tried to create a java8 development environment with Chocolatey
Java program to resize a photo into a square with margins
I tried to modernize a Java EE application with OpenShift.
Java to play with Function
Java --How to make JTable
Try DB connection with Java
How to deploy a simple Java Servlet app on Heroku
Try gRPC with Java, Maven
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ① ~ Hello World ~
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Let's make a simple API with EC2 + RDS + Spring boot ①