Question: 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

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!