python实现井字棋游戏
本文实例介绍了python实现井字棋游戏的方法,分享给大家,具体内容如下
windows7下python3.4.0编译运行通过。由于采用了cmd调用,所以与Linux不兼容,无法在Linux下运行。
游戏就是井字棋,小键盘上的数字位置对应棋盘位置。
#本游戏python3.4.0下编写调试,只能在windows下运行。 importrandom importsubprocess importtime #定义函数 defdraw_board(the_board): subprocess.call("cls",shell=True) print('-------\n'+'|'+the_board[9]+'|'+the_board[8]+'|'+the_board[7]+'|\n'+'-------\n'+'|'+the_board[6]+'|'+the_board[5]+'|'+the_board[4]+'|\n'+'-------\n'+'|'+the_board[3]+'|'+the_board[2]+'|'+the_board[1]+'|\n'+'-------') definput_player_letter(): letter='' whilenot(letter=='X'orletter=='O'): print('请选择X或O作棋子:',end='') letter=input().upper() ifletter=='X': return['X','O'] else: return['O','X'] defwho_first(): if1==random.randint(1,2): return'computer' else: return'player' defis_again(): print('再一次?(YesorNo)') returninput().lower().startswith('y') defis_space_free(the_board,move): returnthe_board[move]=='' defchoose_random_from_list(the_board,move_from_list): possible_moves=[] foriinmove_from_list: ifis_space_free(the_board,i): possible_moves.append(i) iflen(possible_moves)!=0: returnrandom.choice(possible_moves) else: returnNone defmake_move(the_board,the_letter,the_move): the_board[the_move]=the_letter defget_board_copy(the_board): duplicated_board=[] foriinboard: duplicated_board.append(i) returnduplicated_board defis_board_full(the_board): foriinrange(1,9): ifis_space_free(the_board,i): returnFalse else: returnTrue defget_player_move(the_board): the_move=0 whilethe_movenotinlist(range(1,9))ornotis_space_free(the_board,the_move): print('请输入走步:',end='') the_move=int(input()) returnthe_move defis_winner(the_board,the_letter): return(the_board[1]==the_letterandthe_board[2]==the_letterandthe_board[3]==the_letter)or(the_board[4]==the_letterandthe_board[5]==the_letterandthe_board[6]==the_letter)or(the_board[7]==the_letterandthe_board[8]==the_letterandthe_board[9]==the_letter)or(the_board[1]==the_letterandthe_board[5]==the_letterandthe_board[9]==the_letter)or(the_board[2]==the_letterandthe_board[5]==the_letterandthe_board[8]==the_letter)or(the_board[3]==the_letterandthe_board[5]==the_letterandthe_board[7]==the_letter)or(the_board[1]==the_letterandthe_board[4]==the_letterandthe_board[7]==the_letter)or(the_board[2]==the_letterandthe_board[5]==the_letterandthe_board[8]==the_letter)or(the_board[3]==the_letterandthe_board[6]==the_letterandthe_board[9]==the_letter) defget_computer_move(the_board,computer_letter): globalplayer_letter globalmove ifplayer_letter=='X': computer_letter='O' else: player_letter='O' computer_letter='X' #虚拟棋盘查看是否自己可一步得胜 foriinrange(1,9): copy=get_board_copy(board) ifis_space_free(board,i): make_move(copy,computer_letter,i) ifis_winner(copy,computer_letter): returni #虚拟棋盘查看是否对手可一步得胜 foriinrange(1,9): ifis_space_free(board,i): copy=get_board_copy(board) make_move(copy,player_letter,i) ifis_winner(copy,player_letter): returni move=choose_random_from_list(board,[1,3,7,9]) ifmove!=0: returnmove ifis_space_free(board,5): return5 returnchoose_random_from_list(board,[2,4,6,8,7]) print('欢迎玩井字棋游戏!') time.sleep(1) print('''▆▅▅▅▆▅▅▅▅▅▅▅▂▅▅▅▆▆▅▅▃▂▆▅▅▅▅▅▅▅▅ ▆▆▆▃▂▆▆▅▃▄▆▅▂▅▆▇▇▆▆▆▄▂▆▆▆▆▆▆▆▆▅ ▆▅▆▅▁▅▂▃▅▆▅▂▆▆▇▆▅▆▇▄▂▆▆▆▆▆▆▆▆▅ ▆▅▆▆▅▃▆▅▆▅▂▆▇▆▅▅▆▇▅▂▆▆▆▆▆▆▆▆▅ ▆▆▆▆▆▃▁▅▆▆▄▂▇▇▆▅▅▆▇▅▁▆▆▆▆▆▆▆▆▅ ▆▅▆▆▃▂▃▁▁▅▆▄▂▇▇▆▅▆▇▇▅▂▆▆▆▅▅▅▅▅▅ ▆▅▆▃▁▅▆▃▁▁▅▅▂▆▇▆▆▇▆▆▄▂▆▅▅▅▅▅▆▆▅ ▆▅▆▄▅▆▆▆▄▂▂▃▃▆▆▇▇▆▆▆▅▂▆▆▆▆▆▆▆▆▆ ▆▅▄▄▄▄▄▄▄▄▃▂▅▄▄▃▄▄▄▃▂▅▄▄▅▅▅▅▅▅ ▆▅▂▂▂▂▃▃▃▃▃▂▁▃▂▃▃▃▃▂▂▃▂▃▃▃▃▃▅ ▆▅▆▆▆▇▇▇▇▆▆▅▂▁▄▆▆▆▄▅▄▂▆▆▆▆▆▆▆▆▅ ▆▅▆▅▆▇▆▆▆▆▆▄▄▄▃▆▂▂▅▄▂▆▅▅▆▅▅▆▆▅ ▆▅▅▆▆▇▆▅▆▇▆▄▃▆▂▂▃▅▆▄▂▆▅▅▅▅▅▅▆▅ ▆▅▅▆▇▆▅▅▆▇▇▄▃▆▅▂▃▆▅▄▂▆▅▅▅▅▅▆▆▅ ▆▅▅▆▇▆▅▆▆▇▆▃▂▆▄▂▂▁▃▆▅▂▆▅▅▆▆▆▆▆▅ ▆▅▆▆▇▆▆▇▇▆▆▄▂▄▁▄▅▂▁▂▅▂▆▅▆▆▆▆▆▆▅ ▆▅▅▆▆▆▇▆▆▆▆▄▁▃▄▆▆▄▂▁▁▂▆▅▅▆▆▆▆▆▅ ▆▅▂▂▂▂▃▂▂▂▂▂▁▃▃▃▃▂▁▁▂▂▂▂▂▂▃▄▅ ▆▆▆▆▆▅▅▅▅▅▅▄▁▅▅▅▅▄▅▅▄▁▅▆▅▅▅▅▆▆ ▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▃▂▆▆▆▆▅▅▆ ▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▆▂▁▅▆▃▃▆▆ ▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▂▆▅▁▁▃▂▅▆▆ ▆▆▆▆▆▆▆▆▆▆▆▄▃▆▆▆▆▆▆▆▄▃▆▆▄▁▅▇▆▅ ▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▆▄▁▁▁▅▆▅ ▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▄▂▄▃▁▅▆ ▆▆▆▆▆▆▆▆▆▆▆▅▃▆▆▆▆▆▆▆▅▃▅▁▄▆▆▃▁▄ ▆▆▆▆▆▆▆▆▆▆▆▅▄▆▆▆▆▆▆▆▄▃▆▅▆▆▆▆▄▃▂''') time.sleep(2) subprocess.call("cls",shell=True) whileTrue: board=['']*10 player_letter,computer_letter=input_player_letter() turn=who_first() print(turn+'先走') time.sleep(1) game_is_playing=True whilegame_is_playing: ifturn=='player': draw_board(board) move=get_player_move(board) make_move(board,player_letter,move) ifis_winner(board,player_letter): draw_board(board) print('恭喜!你赢了。') game_is_playinig=False else: ifis_board_full(board): draw_board(board) print('平局!') break else: turn='computer' else: move=get_computer_move(board,computer_letter) make_move(board,computer_letter,move) ifis_winner(board,computer_letter): draw_board(board) print('电脑胜利,你挂了!') game_is_playing=False else: ifis_board_full(board): draw_board(board) print('平局!') break else: turn='player' ifnotis_again(): break
更多关于python游戏的精彩文章请点击查看以下专题:
python俄罗斯方块游戏集合
python经典小游戏汇总
python微信跳一跳游戏集合
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。