我正在弄清楚如何打平比赛。我遇到了困难。任何人都可以提供代码来替换它或修复它吗?谢谢。
我在python中编写了一个tic tac toe游戏,它完美无缺。所以我想知道,你能以某种方式对游戏进行编程说,"你想再玩一次吗?"电脑赢了之后还是赢了?
在我的游戏中,一旦计算机获胜或者你赢了,它就不会说什么,我希望游戏能够询问玩家是否想再玩一次。
谢谢。
import os
import time
import random
# Define the board
board = [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
# Print the header
def print_header():
print("""
_____ _ ____ _____ ____ ____ _____ ____ _____ _________ _______ _______
/__ __\/ \/ _\ /__ __\/ _ \/ _\ /__ __\/ _ \/ __/ 1 | 2 | 3 |___ / | / | ____|
/ \ | || / _____ / \ | / \|| / _____ / \ | / \|| \ 4 | 5 | 6 / / | _/ | | ___
| | | || \_\____\| | | |-||| \_\____\| | | \_/|| /_ 7 | 8 | 9 / /____ | \ | |___\ |
\_/ \_/\____/ \_/ \_/ \|\____/ \_/ \____/\____\ |_______| |______| |_______|
To play Tic-Tac-Toe, you need to get three in a row...
Your choices are defined, they must be from 1 to 9...
""")
def print_board():
print(' | | ')
print(" " + board[1] + " | " + board[2] + " | " + board[3] + " ")
print(" | | ")
print("----|----|----")
print(" | | ")
print(" " + board[4] + " | " + board[5] + " | " + board[6] + " ")
print(" | | ")
print("----|----|----")
print(" | | ")
print(" " + board[7] + " | " + board[8] + " | " + board[9] + " ")
print(" | | ")
def is_winner(board, player):
if (board[1] == player and board[2] == player and board[3] == player) or \
(board[4] == player and board[5] == player and board[6] == player) or \
(board[7] == player and board[8] == player and board[9] == player) or \
(board[1] == player and board[4] == player and board[7] == player) or \
(board[2] == player and board[5] == player and board[8] == player) or \
(board[3] == player and board[6] == player and board[9] == player) or \
(board[1] == player and board[5] == player and board[9] == player) or \
(board[3] == player and board[5] == player and board[7] == player):
return True
else:
return False
def is_board_full(board):
count = 0
for a in range(9):
if board[a] == "X" or board[a] == "O":
count += 1
return False
if count == 9:
print("The game ends in a Tie\n")
return True
while True:
os.system("clear")
print_header()
print_board()
choice = input("Please choose an empty space for X. ")
choice = int(choice)
if board[choice] == " ":
board[choice] = "X"
else:
print("Sorry, that space has been checked!")
time.sleep(1)
if is_winner(board, "X"):
os.system("clear")
print_header()
print_board()
print("X wins! Congratulations")
break
os.system("clear")
print_header()
print_board()
if is_board_full(board):
print("Tie!")
break
choice = input("Please choose an empty space for O.")
choice = int(choice)
if board[choice] == " ":
board[choice] = "O"
else:
print("Sorry, that space has been checked!")
time.sleep(1)
if is_winner(board, "O"):
os.system("clear")
print_header()
print_board()
print("O win! Congratulations")
break
答案 0 :(得分:0)
在您的" tie"中发现了问题。码。问题在于is_board_full函数。这对我有用:
def is_board_full(board):
count = 0
for a in range(1,10):
if board[a] == "X" or board[a] == "O":
count += 1
if count == 9:
print("The game ends in a Tie\n")
return True
return False
问题是您将电路板从索引1设置为9.范围(9)返回数字0到8,因此您永远不会检查最后一个单元格。另外,不确定为什么你把"返回false"在那里发表声明。
现在询问用户是否想要其他游戏,只需更改while条件以检查变量,让我们称之为“#34; play"游戏结束后,只需添加此代码或类似内容:
play = False
var = raw_input("If you want to play again press 'y'. If not press any other key.")
if var == 'y':
play = True
确保将其添加为一个函数,并在代码中游戏结束的每个地方使用它。