Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
5.6 kB
1
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

def valid_row_and_col_input(prompt):
  while True: 
    user_input = input(prompt)
    if user_input[0] in ["0", "1", "2"] and user_input[1] == " " and user_input[2] in ["0", "1", "2"] :
      return int(user_input[0]), int(user_input[2])

def update_board(board, new_card, row, col):
  board[row][col] = new_card

  return board
  
  
# 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)
  
  test = print_board(board)

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

  while deal_or_done_input.lower() != "done": 
    score = 0
    
    deal_done_input = input(f"Score: {score}, Deal or Done? > ")

    if deal_or_done_input.lower() == "deal":
      card = shdeck.pop(0)
      prompt = f"New card: {card}, enter location to replace card <row col>:  "
      
      row, col = valid_row_and_col_input(prompt)
      board_update = update_board(board, new_card,row, col)
      print(board_update)

      score -= 1
      print(f"Cards left to play: {len(shdeck)} ")

    elif deal_or_done_input.lower() == "done":
      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_sedealts(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()