ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 28 Apr 2017 My Price 9.00

public class RPSDriver

import java.util.Scanner;
public class  RPSDriver {

 public static void main(String[] args) {
  System.out.println("Enter an int > 0: number of rounds to play.");
  Scanner s = new Scanner(System.in);
  int rounds = s.nextInt();
  System.out.println("Rounds to play: " + rounds);
  RPSGame rps = new RPSGame(rounds);
  rps.playGame();
 }
}

Now comes the principal class, RPSGame, where most of the work is done.

public class RPSGame {

 private int roundsTotal; // number of rounds to play
 private int roundsPlayed; // number of rounds that have completed
 private String roundWinner; // the winner of the current round
 private int oneWins; // number of rounds won by first player
 private int twoWins; // number of rounds won by second player
 private String oneMove; // first player's move
 private String twoMove; // second player's move
 private String winner; // which player won?

 public RPSGame(int numRounds) {
  roundsTotal = numRounds;
 }

 public void playGame() {
  // to be implemented by you
 }

 private void initializeGame() {
  roundsPlayed = 0;
  oneWins = 0;
  twoWins = 0;
  winner = "";
 }

 private boolean gameOver() {
  return (roundsPlayed == roundsTotal);
 }

 private void advancePlay() {
  roundsPlayed++;
  
  oneMove = chooseRPS();
  twoMove = chooseRPS();
  
  if((oneMove.equals("Rock") && twoMove.equals("Scissors")) ||
     (oneMove.equals("Scissors") && twoMove.equals("Paper")) ||
     (oneMove.equals("Paper") && twoMove.equals("Rock"))) {
     oneWins++;
     roundWinner = "Player One";
  }
  else if((twoMove.equals("Rock") && oneMove.equals("Scissors")) ||
    (twoMove.equals("Scissors") && oneMove.equals("Paper")) ||
    (twoMove.equals("Paper") && oneMove.equals("Rock"))) {
   twoWins++;
   roundWinner = "Player Two";
  }
  else {
   roundWinner = "Nobody";
  }
 }

 private String chooseRPS() {
 // To be implemented by you
 // Method should choose one of "Rock", "Paper", "Scissors" with equal likelihood
 }

 private void showGame() {
  System.out.println(oneMove + " vs. " + twoMove + ": " + roundWinner + " wins.");
 }

 private void judgeAndReport() {
  if(oneWins > twoWins)
   winner = "Player One";
  else if(oneWins < twoWins)
   winner = "Player Two";
  else
   winner = "Nobody";
   
  System.out.println();
  System.out.println("Player One rounds won: " + oneWins);
  System.out.println("Player Two rounds won: " + twoWins);
  System.out.println("Winner?: " + winner);
 }
}

In the answer box below, enter code that implements the chooseRPS() method and the playGame method.

Answers

(11)
Status NEW Posted 28 Apr 2017 05:04 AM My Price 9.00

-----------

Not Rated(0)