為什麼我選擇第一個位置後立即收到勝利信息 (Why do i get the victory message right after choosing the first position)


問題描述

為什麼我選擇第一個位置後立即收到勝利信息 (Why do i get the victory message right after choosing the first position)

我正在嘗試使用 python 在終端中製作井字遊戲,但我無法弄清楚為什麼在選擇第一個位置後我會收到勝利消息,其他人可以看到為什麼嗎?我做了一個 for 循環來檢查我是否在行或列中獲勝,並製作了 2 個 if 語句來檢查我是否在對角線上獲勝

import random

board = [[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' ']]

def checkwin(board):
for row in board:
if len(set(row)) == 1:
return True
elif board[0][0] == board[1][1] == board[2][2]:
return True
elif board[2][0] == board[1][1] == board[0][2]:
return True
else:
return False

def check_space_taken(board, number):
if not choose_position(board, number) == ' ':
return True
else:
return False

def choose_position(board, number):
if number <= 3:
board[0][number‑1] = 'X'
elif number <= 6:
board[1][number‑4] = 'X'
elif number <= 9:
board[2][number‑7] = 'X'
return board, number

def computer_position(board, computer_number):
computer_number = random.randint(0,9)
if computer_number <= 3:
board[0][computer_number‑1] = 'O'
elif computer_number <= 6:
board[1][computer_number‑4] = 'O'
elif computer_number <= 9:
board[2][computer_number‑7] = 'O'
return board, computer_number

Game_over = False

while not Game_over:
print(board)
player_input = int(input('move to: '))
changed_board = choose_position(board, player_input)

for line in changed_board:
    print(line)

if checkwin(board):
    print('\n‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑Congrats you won‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑\n')
    Game_over = True

</code></pre>


參考解法

方法 1:

In principle, your logic isn't incorrect, but you need to account for empty spaces. You also forgot to check columns:

def checkwin(board):
    for i in range(3):
        if board[i][0] != " " and board[i][0] == board[i][1] == board[i][2]:
            return True  # found a winning row
        if board[0][i] != " " and board[0][i] == board[1][i] == board[2][i]:
            return True  # found a winning column

    # winning diagonals?
    if board[1][1] != " " and board[0][0] == board[1][1] == board[2][2]:
        return True
    if board[1][1] != " " and board[2][0] == board[1][1] == board[0][2]:
        return True

    return False

(by user14900864L3viathan)

參考文件

  1. Why do i get the victory message right after choosing the first position (CC BY‑SA 2.5/3.0/4.0)

#Python #tic-tac-toe #python-3.x






相關問題

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







留言討論