查看: 16465|回复: 5

[原创发布] 带迷雾的大海战

[复制链接]
梦石
1
星屑
24088
在线时间
3339 小时
注册时间
2011-7-8
回帖
3862

开拓者

发表于 2021-4-18 00:53:31 | 显示全部楼层 |阅读模式
游戏发布
游戏类型:其他
版本信息:v1.0
制作工具:其他
游戏时长:0.1 小时
下载地址:

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 guoxiaomi 于 2021-5-6 15:55 编辑

这是个概念性的游戏,尝试改进了经典游戏《大海战》,基本规则如下:

1. 双方各有10艘战舰,用 x 表示,分布在12x12的棋盘上、下两半
2. 每回合,双方各选1个坐标进行攻击。
3. 如果攻击直接命中了敌方战舰,该战舰会被击沉。
4. 如果攻击未命中,会尝试驱散3x3范围内的战争迷雾,移除战争迷雾的效果会逐渐消散。
5. 战舰本身也会制造战争迷雾,也会驱散对方的战争迷雾,并且多个战舰的效果会叠加。
6. 一开始全场都覆盖有战争迷雾(本底),此迷雾会逐渐消散。
7. 暴露给对方的战舰会立刻被摧毁。
8. 击毁全部敌方战舰获胜。

这个规则具体解释起来还是挺绕的,还是直接看源码比较快。
20210506更新:C++版本 battleship_v3.zip (26.27 KB, 下载次数: 111) ,也不知道能不能玩……

P0由玩家控制,P1由AI控制,AI没有作弊。

四张截图:
游戏开始,我方准备打击B2区域
Screenshot 2021-04-18 004554.png
我方F9区域战舰暴露,自动被击沉
Screenshot 2021-04-18 004656.png
双方战损近半,战况十分焦灼
Screenshot 2021-04-18 004931.png
游戏结束,我方仅剩2艘战舰存活
Screenshot 2021-04-18 005237.png

源码如下,可以去https://replit.com/@gxm11/battleshipv2?lite=true试玩,点击最上面的run即可:
点击展开/收起
  1. # ship battle game
  2. # 1. ships can generate a detect value to every block nearby, v = 1 / (dx * dx + dy * dy)
  3. # 2. the sea has its own detect limit, which will decrease slightly
  4. # 3. attack will add a temp detect value to 3x3 blocks
  5. # 4. any block where detect value * 2 + temp value > sum(detect value) + detect limit will be visible
  6. import numpy as np
  7. import re
  8. import string
  9. import sys


  10. class Ship_Battle:
  11.     def __init__(self, width, height):
  12.         self.width = width
  13.         self.height = height

  14.         def kernel_r2(dx, dy):
  15.             return 1 / (1 + dx * dx + dy * dy)

  16.         self.kernel = self.kernel_cache(kernel_r2)

  17.         def kernel_3x3(dx, dy):
  18.             if dx == 0 and dy == 0:
  19.                 return 8
  20.             if abs(dx) <= 1 and abs(dy) <= 1:
  21.                 return 1
  22.             return 0

  23.         self.attack = {"normal": self.kernel_cache(kernel_3x3)}

  24.     def kernel_cache(self, kernel):
  25.         ret = np.zeros((self.width * 2, self.height * 2))
  26.         for i in range(self.width * 2):
  27.             for j in range(self.height * 2):
  28.                 ret[i, j] = kernel(i - self.width, j - self.height)
  29.         return ret

  30.     def reset(self):
  31.         self.turn = 0
  32.         self.water_level = 1
  33.         self.temp_value = np.zeros((self.width, self.height, 2))

  34.         self.ships = self.create_ships(10)
  35.         self.broken_ships = []

  36.     def winner(self):
  37.         for i in [0, 1]:
  38.             if len(self.ships[i]) == 0:
  39.                 return 1 - i
  40.         return None

  41.     def create_ships(self, n):
  42.         # p0's ship is in 0 ~ w-1  h/2 ~ h-1
  43.         pos = np.arange(self.width * self.height / 2, dtype=int)
  44.         np.random.shuffle(pos)
  45.         ships_0 = []
  46.         for i in pos[0:n]:
  47.             _x, _y = i % self.width, i // self.width + self.height // 2
  48.             ships_0.append((_x, _y))

  49.         # p1's ship is in 0 ~ w-1  0 ~ h/2-1
  50.         pos = np.arange(self.width * self.height / 2, dtype=int)
  51.         np.random.shuffle(pos)
  52.         ships_1 = []
  53.         for i in pos[0:n]:
  54.             _x, _y = i % self.width, i // self.width
  55.             ships_1.append((_x, _y))

  56.         return (ships_0, ships_1)

  57.     def update(self):
  58.         for i, ships in enumerate(self.ships):
  59.             value = self.value_int(1 - i)
  60.             for _x, _y in ships:
  61.                 if value[_x, _y] == 1:
  62.                     ships.remove((_x, _y))
  63.                     self.broken_ships.append((_x, _y))

  64.         self.turn += 1
  65.         self.water_level = self.water_level * 0.9
  66.         self.temp_value = self.temp_value * 0.9

  67.     def action(self, a):
  68.         pid, x, y, kind = a
  69.         x0, y0 = self.width - x, self.height - y
  70.         dv = self.attack[kind][x0:x0 + self.width, y0:y0 + self.height]
  71.         self.temp_value[:, :, pid] += dv

  72.     def calculate_value(self):
  73.         values = np.zeros((self.width, self.height, 2))
  74.         for i, ships in enumerate(self.ships):
  75.             v = np.zeros((self.width, self.height))
  76.             for _x, _y in ships:
  77.                 x0, y0 = self.width - _x, self.height - _y
  78.                 v += self.kernel[x0:x0 + self.width, y0:y0 + self.height]
  79.             values[:, :, i] = v
  80.         return values

  81.     def value_int(self, pid):
  82.         ret = np.zeros((self.width, self.height), dtype=int)
  83.         value = self.calculate_value()
  84.         v0 = value[:, :, pid] * 2 + self.temp_value[:, :, pid]
  85.         v1 = np.sum(value, axis=2) + self.water_level
  86.         ret[v0 >= v1] = 1
  87.         return ret

  88.     def render(self, pid=0):
  89.         template = "T%-2d"
  90.         for i in range(self.width):
  91.             template += "%2s" % string.ascii_uppercase[i]
  92.         for j in range(self.height):
  93.             template += "\n"
  94.             template += "%3d" % (j + 1)
  95.             for i in range(self.width):
  96.                 template += "%2s"

  97.         vars = [self.turn]
  98.         value = self.value_int(pid)

  99.         for j in range(self.width):
  100.             for i in range(self.height):
  101.                 if (i, j) in self.broken_ships:
  102.                     c = "#"
  103.                 elif (i, j) in self.ships[pid]:
  104.                     c = "x"
  105.                 else:
  106.                     if value[i, j] == 1:
  107.                         if (i, j) in self.ships[1 - pid]:
  108.                             c = "!"
  109.                         else:
  110.                             c = " "
  111.                     else:
  112.                         c = "."

  113.                 vars.append(c)

  114.         print(template % tuple(vars))

  115.     def render_action(self, a, pid=0):
  116.         value = self.value_int(pid)
  117.         if value[a[1], a[2]] == 1:
  118.             t = (a[0], string.ascii_uppercase[a[1]], a[2] + 1)
  119.             print("P%d attack %s%d" % t)
  120.         else:
  121.             print("P%d attack ?" % a[0])


  122. class Game:
  123.     def __init__(self, game):
  124.         self.game = game

  125.     def run(self):
  126.         g = self.game
  127.         g.reset()
  128.         g.render(0)
  129.         self.cpu = None
  130.         while g.winner() is None:
  131.             a0 = None
  132.             while a0 is None:
  133.                 a0 = self.human_action(g)
  134.             a1 = self.cpu_action(g)
  135.             g.action(a0)
  136.             g.action(a1)
  137.             g.update()
  138.             g.render_action(a0)
  139.             g.render_action(a1)
  140.             g.render(0)
  141.         print("winner is P%d" % g.winner())

  142.     def run_test(self):
  143.         g = self.game
  144.         w = [0, 0, 0]
  145.         for i in range(1000):
  146.             g.reset()
  147.             self.cpu = None
  148.             while g.winner() is None:
  149.                 a = self.human_action_test(g)
  150.                 a = self.cpu_action(g)
  151.                 g.action(a)
  152.                 g.action(a)
  153.                 g.update()
  154.             w[g.winner()] += 1
  155.             w[2] += g.turn
  156.         w[2] = w[2] / (w[0] + w[1])
  157.         print("P0 win: %d, P1 win %d, Mean Turns = %.2f" % tuple(w))

  158.     def human_action(self, game):
  159.         str = input("[T%d] Type pos to attack: " % game.turn)
  160.         m = re.match(r"^(\w)(\d+)$", str)
  161.         if m is None:
  162.             return
  163.         if m.group(1) in string.ascii_uppercase:
  164.             x = ord(str[0]) - ord('A')
  165.         else:
  166.             x = ord(str[0]) - ord('a')
  167.         y = int(m.group(2)) - 1
  168.         return (0, x, y, "normal")

  169.     def human_action_test(self, game):
  170.         _x = np.random.randint(game.width)
  171.         _y = np.random.randint(game.height // 2)
  172.         return (0, _x, _y, "normal")

  173.     def cpu_action(self, game):
  174.         if self.cpu is None:
  175.             pos = []
  176.             for i in range(game.width * game.height // 2):
  177.                 _x, _y = i % game.width, i // game.width + game.height // 2
  178.                 pos.append((_x, _y))
  179.             self.cpu = [pos, None]

  180.         pos, last = self.cpu
  181.         value = game.value_int(1)
  182.         for i, j in pos:
  183.             if value[i, j] == 1:
  184.                 pos.remove((i, j))
  185.         if len(pos) > 0:
  186.             ix = np.random.randint(len(pos))
  187.             action_x, action_y = pos[ix]
  188.         else:
  189.             _x = np.random.randint(game.width)
  190.             _y = np.random.randint(game.height)
  191.             return (1, _x, _y, "normal")

  192.         if last is not None:
  193.             x, y = last
  194.             x_shift, y_shift = 0, 0

  195.             for i in [-1, 0, 1]:
  196.                 for j in [-1, 0, 1]:
  197.                     if 0 <= x + i < game.width and 0 <= y + j < game.height:
  198.                         if value[x + i, y + j] == 0:
  199.                             x_shift += i
  200.                             y_shift += j
  201.             x_shift = int((x_shift + 0.5) // 1.5)
  202.             y_shift = int((y_shift + 0.5) // 1.5)
  203.             _x = x + x_shift
  204.             _y = y + y_shift
  205.             if (_x, _y) in pos:
  206.                 action_x, action_y = _x, _y

  207.         self.cpu[0].remove((action_x, action_y))
  208.         self.cpu[1] = (action_x, action_y)
  209.         return (1, action_x, action_y, "normal")


  210. if __name__ == "__main__":
  211.     if len(sys.argv) == 2:
  212.         if sys.argv[1] == "test":
  213.             g = Game(Ship_Battle(12, 12))
  214.             g.run_test()
  215.             exit()

  216.     g = Game(Ship_Battle(12, 12))
  217.     while True:
  218.         g.run()
复制代码

评分

参与人数 1赞 +1 收起 理由
赤炎 + 1

查看全部评分

熟悉rgss和ruby,xp区版主~
正在填坑:《膜拜组传奇》讲述膜拜组和学霸们的故事。
已上steam:与TXBD合作的Reformers《变革者》
* 战斗调用公共事件 *
* RGSOS 网络脚本 *
梦石
1
星屑
175
在线时间
17 小时
注册时间
2017-4-24
回帖
9
发表于 2021-4-18 04:06:15 | 显示全部楼层
前排!

评分

参与人数 1赞 +1 收起 理由
guoxiaomi + 1 塞糖

查看全部评分

回复

使用道具 举报

梦石
0
星屑
999
在线时间
73 小时
注册时间
2021-4-25
回帖
63
发表于 2021-4-25 17:05:24 | 显示全部楼层
你………………

评分

参与人数 1赞 +1 收起 理由
guoxiaomi + 1 塞糖

查看全部评分

回复

使用道具 举报

梦石
8
星屑
2872
在线时间
476 小时
注册时间
2010-9-11
回帖
480
发表于 2021-4-25 19:49:54 | 显示全部楼层
感觉好有趣!哪天我也做一个试试

评分

参与人数 1赞 +1 收起 理由
guoxiaomi + 1 塞糖

查看全部评分

Paku
回复

使用道具 举报

梦石
0
星屑
12824
在线时间
1010 小时
注册时间
2019-11-5
回帖
2329

极短27参与极短25参与极短24参与

发表于 2021-4-25 22:43:08 | 显示全部楼层
感觉拓宽了游戏的界限233

点评

猜硬币正反也是游戏,而且规模可以大到全世界一起玩:https://zhuanlan.zhihu.com/p/24071884  发表于 2021-4-26 00:40
风继续吹,不忍远离。年少轻狂,眼神明亮,最好的年纪,最好的时光。希望岁月是一场春梦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部