ITEC313 Writing Command Line Interface Application: River Crossing Game Programming Assessment 3 Answer

pages Pages: 4word Words: 890

Question :

FACULTY OF LAW & BUSINESS

Peter Faber Business School

ITEC313: Object Oriented Programming, Semester 1, 2020 Assessment 3 [Weight 50%]

River Crossing, Game Programming Assignment

Problem Description

One day a farmer goes to the market, taking with him a cabbage and a goat. On the way he captured a wolf, he’s a mighty farmer, and took it with him to the market. They came to a river. And on the Bank, there’s a boat. Now, the problem is that the boat just big enough to carry the farmer and one of the others. If the farmer leaves the wolf and the goat together, in his absence, the wolf would make a meal of the goat. Neither, could be leave the goat and the cabbage alone: the goat would eat the cabbage. Being used to hardship, the farmer was quite prepared to make several trips back and forth, just so long as he could eventually bring them all to the other side and went on to the market. How should he arrange the trips?

 CLI version of the river crossing

Figure 1. CLI version of the river crossing

Part 1 Requirements

Your task as a software developer is to write a Command Line Interface (CLI) application RiverCrossingCLI simulating the river crossing. The farmer (player) can then work out the steps required to safely move all items to the other side of the river. In Part 1 you will need to do two tasks:

  1. Your first task is to design and create a RiverCrossing class to represent the river crossing with the appropriate attributes and methods. Assume river flows west to east (left to right of the page), farmer moves north (straight up) and requires to transport items from the South Bank to North Bank of the river. Design class so it can be re-used in Part 2 of the assignment, the GUI version of River Crossing.
    1. Write the RiverCrossing constructor.
    2. Use a boolean (or integer) array elements as place holders for the Farmer’s items (boat, cabbage, goat, wolf). False value to represent an item located on the South Bank and True on the North Bank of river.
    3. Write a row() method to transport an item from one Bank to another Bank.
    4. Write get() methods to return string with inventory (a list of all items, including the boat) located on the North Bank and another get() method for the South Bank.
    5. Write other methods as required
  2. Write a Command Line Interface class with main method to allow the user to play the game. A sample of a dialogue between the computer and the user (farmer) trying to cross from the south

Sample 1

Welcome to the CLI version of the River Crossing Game. Farmer crossing a river has boat, cabbage, goat and wolf. Boat can carry two items at the time and cannot leave the cabbage alone with the goat or the goat with the wolf.

North Bank:

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:boat, cabbage, goat, wolf

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 3

North Bank:boat, goat

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:cabbage, wolf

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 1

North Bank:goat

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:boat, cabbage, wolf

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 4

North Bank:boat, goat, wolf

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:cabbage

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 3

North Bank:wolf

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:boat, cabbage, goat

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 2

North Bank:boat, cabbage, wolf

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:goat

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 1

North Bank:cabbage, wolf

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:boat, goat

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 3

North Bank:boat, cabbage, goat, wolf

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:

***** You won *****

Sample 2

Welcome to the CLI version of the River Crossing Game. Farmer crossing a river has boat, cabbage, goat and wolf. Boat can carry two items at the time and cannot leave the cabbage alone with the goat or the goat with the wolf.

North Bank:

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:boat, cabbage, goat, wolf

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 2

North Bank:boat, cabbage

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:goat, wolf The wolf ate the goat

***** Game over *****

Sample 3

Welcome to the CLI version of the River Crossing Game. Farmer crossing a river has boat, cabbage, goat and wolf. Boat can carry two items at the time and cannot leave the cabbage alone with the goat or the goat with the wolf.

North Bank:

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:boat, cabbage, goat, wolf

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 3

North Bank:boat, goat

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:cabbage, wolf

You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)? 4

North Bank:boat, goat

────────────────────────────────────────

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

────────────────────────────────────────

South Bank:cabbage, wolf

***** I don't see the wolf on the North Bank You have the following choices:

  • Row across by yourself
  • Take the cabbage with you
  • Take the goat with you
  • Take the wolf with you
  • Quit

What is your choice (1, 2, 3, 4 or 5)?

The above dialogs are meant as a sample only. You are at liberty to design the game in any way you like and you should. In particular, you should inform the user the consequences of a bad decision, i.e., the cabbage or the goat was eaten and the game is over

A sample of an UML diagram shows the class diagram forsample of an UML diagram shows the class diagram river crossing class

RiverCrossing class

Instance attributes are all private;

  • Bank[ ] array contains boolean values representing position of an item, south Bank false and true if its in the north Bank
  • item[ ] array comprise of strings "boat", "cabbage", "goat", "wolf"
  • other attributes as necessary

RiverCrossing Methods

  • RiverCrossing( ) constructor sets all item to false (south Bank)
  • getItemStatus(int itemreturns a String with location of item i
  • getNorthBank( ) return String with north Bank inventory
  • getSouthBank( ) return String with south Bank inventory
  • row(int itemtransport the boat with the selected item. Farmer is allowed to take an empty boat across the river. The method returns the following integer codes:

() the move successful

1 all items transported to north Bank, player won

(error code)

11 the selected item is already across the river, error code 

12 the goat ate the cabbage, end of the game


13 the wolf ate the goat, end of the game

14 the goat ate the cabbage and wolf ate the goat, end of the game.

Show More