我在使用 Turtle 圖形檢查井字遊戲中的獲勝者時遇到了一些問題 (I am having some problems checking the winner in a tic tac toe game using Turtle graphics)


問題描述

我在使用 Turtle 圖形檢查井字遊戲中的獲勝者時遇到了一些問題 (I am having some problems checking the winner in a tic tac toe game using Turtle graphics)

def isWinner (square) :
    (square[0] and square[1] and square[2])
    (square[3] and square[4] and square[5]) 
    (square[6] and square[7] and square[8]) 
    (square[0] and square[3] and square[6]) 
    (square[1] and square[4] and square[7]) 
    (square[2] and square[5] and square[8]) 
    (square[0] and square[4] and square[8]) 
    (square[2] and square[4] and square[6]) 

這是我所到之處,但我真的不知道要從這裡走。任何幫助都將不勝感激。:)


參考解法

方法 1:

I think you are trying to send the list square to the function isWinner and want to check if the tic‑tac‑toe values match on rows or columns or diagonal.

To do this, you need to change the function as follows:

def isWinner (square):
    if (square[0] == square[1] == square[2]) \
    or (square[3] == square[4] == square[5]) \
    or (square[6] == square[7] == square[8]) \
    or (square[0] == square[3] == square[6]) \
    or (square[1] == square[4] == square[7]) \
    or (square[2] == square[5] == square[8]) \
    or (square[0] == square[4] == square[8]) \
    or (square[2] == square[4] == square[6]):
        return True
    else: return False

Assumption here is that square is a list or dictionary and the values in the index or key value represents a O or X.

A few examples:

print ('''0,0,0
X,0,X
X,X,X''')
print (isWinner(['0','0','0','X','0','X','X','X','X']))
print ('''X,0,0,
X,0,X,
X,0,X''')
print (isWinner(['X','0','0','X','0','X','X','0','X']))
print ('''X,0,0,
0,X,X,
X,0,0''')
print (isWinner(['X','0','0','0','X','X','X','0','0']))

Output:

#last row has all X. Result = True
0,0,0
X,0,X
X,X,X
True

#first column has all X, so is second column with all 0. Result = True
X,0,0,
X,0,X,
X,0,X
True

#no match row wise, column wise, or diagonal. Result = False
X,0,0,
0,X,X,
X,0,0
False

(by LouiJoe Ferndz)

參考文件

  1. I am having some problems checking the winner in a tic tac toe game using Turtle graphics (CC BY‑SA 2.5/3.0/4.0)

#Python #tic-tac-toe #turtle-graphics






相關問題

如何從控制台中導入的文件中訪問變量的內容? (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?)







留言討論