[英]Python Connect 4 check for winners (processing 2)
因此,我尝试在Processing 2上制作一个简单的Python connnect 4游戏,现在我尝试使用一种关键算法来检查单轮比赛的获胜者。 它不像井字游戏,所以我不确定从哪里开始。 如果我要问其他地方,也请告诉我。
基本上,每次玩家放置一块棋盘后,游戏板刷新后,就会检查在任何方向(水平,垂直,对角线)是否有连续的四个棋子。 如果不是,则游戏继续。 如果为SO,则游戏切换到获胜屏幕。
到目前为止,这是我的代码(请记住,这是Python,而#是注释):
mode = "win" #initial mode, should be "instruktion" but can be changed for testing
wires=loadFont ("Gulim-64.vlw")
kek = 1337#the score limit variable
points1 = 0 #player 1's score
points2 = 0 #player 2's score
skore1 = points1 #player 1's score as displayed on screen
skore2 = points2 #player 2's score as displayed on screen
win1 = 0 #player 1 wins a round
win2 = 0 #player 2 wins a round
win1text = "Player 1 wins round!"
win2text = "Player 2 wins round!"
viktory1 = 0
viktory2 = 0 #conditionals
viktoryspeech1 = "Player 1 is winner!" #player 1 wins
viktoryspeech2 = "Player 2 is winner!" #player 2 wins
stabber = 0 #y axis, adapting mouseY
jabber = 0 #x axis, adapting mouseX
turn = 1 #1 is player 1, 2 is player 2
ohbabyatriple = loadImage("mlgdew.png") #**** HER RIGHT IN THE *****
board = [ [0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0] ]
#board = [ [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0] ]
def setup():
size(1100,800)
def draw():
global mode,viktory1,viktory2
textFont(wires,100)
rectMode(CENTER)
if mode == "instruktion":#the welcome screen with instructions
background(200,200,200)
textSize(100)
fill(31,73,125)
text ("Welkom!",350,125)
textSize(40)
text("To Konnekt 4!",400,195)
text ("INSTRUKTIONS",390,265)
textSize(30)
#the following is instructional text
text ("1. Two players will first decide on a skore",290,320)
text ("2. To drop a piece, klik on a space on the bord",260,360)
text ("3. First player to konnekt 4 in any direktion wins the round",200,400)
text ("4. First player to reach skore limit wins the game",250,440)
text ("5. To enter a skore limit, from 1-9, press the respektiv" ,200,480)
text (" number on the keyboard.",370,510)
text ("WARNING: WATCH WHAT SKORE YOU PICK.",240,550)
text ("AND NEVER PRESS 'X'",400,590) #shmuck bait
if mode == "game":#the main game interface
global board
if viktory1 == 1:
mode = "kongrats"
elif viktory2 ==1:
mode = "kongrats"
elif viktory1 ==1 and viktory2 == 1:
mode = "kill"
background(200,200,200)
rectMode(CENTER)
fill(31,73,125)
textSize(100)
text("Konnekt 4",300,100)
text ("I",160,300)
textSize(60)
text("Skore",90,200)
textSize(80)
text(skore1,100,290)
text(skore2,200,290)
noFill()
translate(400,230) #shift the board, due to nature of generation
#generating board
for y in range(6): #y axis
for x in range(7): #x axis
if board[y][x] == 1 and turn!=0:
fill(137,2,6)
elif board[y][x] == 2 and turn!=0:
fill(6,2,137)
elif board[y][x] == 0:
noFill()
rect(x*100,y*80,100,80)
#each tile is 100x80
if mode == "win":
global ohbabyatriple,win1,win2,win1text,win2text
background(200,200,200)
textSize(100)
fill(31,73,125)
if win1 ==1:
text(win1text,130,100)
elif win2 ==1:
text(win2text,130,100)
else:
text("Oh hi, tester!",300,100)
textSize(40)
text("Press E to continue",370,175)
image(ohbabyatriple,200,300)
image(ohbabyatriple,875,300)
image(ohbabyatriple,530,300)
if mode == "draw":
global ohbabyatriple
background(200,200,200)
textSize(100)
fill(31,73,125)
text("It's a draw!",310,100)
textSize(40)
text("Press E to continue",370,175)
image(ohbabyatriple,200,300)
image(ohbabyatriple,875,300)
image(ohbabyatriple,530,300)
if mode =="kongrats": #victory screen
background(200,200,200)
textSize(100)
fill(31,73,125)
text("Kongratulations!",200,100)
textSize(60)
if viktory1 ==1:#player 1's win
text (viktoryspeech1,310,200)
text ("Have a nice day!",330,280)
elif viktory2 ==1:#player 2's win
text (viktoryspeech2,310,200)
text ("Have a nice day!",330,280)
else:
text ("Oh, this is just a test then?",200,200)
#^ because you normally only get the victory screen after one player wins
if mode =="OHGODWHY?!":#Shouldn't have pressed it.
doomedbirds= loadImage("birds_nooooo.jpg")
ffffuuuu = loadImage ("self_destruct.png")
background(200,200,200)
textSize(100)
fill(31,73,125)
text("YOU FOOL!",300,100)
text("YOU DOOMED US ALL!",22,222)
text("NOOOOOOOOOOOOOOOOOOOOOOO",22,333)
image(doomedbirds,560,420)
image(ffffuuuu,100,420)
mode = "kill"
if mode =="kill":#part of the don't-press-the-x code
exit()
def keyPressed():
global mode
global kek
global win1, win2, viktory1, viktory2
if key in '123456789' and key != 0 and mode == "instruktion":
kek = int (key)
mode = "game"
if key in 'eE' and mode == "win" or key in 'eE' and mode == "draw":
if win1 ==1 and points1 != kek:
win1 =0
elif win2 == 1 and points2 != kek:
win1 = 0
elif points1 ==kek:
viktory1 = 1
elif points2 == kek:
viktory2 = 1
mode = "game"
#WARNING: SELF DESTRUCT CODE
if key in 'xX':
mode = "OHGODWHY?!"
def mousePressed():
global jabber, stabber, turn
stabber = (mouseY-190)/80 #y axis
jabber =(mouseX-350)/100 #x axis
if mode == "game" and stabber >=0 and stabber <=5 and jabber >=0 and jabber<=6 and board[stabber][jabber] == 0:
board[stabber][jabber] = turn
print turn #extra info
#color that cell with player's color
if turn == 1:
turn = 2
elif turn == 2:
turn = 1
#cycling turns
我认为,这里是我认为您缺少的功能(需要更仔细地检查,可能需要使用print
语句&c,但是尽管可能会有一个单独的&c,但我认为这是正确的总体思路):
def fours(fourthings):
asaset = set(fourthings)
return asaset.pop() if len(asaset)==1 else 0
def winner(board):
""" board is a list of `nrows` list,
each of them a lis of `ncols` columns.
A cell is `0` to mean empty, else some other
value; if 4 in a rows (horz, vert, diag) are
the same non-0 value, that's the result of
the function; otherwise, the result is 0. """
nrows = len(board)
ncols = len(board[0])
# check horizontals
for row in board:
for i in range(ncols-4):
if fours(row[i:i+4)):
return row[i]
# check verticals
for i in range(ncols):
for j in range(nrows-4):
if fours(board[k][i] for k in range(j:j+4)):
return board[j][i]
# check NW -> SE diagonals
for i in range(ncols-4):
for j in range(nrows-4):
if fours(board[i+k][j+k] for k in range(4)):
return board[j][i]
# check NE -> SW diagonals
for i in range(3, ncols):
for j in range(nrows-4):
if fours(board[i-k][j+k] for k in range(4)):
return board[j][i]
一旦以一对一的方式变得正确,就可以通过消除重复来进行改进-四行的/用于/如果/返回块非常重复,应将其分解为嵌套函数...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.