Tic Tac Toe Python 2.7

时间:2013-12-04 19:05:53

标签: python python-2.7

我正在制作一个tic tac toe程序,出于某种原因,当我尝试打印3x3板时,它无法正常工作。我必须打印4x4板。该程序应该工作的方式是我们应该输入一行,然后是一列,然后在那些坐标处打印X或O,但就像我说的那样......它不适用于3x3板。每当我进入第3行或第3列时,它表示坐标超出范围......任何帮助都将非常感激。谢谢。

def playTicTacToe():
    rows,cols = 4,4
    winLength = 3
    board = makeNewBoard(rows, cols)
    moves = 0
    player = 1
    while (moves < rows*cols):
        row,col = getMove(board, player)
        board = setPiece(board, row, col, player)
        if (didWin(board, player, winLength)):
            printBoard(board)
            print getPieceLabel(player), " WINS!!!"
            return
        player = otherPlayer(player)
        moves += 1
    print "TIE GAME!!!!"


def makeNewBoard(rows, cols): return [([0]*cols) for row in xrange(rows)]
def getRows(board): return len(board)
def getCols(board): return len(board[0])
def getPiece(board, row, col): return board[row][col]
def setPiece(board, row, col, value):
    board[row][col] = value
    return board

def isEmpty(board, row, col):
    return (getPiece(board, row, col) == 0)

def isOnBoard(board, row, col):
    rows = getRows(board)
    cols = getCols(board)
    return ((row >= 0) and (row < rows) and
            (col >= 0) and (col < cols))    

def getPieceLabel(piece):
    if (piece == 1): return "X"
    elif (piece == 2): return "O"
    else: return "-"

def printBoard(board):
    print "\n**************************"
    rows = getRows(board)
    cols = getCols(board)
    for row in xrange(rows):
        for col in xrange(cols):
            piece = getPiece(board, row, col)
            label = getPieceLabel(piece)
            print label,
        print


def didWin(board, player, winLength):
    rows = getRows(board)
    cols = getCols(board)
    for startRow in xrange(rows):
        for startCol in xrange(cols):
            if (didWin1(board, player, winLength, startRow, startCol)):
                return True
    return False            

def didWin1(board, player, winLength, startRow, startCol):
    for drow in xrange(-1,+2):
        for dcol in xrange(-1,+2):
            if ((drow != 0) or (dcol != 0)):
                if (didWin2(board, player, winLength,
                            startRow, startCol, drow, dcol)):
                    return True
    return False

def didWin2(board, player, winLength,
            startRow, startCol, drow, dcol):
    rows = getRows(board)
    cols = getCols(board)
    for step in xrange(winLength):
        row = startRow + step*drow
        col = startCol + step*dcol
        if (not isOnBoard(board, row, col)):
            return False
        elif (getPiece(board, row, col) != player):
            return False
    return True


def otherPlayer(player):
    return 1 if (player == 2) else 2


def oops(msg):
    print "  ", msg, "Try again."


def readInt(prompt):
    while (True):
        try:
            return int(raw_input(prompt))
        except:
            oops("Input must be an integer.")

def getMove(board, player):
    while (True):
        printBoard(board)
        print "Enter move for player", getPieceLabel(player)
        row = readInt("  Row --> ")
        col = readInt("  Col --> ")
        if (not isOnBoard(board, row, col)):
            oops("Out of range (not on the board)!")
        elif (not isEmpty(board, row, col)):
            oops("Already occupied!")
        else:
            return (row, col)

playTicTacToe()   

3 个答案:

答案 0 :(得分:2)

当您为行或列输入3时,您正在调用

isOnBoard(board, 3, 3)

调用

rows = getRows(board)

将行设置为3

导致

(row < rows)

评估为假

您希望减少row和col以符合Python的从零开始的索引

答案 1 :(得分:2)

您应该使用小于您输入的值作为索引,因为在python中,大多数其他语言的索引从0开始

设定

row,cols=3,3

并在getMove()函数中使用此代替

row = readInt("  Row --> ")-1
col = readInt("  Col --> ")-1

答案 2 :(得分:1)

我已经尝试过将第二行更改为:

rows,cols = 3,3

我在python 2.7解释器中这样做了,没关系!您是否忘记了Python使用索引0,1和2而不是1,2和3?

使用0代替1 用1代替2 用2代替3

您可以尝试制作它,以便游戏使用1,2和3使其更加用户友好,祝您好运! :)