大家好,我是皮皮。
一、前言 前几天在Python最强王者交流群有个叫【Chloe】的粉丝问了一个Python小游戏的问题,这里拿出来给大家分享下,一起学习下。
二、解决过程 看上去代码有报错,截图如下。
这个错误倒是很常见,因为数据类型不同,直接相加肯定报错,如果需要更改的话,那么需要转一下数据类型,这里【沈复】大佬给出了答案,如下图所示。
当然了,粉丝的代码残缺的太厉害了,少了5-7个函数,【月神】依次补充完整之后,总算可以进入游戏了,然后顺便找到了这个报错位置。
这里问题还是不少的,【月神】帮忙更新了下代码,如下:
def replay(): key = input('Do you want to play again? Enter Yes or No: ') return True if key[0].upper() == 'Y' else False
这样的话,就完美解决了。
最后分享下这个游戏的完整的代码给大家,感兴趣的小伙伴们可以玩玩看。
print('Welcome to Tic Tac Toe!')
from IPython.display import clear_output import random
def choose_first(): if random.randint(0,1) == 0: return 'player2' else: return 'player1'
def player_input(): marker = ''
1while not (marker == 'X' or marker == 'O'): 2 marker = input("Do you want to be X or O? ").upper() 3 4if marker == 'X': 5 return 'X' 6else: 7 return 'O'
def player_choice(board): position = 0
1while position not in [1, 2, 3, 4, 5, 6, 7, 8, 9] or not space_check(board, position): 2 position = int(input('Choose your next position: (1-9): ')) 3return position
def space_check(board, position): return board[position] == ' '
def full_board_check(board): for i in range(1,10): if space_check(board,i): return False return True
def replay(): key = input('Do you want to play again? Enter Yes or No: ') return True if key[0].upper() == 'Y' else False
def place_marker(board, marker, position): board[position] = marker
def win_check(board, mark): return ( (board[1]==mark and board[2]==mark and board[3]==mark) or (board[4]==mark and board[5]==mark and board[6]==mark) or (board[7]==mark and board[8]==mark and board[9]==mark) or (board[1]==mark and board[4]==mark and board[7]==mark) or (board[2]==mark and board[5]==mark and board[8]==mark) or (board[3]==mark and board[6]==mark and board[9]==mark) or (board[1]==mark and board[5]==mark and board[9]==mark) or (board[3]==mark and board[5]==mark and board[7]==mark) )
def display_board(board): clear_output()
1print(' | |') 2print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9]) 3print(' | |') 4print('-----------') 5print(' | |') 6print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6]) 7print(' | |') 8print('-----------') 9print(' | |') 10print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3]) 11print(' | |')
while True: # Reset the board theBoard = [' '] * 10 player1_marker = player_input() player2_marker = player_input()
1turn = choose_first() 2print(turn + ' will go first') 3 4play_game = input('Are you ready to play? yes or no? ') 5 6if play_game[0].lower() == 'y': 7 game_on = True 8else: 9 game_on = False 10 11while game_on: 12 if turn == 'Player1': 13 # Player1 turn 14 15 display_board(theBoard) 16 position = player_choice(theBoard) 17 place_marker(theBoard, player1_marker, position) 18 19 if win_check(theBoard, player1_marker): 20 display_board(theBoard) 21 print('Congratulations! You have won the game!') 22 game_on = False 23 else: 24 if full_board_check(theBoard): 25 display_board(theBoard) 26 print('The game is a draw!') 27 break 28 else: 29 turn = 'Player2' 30 31 32 else: 33 # player2 turn 34 display_board(theBoard) 35 position = player_choice(theBoard) 36 place_marker(theBoard, player2_marker, position) 37 38 if win_check(theBoard, player2_marker): 39 display_board(theBoard) 40 print('Player2 has won!') 41 game_on = False 42 else: 43 if full_board_check(theBoard): 44 display_board(theBoard) 45 print('The game is a draw!') 46 break 47 else: 48 turn = 'Player1' 49 50if not replay(): 51 break
三、总结 大家好,我是皮皮。这篇文章主要分享了使用Python编程打造一款小游戏,针对该问题给出了具体的解析和代码演示,帮助粉丝顺利解决了问题。
最后感谢粉丝【Chloe】提问,感谢【沈复】、【月神】给出的具体解析和代码演示,感谢【dcpeng】、【冯诚】等人参与学习交流。
小伙伴们,快快用实践一下吧!如果在学习过程中,有遇到任何问题,欢迎加我好友,我拉你进Python学习交流群共同探讨学习。
