Cashback Offer from 4th to 8th May 2021. Get Flat 20% Cashback credited to your account for a minimum transaction of $80. Post Your Question Today!

Question DetailsNormal
$ 24.00

CS210 Programming Assignment 2 | Complete Solution

Question posted by
Online Tutor Profile
request

CS210 Programming Assignment 2


Write a program to play roshambo (aka Rock, Paper, Scissors) against either a user or another program. Your program should accept input from the console for a user’s throw, decide what your program wants to throw, determine the winner, and print out statistics.


Part 1 – Interactive Game Play
User Input
You should prompt for user input and accept the user typing one of three strings: “rock”, “paper”, or “scissors”, followed by a newline. You should also allow the user to type “quit” to stop playing, otherwise keep going.
For any unexpected input, prompt the user to try again.
Implement an AI Player to Play Roshambo
This is a two player game, and since we have a single human interacting with the system we need to simulate the second player. In video games, artificial intelligence is used to generate intelligent behaviors primarily in non-player characters (NPCs), often simulating human-like intelligence. [wikipedia]
Specifically, you need to write code to “throw” either “rock”, “paper”, or “scissors”. A trivial algorithm would be to always throw the same thing, such as rock1. A slightly more advanced yet still trivial algorithm would be to use a random number between 1-3 and map that to R, P, or S. Even more advanced algorithms will look at past throws by the opponent to look for tendencies and try guess what he or she is going to throw, then beat it.
Once you have decided what to throw, your code should print what it is throwing, in this specific format.
“I throw rock\n”,
“I throw paper\n”, or
“I throw scissors\n”
1 In the RPS community, this is known as an avalanche. Bart Simpson is particularly fond of this strategy.
Determine the winner
Once you know what the user has thrown and what your AI Player will throw, you need to compute the winner using these rules:
After determining the winner, print out if you (the program) wins, loses, or ties, and why. This output should be read as if the computer is “talking” to the player. There are 9 possibilities for this output:
 rock loses to paper, I WIN
 paper loses to scissors, I WIN
 scissors loses to rock, I WIN
 we both threw paper
 we both threw rock
 we both threw scissors
 rock beats scissors, YOU WIN
 paper beats rock, YOU WIN
 scissors beats paper, YOU WIN
Display Statistics
After each throw and after the user quits, output basic statistics of how many games the user has won, how many the computer has won. This should include both the fraction as well as percentages.
Example interaction
(user input is in italics)
>>> Please throw rock, paper, or scissors (or quit):
rock
I throw paper
rock loses to paper, I WIN; (0/1) = 0.0% wins, (1/1) = 100.0% losses
>>> Please throw rock, paper, or scissors (or quit):
paper
I throw paper
>>> We both threw paper; (0/2) = 0.0% wins, (1/2) = 50.0% losses
>>> Please throw rock, paper, or scissors (or quit):
paper
I throw rock
paper beats rock, YOU WIN; (1/3) = 33.3% wins, (1/3) = 33.3% losses
>>> Please throw rock, paper, or scissors (or quit):
rock
I throw scissors
rock beats scissors, YOU WIN; (2/4) = 50.0% wins, (1/4) = 25.0% losses
>>> Please throw rock, paper, or scissors (or quit):
paper
I throw scissors
paper loses to scissors, I WIN; (2/5) = 40.0% wins, (2/5) = 40.0% losses
>>> Please throw rock, paper, or scissors (or quit):
quit
SUMMARY: (2/5) = 40.0% wins, (2/5) = 40.0% losses
Part II – Tournament!
Once we have an interactive program, with a small tweak we can pit our AI Players against each other in a tournament. Minor tweaks are necessary to allow your program to be driven from another program. Specifically, you must be able to “throw first” before prompting for the interactive input, and trust the driving program to not cheat with this information. We will control this behavior with a command line argument.
Tournament prizes will include bonus points on this assignment!
Parsing a Command Line Argument
Modify your program to accept a command line argument. In Java, command line arguments are passed in as an array of Strings. This is a chapter 7, so I will just give you the code to add to handle this.
Specifically, add this code to your main function:
public static void main(String[] args) {
boolean throwFirst = ((args.length == 1) && (args[0].equals("-throwFirst")));
Modifying Behavior to “throwFirst”
The normal flow of the program is to accept user input, calculate and divulge your throw, then determine and output the winner and statistics.
If the throwFirst argument is passed in, you need to modify your program flow to have your program divulge its throw BEFORE asking for user input. This allows a driver program to run both algorithms at the same time and ensure that neither program knows what the other will throw until after it has made a decision.
Example Interaction with “-throwFirst”
C:\users\Rick\JavaPrograms> java RoShamBo -throwFirst
I throw rock
>>> Please throw rock, paper, or scissors (or quit):
rock
>>> We both threw rock; (0/1) = 0.0% wins, (0/1) = 0.0% losses
I throw paper
>>> Please throw rock, paper, or scissors (or quit):
scissors
scissors beats paper, YOU WIN; (1/2) = 50.0% wins, (0/2) = 0.0% losses
I throw paper
>>> Please throw rock, paper, or scissors (or quit):
rock
rock loses to paper, I WIN; (1/3) = 33.3% wins, (1/3) = 33.3% losses
I throw paper
>>> Please throw rock, paper, or scissors (or quit):
quit
SUMMARY: (1/3) = 33.3% wins, (1/3) = 33.3% losses
Grading Criteria
User input and error handlng
15
Statistics are displayed correctly
15
AI Player
20
Computes winner
20
Program operates with -throwFirst
20
Code is readable and well structured
10
Tournament Rules and Exclusions
It is mathematically impossible to beat a fully random algorithm, and it isn’t fair to award bonus points randomly to a single student. Hence any algorithm that is fully random with an even distribution between the three choices will NOT be eligible for prizes. This will be determined through code inspection. An algorithm that always outputs the exact same throw will also be ineligible for prizes.
In order for one algorithm to “win”, it must beat the other algorithm by at least 52% over 10,000 throws. (Winning criteria subject to change based on class discussion).
Each student is allowed up to three entries into the tournament.
Your program should be named with your name followed by an entry number, e.g., Duncan1.java, Duncan2.java, Duncan3.java
Bounties
Bounty – Beat Avalanche – 5 points
If your eligible entry can consistently beat an opponent that only throws Rock, you get +5 bonus points on this assignment.
Bounty – Beat Guppy – 5 points
If your eligible entry can consistently beat an opponent that always throws what was thrown last round (e.g., if you throw rock, NEXT round Guppy will throw rock), you get +5 bonus points on this assignment.
Bounty – Beat Piranha – 5 points
If your eligible entry can consistently beat an opponent that always throws what will BEAT what was thrown (e.g., if you throw rock, NEXT round Piranha will throw paper) in the last last round, you get +5 bonus points on this assignment.
Bounty – Shark – 50 points
If your eligible entry can beat your professor’s best algorithm, congratulations, you get +50 bonus points on this assignment.
Tournament Prizes
Each student is only eligible for a SINGLE tournament prize, regardless of the number of entries.
Note that if there are no clear winners (based on winning definition defined above), NEITHER algorithm advances. Hence there could be no winner that gets +25 points and two students get +15 points if the finals match results in a tie.
Winner – Pool Play – 5 points / 2 points.
All entries will be entered into 4-8 distinct pools based on number of entries, and each entry will play each other entry within its pool. The top 2 entries from each pool will get 5 and 2 bonus points, respectively, and move onto a single elimination bracket.
Semifinals – 10 points
If you make it to the top 4 in the single elimination bracket, you get +10 points (this replaces the points won from pool play).
Runner Up – 15 points
Second place in the single elimination bracket earns +15 points (this replaces the points won from pool play and semifinals).

Winner – 25 points
First place in the single elimination bracket earns +15 points (this replaces the points won from pool play and semifinals).

Available Answer
$ 24.00

[Solved] CS210 Programming Assignment 2 | Complete Solution

  • This solution is not purchased yet.
  • Submitted On 03 Nov, 2015 05:20:29
Answer posted by
Online Tutor Profile
solution
import java.util.Scanner; import java.lang.String; import java.util.Random; public class Roshambo { public static void main (String[] ar...
Buy now to view the complete solution
Other Similar Questions
User Profile
Exper...

CS210 Programming Assignment 3 | Complete Solution

Else if(str.contains(".")) { if(str.indexOf(".")==str.length()-1) { start=true; sb.append(" "+str+" "); ...
User Profile
Exper...

CS210 Programming Assignment 2 | Complete Solution

Import java.util.Scanner; import java.lang.String; import java.util.Random; public class Roshambo { public static void main (String[] args) { Random rand = new Random(); int myScore = 0; int computerScore = ...

The benefits of buying study notes from CourseMerit

homeworkhelptime
Assurance Of Timely Delivery
We value your patience, and to ensure you always receive your homework help within the promised time, our dedicated team of tutors begins their work as soon as the request arrives.
tutoring
Best Price In The Market
All the services that are available on our page cost only a nominal amount of money. In fact, the prices are lower than the industry standards. You can always expect value for money from us.
tutorsupport
Uninterrupted 24/7 Support
Our customer support wing remains online 24x7 to provide you seamless assistance. Also, when you post a query or a request here, you can expect an immediate response from our side.
closebutton

$ 629.35