import cocos
from cocos import draw
from cocos.scene import Scene
from cocos.director import director
from cocos.particle_systems import Fireworks
from pyglet.gl import *
import pyglet
SIZE = 40
WIDTH = SIZE*15
HEIGHT = SIZE*15
N = '0'
W = 'w'
B = 'b'
S = '0'
cdata=[
[N,N,N,S,B],[B,S,N,N,N],[N,N,N,S,B],[N,B,S,N,N],
[N,N,S,B,N],[N,N,B,S,N],[N,N,N,S,W],[W,S,N,N,N],
[N,N,N,S,W],[N,W,S,N,N],[N,N,S,W,N],[N,N,W,S,N],
[B,B,S,N,N],[N,N,S,B,B],[B,S,B,N,N],[N,N,B,S,B],
[N,B,S,B,N],[N,B,B,S,N],[N,S,B,B,N],[W,W,S,N,N],
[N,N,S,W,W],[W,S,W,N,N],[N,N,W,S,W],[N,W,S,W,N],
[N,W,W,S,N],[N,S,W,W,N],
[N,S,B,B,B],[B,B,B,S,N],[N,B,B,B,S],[N,B,S,B,B],
[B,B,S,B,N],[N,S,W,W,W],[W,W,W,S,N],[N,W,W,W,S],
[N,W,S,W,W],[W,W,S,W,N],
[S,B,B,B,B],[B,S,B,B,B],[B,B,S,B,B],[B,B,B,S,B],
[B,B,B,B,S],[S,W,W,W,W],[W,S,W,W,W],[W,W,S,W,W],
[W,W,W,S,W],[W,W,W,W,S]]
AI_x = -1
AI_y = -1
max_level = -1
board = [['0' for i in range(15)] for j in range(15)]
turn = 'b'
last_turn = 'w'
class GameLayer(cocos.layer.ColorLayer):
def __init__(self):
super().__init__(245, 214, 122, 255)
cocos.layer.Layer.is_event_handler = True
self.label1 = cocos.text.Label('你赢了!', font_name='kaiti', font_size=100, color=(255, 0, 0, 255),
anchor_x="center", anchor_y="center")
self.label1.position = (300, 300)
self.label2 = cocos.text.Label('电脑赢了!', font_name='kaiti', font_size=95, color=(255, 0, 0, 255),
anchor_x="center", anchor_y="center")
self.label2.position = (300, 300)
for i in range(0, 15):
line1 = draw.Line((20, SIZE*i+20), (580, SIZE*i+20), (0, 0, 0, 255), 1)
self.add(line1)
line2 = draw.Line((SIZE*i+20, 20), (SIZE*i+20, 580), (0, 0, 0, 255), 1)
self.add(line2)
def change_side(self):
global turn
if turn == 'b':
turn = 'w'
else:
turn = 'b'
def check_win(self):
a = turn
for i in range(11):
for j in range(11):
if board[i][j] == a and board[i + 1][j + 1] == a and board[i + 2][j + 2] == a \
and board[i + 3][j + 3] == a and board[i + 4][j + 4] == a:
return True
for i in range(11):
for j in range(4, 15):
if board[i][j] == a and board[i + 1][j - 1] == a and board[i + 2][j - 2] == a \
and board[i + 3][j - 3] == a and board[i + 4][j - 4] == a:
return True
for i in range(15):
for j in range(11):
if board[i][j] == a and board[i][j + 1] == a and board[i][j + 2] == a \
and board[i][j + 3] == a and board[i][j + 4] == a:
return True
for i in range(11):
for j in range(15):
if board[i][j] == a and board[i + 1][j] == a and board[i + 2][j] == a \
and board[i + 3][j] == a and board[i + 4][j] == a:
return True
return False
def on_mouse_press(self, x, y, button, _):
global turn, AI_x, AI_y
if button == pyglet.window.mouse.LEFT:
x = round((x - 20) / SIZE)
y = round((y - 20) / SIZE)
if turn == 'b':
if board[y][x] != '0':
return None
elif x < 0 or y < 0 or x > 14 or y > 14:
return None
blk0 = cocos.sprite.Sprite('black.png')
blk0.position = (SIZE * x + 20, SIZE * y + 20)
self.add(blk0)
board[y][x] = 'b'
if self.check_win():
self.add(self.label1)
width, height = director.get_window_size()
pe = Fireworks()
pe.position = (width // 2, 80)
pe.duration = 6
pe.speed = 260
pe.size = 10.0
pe.auto_remove_on_finish = True
self.add(pe)
director.window.remove_handlers(self)
self.change_side()
self.AI_play()
wht0 = cocos.sprite.Sprite('white.png')
wht0.position = (SIZE * AI_x + 20, SIZE * AI_y + 20)
self.add(wht0)
wht0.do(cocos.actions.Blink(2, 0.5))
if self.check_win():
self.add(self.label2)
director.window.remove_handlers(self)
self.change_side()
def AI_play(self):
global AI_x, AI_y, max_level
AI_x = -1
AI_y = -1
max_level = -1
for y in range(15):
for x in range(15):
for level in range(len(cdata) - 1, -1, -1):
if level <= max_level:
break
if x + 4 < 15:
if self.auto_match(x, y, level, 1, 0):
break
if y + 4 < 15:
if self.auto_match(x, y, level, 0, 1):
break
if x - 4 >= 0 and y + 4 < 15:
if self.auto_match(x, y, level, -1, 1):
break
if x + 4 < 15 and y + 4 < 15:
if self.auto_match(x, y, level, 1, 1):
break
if AI_x != -1 and AI_y != -1:
if board[AI_y][AI_x] != '0':
return None
else:
board[AI_y][AI_x] = 'w'
return True
def auto_match(self, x, y, level, dx, dy):
global AI_x, AI_y, max_level
x_sel = -1
y_sel = -1
isfind = True
for j in range(5):
cs = board[y + j * dy][x + j * dx]
if cs == '0':
if cdata[level][j] == S:
x_sel = x + j * dx
y_sel = y + j * dy
elif cdata[level][j] != '0':
isfind = False
break
elif cs == 'b' and cdata[level][j] != 'b':
isfind = False
break
elif cs == 'w' and cdata[level][j] != 'w':
isfind = False
break
if isfind:
max_level = level
AI_x = x_sel
AI_y = y_sel
return True
return False
director.init(WIDTH, HEIGHT, caption="五子棋")
display = director.window.display
screen_width, screen_height = display.get_default_screen().width, display.get_default_screen().height
window_x = (screen_width - WIDTH) // 2
window_y = (screen_height - HEIGHT) // 2
director.window.set_location(window_x, window_y)
mainscene = Scene()
mainscene.add(GameLayer(), z=1)
director.run(mainscene)