我是新手,我用python创建了一个tic tac toe toe游戏,我弄清楚了逻辑,但是代码没有按预期的方式运行,但是我尝试调试它但找不到错误。
我是python的新手,刚开始我被要求自己编写tic tac toe游戏的过程是关于udemy的课程。我已经找到了一个不太好的逻辑,但是它可以完成工作。我只是不知道为什么win_check函数不能正常工作。是win_check函数还是其他?对改进我的代码有什么建议? 抱歉,我没有在代码中给出注释,我只是想先完成并成功运行它。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 27 20:11:38 2019
@author: sandeep
"""
from os import name,system
from time import sleep
from random import randint
def game_board(turns):
print(turns[6],"|",turns[7],"|",turns[8])
print("--|---|---")
print(turns[3],"|",turns[4],"|",turns[5])
print("--|---|---")
print(turns[0],"|",turns[1],"|",turns[2])
def check_input(player1,player2):
if (player1 == 'X' or player1 == 'O') and (player2 == 'X' or player2 == 'O'):
return True
else:
print("Please choose between X and O")
global initial_player_value
initial_player_value = take_input()
return False
def take_input():
player1 = input("Do you want 'X' or 'O'?\n")
if player1 == 'X':
player2 = 'O'
else:
player2 = 'X'
return (player1,player2)
def clear():
if name == 'nt':
_ = system('cls')
else:
_ = system('clear')
def player_turn(initial_player_value0,initial_player_value1):
clear()
global turns
game_board(turns)
print(f"Player {initial_player_value0} turn")
print("Enter position")
player1_input =input()
turns.pop(int(player1_input)-1)
turns.insert(int(player1_input)-1,initial_player_value0)
clear()
game_board(turns)
print(f"Player {initial_player_value1} turn")
print("Enter position")
player2_input = input()
turns.pop(int(player2_input)-1)
turns.insert(int(player2_input)-1,initial_player_value1)
clear()
game_board(turns)
def win_check(check,initial_player_value3,initial_player_value4):
# print(check)
global game_status
if initial_player_value3 == 'X':
if ((['X','X','X'] == check[:3]) or (['X','X','X'] == check[3:6]) or (['X','X','X'] == check[6:])):
game_status = "Player X wins"
elif ((['X','X','X'] == check[::3]) or (['X','X','X'] == check[1::3]) or (['X','X','X'] == check[2::3])):
game_status = "Player X wins"
elif ((['X','X','X'] == check[::4]) or (['X','X','X'] == check[2:7:2])):
game_status = "Player X wins"
else:
game_status = "_"
else:
if ((['O','O','O'] == check[:3]) or (['O','O','O'] == check[3:6]) or (['O','O','O'] == check[6:])):
game_status = "Player O wins"
elif ((['O','O','O'] == check[::3]) or (['O','O','O'] == check[1::3]) or (['O','O','O'] == check[2::3])):
game_status = "Player O wins"
elif ((['O','O','O'] == check[::4]) or (['O','O','O'] == check[2:7:2])):
game_status = "Player O wins"
else:
game_status = "_"
if initial_player_value4 == 'O':
if ((['O','O','O'] == check[:3]) or (['O','O','O'] == check[3:6]) or (['O','O','O'] == check[6:])):
game_status = "Player O wins"
elif ((['O','O','O'] == check[::3]) or (['O','O','O'] == check[1::3]) or (['O','O','O'] == check[2::3])):
game_status = "Player O wins"
elif ((['O','O','O'] == check[::4]) or (['O','O','O'] == check[2:7:2])):
game_status = "Player O wins"
else:
game_status = "_"
else :
if ((['X','X','X'] == check[:3]) or (['X','X','X'] == check[3:6]) or (['X','X','X'] == check[6:])):
game_status = "Player X wins"
elif ((['X','X','X'] == check[::3]) or (['X','X','X'] == check[1::3]) or (['X','X','X'] == check[2::3])):
game_status = "Player X wins"
elif ((['X','X','X'] == check[::4]) or (['X','X','X'] == check[2:7:2])):
game_status = "Player X wins"
else:
game_status = "_"
draw_check = ''.join(str(x) for x in check)
print(draw_check)
#sleep(5)
if draw_check.isalpha():
game_status ="Game draws"
else:
game_status= "_"
#def welcome():
print("Welcome to the tic tac toe game\n")
initial_player_value = take_input()
check = check_input(initial_player_value[0],initial_player_value[1])
while check is False:
check = check_input(initial_player_value[0],initial_player_value[1])
print(f"Player 1 is {initial_player_value[0]} and Player 2 is {initial_player_value[1]}\nLet's start the game" )
turns = [1,2,3,4,5,6,7,8,9]
sleep(2)
clear()
game_board(turns)
game_status = '_'
decide_first = randint(1,2)
replay = 'yes'
while replay == "yes":
#global game_status
if decide_first == 1:
player_turn(initial_player_value[0],initial_player_value[1])
game_board(turns)
win_check(turns,initial_player_value[0],initial_player_value[1])
else:
player_turn(initial_player_value[1],initial_player_value[0])
game_board(turns)
win_check(turns,initial_player_value[0],initial_player_value[1])
#win_check(turns,tuple(initial_player_value))
if game_status != '_':
print(game_status)
replay = input("Do you want to play again?\n")
turns = [1,2,3,4,5,6,7,8,9]
运行代码时没有错误。
答案 0 :(得分:0)
您面临的问题在于win_check。 win_check执行以下步骤:
"Player X wins"
或"Player Y wins"
或"_"
"Player X wins"
或"Player Y wins"
或"_"
"game draws
“或"_"
每一步之后,它都会删除最后一步的结果。
如果您使用单个if
和elif
链来管理控制流,那么它将更具可读性和功能上的正确性。
我还将检查玩家是否赢得了has_won
自己的功能,这将提高可读性并减少代码重复。
def win_check(board, player_1, player_2):
global game_status
if has_won(board, player_1):
game_status = "Player " + player_1 + " has won"
elif has_won(board, player_2):
game_status = "Player " + player_2 + " has won"
elif is_draw(board):
game_status = "Game draws"
else:
game_status = "_"