Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
4.5 kB
2
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 
  """
  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 
  """
  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 
  """
  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 
  """
  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 values that are increasing by 1
  """
  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
  """
  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 simplerun 
  """
  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 
  """
  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 and award points to the user
  score = 0
  print("Welcome to the game!")

  deck = create_deck()

  shdeck = shuffle(deck)

  board = deal(shdeck)
  print_board(board)

  deal_or_done_input = input(f"Score: {score}, Deal or Done? >> ")

  while deal_or_done_input.lower != "done": 
    if deal_or_done_input.lower() == "deal":
      print(f"New card:{print_board(board)}, enter location to replace card <row col>:  ")
      break
    

  
    
# main guard    
if __name__ == "__main__":
  main()