python上的tic tac toe

时间:2013-10-15 19:57:54

标签: python if-statement for-loop python-3.x

我正在试图找出一个for循环和if语句来检查python中的胜利者的tic tac toe游戏。我需要这个,因为我有一个连接4游戏,我打算使用相同的伪代码。

这是我到目前为止所做的:

col_size = 3
        row_size = 3
        ttt = list()
        for n in range(col_size):
            rows = input(msg[n]+ ":")
            ttt.append(rows)
        print(ttt)
        row_ttt= list()
        for i in range(row_size):
            one_row = list()
            for j in range(col_size):
                one_row+= ttt[i][j]
            row_ttt.append(one_row)
        diagonal_ttt= list()
        for j in range(col_size):
            one_diagonal= ""
            for i in range(row_size):
                one_diagonal+= ttt[i][j+1:2]
            diagonal_ttt.append(one_diagonal)
        print(row_ttt)
        valid_symbols = ['x','X','o','O','.']

            for j in range(col_size):
                for i in range(row_size):
                    if row_ttt[i][j]== row_ttt[i:i+1][j] == row_ttt[i:i+2][j]:
                        #print(row_ttt[i][j]) to check the element
                        #print(row_ttt[i:i+1][j]) to check element
                        #print(row_ttt[i:i+2][j]) to check element
                        valid = True
                print("valid board - " + ttt[i][j] + " is the winner")
                break

我手动写出来但我想要一个更短的代码:

            if ttt[0][0] == ttt[1][1] == ttt[2][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[0][2] == ttt[1][1] == ttt[2][0]:
                print("valid board " + ttt[2][0] + " is the winner")
                break
            if ttt[0][0] == ttt[0][1] == ttt[0][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[1][0] == ttt[1][1] == ttt[1][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[2][0] == ttt[2][1] == ttt[2][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[0][0] == ttt[1][1] == ttt[2][2]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[0][0] == ttt[1][0] == ttt[2][0]:
                print("valid board " + ttt[0][0] + " is the winner")
                break
            if ttt[0][1] == ttt[1][1] == ttt[2][1]:
                print("valid board " + ttt[0][1] + " is the winner")
                break
            if ttt[0][2] == ttt[1][2] == ttt[2][2]:
                print("valid board " + ttt[0][2] + " is the winner")
                break

2 个答案:

答案 0 :(得分:1)

这是我为最近制作的井字游戏提出的解决方案。适应它:

win = False
rowsBoard = ttt

colsBoard = [[board[i][j] for i in range(3)] for j in range(3)]
diagsBoard = [[board[i][i] for i in range(3)],[board[i][2-i] for i in range(3)]]
boards = [rowsBoard, colsBoard, diagsBoard]

for board in boards:
    for row in b:
        if row[0] == row[1] == row[2] != "":
            print("valid board "+ row[0] + " is the winner")
            win = True

if "" not in rowsBoard[0] and "" not in rowsBoard[1] and "" not in rowsBoard[2] and not win:
    print("game is a draw!")

答案 1 :(得分:1)

>>> import numpy
>>> game_board = [[0,0,0],[0,1,2],[1,2,1]]
>>> #let 1 be X's and 2 be O's .... 0 is empty
>>> def check_win(board,player):
...     b= numpy.array(board)
...     x= b == player
...     return numpy.hstack([x.all(0),x.all(1),numpy.diag(x).all(),numpy.diag(x[:,::-1]).all()]).any()
...
>>> print check_win(game_board,1) #check for X win
>>> print check_win(game_board,2) # check for O win
相关问题