我需要井字游戏的获胜组合(或至少一些提示/技巧)

问题描述 投票:-1回答:2

对于一个班级项目,我和我的队友将编写井字游戏程序。到目前为止,这就是我们所拥有的。我们所有人都有0位使用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))

[我们被困在如何让程序检测某人何时赢得游戏,然后将其打印为“ You won!”。并让程序检测何时是领带,并打印“这是领带!”。我们已经在整个互联网上寻找解决方案,但是它们都不起作用,我们无法理解其中的说明。没用问老师,因为他们不了解python或如何编码。

python python-3.x tic-tac-toe
2个回答
0
投票

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

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() 

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

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


-1
投票

[首先,您需要一个条件,不允许两次分配相同的空间,例如,在测试运行时,我可以输入所需的任意数量的空间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

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

注意:我仍然是编码的初学者,偶然发现了您的线程,这不一定是可行的解决方案

© www.soinside.com 2019 - 2024. All rights reserved.