A rock-paper-scissors game for two players who play against each other in threads with Java

【Overview】

Create 2 threads, play rock-paper-scissors between the threads, and display the result. It is uploaded on GitHub → threadJankenGame

【code】

JankenCallable.java


package game;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import game.Janken.Hand;

public class JankenCallable implements Callable<Janken.Hand> {

	public static int playerNumber = 1;
	public final static Object lock = new Object();

	@Override
	public Hand call() throws Exception {
		TimeUnit.SECONDS.sleep(1);

		synchronized (lock) {
			playerNumber++;

			if (playerNumber % 2 == 0) {
				System.out.println("First name:" + Thread.currentThread().getName());
			} else {
				System.out.println("Second name:" + Thread.currentThread().getName());
			}
		}

		return Janken.getHand();
	}
}

Janken.java


package game;

public class Janken {
	public enum Hand {
		GUU("Goo"), CHOKI("Choki"), PAA("Par");

		private final String hand;

		private Hand(String hand) {
			this.hand = hand;
		}

		public String getHand() {
			return hand;
		}
	}

	/**
	 *Display the result of rock-paper-scissors
	 *
	 * @param firstPlayerHand
	 *The first hand
	 * @param secondPlayerHand
	 *The second hand
	 */
	public static void getGameResult(Hand firstPlayerHand, Hand secondPlayerHand) {
		//Temporarily place the winner of rock-paper-scissors as the first person
		Hand winner = firstPlayerHand;

		if (firstPlayerHand == Hand.GUU && secondPlayerHand == Hand.PAA) {
			winner = secondPlayerHand;
		} else if (firstPlayerHand == Hand.CHOKI && secondPlayerHand == Hand.GUU) {
			winner = secondPlayerHand;
		} else if (firstPlayerHand == Hand.PAA && secondPlayerHand == Hand.CHOKI) {
			winner = secondPlayerHand;
		}

		System.out.println(getPlayerHand(firstPlayerHand, secondPlayerHand));
		if (firstPlayerHand == secondPlayerHand) {
			System.out.println("This is Aiko.");
		} else if (winner == firstPlayerHand) {
			System.out.println("The first person wins.");
		} else if (winner == secondPlayerHand) {
			System.out.println("The second person wins.");
		}
		System.out.println("");
	}

	/**
	 *Randomly get a rock-paper-scissors hand
	 * @return rock-paper-scissors hand
	 */
	public static Hand getHand() {
		double random = Math.random() * 10;
		if (6.6 < random) {
			return Hand.GUU;
		} else if (3.3 < random) {
			return Hand.CHOKI;
		}
		return Hand.PAA;
	}

	/**
	 *Show the hand that the player put out
	 *
	 * @param firstPlayerHand
	 *The first hand
	 * @param secondPlayerHand
	 *The second hand
	 * @return How to put out the first and second hands
	 */
	private static String getPlayerHand(Hand firstPlayerHand, Hand secondPlayerHand) {
		return "The first hand is" + String.format("%s", firstPlayerHand.getHand()) + ": The second hand"
				+ String.format("%s", secondPlayerHand.getHand());
	}

}

Main.java


package game;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import game.Janken.Hand;

public class Main {

	//Number of thread pools for a rock-paper-scissors game exclusively for 2 players"2"Will be limited
	private static final int THREAD_POOL_COUNT = 2;
	//Number of rock-paper-scissors games
	private static final int GAME_COUNT = 3;

	public static void main(String[] args) {
		ExecutorService executorService = Executors.newScheduledThreadPool(THREAD_POOL_COUNT);

		List<Future<Hand>> jankenList = new ArrayList<>();
		for (int i = 0; i < GAME_COUNT * 2; i++) {
			Future<Hand> future = executorService.submit(new JankenCallable());
			jankenList.add(future);
		}

		int count = 0;
		Hand firstPlayerHand = null;
		Hand currentPlayerHand = null;
		for (Future<Hand> hand : jankenList) {
			count++;

			try {
				currentPlayerHand = hand.get();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			} finally {
				executorService.shutdown();
			}

			//Display the result when the first and second hands are decided
			if (count % THREAD_POOL_COUNT == 0) {
				Janken.getGameResult(firstPlayerHand, currentPlayerHand);
			} else {
				firstPlayerHand = currentPlayerHand;
			}
		}
	}
}

[Execution example]

First name: pool-1-thread-1
Second name: pool-1-thread-2
The first hand is goo: The second hand is goo
This is Aiko.

First name: pool-1-thread-1
Second name: pool-1-thread-2
The first hand is goo: the second hand is par
The second person wins.

First name: pool-1-thread-2
Second name: pool-1-thread-1
The first hand is par: the second hand is goo
The first person wins.

【reference】

Multi-thread processing in Java using ExecutorService If the StaticObject used for the synchronized block changes, it is not effectively synchronized

[Problems / improvements]

・ Rock-paper-scissors games cannot be played by 3 or more players (3 threads). -I wanted to align the colon (:) in the result.

[Bonus corner]

Thread processing is difficult.

Recommended Posts

A rock-paper-scissors game for two players who play against each other in threads with Java
Rock-paper-scissors game for beginners in Java
I made a rock-paper-scissors game in Java (CLI)
Play with Markdown in Java flexmark-java
[Java] for Each and sorted in Lambda
Game development with two people using java 2
Game development with two people using java 1
Split a string with ". (Dot)" in Java
Game development with two people using java 3
For my son who started studying Java with "Introduction to Java" in one hand
Two ways to start a thread in Java + @
Read a string in a PDF file with Java
Create a CSR with extended information in Java
A note for those who live with JMockit
A simple rock-paper-scissors game with JavaFX and SceneBuilder
Enable / disable SNI in Java for each communication