Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program in JAVA a game To pass the time during long winters, the ancient Nordic people would play the two-player game of Lagkapten . In

Program in JAVA

a game

To pass the time during long winters, the ancient Nordic people would play the two-player game of Lagkapten. 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 Olserod, Knarrevik, Utespelare, Yngvar, or Melltorp. The rules are:

  • Melltorp beats Utespelare, Olserod
  • Utespelare beats Knarrevik, Yngvar
  • Knarrevik beats Melltorp, Olserod
  • Olserod Beats Yngvar, Utespelare
  • Yngvar beats Melltorp, Knarrevik

The computer wins in the event of a tie.

Your program should behave as follows:

  • [3 points] The rules should be printed to the screen
  • [5 points] 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:
    • [5 points] The user is asked to enter a move, which may be either Olserod, Knarrevik, Utespelare, Yngvar, or Melltorp. The program should continue to prompt the user until a valid move is entered.
    • [4 points] The computer makes a move at random. (Hint: remember how we generated random numbers in class.)
    • [5 points] Determine the winner
    • 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.
  • [4 points] 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

An important skill in programming is learning how to break up a big job into smaller tasks.

  • [4 points] Make an outline. Make sure that your outline makes sense. Test it out with real input using pencil and paper. Do this before you start writing code. Submit your outline as a separate file or within your java file as a comment.
  • Turn some of the individual steps of your outline into functions. Some obvious choices might be a function which generates the computer's move at random. Another would be a function which is passed two moves and returns whether or not the user has won the round. The goal should be that the functions make your code as readable as your English-language outline. You don't need to implement all of the functions at first. Just write placeholders (we call these stubs) first and fill them in later.
  • Implement and test your stub functions.

Also remember to test things as you go. It's easier to find a mistake in 5 lines of code than it is to find a mistake in 500 lines of code.

style

  • [5 points] at least 5 separate functions defined and used
  • [5 points] no global variables. Data is passed to and copied from functions
  • [4 points] input from the user is validated. If the user enters invalid input, he or she should be prompted to re-enter the input until valid input is given. Note that you are not required to handle the problem of the program crashing when a Scanner detects input of the wrong type.
  • [3 points] all input entered is echoed back to the user. For example, when the user has entered a move, you'd write back something like, "Your move is Olserod"
  • [3 points] no magic numbers. Use named constants instead. For example, one possible way of writing the program is to represent moves as integers (e.g., Knarrevik is represented by 0, Melltorp is represented by 1, ...). Define named constants for each of these moves, e.g., public static final int MELLTORP = 1; (It's entirely appropriate to make these definitions global.), and then instead of writing something like if (move == 1), you'd write if (move == MELLTORP)

testing (up to 5 extra credit 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 one of these.

We'll cover unit testing and JUnit in class this week.

Recall that to create most of the skeleton of these tests for yourself, in Eclipse, under the package tab, right-click on your .java file (CTRL-click on a Mac), select New, then JUnit Test Case. Check off the functions for which you'd like some starter tests written, and then click Finish.

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

  • assertEquals(x, y)
  • assertTrue(x)
  • assertFalse(x)

Step by Step Solution

3.53 Rating (146 Votes )

There are 3 Steps involved in it

Step: 1

Answer Now lets outline the main steps in Java import javautilScanner public class LagkaptenGame public static final ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Management

Authors: Richard L. Daft

9th Edition

0324595840, 978-0324595840

More Books

Students also viewed these Operating System questions