先拿资源:链接:https://pan.baidu.com/s/1wpCIIVsfm2YHY6IYp9slsQ
提取码:lwmm
def __pc__(self):
这个函数是电脑落子的函数,内部不同情况的权值不同,全部经过了测试,智能模式无论先后手都可以做到不败。
# 增加平局功能
# 增加强人工智能
import pygame
import sys
import time
import random
class Game(object): # 棋盘类
def __init__(self, x = 0, y = 0, score = 0, value = 0):
self.x = x
self.y = y
self.score = score # 权值
self.value = value # 空 白 黑
def __init_chess__(self):
# 游戏初始化
self.init_chess_list = []
for i in range(3): # 每一行
row_list = []
for j in range(3): # 每一列
# 以下为一个点,不是一个格子
point_x = 125 + j * 175
point_y = 125 + i * 175
score = 0
sp = Game(point_x, point_y, score, 0) # 获取点位
row_list.append(sp) # 集合一行全部的点位
self.init_chess_list.append(row_list) # 将一行存入棋盘 # 保存棋盘坐标
self.init_role = 1 # 保存棋子(1:白--3:黑)
self.result_flag = 0 # 保存结果 1 white 2 ping 3 black
self.modoul = 0 # 保存模式
self.menu_print = 0 # 记录是否打印地图
def __image_load__(self):
pygame.init() # 初始化游戏
self.screen = pygame.display.set_mode((600, 798), 0, 0) # 设置游戏窗口
pygame.display.set_caption("井字棋V2.48") # 设置窗口标题
self.bg = pygame.image.load("./images_jin/bg.png") # 背景图片
self.white = pygame.image.load("./images_jin/storn_white.png") # 白棋图片
self.black = pygame.image.load("./images_jin/storn_black.png") # 黑棋图片
self.game = pygame.image.load("./images_jin/gaming.png")
self.whiteWin = pygame.image.load("./images_jin/white_win.png")
self.blackWin = pygame.image.load("./images_jin/black_win.png")
self.restart = pygame.image.load("./images_jin/restart.png")
self.menu = pygame.image.load("./images_jin/menu.png")
self.rect = self.white.get_rect() # 获取棋子图片 x,y
pygame.display.update() # 更新视图
def __image_print__(self):
if self.modoul == 0:
self.screen.blit(self.menu, (0, 0))
if self.modoul > 0 and self.menu_print == 0:
self.screen.blit(self.bg, (0, 0))
self.screen.blit(self.game, (0, 600))
self.menu_print = 1
if self.modoul != 0 and self.result_flag == 0:
for i in range(3):
for j in range(3): # 落子贴图
if self.init_chess_list[i][j].value == 1:
self.screen.blit(self.white, (self.init_chess_list[i][j].x - 85, self.init_chess_list[i][j].y - 85))
if self.init_chess_list[i][j].value == 3:
self.screen.blit(self.black, (self.init_chess_list[i][j].x - 85, self.init_chess_list[i][j].y - 85))
if self.result_flag == 1:
self.screen.blit(self.whiteWin, (0, 600)) # 绘制获胜时的图片
self.screen.blit(self.restart, (245, 755))
elif self.result_flag == 2:
self.screen.blit(self.restart, (245, 755))
elif self.result_flag == 3:
self.screen.blit(self.blackWin, (0, 600)) # 绘制获胜时的图片
self.screen.blit(self.restart, (245, 755))
pygame.display.update() # 更新视图
def __resultType__(self):
flag = False
role = 0
if self.result_flag == 0:
for i in range(0, 3, 1):
j = 0
if (self.init_chess_list[i][j].value == self.init_chess_list[i][j + 1].value \
== self.init_chess_list[i][j + 2].value) and self.init_chess_list[i][j].value != 0:
flag = True
role = self.init_chess_list[i][j].value
break
i = 1
j = 1
if ((self.init_chess_list[i][j].value == self.init_chess_list[i + 1][j + 1].value \
== self.init_chess_list[i - 1][j - 1].value) or \
(self.init_chess_list[i][j].value == self.init_chess_list[i + 1][j - 1].value \
== self.init_chess_list[i - 1][j + 1].value)) and self.init_chess_list[i][j].value != 0:
flag = True
role = self.init_chess_list[i][j].value
for j in range(0, 3, 1):
i = 0
if (self.init_chess_list[i][j].value == self.init_chess_list[i + 1][j].value \
== self.init_chess_list[i + 2][j].value) and self.init_chess_list[i][j].value != 0:
flag = True
role = self.init_chess_list[i][j].value
break
j += 1
sum = 0
for i in range(3):
for j in range(3):
if self.init_chess_list[i][j].value != 0:
sum += 1
if sum == 9:
self.result_flag = 2
print("和棋")
if flag:
self.result_flag = role
if role == 1:
print("白棋胜")
elif role == 3:
print("黑棋胜")
def __event_hander__(self): # 按键检测和玩家落子
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos() # 获取鼠标点击坐标值
if self.modoul > 0 and self.result_flag == 0 and (
self.modoul == 1 or self.init_role == 1):
for i in range(3):
for j in range(3):
# 一个格子175个像素,留五个像素格
if x >= self.init_chess_list[i][j].x - 85 and x <= self.init_chess_list[i][
j].x + 85 and y <= self.init_chess_list[i][j].y + 85 and y >= self.init_chess_list[i][
j].y - 85:
if self.init_chess_list[i][j].value == 0 and self.init_role == 1:
self.init_chess_list[i][j].value = 1
print("白玩家落子%d,%d" % (self.init_chess_list[i][j].x, self.init_chess_list[i][j].y))
self.init_role = 3
elif self.init_chess_list[i][j].value == 0 and self.init_role == 3:
self.init_chess_list[i][j].value = 3
print("黑玩家落子%d,%d" % (self.init_chess_list[i][j].x, self.init_chess_list[i][j].y))
self.init_role = 1
break
elif self.modoul == 0 and x >200 and x < 402 and y > 235 and y < 280:
self.modoul = 1
print(self.modoul)
elif self.modoul == 0 and x >146 and x < 460 and y > 368 and y < 430:
self.modoul = 2
self.init_role = 3
print(self.modoul)
elif self.modoul == 0 and x >146 and x < 460 and y > 466 and y < 525:
self.modoul = 3
print(self.modoul)
elif self.modoul == 0 and x >146 and x < 460 and y > 570 and y < 625:
self.modoul = 4
self.init_role = 3
print(self.modoul)
elif self.modoul == 0 and x >146 and x < 460 and y > 675 and y < 730:
self.modoul = 5
print(self.modoul)
elif self.result_flag > 0 and x >= 250 and x <= 365 and y >= 770 and y <= 810:
self.result_flag = 0
self.modoul = 0
self.init_chess_list = [] # 清空棋盘
self.init_role = 1
self.main() # 重新开始
"""
for i in range(3):
for j in range(3):
print(self.init_chess_list[i][j].value, end=" ")
print()
"""
for i in range(3):
for j in range(3):
print(self.init_chess_list[i][j].value, end=" ")
print()
def __pc__(self):
# 引入每个位置的权值
"""
---3个为空,重要性最低,权值设置为1
---2个空1个对方,重要性次低,权值为10
----1个空格1个对方1个己方,重要行较低,权值50
----2个空格1个己方,重要性较高,权值为200
---1个空格2个对方,重要性次高,权值500
---1个空格2个己方,重要性最高,权值1000
同时考虑该位置上多个方向上的权值
"""
if self.modoul == 1 or self.init_role == 1 or self.modoul == 0 or self.result_flag != 0:
return
else:
for i in range(3): # 先清空各点的权值
for j in range(3):
self.init_chess_list[i][j].score = 0
sum = 0
for i in range(3): # 每一行
for j in range(3):
sum += self.init_chess_list[i][j].value
for k in range(3):
if self.init_chess_list[i][k].value == 0: # 空的位置才有权值
if sum == 0:
self.init_chess_list[i][k].score += 1
elif sum == 1:
self.init_chess_list[i][k].score += 30
elif sum == 4:
self.init_chess_list[i][k].score += 25
elif sum == 3:
self.init_chess_list[i][k].score += 200
elif sum == 2:
self.init_chess_list[i][k].score += 500
elif sum == 6:
self.init_chess_list[i][k].score += 1000
sum = 0
sum = 0
for j in range(3): # 每一列
for i in range(3):
sum += self.init_chess_list[i][j].value
for k in range(3):
if self.init_chess_list[k][j].value == 0: # 空的位置才有权值
if sum == 0:
self.init_chess_list[k][j].score += 1
elif sum == 1:
self.init_chess_list[k][j].score += 30
elif sum == 4:
self.init_chess_list[k][j].score += 25
elif sum == 3:
self.init_chess_list[k][j].score += 200
elif sum == 2:
self.init_chess_list[k][j].score += 500
elif sum == 6:
self.init_chess_list[k][j].score += 1000
sum = 0
sum = 0
for i in range(3): # 对角线
sum += self.init_chess_list[i][i].value
for k in range(3):
if self.init_chess_list[k][k].value == 0: # 空的位置才有权值
if sum == 0:
self.init_chess_list[k][k].score += 1
elif sum == 1:
"""
self.init_chess_list[k][k].score += 10
self.init_chess_list[k][k].score += 4
self.init_chess_list[1][1].score += 6
"""
self.init_chess_list[k][k].score += 30
if self.init_chess_list[1][1].value == 0:
self.init_chess_list[1][1].score += 16
elif sum == 4:
self.init_chess_list[k][k].score += 25
elif sum == 3:
"""
self.init_chess_list[k][k].score += 200
"""
self.init_chess_list[k][k].score += 165 # 中间格子的权重正常
if self.init_chess_list[1][1].value == 0:
self.init_chess_list[1][1].score += 35 # 两端格子权重降低,必须比四条边低
elif sum == 2:
self.init_chess_list[k][k].score += 500
elif sum == 6:
self.init_chess_list[k][k].score += 1000
sum = 0
for i in range(3): # 反对角线
sum += self.init_chess_list[i][2 - i].value
for k in range(3):
if self.init_chess_list[k][2 - k].value == 0: # 空的位置才有权值
if sum == 0:
self.init_chess_list[k][2 - k].score += 1
elif sum == 1:
"""
self.init_chess_list[k][2 - k].score += 4
self.init_chess_list[1][1].score += 6
self.init_chess_list[k][2 - k].score += 10
"""
self.init_chess_list[k][2 - k].score += 30
if self.init_chess_list[1][1].value == 0:
self.init_chess_list[1][1].score += 16
elif sum == 4:
self.init_chess_list[k][2 - k].score += 25
elif sum == 3:
self.init_chess_list[k][2 - k].score += 165
"""
self.init_chess_list[k][2 - k].score += 200
"""
if self.init_chess_list[1][1].value == 0:
self.init_chess_list[1][1].score += 35
elif sum == 2:
self.init_chess_list[k][2 - k].score += 500
elif sum == 6:
self.init_chess_list[k][2 - k].score += 1000
max_col = max_row = 0
if self.modoul == 2 or self.modoul == 3:
for i in range(3):
for j in range(3):
if self.init_chess_list[i][j].score > self.init_chess_list[max_col][max_row].score:
max_col = i
max_row = j
elif self.modoul == 4 or self.modoul == 5: # 弱人工智能
for i in range(3):
for j in range(3):
if self.init_chess_list[i][j].score > self.init_chess_list[max_col][max_row].score:
max_col = i
max_row = j
if self.init_chess_list[max_col][max_row].score < 500:
while self.init_chess_list[max_col][max_row].value > 0:
max_col = random.randint(0, 3)
max_row = random.randint(0, 3)
time.sleep(1) # 停顿一秒,增强游戏体验感
"""
for i in range(3):
for j in range(3):
print(self.init_chess_list[i][j].score, end=" ")
print()
"""
for i in range(3):
for j in range(3):
print(self.init_chess_list[i][j].score, end=" ")
print()
self.init_chess_list[max_col][max_row].value = 3 # 落子
self.init_chess_list[max_col][max_row].score = 0 # 清空落子点权值
"""
for i in range(3):
for j in range(3):
print(self.init_chess_list[i][j].value, end=" ")
print()
"""
for i in range(3):
for j in range(3):
print(self.init_chess_list[i][j].value, end=" ")
print()
self.init_role = 1 # 变换棋子颜色
def main(self):
self.__init_chess__()
self.__image_load__()
self.__image_print__()
while True:
self.__event_hander__()
self.__image_print__()
self.__resultType__()
self.__pc__()
self.__image_print__()
self.__resultType__()
if __name__ == '__main__':
game = Game()
game.main()