《课程格子》的一个笔试题目

        题目如下,感觉很适合喜欢琢磨的程序员,也是考验你编码风格的时候。

Let's make a tower defense game(塔防游戏):

1. You have 1 tower, with H health and D dps(damage per second).
2. There are n attackers, each with h_i health and d_i dps.
3. The attackers are represented by an array [h_0, d_0, h_1, d_1, ...]
4. The tower deals damage to attackers in order, starting with i = 0
5. When the health of an attacker reaches 0, they die and cease to deal further damage
6. If the tower's health reaches 0, you lose

Please write a function to report the result of the battle:

If your tower dies and you lose, return 0
If your tower survives the attack, please return the amount of health left on the tower

Example function:

boolean tower_defense(int H, int D, int[] attackers);

Bonus Question:

If you do not have to deal damage to attackers in order, and can instead pick your targets, how would you improve the "AI" of your tower?

Example function:

boolean smart_tower_defense(int H, int D, int[] attackers);

Please submit sample code in a language of your choice ALONG WITH UNIT TESTS to demonstration 3-4 interesting cases. Bonus points for the following set of languages(Ruby, Objective-C, Java, Javascript)

FAQ:
If the tower and the last enemy dies at the same time, still return 0
The tower is not able to split its attack(each second, it deals exactly D damage to a single attacker)
 
//翻译如下.........................................................
让我们来制作一个塔防游戏:
(1)仅有一个塔,有H生命值和D攻击值;
(2)有n个进攻者,每个有h_i生命值和d_i攻击值;
(3)进攻者用数组[h_0,d_0,h_1,d-1...]表示;
(4)塔对进攻者的伤害是有序的,由i=0开始;
(5)当进攻者的生命值为0时,将会死亡并停止造成进一步的伤害;
(6)如果塔的生命值为0,游戏失败。
请书写一个函数来报告战斗的结果:
如果塔死掉和失败,返回0;
如果塔在攻击中存活,返回塔的生命值。
样例函数:
boolean tower_defense(int H, int D, int[] attackers);

奖励问题:
如果塔不是对进攻者采用顺序攻击,而是挑选目标,你如何改进塔的AI?
样例函数:
boolean smart_tower_defense(int H, int D, int[] attackers);

请提交代码和测试用例来说明3-4个有趣的例子。
FAQ:如果塔和最后一名进攻者在同时死掉,仍返回0。
        塔不能分裂攻击(每次,它仅仅对一个进攻者造成D伤害)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值