Question: I have a Rock, Paper Scissors program that I have written in Java using threads and am looking to adapt this into an applet but

I have a Rock, Paper Scissors program that I have written in Java using threads and am looking to adapt this into an applet but have had no luck with my search on how to do so. I simply need to create an applet that displays a bar chart that updates as wins and draws occur in addition to the command line output that my program uses to print the summary statement. Here is the code I have, any help would be greatly appreciated

import javax.swing.*;

import java.util.Scanner;

public class Game { public static final int rounds = 1000;

public static void main (String[] args) {

//User inputs number here Scanner reader = new Scanner(System.in); // Reading from System.in

System.out.println("Enter the number of players: ");

int numPlayers = reader.nextInt(); // Scans the next token of the input as an int.

while (numPlayers < 2 || numPlayers > 10) {

System.out.println("Oops... Enter a valid number between 2 and 10");

numPlayers = reader.nextInt(); }

Barrier go = new Barrierimp(numPlayers);

Player[] p; p = new Player[numPlayers]; Thread Players[] = new Thread[numPlayers];

int rockWins = 0, paperWins = 0, scissorsWins = 0;

for(int i = 0; i < numPlayers; i++) { p[i] = new Player(go, i, p, numPlayers, rounds);

Players[i] = new Thread(p[i]); Players[i].start(); }

for(int j = 0; j < numPlayers; j++) { try { Players[j].join(); } catch (InterruptedException e) {}; }

for(int j = 0; j < numPlayers; j++) { scissorsWins += p[j].scissorsWins();

paperWins += p[j].paperWins(); rockWins += p[j].rockWins(); }

System.out.println("Summary Statistics:");

System.out.println("Number of times scissors won: " + scissorsWins);

System.out.println("Number of times rock won: " + rockWins);

System.out.println("Number of times paper won: " + paperWins);

System.out.println("Number of draws: " + (rounds - (scissorsWins + rockWins + paperWins))); }

import java.util.Random;

class Player extends Thread {

private Barrier go;

private int playernum;

private int selection;

private Player Players[];

private int numPlayers;

private int Totalrounds; //player victory conditions

private boolean win;

private boolean draw;

private boolean loss;

private int rockWins = 0, paperWins = 0, scissorsWins = 0;

public Player(Barrier B, int value, Player P[], int nP, int r) {

go = B; playernum = value+1; Players = P; numPlayers = nP; Totalrounds = r; } //returns current player selection public int selection() { return selection; } //returns total wins public int rockWins() { return rockWins; } //returns total loss

public int paperWins() { return paperWins; } //returns total loss

public int scissorsWins() { return scissorsWins; } //Decides on end conditions for winning, drawing or losing

private void DidIwin() {

int count = 0; while(count < numPlayers) {

if(count+1 != playernum) { int enemypick; enemypick = Players[count].selection();

if(selection == 0) { if(enemypick == 0) draw = true;

if(enemypick == 1) loss = true;

if(enemypick == 2 && draw == false && loss == false) win = true; }

if(selection == 1) { if(enemypick == 1) draw = true; if(enemypick == 2) loss = true;

if(enemypick == 0 && draw == false && loss == false) win = true; }

if(selection == 2) { if(enemypick == 2) draw = true;

if(enemypick == 0) loss = true;

if(enemypick == 1 && draw == false && loss == false) win = true; } } //a catch for all players if they //have a draw or loss they cannot win

if(draw == true || loss == true) win = false; count++; }

if (win) { if (selection == 0) rockWins++;

if (selection == 1) paperWins++; if (selection == 2) scissorsWins++; } } //Player will make a selection then wait for other players //next they check to see how they did, then wait //finally they report on how they did compared to others.

public void run() {

int round = 1; while(round <= Totalrounds) { Random rand = new Random(); //A random number between 0 and 2

selection = rand.nextInt(3);

//rock = 0

if(selection == 0) System.out.println ("Round " + round + ": Player " + playernum + " has selected Rock");

//paper = 1 else if(selection == 1)

System.out.println ("Round " + round + ": Player " + playernum + " has selected Paper");

//scissors = 2 else if(selection == 2)

System.out.println ("Round " + round + ": Player " + playernum + " has selected Sicossors");

go.waitForOthers();

win = false;

draw = false;

loss = false; //Selection of winning status

DidIwin();

go.waitForOthers();

//Still needs work to handle multiple players

if(win == true)

System.out.println ("I won: " + playernum);

else if(draw == true)

System.out.println ("I draw: " + playernum);

else if(loss == true)

System.out.println ("I loss: " + playernum); round++; } } }

import java.util.Random;

import java.util.Scanner;

interface Barrier {

public void waitForOthers();

public void freeAll();

}

class Barrierimp implements Barrier {

private int barriersize;

private int waitingT;

private int save;

public Barrierimp(int thread) { waitingT = 0; barriersize = thread; save = 0; } //has the threads wait until all threads finish //and then resets for the next run //not sure if perfectly sycronized, but so far it works.

public synchronized void waitForOthers() {

waitingT++;

if(waitingT < barriersize) {

try { wait(); }

catch (InterruptedException e) {} }

else { waitingT = 0; notifyAll(); } } //currently used for nothing public void freeAll() { waitingT = barriersize; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!