联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2018-10-21 09:08

CMPSC-121: Intro to Programming Techniques

Homework 9 - 20 points (Fall 2018)

Due Sunday, October 21 at 11:59pm

Objectives

After this homework assignment, students should be able to:

Use multi-dimensional arrays

Use random number generation in programs

Background

A game uses a 10*10 grid to represent the terrain of the surface of the moon on which a lunar lander may land.

Unfortunately, not all parts of the surface are safe to land. Unsafe locations are indicated by 0s and safe

locations are left blank.

| 0 1 2 3 4 5 6 7 8 9

--+--------------------

0 | 0 0 0

1 | 0 0 0 0 0 0

2 | 0 0 0 0 0

3 | 0 0

4 | 0 0 0 0 0

5 | 0 0 0 0

6 | 0 0 0

7 | 0 0 0 0 0 0

8 | 0 0 0 0 0

9 | 0 0 0 0 0 0

Instructions

Write a program named lunarlander.cpp that:

Declares a 2D integer array of size 10*10 that represents the terrain

Uses random number generation to populate safe and unsafe locations of the terrain

o Each location in the grid has 50% probability to be safe and 50% to be unsafe

o Safe locations are represented by the number 1

o Unsafe locations are represented by the number 0

Displays a well-formatted table that visualizes the terrain

Prompts the user to enter the (x, y) coordinates of the desired landing location

Implements and calls a function that attempts to land the lunar lander

o bool land(int location);

If the location is safe, whomever calls land should change its value to 3 to indicate the

lunar lander has landed successfully

Parameters:

int location: a variable to the element in the 2D array that was selected by

the user.

Return value:

true: if the location is safe

false: if the location is unsafe

If the location is unsafe, displays a message to the user and prompts for another (x, y) coordinates to

attempt a landing, until a successful one is entered.

You may NOT use any global variables in this program.

Optional Bonus (+4 points)

Write a program named lunarrover.cpp that extends the functionality of lunarlander.cpp by:

Prompting the user to enter one of four directions in which to move: left, right, up, down

o Validate the user input for one of the four possibilities

Making sure that the proposed move keeps the rover inside the grid

o For example, do not accept an attempted move to the left from (0, 5)

Implementing a function that simulates the movement of the lunar rover:

o bool move(int *old_location, int *new_location);

? If the new location is safe, change its value to 3 and the old location to 1 to indicate a

successful move, then return true

If the new location is unsafe, then return false

In a loop, prompt the user to move the rover until it attempts an unsafe move

You may NOT use any global variables in this program.

Sample Output

Task 1 Sample Output

| 0 1 2 3 4 5 6 7 8 9

--+--------------------

0 | 0 0 0 0 0

1 | 0 0 0 0 0 0 0

2 | 0 0 0 0 0 0

3 | 0 0 0 0 0 0 0 0

4 | 0 0 0 0

5 | 0 0 0 0 0

6 | 0 0 0 0 0 0

7 | 0 0 0 0 0 0

8 | 0 0 0 0

9 | 0 0 0 0

Enter (x, y) coordinates: 3 3

CRASH! Try again!

Enter (x, y) coordinates: 4 4

| 0 1 2 3 4 5 6 7 8 9

--+--------------------

0 | 0 0 0 0 0

1 | 0 0 0 0 0 0 0

2 | 0 0 0 0 0 0

3 | 0 0 0 0 0 0 0 0

4 | 0 3 0 0 0

5 | 0 0 0 0 0

6 | 0 0 0 0 0 0

7 | 0 0 0 0 0 0

8 | 0 0 0 0

9 | 0 0 0 0

SUCCESSFUL LANDING!

Optional Bonus Sample Output

| 0 1 2 3 4 5 6 7 8 9

--+--------------------

0 | 0 0 0 0

1 | 0 0 0 0 0 0

2 | 0 0 0 0 0

3 | 0 0 0 0 0 0

4 | 0 0 0 0 0 0

5 | 0 0 0 0

6 | 0 0 0 0 0 0

7 | 0 0 0 0 0

8 | 0 0 0 0 0

9 | 0 0

Enter (x, y) coordinates: 1 1

CRASH! Try again!

Enter (x, y) coordinates: 4 5

| 0 1 2 3 4 5 6 7 8 9

--+--------------------

0 | 0 0 0 0

1 | 0 0 0 0 0 0

2 | 0 0 0 0 0

3 | 0 0 0 0 0 0

4 | 0 0 0 3 0 0 0

5 | 0 0 0 0

6 | 0 0 0 0 0 0

7 | 0 0 0 0 0

8 | 0 0 0 0 0

9 | 0 0

SUCCESSFUL LANDING!

Enter a move (L, R, U, or D): N

Invalid move! Try again!

Enter a move (L, R, U, or D): D

| 0 1 2 3 4 5 6 7 8 9

--+--------------------

0 | 0 0 0 0

1 | 0 0 0 0 0 0

2 | 0 0 0 0 0

3 | 0 0 0 0 0 0

4 | 0 0 0 0 0 0

5 | 0 0 3 0 0

6 | 0 0 0 0 0 0

7 | 0 0 0 0 0

8 | 0 0 0 0 0

9 | 0 0

Optional Bonus Sample Output (continued)

Submission

Submit the following file(s) to Canvas before the deadline:

1. lunarlander.cpp

2. (optional bonus) lunarrover.cpp


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp