Use of C programming to build a game called Car Board. The rules for the game are simple: a car can move around inside a board; the player can move the car by entering a set of specific commands; and the car must stay within the board boundaries and must not hit the road blocks.

Attached is some start-up code which needs to be used.

1: MAIN MENU

When the program starts, the following menu should be displayed. See image.

Your program should reject any invalid data. If invalid input is entered (for example: 10, c ), you should simply ignore the input and show the prompt again.

If user enters number 2, your program should print your name, your number as below. Note that you should replace, the sections with your name, number and email address. see image.

If user selects menu number 3, your program should exit without crashing. This is specified in section 7 below.

If user selects menu number 1, the following list of commands will be displayed. see image.

The load command is responsible to initialise the game board. More details about this command is available in section 3 below.

The init command is responsible to set the initialise values of the cars position and movement. More detail about this command is available in section 4 below.

The forward command is responsible to move the car one position forward according to the current direction of the car. The turn_left (or l) and turn_right (or r) commands are responsible to change the direction of the cars movement. See the details in section 5 below.

The quit command will exit the current game and return to the main menu. This requirement is defined in section 8 below.

It is important to note that the board (containing the blocks, the position and the direction of the car) must be displayed on the screen after it is loaded, initialised, moved forward and turned left or right. See the requirement for printing the board in section 2 below.

Note that you should print a statement that at each stage of the program you must also display the list of currently acceptable commands in the output. The acceptable commands are mentioned in each section below

2: DISPLAYING THE BOARD

This game has a 10x10 board. When the game starts (right after the Play Game command and before loading any board), the following board should be displayed on the screen. see image.

Note that the board is completely empty. This is exactly how it should look like before any particular board is loaded in the next section.

At this stage of the program, only two commands are acceptable:

load < g >
quit

If user enters any other command, or an incorrect command, an error message (e.g. Invalid Input.) must be displayed and the menu should be displayed again.

3: Loading Game Boards and Data Structure Initialization

The application comes with two different predefined boards. The data structure associated with each board is available in the start-up code. The user can load either of these boards as follows.

When user starts a game using board number 1: see image.

This is another example where the user starts a game using board number 2: see image.

At this stage you are required to initialise the board with the predefined data values which are available in the start-up code.

The cells in the board which contain a star sign (*) are blocked. The car cannot move to those cells. If the user attempts to move the car to one of those cells by the forward command, an error will be shown as discussed in section 5 below.

At this stage of the program, only three commands are acceptable:

load < g >
init < x >, < y >, < direction >
quit

If user enters any other command, or an incorrect command, an error message (e.g. Invalid Input.) must be displayed and the menu should be displayed again.

4: Initialise Game

When the user loads a game board, then she should specify the initial position and direction of the car in the game board. This can be done through the < x >, < y >, < direction > command. The meaning of the arguments of this command are as follows.

  • < x >: an integer between 0-9 that specifies the horizontal location of the car
  • < y >: an integer between 0-9 that specifies the vertical location of the car
  • < direction >: any one of the values north, east, south or west. These specify the direction of the cars moving forward. For example, if north is specified and then forward command is issued, the cars current position will change to one cell towards the north (up).

Below is an example of initialising the game after loading game board 1: see image.

You can see that the user specified arguments 4,3,north and accordingly the car is positioned in the cell x=4 and y=3. The car should be shown in the grid with an arrow sign (, , , or according to the current direction of the car).

At this stage of the program, only four commands are acceptable:

  • forward (or f)
  • turn_left (or l)
  • turn_right (or r)
  • quit

If user enters any other command, or an incorrect command, an error message (e.g. Invalid Input.) must be displayed and the menu should be displayed again.

5: PLAY GAME

When the game is initialised as explained in section 4 above, the user can move the car in the grid by using the forward command. This command will move the car one cell to up, right, down or left. The direction of the move is decided according to the current direction of the car.

For example, if the current direction of the car is north, moving forward will increase the vertical location of the car by one cell. If the current direction is left, moving forward will decrease the current vertical position of the car by one cell.

Below is an example of how forward command would work on the board shown in section 4. Remember that in section 4 the board was initialised with x=4, y=3 and direction=north. Here a forward command is taking the car one cell towards the north (up). The new position of the car is x=4 and y=4, and the direction is not affected (remains north). see image.

The turn_right command will change the direction of the car in the following sequence.

  • North -> East
  • East -> South
  • South -> West
  • West -> North

The turn_left (or l) command will affect the direction of the car in the opposite sequence:

  • North -> West
  • West -> South
  • South -> East
  • East -> North

Below is an example of how the previously shown board will be affected by a turn_right (or r) command followed by a forward command. see image.

Note that in the example above the user first issued a turn_right (or r) command, which changed the direction of the car from north to east.

Given the new direction, when the user used the forward command, the program attempted to move the car towards the east. However, there is a road block on the right side of the current position (* on x=5, y=5) and the car cannot be moved forward. The error message indicates this problem. This is the way you are expected to show hitting the road blocks.

A further example of moving the car is shown below. see image.

The car shown in the previous example has turned right again, which changed its direction from east to south. It then moved forward twice.

NOTE: The command turn_left should be interchangeable with the command l, and the command turn_right should be interchangeable with the command r. That is, the command l should behave exactly similar to turn_left, and the command r should behave exactly similar to turn_right.

6: Stopping at the Edges of the Board and Before the Road Blocks

The car must not move beyond the edges of the board. For example, a car in a position with x=9 should not be able to move towards the east any more. Similarly, a car in a position with y=0 should not be able to move towards the north any more.

If the user attempts to move the car beyond the edges of the board, you should display the error message The car is at the edge of the board and cannot move further in that direction.

7: Quit Main Menu

This option should terminate your program without any crash for any reasons (i.e. exit from function main() of your C code).

8: Return to Main Menu

When the quit command is entered in the middle of the game, the game should terminate and the control should be returned to the main menu.

Additionally, the total number of successful forward moves in the game should be displayed before exiting the game. For example, if the player attempts to move the car forward four times and only three of these attempts were successful (e.g. one move hit a road block), the following message should be displayed when exiting the current game: Total player moves: 3

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.