TicTacToe Python 檢查獲勝者 (TicTacToe Python Check for Winner)


問題描述

TicTacToe Python 檢查獲勝者 (TicTacToe Python Check for Winner)

我是編程新手,在 TicTacToe 遊戲中檢查獲勝者時遇到問題。

我的問題是 def printBoard(board):,它沒有正確檢查為獲勝者,即使有人獲勝,遊戲也會繼續運行。

(有2個文件,一個名為ticTacToeRunner和ticTacToe,第一個只是運行第二個)。

這是我目前擁有的,非常感謝任何幫助:

#ticTacTorRunner file

import ticTacToe

theBoard = {'top‑L': ' ', 'top‑M': ' ', 'top‑R': ' ',
'mid‑L': ' ', 'mid‑M': ' ', 'mid‑R': ' ',
'low‑L': ' ', 'low‑M': ' ', 'low‑R': ' '}

ticTacToe.startGame('X', theBoard)



#ticTacToe file

def printBoard(board):

     print(board['top‑L'] + '|' + board['top‑M'] + '|' + board['top‑R'])
     print('‑+‑+‑')
     print(board['mid‑L'] + '|' + board['mid‑M'] + '|' + board['mid‑R'])
     print('‑+‑+‑')
print(board['low‑L'] + '|' + board['low‑M'] + '|' + board['low‑R'])

這是我遇到問題的地方,正在檢查獲勝者:

def checkWinner(board, player): 
 print('Checking if ' + player + ' is a winner...')

#X WIN

if 'top‑L' == 'X' and 'top‑M' == 'X' and 'top‑R' == 'X':
    return True

     elif board == """{'top‑L': ' ', 'top‑M': ' ', 'top‑R': ' ',
            'mid‑L': 'X', 'mid‑M': 'X', 'mid‑R': 'X',
            'low‑L': 'O', 'low‑M': 'O', 'low‑R': ' '}""":
         return True


     elif board == """{'top‑L': 'X|O| ', 'top‑M': 'X|O| ', 'top‑R': 'X|O| ',
            'mid‑L': 'X|O| ', 'mid‑M': 'X|O| ', 'mid‑R': 'X|O| ',
            'low‑L': 'X', 'low‑M': 'X', 'low‑R': 'X'}""":
         return True


     elif board == """{'top‑L': 'X', 'top‑M': 'X|O| ', 'top‑R': 'X|O| ',
            'mid‑L': 'X', 'mid‑M': 'X|O| ', 'mid‑R': 'X|O| ',
            'low‑L': 'X', 'low‑M': 'X|O| ', 'low‑R': 'X|O| '}""":
         return True


     elif board == """{'top‑L': 'X|O| ', 'top‑M': 'X', 'top‑R': 'X|O| ',
            'mid‑L': 'X|O| ', 'mid‑M': 'X', 'mid‑R': 'X|O| ',
            'low‑L': 'X|O| ', 'low‑M': 'X', 'low‑R': 'X|O| '}""":
         return True

     elif board == """{'top‑L': 'X|O| ', 'top‑M': 'X|O| ', 'top‑R': 'X',
            'mid‑L': 'X|O| ', 'mid‑M': 'X|O| ', 'mid‑R': 'X',
            'low‑L': 'X|O| ', 'low‑M': 'X|O| ', 'low‑R': 'X'}""":
         return True


     elif board == """{'top‑L': 'X', 'top‑M': 'X|O| ', 'top‑R': 'XX|O| ',
            'mid‑L': 'X|O| ', 'mid‑M': 'X', 'mid‑R': 'X|O| ',
            'low‑L': 'X|O| ', 'low‑M': 'X|O| ', 'low‑R': 'X'}""":
         return True


     elif board == """{'top‑L': 'X|O| ', 'top‑M': 'X|O| ', 'top‑R': 'X',
            'mid‑L': 'X|O| ', 'mid‑M': 'X', 'mid‑R': 'X|O| ',
            'low‑L': 'X', 'low‑M': 'X|O| ', 'low‑R': 'X|O| '}""":
         return True



#O WIN

     elif board == """{'top‑L': 'O', 'top‑M': 'O', 'top‑R': 'O',
            'mid‑L': 'X|O| ', 'mid‑M': 'X|O| ', 'mid‑R': 'XX|O| ',
            'low‑L': 'X|O| ', 'low‑M': 'X|O| ', 'low‑R': 'X|O| '}""":
         return True


     elif board == """{'top‑L': 'X|O| ', 'top‑M': 'X|O| ', 'top‑R': 'X|O| ',
            'mid‑L': 'O', 'mid‑M': 'O', 'mid‑R': 'O',
            'low‑L': 'X|O| ', 'low‑M': 'X|O| ', 'low‑R': 'X|O| '}""":
         return True


     elif board == """{'top‑L': 'X|O| ', 'top‑M': 'X|O| ', 'top‑R': 'X|O| ',
            'mid‑L': 'X|O| ', 'mid‑M': 'X|O| ', 'mid‑R': 'X|O| ',
            'low‑L': 'O', 'low‑M': 'O', 'low‑R': 'O'}""":
         return True


     elif board == """{'top‑L': 'O', 'top‑M': 'X|O| ', 'top‑R': 'X|O| ',
            'mid‑L': 'O', 'mid‑M': 'X|O| ', 'mid‑R': 'X|O| ',
            'low‑L': 'O', 'low‑M': 'X|O| ', 'low‑R': 'X|O| '}""":
         return True


     elif board == """{'top‑L': 'X|O| ', 'top‑M': 'O', 'top‑R': 'X|O| ',
            'mid‑L': 'X|O| ', 'mid‑M': 'O', 'mid‑R': 'X|O| ',
            'low‑L': 'X|O| ', 'low‑M': 'O', 'low‑R': 'X|O| '}""":
         return True


     elif board == """{'top‑L': 'X|O| ', 'top‑M': 'X|O| ', 'top‑R': 'O',
            'mid‑L': 'X|O| ', 'mid‑M': 'X|O| ', 'mid‑R': 'O',
            'low‑L': 'X|O| ', 'low‑M': 'X|O| ', 'low‑R': 'O'}""":
         return True


     elif board == """{'top‑L': 'O', 'top‑M': 'X|O| ', 'top‑R': 'XX|O| ',
            'mid‑L': 'X|O| ', 'mid‑M': 'O', 'mid‑R': 'X|O| ',
            'low‑L': 'X|O| ', 'low‑M': 'X|O| ', 'low‑R': 'O'}""":
         return True


     elif board == """{'top‑L': 'X|O| ', 'top‑M': 'X|O| ', 'top‑R': 'O',
            'mid‑L': 'X|O| ', 'mid‑M': 'O', 'mid‑R': 'X|O| ',
            'low‑L': 'O', 'low‑M': 'X|O| ', 'low‑R': 'X|O| '}""":
         return True


     else:
         return False

這是循環遊戲:

def startGame(startingPlayer, board):

     turn = startingPlayer
     for i in range(9):
         printBoard(board)
         print('Turn for ' + turn + '. Move on which space?')
         move = input()
         board[move] = turn

         if ( checkWinner(board, 'X') ):
             print('X wins!')
             break
         elif ( checkWinner(board, 'O') ):
             print('O wins!')
             break

         if turn == 'X':
             turn = 'O'
         else:
             turn = 'X'

     print (board)

參考解法

方法 1:

You're comparing the board, which is a python dict, against a string representation of a dict, which will never pass. Remove the triple‑quotes from around your if conditions and it may work.

I will add that you've picked a horrendous way to represent a tic‑tac‑toe board; I'd suggest using a list‑of‑lists instead, which is a lot more compact, lets you access board positions as board[0][0] to board[2][2] instead, and check your conditions using loops instead of listing out each one separately.

(by yoshi2132tzaman)

參考文件

  1. TicTacToe Python Check for Winner (CC BY‑SA 2.5/3.0/4.0)

#Python #tic-tac-toe






相關問題

如何從控制台中導入的文件中訪問變量的內容? (How do I access the contents of a variable from a file imported in a console?)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

為 KeyError 打印出奇怪的錯誤消息 (Strange error message printed out for KeyError)

django 1.9 中的 from django.views.generic.simple import direct_to_template 相當於什麼 (What is the equivalent of from django.views.generic.simple import direct_to_template in django 1.9)

查詢嵌入列表中的數組 (Querying for array in embedded list)

如何在 Python 中搜索子字符串是否在二進製文件中? (How to search if a substring is into a binary file in Python?)

為什麼要避免 while 循環? (Why avoid while loops?)

使用python的json模塊解析json請求 (Parse a json request using json module of python)

為什麼使用 py2app 模塊創建 mac 文件時出現錯誤? (Why i am getting Error when creating mac file using py2app module?)

當 python 線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼? (What happens when the python thread is in network call(HTTPS) and the context switch happens?)

如何繪製一條帶斜率和一個點的線?Python (How to plot a line with slope and one point given? Python)

Pickle 找不到我不使用的模塊? (Pickle can't find module that I am not using?)







留言討論