获取连接4中的所有获胜组合,无需手动放入

问题描述 投票:0回答:1

我正在使用 Python 3.10 进行编码,并且使用 PyCharm 社区版 2021.2.2 进行编码。 我想知道Python中是否有一种方法可以编码连接4(我已经编码了放置的计数器和所有内容),然后还使用某种公式来检查是否有4个连续。为了检查到目前为止是否有 4 个连续的计数器,我只能想到手动检查并输入连续 4 个可能的组合,无论是对角线、垂直、水平还是其他。正如我所说,有没有一种简单的方法可以做到这一点,而无需手动输入所有获胜组合?

我还没有手动尝试过,因为我希望能得到一些关于如何不手动执行此操作的帮助。 这是我到目前为止的代码:

import turtle
import mouse
from tkinter import *


#turtles
redturtle = turtle.Turtle()
blueturtle = turtle.Turtle()
boredturtle = turtle.Turtle()


#column values- positions
column1 = [380, 164, 476, 860]
column2 = [488, 164, 575, 860]
column3 = [587, 164, 678, 860]
column4 = [684, 164, 779, 860]
column5 = [783, 164, 875, 860]
column6 = [884, 164, 976, 860]
column7 = [983, 164, 1078, 860]

#connect 4 board values - where a placement is for background and to checkwin
col7 = [1,2,3,4,5,6]
col6 = [1,2,3,4,5,6]
col5 = [1,2,3,4,5,6]
col4 = [1,2,3,4,5,6]
col3 = [1,2,3,4,5,6]
col2 = [1,2,3,4,5,6]
col1 = [1,2,3,4,5,6]

#square values- 1 is bottom left corner
x1 = -305
x2 = -200
x3 = -100
x4 = 0
x5 = 100
x6 = 200
x7 = 300

listy = [-290, -170, -50, 70, 190, 300]
y1 = -290
y2 = -170
y3 = -50
y4 = 70
y5 = 190
y6 = 300

#column counters - to check what "Y" to put it at
count1 = [0]
count2 = [0]
count3 = [0]
count4 = [0]
count5 = [0]
count6 = [0]
count7 = [0]


def drawboard():
    boredturtle.hideturtle()
    boredturtle.speed(0)
    boredturtle.pensize(15)
    boredturtle.penup()
    #sideways
    boredturtle.setposition(-1000, -350)
    boredturtle.pendown()
    boredturtle.setposition(1000, -350)

    boredturtle.penup()
    boredturtle.setposition(-1000, -230)
    boredturtle.pendown()
    boredturtle.setposition(1000, -230)
    boredturtle.penup()
    boredturtle.setposition(-1000, -110)
    boredturtle.pendown()
    boredturtle.setposition(1000, -110)
    boredturtle.penup()
    boredturtle.setposition(-1000, 10)
    boredturtle.pendown()
    boredturtle.setposition(1000, 10)
    boredturtle.penup()
    boredturtle.setposition(-1000, 130)
    boredturtle.pendown()
    boredturtle.setposition(1000, 130)
    boredturtle.penup()
    boredturtle.setposition(-1000, 250)
    boredturtle.pendown()
    boredturtle.setposition(1000, 250)
    #top
    boredturtle.penup()
    boredturtle.setposition(-1000, 350)
    boredturtle.pendown()
    boredturtle.setposition(1000, 350)

    #downwards
    boredturtle.penup()
    boredturtle.setposition(-360, 1000)
    boredturtle.pendown()
    boredturtle.setposition(-360, -1000)
    boredturtle.penup()
    boredturtle.setposition(350, 1000)
    boredturtle.pendown()
    boredturtle.setposition(350, -1000)
    boredturtle.penup()
    boredturtle.setposition(-250, 1000)
    boredturtle.pendown()
    boredturtle.setposition(-250, -1000)
    boredturtle.penup()
    boredturtle.setposition(-150, 1000)
    boredturtle.pendown()
    boredturtle.setposition(-150, -1000)
    boredturtle.penup()
    boredturtle.setposition(-50, 1000)
    boredturtle.pendown()
    boredturtle.setposition(-50, -1000)
    boredturtle.penup()
    boredturtle.setposition(50, 1000)
    boredturtle.pendown()
    boredturtle.setposition(50, -1000)
    boredturtle.penup()
    boredturtle.setposition(150, 1000)
    boredturtle.pendown()
    boredturtle.setposition(150, -1000)
    boredturtle.penup()
    boredturtle.setposition(250, 1000)
    boredturtle.pendown()
    boredturtle.setposition(250, -1000)
    boredturtle.penup()
    boredturtle.setposition(350, 1000)
    boredturtle.pendown()
    boredturtle.setposition(350, -1000)

drawboard()

whoseturn = [0]

def redcounterplace(x, y, count):
    redturtle.speed(0)
    redturtle.hideturtle()
    redturtle.color("red")
    redturtle.pensize(80)
    redturtle.penup()
    listofy = y
    valy = listofy[count]
    redturtle.setposition(x,valy)
    redturtle.pendown()
    redturtle.circle(1)

def bluecounterplace(x,y, count):
    blueturtle.speed(0)
    blueturtle.hideturtle()
    blueturtle.color("blue")
    blueturtle.pensize(80)
    blueturtle.penup()
    listofy = y
    valy = listofy[count]
    blueturtle.setposition(x, valy)
    blueturtle.pendown()
    blueturtle.circle(1)

def columnchecker():
    mousepos = mouse.get_position()
    if mousepos[0] >= column1[0] and mousepos[0] <= column1[2] and mousepos[1] >= column1[1] and mousepos[1] <= column1[3]:
        return ("y1")
    if mousepos[0] >= column2[0] and mousepos[0] <= column2[2] and mousepos[1] >= column2[1] and mousepos[1] <= column2[3]:
        return ("y2")
    if mousepos[0] >= column3[0] and mousepos[0] <= column3[2] and mousepos[1] >= column3[1] and mousepos[1] <= column3[3]:
        return ("y3")
    if mousepos[0] >= column4[0] and mousepos[0] <= column4[2] and mousepos[1] >= column4[1] and mousepos[1] <= column4[3]:
        return ("y4")
    if mousepos[0] >= column5[0] and mousepos[0] <= column5[2] and mousepos[1] >= column5[1] and mousepos[1] <= column5[3]:
        return ("y5")
    if mousepos[0] >= column6[0] and mousepos[0] <= column6[2] and mousepos[1] >= column6[1] and mousepos[1] <= column6[3]:
        return ("y6")
    if mousepos[0] >= column7[0] and mousepos[0] <= column7[2] and mousepos[1] >= column7[1] and mousepos[1] <= column7[3]:
        return ("y7")

def putinbackgroundcheck(person, columnnum, count):
    if person == 1:
        if columnnum == "y1":
            col1[count] = "b"
        if columnnum == "y2":
            col2[count] = "b"
        if columnnum == "y3":
            col3[count] = "b"
        if columnnum == "y4":
            col4[count] = "b"
        if columnnum == "y5":
            col5[count] = "b"
        if columnnum == "y6":
            col6[count] = "b"
        if columnnum == "y7":
            col7[count] = "b"
    if person == 0:
        if columnnum == "y1":
            col1[count] = "r"
        if columnnum == "y2":
            col2[count] = "r"
        if columnnum == "y3":
            col3[count] = "r"
        if columnnum == "y4":
            col4[count] = "r"
        if columnnum == "y5":
            col5[count] = "r"
        if columnnum == "y6":
            col6[count] = "r"
        if columnnum == "y7":
            col7[count] = "r"

def main():
    if columnchecker() == "y1" and whoseturn[0] == 0:
        redcounterplace(x1, listy, count1[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count1[0])
        count1[0] = count1[0] + 1
        whoseturn[0] = 1
    elif columnchecker() == "y2" and whoseturn[0] == 0:
        redcounterplace(x2, listy, count2[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count2[0])
        count2[0] = count2[0] + 1
        whoseturn[0] = 1
    elif columnchecker() == "y3" and whoseturn[0] == 0:
        redcounterplace(x3, listy, count3[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count3[0])
        count3[0] = count3[0] + 1
        whoseturn[0] = 1
    elif columnchecker() == "y4" and whoseturn[0] == 0:
        redcounterplace(x4, listy, count4[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count4[0])
        count4[0] = count4[0] + 1
        whoseturn[0] = 1
    elif columnchecker() == "y5" and whoseturn[0] == 0:
        redcounterplace(x5, listy, count5[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count5[0])
        count5[0] = count5[0] + 1
        whoseturn[0] = 1
    elif columnchecker() == "y6" and whoseturn[0] == 0:
        redcounterplace(x6, listy, count6[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count6[0])
        count6[0] = count6[0] + 1
        whoseturn[0] = 1
    elif columnchecker() == "y7" and whoseturn[0] == 0:
        redcounterplace(x7, listy, count7[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count7[0])
        count7[0] = count7[0] + 1
        whoseturn[0] = 1
    elif columnchecker() == "y1" and whoseturn[0] == 1:
        bluecounterplace(x1, listy, count1[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count1[0])
        count1[0] = count1[0] + 1
        whoseturn[0] = 0
    elif columnchecker() == "y2" and whoseturn[0] == 1:
        bluecounterplace(x2, listy, count2[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count2[0])
        count2[0] = count2[0] + 1
        whoseturn[0] = 0
    elif columnchecker() == "y3" and whoseturn[0] == 1:
        bluecounterplace(x3, listy, count3[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count3[0])
        count3[0] = count3[0] + 1
        whoseturn[0] = 0
    elif columnchecker() == "y4" and whoseturn[0] == 1:
        bluecounterplace(x4, listy, count4[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count4[0])
        count4[0] = count4[0] + 1
        whoseturn[0] = 0
    elif columnchecker() == "y5" and whoseturn[0] == 1:
        bluecounterplace(x5, listy, count5[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count5[0])
        count5[0] = count5[0] + 1
        whoseturn[0] = 0
    elif columnchecker() == "y6" and whoseturn[0] == 1:
        bluecounterplace(x6, listy, count6[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count6[0])
        count6[0] = count6[0] + 1
        whoseturn[0] = 0
    elif columnchecker() == "y7" and whoseturn[0] == 1:
        bluecounterplace(x7, listy, count7[0])
        putinbackgroundcheck(whoseturn[0], columnchecker(), count7[0])
        count7[0] = count7[0] + 1
        whoseturn[0] = 0

def checkwin():



mouse.on_click(lambda: main())



turtle.done()
python turtle-graphics python-turtle
1个回答
0
投票

您应该使用列表的列表而不是单个变量来表示和操作棋盘。将游戏逻辑与演示分离也会更容易。这将允许您全面测试图形 UI 之外的逻辑。

将棋盘表示为列列表,每个列都是棋盘单元格内容的列表(包含“.”、“X”或“O”),您的最终游戏检查可以使用嵌套循环来提取 4 单元格每个 4x4 子块的线:

def winOrDraw(board):
    xo  = ""                             # 4-cell segments as a string
    for c in range(len(board)-3):        # top corner of each 4x4 blocks 
        for r in range(len(board[0])-3):
            xo += "|"+"".join(board[c][r+i] for i in range(4))     # vertical
            xo += "|"+"".join(board[c+i][r] for i in range(4))     # horiz.
            xo += "|"+"".join(board[c+i][r+i] for i in range(4))   # diag 1
            xo += "|"+"".join(board[c+3-i][r+i] for i in range(4)) # diag 2   
    if "XXXX"  in xo: return "X"
    if "OOOO"  in xo: return "O"
    if "." not in xo: return "DRAW"
    return None 

然后可以在没有图形 UI 的情况下执行这些操作,一旦你让它工作,UI 只需要显示面板的内容并接受用户输入。

基于文本的测试:

# setup
cols  = 7
rows  = 6
board = [ ["."]*rows for _ in range(cols) ]    
player = "X"

while not winOrDraw(board): # loop until end of game

    # display board
    print(*range(1,cols+1))
    for r in reversed(range(rows)):
        print(*(board[c][r] for c in range(cols)))

    # get and validate user input
    col = input(f"player {player} select column (1-{cols}): ")
    if col not in map(str,range(1,cols)):
        print("invalid column number")
        continue
    col = int(col)
    if not "." in board[col]:
        print("column already full")
        continue

    # perform move 
    row = board[col].index(".")
    board[col][row] = player

    # switch player
    player = "X" if player == "O" else "O"

print("GAME OVER.  WINNER IS ",winOrDraw(board))
© www.soinside.com 2019 - 2024. All rights reserved.