Python 我需要Tic Tac Toe的获奖组合(或至少一些提示/提示)

Python 我需要Tic Tac Toe的获奖组合(或至少一些提示/提示),python,python-3.x,tic-tac-toe,Python,Python 3.x,Tic Tac Toe,对于一个班级项目,我和我的小组成员将编写一个tic-tac-toe程序。到目前为止,这就是我们所拥有的。我们所有人都没有python方面的经验,这是我们第一次真正用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

对于一个班级项目,我和我的小组成员将编写一个tic-tac-toe程序。到目前为止,这就是我们所拥有的。我们所有人都没有python方面的经验,这是我们第一次真正用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))

我们被困在如何让程序检测某人何时赢得比赛,然后让它打印“你赢了!”,以及让程序检测何时是平局并打印“平局!”。我们在互联网上到处寻找解决方案,但没有一个有效,我们也无法理解说明。问老师没有用,因为他们不懂python或如何编码。

首先,你需要一个条件,不允许同一个空间被分配两次,当测试运行时,我可以输入我想要的任意多的空间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个坐标。您可能需要调整此代码,但这似乎是一个解决方案


注意:我还是一个编码新手,我偶然发现了你的思路,这是一个想法,不一定是一个有效的解决方案

我更改了你的代码,首先是“保存玩家选择”,然后是“检查玩家是否赢了并打破循环”:

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


编辑:我的答案中有一个空格小问题,我在编辑中解决了。现在,您可以直接将其复制到py文件中并运行它

一旦你确定了获胜的条件,平局就很容易了,如果刚刚比赛的人没有获胜,而董事会没有更多的空间,那么平局就是平局了。对于win条件,您需要在一行中检查3个相同的令牌,可以是任何行、列或对角线。
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()