Question

In: Computer Science

JAVA PROGRAM Without changing changing main or PlayOneGame, you need to write the rest of the...

JAVA PROGRAM

Without changing changing main or PlayOneGame, you need to write the rest of the methods. Please complete "Rock, Scissor, Paper" game.

Main:

public static void main(String[] args )
{
   int wins = 0;
   int losses = 0;
   PrintWelcomeMessage();            // Just says hello and explains the rules
   while( PlayerWantsToPlay() )      // Asks the player if they want to play, and returns true or false
   {
      boolean outcome = PlayOneGame();  // One full game.  We do NOT come back here on a tie.  The game is not over on a tie.  
      if( outcome == true )
         wins++;
      else
         losses++;
   }
   PrintGoodByeMessage( wins, losses ); // Prints the outcome of the games
}

Playonegame:

static boolean PlayOneGame()
{
   int AI; int player; int outcome;
   do
   {
      AI = AIPick(); // 0,1,2
      player = PlayerPick() // 0,1,2
      outcome = ComputeWinner(AI, Player)// 0,1,2
   } while( outcome == 0 );// while tied

   if( outcome == 1 )
      return true; // they won
   else
      return false;// they lost
}

Solutions

Expert Solution

Included all the other functions that are to be implemented. Simply Paste this code in your Class and run.

static int AIPick()

{

Random random = new Random();

return random.nextInt(3);

}

static int PlayerPick()

{

Scanner scanner = new Scanner(System.in);

int playerChoice;

System.out.print("Please enter your choice, 0 ==> Rock, 1 ==> Paper, 2 ==> Scissors: ");

playerChoice = scanner.nextInt();

while (playerChoice < 0 || playerChoice > 2)

{

System.out.println("Please enter a choice among 0, 1, 2");

System.out.print("Please enter your choice, 0 ==> Rock, 1 ==> Paper, 2 ==> Scissors: ");

playerChoice = scanner.nextInt();

}

return playerChoice;

}

static int ComputeWinner(int AIChoice, int playerChoice)

{

if (AIChoice == playerChoice) // Tie

{

System.out.println("Game tied");

return 0;

}

if (AIChoice < playerChoice)

{

if (AIChoice == 0 && playerChoice == 2) // Rock wins over Scissors

{

// System.out.println("Computer Won");

return -1;

}

// Paper wins over Rock and Scissors wins over Paper

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

return 1;

}

// AIChoice greater than playerChoice

if (AIChoice == 2 && playerChoice == 0) // Rock wins over Scissors

{

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

return 1;

}

// Paper wins over Rock and Scissors wins over Paper

// System.out.println("Computer Won");

return -1;

}

static void PrintWelcomeMessage()

{

System.out.println("Hi there!");

System.out.println("Please enter your choice when you are prompted to pick");

System.out.println("Rock beats Scissors\nPaper beats Rock\nScissors beat Paper");

}

static void PrintGoodByeMessage(int wins, int losses)

{

System.out.println("You won "+ wins + " games");

System.out.println("Computer won "+ losses + " games");

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

}

static boolean PlayerWantsToPlay()

{

int wantToPlay;

Scanner sc = new Scanner(System.in);

System.out.print("Would you like to play ? Yes(1)/No(0): ");

wantToPlay = sc.nextInt();

return wantToPlay > 0;

}


Related Solutions

Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT