python编写的扫雷游戏
使用python语言编写的命令行方式的交互的《扫雷》游戏:
import numpy as np
import random
from typing import List
H = 36
W = 64
def belong_to(h, w, H=H, W=W):
near = []
for i in range(h-2, h+3):
for j in range(w-2, w+3):
if i>=0 and j>=0 and i<H and j<W and (i,j)!=(h,w):
near.append((i, j))
return near
def near_by(h, w, H=H, W=W):
near = []
for i in range(h-1, h+2):
for j in range(w-1, w+2):
if i>=0 and j>=0 and i<H and j<W and (i,j)!=(h,w):
near.append((i,j))
return near
def mine_count(h, w, real_state:np.array, H=H, W=W):
count = 0
for i, j in near_by(h, w, H=H, W=W):
if real_state[i][j]==1:
count += 1
return count
class Env():
def __init__(self, H=H, W=W, N=100):
self.H = H
self.W = W
self.N = N
# real state中0表示无雷,1表示有雷
self.real_state = np.zeros((H, W), dtype=np.int32)
self.mine = set()
while len(self.mine)!=N:
self.mine.add(random.randint(0, H*W))
for x in self.mine:
self.real_state[x//self.W][x%self.W] = 1
# state_type中0表示无雷,1-8表示有雷, 用此来表示对附近雷的计数
self.state_type = np.zeros((H, W), dtype=np.int32)
for i in range(H):
for j in range(W):
self.state_type[i][j] = mine_count(h=i, w=j, H=H, W=W, real_state=self.real_state)
# obs为-100表示未翻开(未知),0-8表示翻开但无雷,数值大小表示翻开位置周边雷的数量
# agent的状态记录所用,也可以用来作为打印之用
self.obs = np.zeros((H, W), dtype=np.int32) -100
def act(self, i, j):
done = False
if self.obs[i][j]!=-100:
return ValueError
if self.real_state[i][j] == 1:
# game over 触雷
done = True
return None, done
self.obs[i][j] = self.state_type[i][j]
return self.obs[i][j], done
def pp(self):
for i in range(self.H):
for j in range(self.W):
if self.obs[i][j]>=0:
print(self.obs[i][j], end=' ')
else:
print('*', end=' ')
print()
def input(self):
while True:
i, j = input('请输入坐标:').split()
_, done = self.act(int(i), int(j))
if done:
print('game over!!!')
print(self.real_state)
print(self.state_type)
break
self.pp()
# 测试用
# env=Env(5, 5, 5)
# env.input()
强化学习算法library库:(集成库)
https://github.com/Denys88/rl_games
https://github.com/Domattee/gymTouch
个人github博客地址:
https://devilmaycry812839668.github.io/
本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址,还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注处,如有侵权请与博主联系。
如果未特殊标注则为原创,遵循 CC 4.0 BY-SA 版权协议。
posted on 2024-11-15 12:49 Angry_Panda 阅读(0) 评论(0) 编辑 收藏 举报