跳至主要內容

Lab Participation 7

AI悦创原创大约 7 分钟...约 2214 字

Checkpoint 1: Implement Simplified Battleship Game (2pt)

Let's do some more coding! Your task is to create a simplified version of the Battleship game (What is Battleship Game?open in new window) using object-oriented programming (OOP) principles. In this game, you'll work with a 5x5 2D list representing the game board and 3 ships placed on the board. The objective is for the player to guess the ships' locations correctly within 10 attempts.

Here's what you need to do:

  • Ship class implementation: Define the constructor and all necessary methods of the Ship class. This includes checking hits (when the player guesses correctly) and updating the ship's status when hit.
  • is_finish function implementation : This function takes a 2D list representing the game board as input and returns True if all ships on the board are sunk; otherwise, it returns False. You'll need to loop through the 2D list to check the status of each ship.
  • main function implementation : This function runs the game. Follow the provided instructions in the comments to create three instances of the Ship class and a 2D list board. Continue prompting the user for input until all ships are sunk or there are no turns remaining (i.e., the player fails to guess the locations of all ships within 10 attempts).

Example outputs of the programs are as follows:

$ python3 battleship.py
Game started. Fire at will!
Enter X, Y coordinate [1/10]: #3, 4
Miss!
Enter X, Y coordinate [2/10]: #2, 5
Invalid coordinates. Try again.
Enter X, Y coordinate [3/10]: #1, 4
You sank ShipA!
Enter X, Y coordinate [4/10]: #3, 2
You sank ShipB!
Enter X, Y coordinate [5/10]: #6, 5
Invalid coordinates. Try again.
Enter X, Y coordinate [6/10]: #3, 3
You sank ShipC!
Congratulations! All ships are sunk.
$ python3 battleship.py
Game started. Fire at will!
Enter X, Y coordinate [1/10]: #3, 4
Miss!
Enter X, Y coordinate [2/10]: #2, 5
Invalid coordinates. Try again.
Enter X, Y coordinate [3/10]: #2, 6
Invalid coordinates. Try again.
Enter X, Y coordinate [4/10]: #1, 4
You sank ShipA!
Enter X, Y coordinate [5/10]: #3, 2
You sank ShipB!
Enter X, Y coordinate [6/10]: #3, 4
Miss!
Enter X, Y coordinate [7/10]: #6, 5
Invalid coordinates. Try again.
Enter X, Y coordinate [8/10]: #5, 6
Invalid coordinates. Try again.
Enter X, Y coordinate [9/10]: #4, 7
Invalid coordinates. Try again.
Enter X, Y coordinate [10/10]: #7, 4
Invalid coordinates. Try again.
Game over after 10 attempts! Try again next time!

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

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

template code:

class Ship:
    def __init__(self, x: int, y: int, name: str) -> None:
        """
        Initialize a ship object with given coordinates and name.

        There are 4 attributes of the the ship:
        x (int): The x coordinate of the ship.
        y (int): The y coordinate of the ship.
        name (str): The name or type of the ship.
        sunk (bool): Indicates whether the ship has sunk.
        """
        
    def got_hit(self):
        """
        Update the status of the ship when it gets hit.
        If the ship is hit, it is marked as sunk.
        """
        
    def has_sunk(self) -> bool:
        """Get the status of the ship"""

    def get_name(self) -> str:
        """Get the name of the ship"""

    def get_coord(self) -> tuple[int, int]:
        """Get the coordinate of the ship"""

    def __repr__(self) -> str:
        """
        Return a formatted string representation of the ship.

        The formatted string includes the ship's name followed by its status:
        - If the ship has sunk, the format is "{name}: Sunk".
        - If the ship hasn't sunk, the format is "{name}: Afloat".
        """

    def create_ships() -> list["Ship"]:
        """Create the following Ships in a list and return the list.

        * ShipA at (1, 4)
        * ShipB at (3, 2)
        * ShipC at (3, 3)
        """


def is_finished(board: list[list[Ship | None]]):
    """
    Check if all ships on the 2D list game board are sunk.
    Return True if all ships are sunk, otherwise False.
    """

def create_board() -> list[list[Ship | None]]:
    """Create a 5x5 2D list representing the game board.

    You should fill all slots with None."""

def main():
    # 1. Create the list of 3 ships
    ships = ...

    # 2. Create the 5x5 board with the function you just completed
    board = create_board()

    # 3. Place your ships in the board with based on their coordinates
    # You will have the following board after you placed your ships.
    # [
    #     [None  , None  , None  , None  , None ],
    #     [None  , None  , None  , None  , ShipA],
    #     [None  , None  , None  , None  , None ],
    #     [None  , None  , ShipB , ShipC , None ],
    #     [None  , None  , None  , None  , None ],
    # ]


    # 4. Continuously prompt the user for input until all ships are sunk
    # or the player has attempted 10 times.
    # Whenever a ship is sunk, update the ship's status accordingly.
    max_attempt = 10 
    print("Game started. Fire at will!")
    while ...:
        user_input = input("Enter X, Y coordinate: ")
        ...

    print("Congratulations! All ships are sunk.")


if __name__ == "__main__":
    main()

Checkpoint 2: Classes and Instances (1pt)

  1. Which tutorial exercise demonstrates the varied usage of methods, including those that are independent of the instance and those that depend on specific instances? Give some examples.
  2. What are the notable issues when modifying the value of an instance's attribute directly?
  3. In the Battleship game, Which methods are independent of the instance, and which ones are dependent on a specific instance?
公众号:AI悦创【二维码】

AI悦创·编程一对一

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

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

方法一:QQopen in new window

方法二:微信:Jiabcdefh

Code
Code1
class Ship:
    def __init__(self, x: int, y: int, name: str) -> None:
        self.x = x
        self.y = y
        self.name = name
        self.sunk = False

    def got_hit(self):
        self.sunk = True

    def has_sunk(self) -> bool:
        return self.sunk

    def get_name(self) -> str:
        return self.name

    def get_coord(self) -> tuple[int, int]:
        return self.x, self.y

    def __repr__(self) -> str:
        status = "Sunk" if self.sunk else "Afloat"
        return f"{self.name}: {status}"

    @staticmethod
    def create_ships() -> list:
        return [
            Ship(1, 4, "ShipA"),
            Ship(3, 2, "ShipB"),
            Ship(3, 3, "ShipC"),
        ]


def is_finished(board: list):
    row_index = 0
    while row_index < 5:
        col_index = 0
        while col_index < 5:
            if board[row_index][col_index] is not None and not board[row_index][col_index].has_sunk():
                return False
            col_index += 1
        row_index += 1
    return True


def create_board() -> list:
    # return [[None for _ in range(5)] for _ in range(5)]
    board = []
    row = 0
    while row < 5:
        current_row = []
        col = 0
        while col < 5:
            current_row.append(None)
            col += 1
        board.append(current_row)
        row += 1
    return board

def main():
    ships = Ship.create_ships()
    board = create_board()

    # Placing ships on the board
    index = 0
    while index < len(ships):
        ship = ships[index]
        x, y = ship.get_coord()
        board[x-1][y-1] = ship
        index += 1

    attempts = 0
    max_attempts = 10
    print("Game started. Fire at will!")
    while attempts < max_attempts:
        user_input = input(f"Enter X, Y coordinate [{attempts+1}/{max_attempts}]: ")
        try:
            x, y = user_input.split(",")
            x = int(x.strip()) - 1
            y = int(y.strip()) - 1
            if x < 0 or x >= 5 or y < 0 or y >= 5:
                print("Invalid coordinates. Try again.")
            else:
                if board[x][y] is not None:
                    if not board[x][y].has_sunk():
                        board[x][y].got_hit()
                        print(f"You sank {board[x][y].get_name()}!")
                        if is_finished(board):
                            print("Congratulations! All ships are sunk.")
                            return
                    else:
                        print("Miss!")
                        attempts += 1
                else:
                    print("Miss!")
                    attempts += 1
        except ValueError:
            print("Invalid input. Please enter coordinates in X, Y format.")

    if not is_finished(board):
        print("Game over after 10 attempts! Try again next time.")


if __name__ == "__main__":
    main()

上次编辑于:
贡献者: AndersonHJB
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度