Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To pass the time during long voyages, Viking sailors would play the two-player g

ID: 3728581 • Letter: T

Question

To pass the time during long voyages, Viking sailors would play the two-player game of Hemnesbrim. In this assignment, you'll implement the game, but play will be against the computer.

During each round, players choose a move, which may be either Knoppäng, Björnarp, Grönby, or Vilshult. The rules are:

Knoppäng beats Björnarp and Grönby

Björnarp beats Grönby

Grönby beats Vilshult

Vilshult beats Knoppäng and Björnarp

The computer wins in the event of a tie.

Your program should behave as follows:

The rules should be printed to the screen

The user is asked if they'd like to play a round

if they choose 'y', a round is played

if they choose 'n', the program ends

Until the user has chosen to quit, another round is played.

In a round of play:

The user is asked to enter a move, which may be either 'B'jornarp, 'G'ronby, 'K'noppang, or 'V'ilshult. The program should continue to prompt the user until a valid move is entered.

The computer makes a move at random. (Hint: remember how we generated random numbers in class.)

The program prints the computer's move, the user's move, and who is the winner of this round.

The user is asked if they'd like to continue.

When the user has decided to quit the game, the program prints the number of:

rounds played

times the user won

times the computer won

suggestions

testing (15 points)

Remember that good code is readable and testable. In order to receive full credit for this assignment, you'll need to break up the problem into at least 5 methods separate from main( ). Develop and submit JUnit tests for at least two of these.

Recall that to create most of the skeleton of these tests for yourself, in Netbeans you can right-click (CMD-click on a Mac) on name of your Java source file in the project view, then Tools, then Create/Update Tests. The test file may then be found under the Test Packages folder.

Some functions that we've used that should be helpful are:

assertEquals

assertTrue

assertFalse

Explanation / Answer

package Chegg_6; import java.util.Random; import java.util.Scanner; public class Viking_Sailors { static String userChance; static String playRound; static char compMove; static String computerMove; static int count = 0; static int countOfUserWin = 0; static int countOfCompWin = 0; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Would you like to play a round?"); playRound = scanner.next(); while (!playRound.equals("n")) { Viking_Sailors v = new Viking_Sailors(); //printing the rules of the game v.gameRules(); //take user input as K, B G or V userChance = scanner.next(); //Fetching user input as per the entry userChance = v.getUserInput(scanner); System.out.println("Your move : " + userChance); //Fetching Computer move compMove = v.computerMove(); if (compMove == 'K') { computerMove = "Knoppäng"; System.out.println("Computer move = " + computerMove); } else if (compMove == 'B') { computerMove = "Björnarp"; System.out.println("Computer move = " + computerMove); } else if (compMove == 'G') { computerMove = "Grönby"; System.out.println("Computer move = " + computerMove); } else { computerMove = "Vilshult"; System.out.println("Computer move = " + computerMove); } //Calculating who won as per the rules if(v.whoWon(userChance,computerMove).equalsIgnoreCase("You won!!!")) {countOfUserWin = countOfUserWin+1;} else countOfCompWin = countOfCompWin +1; System.out.println(v.whoWon(userChance,computerMove)); System.out.println("Would you like to continue?"); playRound = scanner.next(); } Viking_Sailors v = new Viking_Sailors(); v.gameEnd(count,countOfUserWin,countOfCompWin); } public void gameRules() { System.out.println("Following are the rules of the game: " + "1. Knoppäng beats Björnarp and Grönby " + "2. Björnarp beats Grönby " + "3. Grönby beats Vilshult " + "4. Vilshult beats Knoppäng and Björnarp " + "5. The computer wins in the event of a tie. "); System.out.println("Enter your move : Knoppäng, Björnarp, Grönby, or Vilshult"); } //Calculate Computer move public char computerMove() { String alphabet = "KBGV"; Random rnd = new Random(); //get random move of computer char compMove = alphabet.charAt(rnd.nextInt(alphabet.length())); return compMove; } //Calculate who won public String whoWon(String userChance, String computerMove) { if ((userChance.equals("Knoppäng") && (computerMove.equals("Björnarp") || computerMove.equals("Grönby"))) || (userChance.equals("Björnarp") && computerMove.equals("Grönby")) || (userChance.equals("Grönby") && computerMove.equals("Vilshult")) || (userChance.equals("Vilshult") && (computerMove.equals("Knoppäng") || computerMove.equals("Björnarp")))) { { return ("You won!!!"); } } else if (userChance.equals(computerMove)) { return ("Computer Won!!!"); } else { return ("Computer Won!!!"); } } //Get user input public String getUserInput(Scanner scanner) { while (!(userChance.equalsIgnoreCase("K") || userChance.equalsIgnoreCase("B") || userChance.equalsIgnoreCase("G") || userChance.equalsIgnoreCase("V"))) { System.out.println("Enter correct move : Knoppäng, Björnarp, Grönby, or Vilshult"); userChance = scanner.next(); } count = count +1; if (userChance.equalsIgnoreCase("K")) { userChance = "Knoppäng"; } else if (userChance .equalsIgnoreCase("B")) { userChance = "Björnarp"; } else if (userChance.equalsIgnoreCase("G")) { userChance = "Grönby"; } else if (userChance.equalsIgnoreCase("V")){ userChance = "Vilshult"; } return userChance; } //On game end when user does not want to continue public void gameEnd(int count, int countOfUserWin, int countOfCompWin) { System.out.println("Total number of times played : " + count); System.out.println("Number of time you won : " + countOfUserWin); System.out.println("Number of time computer won : " + countOfCompWin); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote