Write some code to manage a simple board game.

Thu, 4 Apr 2024 18:00

Write some code to manage a simple board game.

Tip

This is a bonus assignment. You do not have to complete it, but if you do, I will grade your assignments on a "best three out of four" basis.

Tip

In the contract for the moves function, I’ve clarified how to represent "you could place a new piece" as a possible move. Just like the other moves, it should be represented as a tuple of (starting_square, ending_square). Unlike the other moves, there is no starting square. So, you should represent "place a triangle" as (None, 0), and "place a square" as (None, n-1) (where n is the size of the board).

Background

A couple of lectures ago, we made up a simple one-dimensional board game based on the book Triangle by Mac Barnett and Jon Klassen. In this game, there is a one-dimensional board in which each square can hold one piece or be empty. There are two kinds of pieces: triangles and squares. We will represent these pieces using the following characters:

Symbol Hex value Decimal value

' '

20

32

'▲'

25B2

9650

'■'

25A0

9632

That is, a blank space is represented by a space character, a triangle is represented by the triangle symbol obtained from chr(0x25b2), etc. Triangles start at the left-hand side of the board and squares start at the right-hand side. The first player to get one piece all the way to the other side wins. At each turn, a player must do one of the following:

  1. place a piece on their starting square (far left for triangles, far right for squares) if it is unoccupied,

  2. move one piece one square towards the opponent’s side of the board if that square is unoccupied or

  3. jump over an opponent’s piece to an unoccupied square, removing the jumped piece from the board.

If a player cannot do any of these actions, they lose immediately.

Requirements

You will write several Python functions related to this game. Taken together, these functions form the building blocks of a playable game with an AI player (all you need to add is visualization):

def create_board(n):
    """Create a new, empty game board.

    Parameters
    ----------
    n : int
      The width of the game board

    Returns
    -------
    A representation of the game board that can be passed back into other
    functions in this module. This representation should be iterable and
    support the len() function.
    """

def place_piece(board, p):
    """Place a piece on the board.

    Parameters
    ----------
    board
      A board as returned from create_board().

    p : str
      The piece to place.
      If it is a triangle, place at the far left of the board.
      If it is a square, place at the far right of the board.
      If it is neither, return False.

    Returns
    -------
    True if the piece could be placed, False if it could be not
    (e.g., if the relevant starting square is already occupied or if the
     piece is not a valid piece).
    """

def check_move(board, starting_square, ending_square):
    """Is it legal to move the piece at one square to another?

    Parameters
    ----------
    board
      A board as returned from create_board().

    starting_square : int [0, len(board))
      The index of a piece

    ending_square : int [0, len(board))
      The index where the piece would like to move

    Returns
    -------
    True if the proposed move is legal,
    False if it is not.
    """

def move_piece(board, starting_square, ending_square):
    """Move a piece from one square to another.

    Parameters
    ----------
    board
      A board as returned from create_board().

    starting_square : int [0, len(board))
      The index of a piece

    ending_square : int [0, len(board))
      The index where the piece would like to move

    Pre-condition
    --------------
    The proposed move is legal

    Post-condition
    --------------
    The piece has been moved in the board
    """

def moves(board, player):
    """Find all of the valid moves available to a player.

    Parameters
    ----------
    board
      A board as returned from create_board().

    player : str (▲ or ■)
      The player whose turn it is to move.

    Returns
    -------
    A set of possible legal moves, where each move is represented by a
    tuple of (starting_square, ending_square). Placing a piece on the board is
    represented by the tuple (None, starting_square).

    If no legal moves are possible for a player, the empty set is returned.
    """

def choose_move(board, player):
    """Choose a move for a player.

    Parameters
    ----------
    board
      A board as returned from create_board().

    player : str (▲ or ■)
      The player whose turn it is to move.

    Returns
    -------
    A tuple of (starting_square, ending_square) representing a legal move
    for that player, or None if there is no legal move.
    """

You should not need any external modules for this assignment. In fact, you may not import any external modules.

Define these three functions in a file called game.py and submit to Gradescope. As always, please don’t hesitate to contact me if you have questions! Also as always, remember that assignments are individual work.