python井字棋小游戏Tic Tac Toe game

Rae ·
更新时间:2024-11-01
· 966 次阅读

Your assignment: Create a Tic Tac Toe game.

Here are the requirements:
• 2 players should be able to play the game (both sitting at the same computer)
• The board should be printed out every time a player makes a move
• You should be able to accept input of the player position and then place a symbol on the
board
Feel free to use Internet to help you figure anything out. Keep in mind that this project can
take anywhere between several hours to several day

老师布置的作业,刚写出来,没有经过任何优化,仅供参考,当然也欢迎评论区交流 import random def display_board(board): print('%c|%c|%c' % (board[0],board[1],board[2])) print('-----') print('%c|%c|%c' % (board[3],board[4],board[5])) print('-----') print('%c|%c|%c' % (board[6],board[7],board[8])) def player_input(): while True: p1_choice = input('Player 1:Do you want to be X or O ?') if p1_choice in ['x','o','X','O']: return p1_choice.upper() def place_marker(board, marker, position): board[position - 1] = marker def win_check(board, mark): #将board每个mark的位置索引放入列表中 win_numbers = [[1,2,3],[1,5,9],[1,4,7],[2,5,8],[3,6,9],[3,5,7],[4,5,6], [7,8,9]] check = [i+1 for i, j in enumerate(board) if j == mark] for k in range(len(win_numbers)): if set(win_numbers[k]) <= set(check): return True return False def choose_first(): f_number = random.randint(1,2) return f_number def space_check(board, position): try: if board[position - 1] == ' ': return True else: return False except: return False def full_board_check(board): for x in board: if x == ' ': return False return True def player_choice(board, order): ud_position = input('Player %d Choose your next position(1-9):'% order) while not space_check(board, int(ud_position)): ud_position = input('Player %d Choose your next position(1-9):'% order) return int(ud_position) def replay(): reply = input('Do you want to play again (Yes/No)?') if reply.lower().strip() == 'yes': return True else: return False def ready(): judge = input('Are you ready to play (Yes/No)?') if judge.lower().strip() == 'yes': return True else: return False print('Welcome to Tic Tac Toe!') board = [' ',' ',' ',' ',' ',' ',' ',' ',' '] while True: p1_choice = player_input() if p1_choice == 'O': p2_choice = 'X' else: p2_choice = 'O' order = choose_first() print('Player %d will go first'% order) if order == 1: marker_1 = p1_choice marker_2 = p2_choice flag = True else: marker_1 = p2_choice marker_2 = p1_choice flag = False game_on = ready() try: while game_on: if flag: position = player_choice(board, order) place_marker(board, marker_1, position) display_board(board) order = order+1 if win_check(board, marker_1): print('Player 1 has won!') break if full_board_check(board): print('Draw!') break position = player_choice(board, order) place_marker(board, marker_2, position) display_board(board) order = order-1 if win_check(board, marker_2): print('Player 2 has won!') break else: position = player_choice(board, order) place_marker(board, marker_1, position) display_board(board) order = order-1 if win_check(board, marker_1): print('Player 2 has won!') breakSW if full_board_check(board): print('Draw!') break position = player_choice(board, order) place_marker(board, marker_2, position) display_board(board) order = order+1 if win_check(board, marker_1): print('Player 2 has won!') break if not replay(): break; except: exit(-1)
作者:RecycleBins



tic tac Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号