Python tic tac toe检测作弊

时间:2014-04-25 11:58:58

标签: python

所以现在我写了一个简单的Tic tac toe游戏。我甚至没有使用我所做的所有功能,但这是我的问题: 我怎样才能确定玩家是否将1或2置于已有的位置,我想我知道如何做到这一点,但是如果他们输入非法角色,我怎么能把它们放回“输入您的号码”提示他们试图覆盖已经放置的1或2。

还有更紧凑的方法吗?

以下是游戏的代码:

nr = [0,0,0,0,0,0,0,0,0]
keepGoing = True

def checkP1():
    if nr[0] and nr[1] and nr[2] or nr[3] and nr[4] and nr[5] or nr[6] and nr[7] and nr[8] or nr[0] and nr[3] and \
       nr[6] or nr[1] and nr[4] and nr[7] or nr[2] and nr[5] and nr[8] or nr[0] and nr[4] and nr[8] or nr[2] and nr[4] and nr[6] == 1:
        print("P1 Wins")
        keepGoing = False
        return keepGoing



def checkP2():
    if nr[0] and nr[1] and nr[2] or nr[3] and nr[4] and nr[5] or nr[6] and nr[7] and nr[8] or nr[0] and nr[3] and \
       nr[6] or nr[1] and nr[4] and nr[7] or nr[2] and nr[5] and nr[8] or nr[0] and nr[4] and nr[8] or nr[2] and nr[4] and nr[6] == 2:
        print("P2 Wins")
        keepGoing = False
        return keepGoing

def Game():
    while keepGoing:
        PrintBoard()
        in1 = 0
        in2 = 0
        in1 = input("Please enter the number of the position you want to put your symbol P1.")
        nr[int(in1)-1] = 1
        check = checkP1()
        if check == 0:
            PrintBoard()
            break
        in2 = input("Please enter the number of the position you want to put your symbol P2.")
        check = checkP2()
        if check == 0:
            PrintBoard()
            break
        nr[int(in2)-1] = 2

def PrintBoard():
    print("",nr[0],nr[1],nr[2],"\n",nr[3],nr[4],nr[5],"\n",nr[6],nr[7],nr[8])

def Reset():
    nr = [0,0,0,0,0,0,0,0,0]

    keepGoing = True

2 个答案:

答案 0 :(得分:1)

回答您的具体问题

  

如果他们输入了非法字符,或者他们试图覆盖已放置的"Input your number"1

,我怎么能将它们放回2提示符

我会做一个功能:

def get_valid_input(board):
    while True:
        try:
            move = int(input("Please enter the number of the position you want to put your symbol."))
        except ValueError:
            print("Input must be an integer number.")
        else:
            if move not in range(1, 10):
                print("Move must be 1-9.")
            elif board[move-1] in (1, 2):
                print("Location already used.")
            else:
                return move

这将持续到目前正在进行的任何玩家提供有效的move

in1 = get_valid_input(nr)

一些更一般的指示:

  • 您当前检查获胜者的行为不起作用 - if a or b == c不符合您的想法(例如this question)。例如,nr[0] and nr[1] and nr[2] == 2实际测试bool(nr[0]) and bool(nr[1]) and (nr[2] == 2);只要最后一个值为2而另外两个不为零,它就是True。 CoDEmanX在评论中的建议在这里很有用。
  • 使用标志keepgoing不是非常Pythonic;我会创建一个函数game_over(board),如果游戏结束(获胜或平局),则返回True,否则返回False,然后整个循环变为while True: ... if game_over(board): break。< / LI>
  • 不是依赖范围来访问变量,而是明确地传递您需要的内容(例如board参数到get_valid_inputgame_over)。摆脱keepgoing会删除一个全局变量,但您可以将board作为参数提供给其他函数,并return视情况而定。或者,考虑使用class来保存board和所有功能。

答案 1 :(得分:0)

如何使用切片确定获胜者的示例:

def same(iterable):
    it = iter(iterable)
    start_val = it.__next__()
    for val in it:
        if val != start_val:
            return
    if start_val != 0:
        return start_val

nr = [0] * 9

nr[1] = 1
nr[2] = 2
nr[4] = 2
nr[6] = 2
nr[7] = 1
nr[8] = 1

for i in range(0, len(nr), 3):
    print(nr[i:i+3])

winner = (
    same(nr[0:3]) or same(nr[3:6]) or same(nr[6:9]) or
    same(nr[0::3]) or same(nr[1::3]) or same(nr[2::3]) or
    same(nr[::4]) or same(nr[2:-1:2])
)

if winner is not None:
    print("Player %i won!" % winner)

same()是一个辅助函数,用于检查iterable的所有元素是否具有相同的值,如果是,则返回该值,除非它是0(因此我们只捕获1 s和玩家2。)

您可以在切片表示法中看到模式,可以使用for循环轻松缩放它们。