- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是编码新手,最近开始学习Python。我的第一个挑战是制作一款井字棋游戏。以下是我的游戏代码,除了没有获胜者(即游戏平局)外,一切正常;我想显示没有人赢得了比赛。
我尝试在不同的地方合并else
。但是,他们没有帮助。这是我的代码。
# a dictionary to display the grid
grid = {
'1':'1', '2':'2', '3':'3',
'4':'4', '5':'5', '6':'6',
'7':'7', '8':'8', '9':'9'
}
# a list to store the values that has already been entered
used_places = []
# players and their symbols
player_1 = 'x'
player_2 = 'o'
# to store result
result = None
def grid_display():
#this function displays the grid
print('\n\n')
print(grid['1'], '\t|\t', grid['2'], '\t|\t', grid['3'])
print(''.rjust(19,'*'))
print(grid['4'], '\t|\t', grid['5'], '\t|\t', grid['6'])
print(''.rjust(19, '*'))
print(grid['7'], '\t|\t', grid['8'], '\t|\t', grid['9'])
def win_the_game():
# this function checks for result and returns true or false
if(
(grid['1'] == grid['2'] == grid['3']) or (grid['4'] == grid['5'] == grid['6']) or
(grid['7'] == grid['8'] == grid['9']) or (grid['1'] == grid['4'] == grid['7']) or
(grid['2'] == grid['5'] == grid['8']) or (grid['3'] == grid['6'] == grid['9']) or
(grid['1'] == grid['5'] == grid['9']) or (grid['3'] == grid['5'] == grid['7'])
):
return True
else:
return False
def taking_turns(turn):
#this function asks user for input
print("its turn of ", turn)
choice = input("enter your choice")
while not (choice.isdecimal() and choice in grid and (choice not in used_places)):
# taking proper input by checkin it with already entered numbers
# and if it is a number
# and if number is in between 1 and 9
choice = input("\nenter your choice properly:")
player_move(choice, turn)
def player_move(move, assign):
# this function fills the entered numbers into used_places list
# and replaces numbers in grid with user input
used_places.append(move)
grid[move] = assign
print("player 1 : 'X'")
print("player 2 : 'O'")
for i in range(0,10): # loops 9 times to play the game
if i % 2 == 0: # giving turns. if i is even, player 1 gets turn and odd, player 2
grid_display() # displaying complete grid
turn = player_1 # to display whose turn it is; refer the function
taking_turns(turn)
if win_the_game() == True: # if the called function returns true in this 'if'
result = turn # player 1 wins
break
else:
grid_display() # same code as above
turn = player_2
taking_turns(turn)
if win_the_game() == True:
result = turn
break
print('\n\n',result, "won the game!!") # printing result
最佳答案
不要设置 result = None,而是设置 result = 'Nobody'。
从你的逻辑来看,你只有在有人获胜时才设置结果,这会将默认结果留给任何人。
--编辑--
抱歉,我有点得意忘形,重写了你的游戏逻辑来证明我的解决方案。接受或保留它,但它现在工作得很好,也许您可以使用它作为示例来修复您自己的问题。
import os
# a dictionary to display the grid
grid = {
'1':'1', '2':'2', '3':'3',
'4':'4', '5':'5', '6':'6',
'7':'7', '8':'8', '9':'9'
}
# a list to store the values that are available
places = ['1','2','3','4','5','6','7','8','9']
# to store result
result = 'Nobody'
# starting player
turn = 'O'
# display a game grid
def grid_display():
os.system('cls' if os.name == 'nt' else 'clear')
#this function displays the grid
print
print(grid['1'], '|', grid['2'], '|', grid['3'])
print(''.rjust(25,'*'))
print(grid['4'], '|', grid['5'], '|', grid['6'])
print(''.rjust(25, '*'))
print(grid['7'], '|', grid['8'], '|', grid['9'])
# check to see if anyone has won or are we out of moves
def win_the_game():
# this function checks for result and returns true or false
if(
(grid['1'] == grid['2'] == grid['3']) or (grid['4'] == grid['5'] == grid['6']) or
(grid['7'] == grid['8'] == grid['9']) or (grid['1'] == grid['4'] == grid['7']) or
(grid['2'] == grid['5'] == grid['8']) or (grid['3'] == grid['6'] == grid['9']) or
(grid['1'] == grid['5'] == grid['9']) or (grid['3'] == grid['5'] == grid['7'])
):
return True
# checks if there are any moves left
elif not places:
return False
else:
return False
# input / grid update function
def taking_turns():
# this function asks user for input
# use RAW_INPUT to make it a string
print
print map(str, places)
choice = raw_input("\nEnter "+turn+"'s choice: ")
if choice in places:
# this removes the number from the available list
# and replaces numbers in grid with user input
places.remove(choice)
grid[choice] = turn
# Logic loop
while places:
grid_display() # always display the grid
if turn == "O": # giving turns.
turn = 'X'
taking_turns()
if win_the_game() == True: # if the called function returns true in this 'if'
result = turn # player 1 wins
grid_display() # Winners GRID
break
else:
turn = 'O'
taking_turns()
if win_the_game() == True:
result = turn
grid_display() # Winners GRID
break
# results
print
print(result, "won the game!!") # printing result
print
关于python - 在我的井字棋游戏中,我应该在哪里加入没有人获胜的事实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51348009/
我是一个学习Java编程的初学者,我正在玩井字棋游戏。 当我完成游戏后,我无法继续玩游戏,因为程序将退出。我应该在这段代码中添加什么。由于我没有使用paint方法,所以无法使用repaint()。 i
我正在为一项作业编写一个井字棋游戏。它需要使用面向对象的编程,并且必须相对智能——它需要阻止玩家的成功。我在这方面遇到了很多麻烦。 我的麻烦来 self 的 rowabouttowin 方法:我做了一
本文实例为大家分享了C语言实现三子棋(井字棋)小游戏的具体代码,供大家参考,具体内容如下 推荐阅读顺序(不建议跳过) 先看实现之后的界面 —— 然后看分析程序要实现的步骤 —— 之后在看翻到te
本文实例为大家分享了C语言实现三子棋算法,供大家参考,具体内容如下 游戏文件主干(test.c): ?
我正在用 javascript/jquery 制作一个简单的井字游戏,但我不知道如何检查是否有人赢了。这是游戏场:
我尝试创建 Tic Tac Toe,我能够填满我的棋盘,并且能够检查行和列以确定谁获胜。然而,我需要一些帮助来检查对角线,看看谁赢了。这是我到目前为止所拥有的。我是初学者,所以请不要让代码太难。 检查
在一项作业中,我被要求创建一个 [7] x [7] 矩阵,以及一个与计算机对战的井字棋游戏。玩家是 X,计算机是 O。[1][1] 是选择 1,[1][3] 是选择 2,[1][5] 是选择 3,[3
我正在制作井字游戏的 C 程序。我现在正在努力让AI立于不败之地,但是我遇到了一个问题。问题是人工智能只是在下一个可用问题中打印符号。为什么?我该如何解决它? 这是我的调用函数: void deter
我一直在转来转去,试图找出如何给我的玩家轮流。问题是有两个玩家玩井字棋,但我不知道该怎么做。这是我的代码: #include #include void displayBoard(char [3]
我遇到了基于我的 tic tac toe javascript 代码中创建的指标的问题。你能帮我解决吗? cell.indicator = 指示器; 控制台提到了这一点未捕获的类型错误:无法将属性“指
我正在创建一个管理井字游戏的程序,我正在创建一个列表列表 [['', '', ''], ['', '', ''], ['', '', '']] 为了创建游戏网格,我希望程序在找到像这样的匹配时停止
我是一名优秀的程序员,十分优秀!