May-08-2020, 09:15 AM
def status_print(game): print("\n") print(game[0]) print(game[1]) print(game[2]) def entry_check(num, XO, entry, valid_entries): player_entry = input(f"Player {num}, enter positional number(1-9) of {XO}: ") if player_entry in entry: print("\nSorry! That position is already taken. Try again\n") return None elif player_entry in valid_entries: return player_entry else: print("\nonly 1-9 numbers are allowed. Try again\n") return None def player_win(num, XO): print(f"Player {num} won ({XO}-{XO}-{XO})!!") print("Congratulations!!") def start_game(): new_game = [ ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ] game_dict = {'1': (0, 0), '2': (0, 1), '3': (0, 2), '4': (1, 0), '5': (1, 1), '6': (1, 2), '7': (2, 0), '8': (2, 1), '9': (2, 2)} status_print(new_game) result_pending = True game = new_game.copy() entry_count = 0 valid_entries = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] entry = [] while result_pending or entry_count < 5: while True: player_1 = entry_check(1, 'X', entry, valid_entries) if player_1: break entry.append(player_1) entry_count += 1 x, y = game_dict.get(player_1) game[x].pop(y) game[x].insert(y, 'X') status_print(game) any_rowX = list((game[0].count('X'), game[1].count('X'), game[2].count('X'))) column1 = list((game[0][0], game[1][0], game[2][0])) column2 = list((game[0][1], game[1][1], game[2][1])) column3 = list((game[0][2], game[1][2], game[2][2])) any_columnX = list((column1.count('X'), column2.count('X'), column3.count('X'))) diagonal1 = list((game[0][0], game[1][1], game[2][2])) diagonal2 = list((game[2][0], game[1][1], game[0][2])) any_diagonalX = list((diagonal1.count('X'), diagonal2.count('X'))) if 3 in any_rowX or 3 in any_columnX or 3 in any_diagonalX: print("Player 1 won (X-X-X)!!") print("Congratulations!!") break if entry_count == 5: print("Drawn") break while True: player_2 = entry_check(2, 'O', entry, valid_entries) if player_2: break entry.append(player_2) x, y = game_dict.get(player_2) game[x].pop(y) game[x].insert(y, 'O') status_print(game) any_rowO = list((game[0].count('O'), game[1].count('O'), game[2].count('O'))) column1 = list((game[0][0], game[1][0], game[2][0])) column2 = list((game[0][1], game[1][1], game[2][1])) column3 = list((game[0][2], game[1][2], game[2][2])) any_columnO = list((column1.count('O'), column2.count('O'), column3.count('O'))) diagonal1 = list((game[0][0], game[1][1], game[2][2])) diagonal2 = list((game[2][0], game[1][1], game[0][2])) any_diagonalO = list((diagonal1.count('O'), diagonal2.count('O'))) if 3 in any_rowO or 3 in any_columnO or 3 in any_diagonalO: print("Player 2 won (O-O-O)!!") print("Congratulations!!") break play_wish = input("Want to play again?(Y/N): ").upper() if play_wish == "Y": start_game() else: print("Thank you for playing") print("\nLet's Play Tic-Tac-Toe Game") start_game()I have coded this same game but i haven't learn GUI implementation. Just like to share my code for reviews instead of making new thread.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.
"Nothing can stop you from learning new things except your own will"
"Nothing can stop you from learning new things except your own will"