Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

convert the following java program to a java gui with radio buttons for control import java.util.*; public class Main { public int playGame(int playerChoice, int

convert the following java program to a java gui with radio buttons for control

import java.util.*;

public class Main

{

public int playGame(int playerChoice, int computerChoice)

{

int ans;

switch(playerChoice)

{

// if player choice is scissor

case 0 :

// if computer choose Rock

if(computerChoice == 1)

ans = 0;

// if computer choose Paper

else if(computerChoice == 2)

ans = 1;

// if computer choose Scissor

else

ans = -1;

break;

// if player choice is Rock

case 1 :

// if computer choose Paper

if(computerChoice == 2)

ans = 0;

// if computer choose Scissor

else if(computerChoice == 0)

ans = 1;

// if computer choose Rock

else

ans = -1;

break;

// if player choice is Paper

case 2 :

// if computer choose Rock

if(computerChoice == 1)

ans = 1;

// if computer choose Scissor

else if(computerChoice == 0)

ans = 0;

// if computer choose Paper

else

ans = -1;

break;

default : // for invalid choice

ans = -2;

}

return ans;

}

public static void main(String[] args)

{

Main ob = new Main();

int i;

// Create a Scanner object

Scanner sc = new Scanner(System.in);

// create a Rando object

Random rand = new Random();

int playerPoints = 0, computerPoints = 0;

System.out.println("Welcome to the rock paper scissors game! ");

// infinite loop

while(true)

{

// generate random number in range 0 to 2

int computerChoice = rand.nextInt((2 - 0) + 1) + 0;

int playerChoice;

// infinite loop

while(true)

{

System.out.println("Enter either \"rock\", \"paper\", or \"scissors\" to compete");

String pc = sc.next();

if(pc.equals("scissors"))

{

playerChoice = 0;

break;

}

else if(pc.equals("rock"))

{

playerChoice = 1;

break;

}

else if(pc.equals("paper"))

{

playerChoice = 2;

break;

}

else

System.out.println("Error! Please enter a valid choice. ");

}

System.out.print("The computer picked ");

if(computerChoice == 0)

System.out.println("scissor");

else if(computerChoice == 1)

System.out.println("rock");

else

System.out.println("paper");

int result = ob.playGame(playerChoice, computerChoice);

if(result == 1)

{

System.out.println("You Won!");

playerPoints++;

}

else if(result == 0)

{

System.out.println("You Loose!");

computerPoints++;

}

else if(result == -1)

System.out.println("It's a tie!");

else

System.out.println("Error!!");

System.out.println("You have " + playerPoints + " points.");

System.out.println("The computer has " + computerPoints + " points.");

System.out.println("Press enter to continue or enter \"quit\" to quit");

String ch = sc.nextLine();

ch = sc.nextLine();

if(ch.equals("quit"))

break;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago