0% found this document useful (0 votes)
13 views2 pages

Javamp

This Java applet implements a simple Rock Paper Scissors game against a computer opponent. The applet initializes text fields, buttons, and areas to accept a password, start/stop the game, and display output. A game thread runs every 5 seconds to play a computer move by randomly selecting Rock, Paper, or Scissors and determining a winner against the player's random selection. The player and computer scores are tracked and displayed after each round.

Uploaded by

Irfan MBaig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Javamp

This Java applet implements a simple Rock Paper Scissors game against a computer opponent. The applet initializes text fields, buttons, and areas to accept a password, start/stop the game, and display output. A game thread runs every 5 seconds to play a computer move by randomly selecting Rock, Paper, or Scissors and determining a winner against the player's random selection. The player and computer scores are tracked and displayed after each round.

Uploaded by

Irfan MBaig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.applet.

*;
import java.awt.*;
import java.awt.event.*;

// Applet for Rock Paper Scissors Game


public class RockPaperScissorsApplet extends Applet implements ActionListener {
private TextField inputField;
private Button submitButton;
private TextArea outputArea;

private String[] options = {"Rock", "Paper", "Scissors"};


private int playerScore = 0;
private int computerScore = 0;

private boolean gameRunning = false;


private boolean passwordEntered = false;

private Thread gameThread;

// Initialize the applet


public void init() {
Label passwordLabel = new Label("Enter Password: ");
inputField = new TextField(20);
submitButton = new Button("Submit");
outputArea = new TextArea(10, 30);

add(passwordLabel);
add(inputField);
add(submitButton);
add(outputArea);

submitButton.addActionListener(this);

gameThread = new Thread(() -> {


while (gameRunning) {
try {
Thread.sleep(5000); // Game timer: 5 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
if (gameRunning) {
playComputerMove();
}
}
});
}

// Action performed when buttons are clicked


public void actionPerformed(ActionEvent e) {
if (!passwordEntered) {
String password = inputField.getText();
if (password.equals("yourpassword")) { // Set your desired password
here
passwordEntered = true;
inputField.setText("");
inputField.setEnabled(false);
submitButton.setLabel("Start Game");
} else {
outputArea.append("Invalid password!\n");
}
} else if (!gameRunning) {
outputArea.setText("");
outputArea.append("Game started!\n");
gameRunning = true;
gameThread.start();
submitButton.setLabel("Stop Game");
} else {
gameRunning = false;
gameThread.interrupt();
outputArea.append("Game stopped!\n");
submitButton.setLabel("Start Game");
}
}

// Play a move for the computer


private void playComputerMove() {
int computerMoveIndex = (int) (Math.random() * options.length);
String computerMove = options[computerMoveIndex];

outputArea.append("Computer chooses: " + computerMove + "\n");

// Choose a winner
String playerMove = options[(int) (Math.random() * options.length)];
outputArea.append("Player chooses: " + playerMove + "\n");

if (playerMove.equals(computerMove)) {
outputArea.append("It's a tie!\n");
} else if ((playerMove.equals("Rock") && computerMove.equals("Scissors"))
||
(playerMove.equals("Scissors") && computerMove.equals("Paper")) ||
(playerMove.equals("Paper") && computerMove.equals("Rock"))) {
outputArea.append("Player wins!\n");
playerScore++;
} else {
outputArea.append("Computer wins!\n");
computerScore++;
}

outputArea.append("Player Score: " + playerScore + ", Computer Score: " +


computerScore + "\n");
}
}

You might also like