JamCoders

💾 Download
In [ ]:
# PLEASE RUN THIS CELL
%config InteractiveShell.ast_node_interactivity="none"
%pip install termcolor
def print_locals(*which):
    """Print the local variables in the caller's frame."""
    import inspect
    ls = inspect.currentframe().f_back.f_locals
    which = set(which if which else ls.keys())
    ll = {k: v for k, v in ls.items() if k in which}
    print("; ".join(f"{k}{v}" for k, v in ll.items()))

PROJECT!

Starting from today, we will be starting on a project to build a game called connect four!

Here is a link to the game:

The first step to be able to print a board, which we will do today! The board is a 7 x 6 grid, meaning it is 7 across and 6 tall.

The input to the function is a list of lists called board_list.

  • A value of 0 means the square is empty.
  • A value of 1 means the square is taken by player 1 who should be displayed in a blue color
  • A value of 2 means the square is taken by player 2 who should be displayed in a red color

For example:

example_board_list = [[0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 2, 1, 0, 0, 0],
                      [0, 0, 1, 2, 0, 0, 0],
                      [0, 1, 2, 2, 0, 1, 0]]

# Should print something like (except with different colors)
⦿⦿⦿⦿⦿⦿⦿
⦿⦿⦿⦿⦿⦿⦿
⦿⦿⦿⦿⦿⦿⦿
⦿⦿⦿⦿⦿⦿⦿
⦿⦿⦿⦿⦿⦿⦿
⦿⦿⦿⦿⦿⦿⦿
In [ ]:
from termcolor import colored 

# Hint: you can use end = '' to specify no new line after printing
# You can try changing end to something else. Then, something else will be printed after each print()!

# Positions with a 0 should be grey:
print(colored('⦿', 'grey'), end = '')

# Positions with a 1 should be red:
print(colored('⦿', 'red'), end = '')

# Positions with a 2 should be blue:
print(colored('⦿', 'blue'), end = '')

# this is a list of lists, just like we saw in lecture on day 3
example_board_list = [[0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 2, 1, 0, 0, 0],
                      [0, 0, 1, 2, 0, 0, 0],
                      [0, 1, 2, 2, 0, 1, 0]]
⦿⦿⦿

QUESTION 1

How can we write a function that will return the element in board_list at row r and column c?

In [ ]:
def return_element(board_list, r, c):
  element = # How can we find the element within board_list?
  print(element)
  return element

# Test your code here by using your function
return_element(example_board_list, 3, 2)

QUESTION 2 Write a function that will print out the board.

  • Function name: print_board
  • Function inputs: a board_list which is a list of lists
  • Function outputs: no outputs, so there is no return statement

Function behavior: the function should use for loops to print out each element in the board.

In [ ]:
# Helper function, which takes the element from the board and prints the matching color
def print_tile(tile):
  if tile == 0:
    print(colored('⦿', 'grey'), end = '')
  elif tile == 1:
    print(colored('⦿', 'red'), end = '')
  elif tile == 2:
    print(colored('⦿', 'blue'), end = '')
  else:
    print("Error: the tile number given was not 0, 1, or 2")

# Helper function that prints a new line
def print_new_line():
  print('', end='\n')

def print_board(board_list):
  # YOUR CODE HERE!
  # Hint 1: You might need nested for loops and use print_tile
  # Hint 2: Don't forget to print a new line with print_new_line
      
print_board(example_board_list)