Untitled

mail@pastecode.io avatar
unknown
python
a year ago
4.7 kB
3
Indexable
Never
import random

def create_deck():
  """ return: 2-D list
      Creates and returns a 2D-list 
  """
  deck = []
  suits = ["RED", "BLK"]
  values = []
  
  # creates a list of all face values
  for x in range(2,10):
    values.append(x)
  
  # creates cards [s,f] and inserting them to the deck
  for s in suits:
    for f in values:
      deck.append([s, f])
  return deck

def shuffle(deck):
  """ return: 2-D list
      Takes a 2-D list and returns a shuffled 2-D list
  """
  shdeck = deck[:]
  random.shuffle(shdeck)
  return shdeck

def deal(deck):
  """ return: 3-D list
      Takes the shuffled deck as the parameter and deals the first nine 
  """
  board = []
  for row in range(3):
    board.append([])
    for col in range(3):
      board[row].append(deck.pop(0))

  return board

def print_board(board):
  """ return: 3-D list
      Takes the board as a parameter and prints the elements of the board to
      the terminal 
  """
  for row in board:
    for card in row:
      print(f"| {card[0]}, {card[1]} ", end = "")
    print("|")


def check_triplet_for_set(triplet):  
  """ input: 2-D list
      return: bool
      Checks for simple set to match for suit values (RED, BLK)
  """
  return triplet[0][0] == triplet[1][0] == triplet[2][0]

def check_board_row_for_set(board):
  """ input: 3-D list
      return: bool
      Checks board for rows in simple set to match for suit values (RED, BLK)
  """
  for row in board:
    if check_triplet_for_set(row):
      return True
  return False

def check_board_coloumn_for_set(board):
  """ input: 3-D list
      return: bool
      Checks board for coloumn in simple set to match for suit values (RED, BLK)
  """
  for column in range(len(board)):
    coloumn_list = board[0][column], board[1][column], board[2][column]
    if check_triplet_for_set(coloumn_list):
      return True
  return False

def check_board_diagonal_for_set(board):
  """ input: 3-D list
      return: bool
      Checks board for diagnol pattern in simple set to match for suit values (RED, BLK)
  """
  diagnoal_list_1 = board[0][0], board[1][1], board[2][2] 
  diagnoal_list_2 =  board[0][2], board[1][1], board[2][0] 
  
  return check_triplet_for_set(diagnoal_list_1) or check_triplet_for_set(diagnoal_list_2)

def check_board_for_sets(board):
  """ input: 3-D list
      return: bool
      Checks if one of the simple set functions runs and returns True, if true
  """
  if check_board_row_for_set(board) or check_board_coloumn_for_set(board) or check_board_diagonal_for_set(board):
    return True

def check_triplet_for_run(triplet): 
  """ input: 2-D list
      return: bool
      Checks for simple run to match for  values (1-9)
  """
  return triplet[0][1] + 2== triplet[1][1] + 1 == triplet[2][1] 

def check_board_row_for_run(board):
  """ input: 3-D list
      return: bool
      Checks board for row  in simple run to match for  values (1-9)
  """
  for row in board:
    if check_triplet_for_run(row):
      return True
  return False

def check_board_coloumn_for_run(board):
  """ input: 3-D list
      return: bool
      Checks board for coloumn  in simple run to match for  values (1-9)
  """
  for column in range(len(board)):
    coloumn_list = board[0][column], board[1][column], board[2][column]
    if check_triplet_for_run(coloumn_list):
      return True
  return False

def check_board_diagnol_for_run(board):
  """ input: 3-D list
      return: bool
      Checks board for diagnol pattern in simple run to match for  values (1-9)
  """
  diagnoal_list_1 = board[0][0], board[1][1], board[2][2] 
  diagnoal_list_2 =  board[0][2], board[1][1], board[2][0] 
  
  return check_triplet_for_run(diagnoal_list_1) or check_triplet_for_run(diagnoal_list_2)

def check_board_for_runs(board):
  """ input: 3-D list
      return: bool
      Checks if one of the simple run functions runs and returns True, if true
  """
  if check_board_row_for_run(board) or check_board_coloumn_for_run(board) or check_board_diagnol_for_run(board):
    return True

  
# main() function that calls all other functions in the appropriate order to run the game
def main():
  """
  runs the game
  """

  # set score value to award points to the user
  score = 0
  print("Welcome to the game!")

  deck = create_deck()

  shdeck = shuffle(deck)

  board = deal(shdeck)
  print_board(board)

  if check_board_for_runs(board):
    score += 20
    print(f"Congrats! You've got a simple run on the board, score: {score}")
  elif check_board_for_sets(board):
    score += 10
    print(f"Congrats! You've got a simple set on the board, score: {score}")
  else:
    print(f"sorry, no match on the board. Score: {score}")
     
# main guard    
if __name__ == "__main__":
  main()