跳至主要內容

Lab Participation 8

AI悦创原创2024年4月17日大约 6 分钟...约 1901 字

You have started to grow bored of the battleship game that you developed over the last week. You decided you want to spice it up a notch. You decide you want to allow the user to make their own ships and choose where they place it on the board, as well as display the board when firing missiles.

Checkpoint 1: Customised Battleship Game (2 pts)

We have provided a finished scaffold for last week's battleship game. We have separated the ship class to ship.py, and all other functionalities to a new file run.py. Firstly, have a read of the solution and run the program. Become familiar with what you've been given. In real world programming, you are often enhancing and modifying existing code.

create_ships (ship.py)

Our first step is allowing the user to create their own ships. This involves specifying the name and position of each ship, as well as how many ships will be on the board (maximum of 10). Hence, we will be modifying the create_ships method in the Ship class, as this method is responsible for creating the ships.

This method will now ask for the user to create ships by asking for a symbol, x and y coordinate. The symbol will be used for the ship's name e.g. a symbol A will result in the name ShipA. A correct input is in the format <symbol> <x> <y> where symbol is a letter from A to J , and x and y are valid coordinates that can fit the ship onto the board.

A maximum of 10 ships can be made. If this maximum is reached, it will stop and return the ships made. However, if the user has not hit the maximum but is finished, the user can enter END SHIPS which will also stop and return the ships made.

When an input is given, the following must be checked in order:

  1. There are 3 tokens (single space-separated inputs).
  2. symbol is between A to J.
  3. x and y are integers.
  4. (x, y) is out-of-bounds on the board.
  5. The coordinates aren't already occupied by another ship.
  6. The symbol isn't already occupied by another ship.

An example below shows calling Ship.create_ships, showing all errors that can possibly come up.

The return value in this scenario would be a list of 3 ships, the first ship ShipA with coordinates (3, 3), the second ship ShipC with coordinates (0, 0), and lastly ShipI with coordinates (2, 2).

This is a new function we will be implementing that will take in one argument board which is the game board and print it in a specific format to the user.

Ships that are afloat are represented by their symbol (the last character in its name). Ships that have been sunk appear as a capital X.

Let us see an example with a board where ShipA is at (1, 3) and ShipB is at (4, 4) and are both afloat, while ShipJ is at (0, 0) but has sunk.

-------
|X    |
|     |
|     |
| A   |
|    B|
-------

To access position (x, y) on the board, it is board[y][x] as the first index represents the row (height) and the second index represents the column (width).

main (run.py)

Each time a ship is sunk, the board should be printed immediately after.

Output Example

Hint: You may find it helpful to modify the value of the keyword parameter end when printing the board.

The following keywords and functions are banned from this task, any use of them will lead to a zero mark.

Keywords:
for, in, import *, global, lambda, nonlocal

*: except from ship import Ship

Built-in functions:
any, all, filter, eval, locals, exec, globals, map, open, __import__, __contains__. enumerate, sum, min, max

Template Code

公众号:AI悦创【二维码】

AI悦创·编程一对一

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh

C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh

方法一:QQ

方法二:微信:Jiabcdefh

通知
关于编程私教&加密文章