五子棋人机对战
用python cocos2d写的五子棋人机对战。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=[
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,
,,,,
,,,,
,,
,,,,
,,,,
,]
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 == a and board == a and board == a \
and board == a and board == a:
return True
for i in range(11):
for j in range(4, 15):
if board == a and board == a and board == a \
and board == a and board == a:
return True
for i in range(15):
for j in range(11):
if board == a and board == a and board == a \
and board == a and board == a:
return True
for i in range(11):
for j in range(15):
if board == a and board == a and board == a \
and board == a and board == 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 != '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 = '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 != '0':
return None
else:
board = '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
if cs == '0':
if cdata == S:
x_sel = x + j * dx
y_sel = y + j * dy
elif cdata != '0':
isfind = False
break
elif cs == 'b' and cdata != 'b':
isfind = False
break
elif cs == 'w' and cdata != '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) 加油 支持{:10_256:} {:10_256:}{:10_256:}厉害了 厉害!{:10_254:}{:10_254:}
页:
[1]