我需要 Tic Tac Toe 的获胜组合(或至少一些 hints/tips)

I need winning combinations for Tic Tac Toe (or at least some hints/tips)

对于一个 class 项目,我和我的队友要编写一个井字游戏程序。到目前为止,这就是我们所拥有的。我们所有人在 python 中的经验都是 0,这是我们第一次在 python 中实际编码。

import random
import colorama 
from colorama import Fore, Style 
print(Fore.LIGHTWHITE_EX + "Tic Tac Toe - Below is the key to the board.") 

Style.RESET_ALL 

player_1_pick = ""
player_2_pick = "" 

if (player_1_pick == "" or player_2_pick == ""):
  if (player_1_pick == ""):
    player_1_pick = "Player 1"
  if (player_2_pick == ""):
    player_2_pick = "Player 2"
else:
  pass

board = ["_"] * 9 

print(Fore.LIGHTBLUE_EX + "0|1|2\n3|4|5\n6|7|8\n") 

def print_board():
  for i in range(0, 3):
    for j in range(0, 3):
      if (board[i*3 + j] == 'X'):
        print(Fore.RED + board[i*3 + j], end = '') 
      elif (board[i*3 + j] == 'O'):
        print(Fore.BLUE + board[i*3 + j], end = '') 
      else:
        print(board[i*3 + j], end = '') 

      print(Style.RESET_ALL, end = '') 

      if j != 2:
        print('|', end = '') 

    print() 

print_board() 

while True:
  x = input('Player 1, pick a number from 0-8: ') #
  x = int(x)
  board[x] = 'X' 
  print_board()  
  o = input('Player 2, pick a number from 0-8:') 
  o = int(o)
  board[o] = 'O' 
  print_board() 

answer = raw_input("Would you like to play it again?") 
if answer == 'yes': 
  restart_game()     
else:
  close_game() 

WAYS_T0_WIN = ((0,1,2)(3,4,5)(6,7,8)(0,3,6)(1,4,7)(2,5,8)(0,4,8)(2,4,6))

我们一直在研究如何让程序检测某人何时赢得比赛然后打印 "You won!" 以及如何让程序检测平局并打印 "It's a tie!"。我们在整个互联网上寻找解决方案,但 none 的解决方案有效,但我们无法理解其中的说明。问老师也没用,因为他们不知道 python 或如何编码。

首先,你需要一个不允许相同的 space 被分配两次的条件,当测试 运行 我可以输入我想要的 space 3没有它阻止我的例子。您需要对此进行某种检查。

其次,对于实际的获胜系统,你让它变得简单,因为你已经有了所有获胜游戏的坐标,我推荐一些类似的东西:

def checkwin(team):
  for i in WAYS_TO_WIN:
    checked = False
    while not checked:
      k = 0
      for j in i:
        if board[j] == team:
          k+=1
        if k == 3:
          return True
          checked = True

这种方法是检查是否有任何坐标具有任何集合中的所有 3 个坐标。您可能需要调整此代码,但这看起来像是一个解决方案。

注意:我仍然是编码的初学者,我偶然发现了你的帖子,这是一个想法不一定是可行的解决方案

我已经更改了您的代码,首先 "save players choices" 然后 "check if a player won and break the loop":

import random
import colorama 
from colorama import Fore, Style 
print(Fore.LIGHTWHITE_EX + "Tic Tac Toe - Below is the key to the board.") 

Style.RESET_ALL 

player_1_pick = ""
player_2_pick = "" 

if (player_1_pick == "" or player_2_pick == ""):
    if (player_1_pick == ""):
        player_1_pick = "Player 1"
    if (player_2_pick == ""):
        player_2_pick = "Player 2"
else:
    pass

board = ["_"] * 9 

print(Fore.LIGHTBLUE_EX + "0|1|2\n3|4|5\n6|7|8\n") 

def print_board():
    for i in range(0, 3):
        for j in range(0, 3):
            if (board[i*3 + j] == 'X'):
                print(Fore.RED + board[i*3 + j], end = '') 
            elif (board[i*3 + j] == 'O'):
                print(Fore.BLUE + board[i*3 + j], end = '') 
            else:
                print(board[i*3 + j], end = '') 

            print(Style.RESET_ALL, end = '') 

            if j != 2:
                print('|', end = '') 

        print() 


def won(choices):
    WAYS_T0_WIN = [(0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (2,4,6)]
    for tpl in WAYS_T0_WIN:
        if all(e in choices for e in tpl):
            return True
    return False

print_board()


turn = True
first_player_choices = []
second_player_choices = []

while True:
    if turn:
        x = input('Player 1, pick a number from 0-8: ') #
        x = int(x)
        if board[x] == '_':
            board[x] = 'X'
            first_player_choices.append(x)
            turn = not turn
            print_board()
            if won(first_player_choices):
                print('Player 1 won!')
                break
        else:
            print('Already taken! Again:')
            continue
    else:
        o = input('Player 2, pick a number from 0-8: ') #
        o = int(o)
        if board[o] == '_':
            board[o] = 'O'
            second_player_choices.append(o)
            turn = not turn
            print_board()
            if won(second_player_choices):
                print('Player 2 won!')
                break
        else:
            print('Already taken! Again:')
            continue


# answer = input("Would you like to play it again?") 
# if answer == 'yes': 
#     restart_game()     
# else:
#     close_game() 

我还添加了一个条件来检查玩家的选择是否已经被选中!顺便说一句,你可以做得更好。 :)

编辑: 我在这里的回答中有一个空格的小问题,我在编辑中解决了它。现在可以直接复制成py文件然后运行了!