Untitled
unknown
python
2 years ago
3.0 kB
2
Indexable
Never
import random def create_deck(): """ Takes no parameter Creates and returns a 2D-list """ # an intially empty deck 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): """Takes a 2-D list Returns a shuffled 2-D list""" # copies the deck to a new list, so the original deck remains same shdeck = deck[:] random.shuffle(shdeck) return shdeck def deal(deck): board = [] for row in range(3): board.append([]) for col in range(3): board[row].append(deck.pop(0)) return board def show_board(board): for row in board: for card in row: print(f"| {card[0]}, {card[1]} ", end = "") print("|") # def horizontal(board): # score = 0 # for i in range(len(board)): # if board[i][0][0]== board[i][1][0]== board[i][2][0]: # 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}") # return board # def vertical(board): # score = 0 # for i in range(len(board)): # if board[0][i][0]== board[1][i] [0]== board[2][i][0]: # 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}") # return board def check_triplet_for_set(triplet): return triplet[0][0] == triplet[1][0] == triplet[2][0] def check_board_for_row(board): for row in board: if check_triplet_for_set(row): return True return False def check_board_for_column(board): for column in range(len(board)): coloumn_list = board[0][i], board[1][i], board[2][i] if check_triplet_for_set(coloumn_list): return True return False def check_board_for_diagonal(board): 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): if check_board_for_column(board) or check_board_for_row(board) or check_board_for_diagonal(board): return True # main() function that calls all other functions in the appropriate order to run the game def main(): print("Welcome to the game!") #create and print the deck of cards deck = create_deck() # print(f"\nDeck: {deck}") #create and print the shuffled deck of cards shdeck = shuffle(deck) # print(f"\nShuffled deck: {shdeck}") testOutput_0 = deal(shdeck) # print(f"\n{testOutput_0}") # print() show_board(testOutput_0) if check_board_for_sets(testOutput_0): score += 10 print("You got a set or w/e") # main guard if __name__ == "__main__": main()