Skip to content

Commit

Permalink
Making balance changes (#3)
Browse files Browse the repository at this point in the history
* Kill player when timeout

* Update README.md

* Adjust consts

* Code style update

* Added timeleft records
  • Loading branch information
stevenjoezhang authored May 26, 2019
1 parent 6c70720 commit a69276a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 38 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,34 @@ Osmo是受到到Osmos启发,而制作的一个简化版游戏框架。这里

## 游戏结束判定

后文中的『代码出错』定义为:抛出未被捕获的异常,或者返回值不满足输入输出要求。
后文中的『代码出错』定义为:抛出未被捕获的异常,或者返回值不满足输入输出要求。
『超时』定义为:某一方的代码运行总时间超出`MAX_TIME`(单位为秒)的限制。

### 获胜条件

- 对方星体被吞噬且己方存活
- 达到最大回合数且己方星体半径大于对方
- 对方代码出错且己方运行正常
- 对方超时且己方未超时

### 失利条件

- 己方星体被吞噬且对方存活
- 达到最大回合数且对方星体半径大于己方
- 己方代码出错且对方运行正常
- 己方超时且对方未超时

### 平局条件

- 双方星体同时被吞噬
- 达到最大回合数且双方星体半径相等
- 双方代码同时出错
- 双方同时超时

## 数据结构与算法

对于游戏内核的描述可以参见[KERNEL.md](KERNEL.md)

## 时间空间限制

双方代码的运算总时长均限制为10s。在运算时长耗尽后,玩家将会失去对星体的控制(只是无法再进行弹射,而非立即判负),直到游戏以某种方式结束。

## 备注

欢迎通过Issue或者Pull Request帮助我们完善代码、修复问题。
Expand Down
4 changes: 2 additions & 2 deletions src/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def move(self, frame_delta):
"""Move the cell according to its velocity.
Args:
frame_delta: Time interval between two frames.
frame_delta: time interval between two frames.
Returns:
Expand All @@ -141,7 +141,7 @@ def copy(self):
Args:
Returns:
the copy
the copy of the cell.
"""
res = Cell(self.id, self.pos[:], self.veloc[:], self.radius)
Expand Down
4 changes: 2 additions & 2 deletions src/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
# DELTA_VELOC
"DELTA_VELOC": 5,
# MAX_TIME
"MAX_TIME": 10,
"MAX_TIME": 30,
# MAX_FRAME
"MAX_FRAME": 5000,
"MAX_FRAME": 2000,
# SAFE_DIST
"SAFE_DIST": 30,

Expand Down
49 changes: 20 additions & 29 deletions src/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def __init__(self, player0, player1, names = None, recorders = None):
self.recorders = recorders
# Init
self.new_game()
self.player0 = player0
self.player1 = player1
self.player = [player0, player1]
self.names = names

# Methods
Expand Down Expand Up @@ -185,7 +184,7 @@ def update(self, frame_delta):
"""Create new frames.
Args:
frame_delta: Time interval between two frames.
frame_delta: time interval between two frames.
Returns:
Expand Down Expand Up @@ -235,40 +234,30 @@ def update(self, frame_delta):
allcells = [cell for cell in self.cells if not cell.dead]
self.cells_count = len(allcells)

theta0 = theta1 = None
flag0 = flag1 = False
theta = [None, None]
flag = [False, False]

if self.timer[0] > 0:
for i in 0, 1:
try:
ti = pf()
theta0 = self.player0.strategy([c.copy() for c in allcells])
theta[i] = self.player[i].strategy([c.copy() for c in allcells])
tf = pf()
self.timer[0] -= tf - ti
self.timer[i] -= tf - ti
except Exception as e:
logging.error(traceback.format_exc())
flag0 = e
if self.timer[1] > 0:
try:
ti = pf()
theta1 = self.player1.strategy([c.copy() for c in allcells])
tf = pf()
self.timer[1] -= tf - ti
except Exception as e:
logging.error(traceback.format_exc())
flag1 = e
flag[i] = e

if isinstance(theta0, (int, float, type(None))):
if self.timer[0] >= 0:
self.eject(self.cells[0], theta0)
else:
flag0 = True
if isinstance(theta1, (int, float, type(None))):
if self.timer[1] >= 0:
self.eject(self.cells[1], theta1)
else:
flag1 = True
if self.check_point(flag[0], flag[1], "RUNTIME_ERROR"):
return

if self.check_point(not isinstance(theta[0], (int, float, type(None))), not isinstance(theta[1], (int, float, type(None))), "INVALID_RETURN_VALUE"):
return

if self.check_point(self.timer[0] < 0, self.timer[1] < 0, "TIMEOUT"):
return

self.check_point(flag0, flag1, "RUNTIME_ERROR")
self.eject(self.cells[0], theta[0])
self.eject(self.cells[1], theta[1])

def update_recorders(self):
"""Put values into recorders.
Expand All @@ -283,9 +272,11 @@ def update_recorders(self):
for i, rec in enumerate(self.recorders):
rec.frame = self.frame_count # Current frame
rec.cells_count = len(self.cells)
rec.timer = self.timer[:]


class WorldStat:
def __init__(self, frames):
self.total_frames = frames
self.frame = 0
self.timer = [Consts["MAX_TIME"]] * 2

0 comments on commit a69276a

Please sign in to comment.