python游戏代码运行不了_无法使我的tic tac toe游戏在python中正确运行
转不到“玩家1”的原因是你的支票中缺少一个空格。你也没有正确地检查一个玩家何时获胜,这就是为什么你会有这种奇怪的行为。你需要检查每个位置,而不仅仅是最后一个。我还添加了对用户输入的检查,以确保用户输入的是一个数字,如果不是,它不会崩溃。
我完全不明白为什么你基本上把你的板打印颠倒,但我留下了原来的。
试试这个:
import random
def display_board(board): # Board setup
print('\n'*100)
print(board[7] + '|' + board[8] + '|' + board[9])
print(board[4] + '|' + board[5] + '|' + board[6])
print(board[1] + '|' + board[2] + '|' + board[3])
def player_input(): # Player assignment - use while loop to keep asking until a valid character is entered
marker = ''
while marker != 'X' and marker != 'O':
marker = input('Player1: Choose X or O: ').upper()
if marker == 'X':
return ('X','O')
else:
return ('O','X')
def place_marker(board, marker, position): # Allows player to place their marker at specific board index
board[position] = marker
def win_check(board, mark): # Check all rows, columns, diagonals for sharing the same marker to check for a winner
return ((board[1] == mark and board[2] == mark and board[3] == mark) or # Rows
(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 # Columns
(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 # Diagonals
(board[3] == mark and board[5] == mark and board[7] == mark))
def choose_first(): # Randomize who goes first
flip = random.randint(0,1)
if flip == 0:
return 'Player 1'
else:
return 'Player 2'
def space_check(board, position): # Check to see if a space on the board is still available
return board[position] == ' '
def full_board_check(board): # Checks to see if board is full resulting in a draw
for i in range(1,10):
if space_check(board,i): # If there is free space, board is NOT full
return False
return True
def player_choice(board): # Asks for the player's next move choice
position = 0
while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):
try:
position = int(input('Choose a position: (1-9): '))
except:
print("Please enter a valid number.")
return position
def replay(): # Asks if the players want to play again
choice = input("Play again? Enter Yes or No: ")
return choice == 'Yes'
# Logic to run the game
# While loop needed to keep running the game
# Need to break out of the while loop on replay()
print ('Welcome to TIC TAC TOE')
while True:
# Game Setup (Board, Players, Player turns)
the_board = [' ']*10
player1_marker,player2_marker = player_input()
turn = choose_first()
print(turn + ' will go first')
play_game = input('Ready to play? y or n: ')
if play_game == 'y':
game_on = True
else: game_on = False
# Game Play
while game_on:
if turn == 'Player 1':
# Show the board
display_board(the_board)
# Choose a place to move
position = player_choice(the_board)
# Place the marker on the position
place_marker(the_board,player1_marker,position)
# Check if they won
if win_check(the_board,player1_marker):
display_board(the_board)
print('PLAYER 1 HAS WON!')
game_on = False
else:
if full_board_check(the_board): # Check for tie
display_board(the_board)
print("TIE GAME!")
game_on = False
else:
turn = 'Player 2'
else:
# Show the board
display_board(the_board)
# Choose a place to move
position = player_choice(the_board)
# Place the marker on the position
place_marker(the_board,player2_marker,position)
# Check if they won
if win_check(the_board,player2_marker):
display_board(the_board)
print('PLAYER 2 HAS WON!')
game_on = False
else:
if full_board_check(the_board):
display_board(the_board)
print("TIE GAME!")
game_on = False
else:
turn = 'Player 1'
if not replay():
break
python游戏代码运行不了_无法使我的tic tac toe游戏在python中正确运行相关推荐
- python井字棋游戏代码_Python实现的井字棋(Tic Tac Toe)游戏示例
Python实现的井字棋(Tic Tac Toe)游戏示例 来源:中文源码网 浏览: 次 日期:2018年9月2日 [下载文档: Python实现的井字棋(Tic Tac Toe)游戏示 ...
- react中使用构建缓存_通过在React中构建Tic Tac Toe来学习ReasonML
react中使用构建缓存 3. 7. 2018: UPDATED to ReasonReact v0.4.2 3. 7. 2018:更新为ReasonReact v0.4.2 You may have ...
- python二维游戏示例_Python实现的井字棋(Tic Tac Toe)游戏示例
本文实例讲述了Python实现的井字棋(Tic Tac Toe)游戏.分享给大家供大家参考,具体如下: 说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码 ...
- python井字棋ai,python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...
- python恶搞代码打开对方摄像头_用Python获取摄像头并实时控制人脸的实现示例
实现流程 从摄像头获取视频流,并转换为一帧一帧的图像,然后将图像信息传递给opencv这个工具库处理,返回灰度图像(就像你使用本地静态图片一样) 程序启动后,根据监听器信息,使用一个while循环,不 ...
- c++游戏代码坦克大作战_一红一蓝多种模式的双人小游戏:红蓝大作战
作者有话说:上次推荐的森林冰火人很多小伙伴后台找我要链接,或者搜索不到:首先声明下森林冰火人.同桌大作战都不是辣椒人游戏工作室研发的,小编也是微信小游戏双人栏目下搜索到的,如果想要玩双人小游戏的可以打 ...
- python从安装开始加粉_清华教授用了12小时讲完的Python,整整400集,拿走不谢!...
Python在近几年越来越受追捧,很多童鞋或者职场小伙伴想要提升技能-学习Python. 这是非常好的事情,但问题在于很多人不知道学Python做什么,所以什么零碎细末.艰难晦涩.长篇大论的都去看,很 ...
- python语言是编译性语音_最强编程语言 Java 和最受欢迎之 Python 的巅峰对决
作为程序员们的看家利器之编程语言,倘若技术开发者对其掌握得越深则对自身职业生涯越有益处,然随着技术的更新变革,我们在日常使用不单单只会用到一门编程语言,那么究竟该如何选择一门新的语言来上手?本文中,将 ...
最新文章
- SpringMVC入门学习---拦截器
- AD9833所产生的高频信号质量分析
- 零基础学python看什么视频-零基础自学Python是看书还是看视频?
- Java学习笔记(一)--JDK环境
- STM32F10x之RCC
- nodejs express
- 软件设计是怎样炼成的(1)——什么是优秀的设计?
- 三星可折叠手机正式亮相
- 网络工程师考试第一节计算机硬件基础
- Python读写txt文件
- J2ME 2D小游戏入门之旅(五) 实现爆炸效果、并加入道具导弹(转)
- (海伦公式)已知三角形三条边长,求面积
- 【笔记】H5跳转手机应用商店(指定应用页/第三方应用商店)
- Latex矩阵和表达式组的绘制
- 全球疫苗接种状况数据分析
- 编译原理实验一:词法分析程序设计与实现
- 项目分享|小师弟手把手教你用蓝牙模块
- python 高等数学实验_Python数学实验与建模
- RPC编程:RPC概述和架构演变
- 快来智影:微电影的特征有哪些?
热门文章
- OSS.Social微信项目标准库介绍
- Apdex(Application Performance Index)量化应用性能
- 一步一步封装自己的HtmlHelper组件:BootstrapHelper
- powershell 常用命令笔记
- C# 读写ini文件 保存信息
- 跨服务器 快速 导入数据表记录 Insert into SELECT
- 利用浏览器调试功能 计算 百度网盘 文件数量 V2
- 【ArcGIS遇上Python】ArcGIS Python获取Shapefile矢量数据字段名称
- 【Pix4d精品教程】大疆精灵4A无人机航空摄影测量外业数据采集完整操作流程
- C#趣味程序---真分数序列