【问题标题】:Using Iteration in Battle'tanks' Simulation MATLAB在 Battle'tanks' Simulation MATLAB 中使用迭代
【发布时间】:2014-10-03 13:28:22
【问题描述】:

第三轮:杰西卡 vs 战车

**tl;博士: 现在,我需要帮助使我的尺寸匹配。我的函数将我标记为“如果受到攻击 == 0” 我想要做的是读取该位置的值是否为零。如果它是零,那么那里没有船,所以它是一个“未命中”。如果那里有一个数字(4、3,2 或 1),那么它就是成功的。

基本上我需要为下一个问题做的是弄清楚如何编写一个模拟战车(或战舰)游戏的函数。

http://en.wikipedia.org/wiki/Battleship_(game) in case you've never played

在这个函数中,我输入了三个输入:两个坦克位置数组(即 player1 和 player2)和一串动作(它们都用空格隔开)。该字段是一个 8x8 数组,移动由诸如​​“A7 H6”等字符串表示。A7 表示击中第 1 列第 6 行的位置。“H6”表示击中第 8 列第 6 行。我必须在我的功能中反映这一点。

我需要输出的是:获胜者坦克的数组、一个详细说明获胜者命中的向量(按击中的顺序)和一个显示获胜者的向量(或字符串“停火!”)

function[winner_field, winner_hits, tanks_destroyed] = battleTanks(player1_tanks, player2_tanks, battle_str)

游戏中有四种类型的坦克: 重型坦克由数字“4”表示,尺寸为 3x2

中型坦克用数字“3”表示是 2x2

轻型坦克由数字“1”表示,是一个 2x1

坦克歼击车用数字“2”表示,尺寸为 3x1

空格用'0'表示

注意:坦克可以垂直或水平对齐

第一个玩家总是先走,并且交替进行

不是所有的坦克都必须玩,但是如果你只玩一个坦克并且它被摧毁了,你就输了

我需要跟踪船只被击中的顺序,然后哪些被摧毁

你可以重复同样的动作

在我的输出数组中,任何坦克被击中但未被摧毁的地方都应该用 0 表示。所以可能会有“锯齿状坦克”

如何获胜:

如果玩家摧毁了玩家的所有坦克,他们就赢了

如果在所有动作结束后双方都没有失去所有坦克,则获胜者取决于对敌方舰队的命中数(而不是他们摧毁的坦克数)。

可能会出现平局,在这种情况下,函数的输出将成为辅助输出 1)第一个玩家最终 8x8 数组,2)第二个玩家最终 8x8 数组,3)a字符串,'停火!'。

对于问题的长度和我的代码,我提前道歉。

function[winner_field, winner_hits, tanks_destroyed] = battleTanks(player1_tanks, player2_tanks, battle_str)
player1_moves = mod(battle_str, 2) == 1;
player2_moves = mod(battle_str, 2) == 0; %// Player 2 is the even moves

[player1_moves, player2_moves] = strtok(battle_str);
player2_moves = char(player2_moves);
player1_moves = char(player1_moves);

column = upper(player1_moves(1)) - 64; 
row = player1_moves(2) - 48; %// Does the conversion so my funciton knows that A7 means column 1,     row 7

attacked = find(player1_moves);
attacked2 = find(player2_moves);

winner_hits = []; %// Empty vector to populate with answers
tanks_destroyed = []; %// Same as above

Counter1 = 0; %// My counter for hits, just in case they both run out of moves before finishing
Counter2 = 0;

Tanks_destroyed1 = 0; %// Counts how many tanks are destroyed by player 1
Tanks_destroyed2 = 0;

heavy_tank1 = find(player1_tanks, 4); %// locates the tanks
heavy_tank2 = find(player2_tanks, 4);
medium_tank1 = find(player1_tanks, 3);
medium_tank2 = find(player2_tanks, 3);
tank_destroyer1 = find(player1_tanks, 2);
tank_destroyer2 = find(player2_tanks, 2);
light_tank1 = find(player1_tanks, 1);
light_tank2 = find(player2_tanks, 1);

if attacked == 0  %// If they hit a zero, it counts as a mis
    attack = 'missed';
else
    attack = 'hit'; %// if they find a number, they hit a tank
   end

hit = 'hit'; %// Translates the string into a variable I can use
missed = 'missed';

if attacked == hit %// Does the counter
    Counter1 = Counter1 + 1;
elseif attacked2 == hit
    Counter2 = Counter2 + 1;
else
    Counter1 = Counter1 + 0;
    end

for field = 1:length(player1_tanks)
    if heavy_tank1 == 6 %// Counts the destroyed tanks
        tank = 'destroyed';
    Tanks_destroyed2 = Tanks_destroyed2 + 1;
elseif tank_destroyer1 == 3
    tank = 'destroyed';
    Tanks_destroyed2 = Tanks_destroyed2 + 1;
elseif medium_tank1 == 4
    tank = 'destroyed';
    Tanks_destroyed2 = Tanks_destroyed2 + 1;
elseif light_tank1 == 2
    tank = 'destroyed';
    Tanks_destroyed2 = Tanks_destroyed2 + 1;
else
    tank = 'missed';

end
end
for field = 1:length(player2_tanks)
 if heavy_tank2 == 6
    tank = 'destroyed';
    Tanks_destroyed1 = Tanks_destroyed1 + 1;
elseif tank_destroyer2 == 3
    tank = 'destroyed';
    Tanks_destroyed1 = Tanks_destroyed1 + 1;
elseif medium_tank2 == 4
    tank = 'destroyed';
    Tanks_destroyed1 = Tanks_destroyed1 + 1;
elseif light_tank2 == 2
    tank = 'destroyed';
    Tanks_destroyed1 = Tanks_destroyed1 + 1;
 else
     tank = 'missed';

 end
end

if Tanks_destroyed1 == player2_tanks %// Determines the winner
    winner_field = player1_tanks;
elseif Tanks_destroyed2 == player1_tanks
    winner_field = player2_tanks;
elseif Counter1 > Counter2
    winner_field = player1_tanks;
elseif Counter2 > Counter1
    winner_field = player2_tanks;
else
    winner_field = 'Cease Fire!';

end
if winner_field == player1_tanks
    winner_hits = hits;

end

end

这段代码真的是在扼杀我的手腕,所以我为其他手腕可能受伤的人道歉。虽然我可能过于复杂了。

Testcases
[results1, winHits1, winDestroy1] = battleTanks(battleAP1,battleAP2,moveA)
 results1 should be the same as resultsA (which is saved in battleTanks.mat)
 winHits1 = [1 1 2 2 2 3 3 3 3]
 winDestroy1 = [1 2 3]

battleAP1、battleAP2 和 moveA 都是包含硬件问题的文件。我将尝试访问他们以获取他们的信息。

所以我需要帮助的基本上是弄清楚如何格式化我的输出语句并让函数意识到 A = 第 1 列。

battleAP1:
0   0   0   0   0   0   0   0
0   0   0   3   3   0   0   0
0   2   0   3   3   0   0   0
0   2   0   4   4   4   0   0
0   2   0   4   4   4   0   0
0   0   0   0   0   0   1   0
0   0   0   0   0   0   1   0
0   0   0   0   0   0   0   0
battleAP2
0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0
0   0   0   0   0   3   3   0
0   0   4   4   0   3   3   0
0   0   4   4   0   0   0   0
0   0   4   4   2   2   2   0
0   0   0   0   0   0   0   0
0   0   0   0   0   1   1   0
MoveA: 'A5 H7 C3 G1 D3 F7 G6 C2 H6 D4 G7 H2 G8 H4 E5 D8 F6 B6 E6 A5 F5 G2 H3'

【问题讨论】:

  • 对不起,我有点糊涂了!什么不工作?或者你在挣扎什么?
  • 技术上是整个问题。抱歉,我早早按下了“提交”按钮。我不完全确定如何设置我的输出,我觉得我需要一个 while 循环,但我不确定如何去做。
  • 你必须更好地封装你的问题。没有人有时间调试整个程序。如果您确切地说出第一步应该发生的事情,您将获得更多帮助,而您却看到了。
  • 我同意@EugeneK。尽管我们在这里为您提供帮助,但您要求我们调试您的整个程序。我也没有时间 :( 你能把它缩小到你需要帮助的地方吗?我自己还没有阅读整篇文章,但这有点吓人。
  • 这是一个令人生畏的问题。现在我的主要斗争是格式化输出并弄清楚如何使函数实现 A7 表示 column1、row7。

标签: matlab if-statement while-loop iteration


【解决方案1】:

关于您在 cmets 中的特定问题,如果我有一个输入 A7 并且我想将其转换为一组索引,一种方法是将其拆分,然后减去 ASCII 偏移量。如果您不了解我使用的数字,请使用 Google ASCII 表。基本上当你在matlab中匹配一个char和一个数字时,它会自动将char转换为它的ASCII数字。

input = 'A7';
column = upper(input(1)) - 64; %upper guarantees that my letter will be upper case
row = input(2) - 48;

【讨论】:

  • 这是我的建议。 +1。如果您不记得A 的ASCII 代码是什么,另一种更稳健的方法是用double('A') 减去。换句话说:column = upper(input(1)) - double('A');。另外,一些小问题:input 是 MATLAB 中的预定义函数。您可能希望将名称更改为另一个变量,以免无意中遮住函数。
  • 这是有道理的。我差点忘了 ASCII 值。
  • @JessicaMarie - 你怎么能:P!这是您在此处发布的第一篇文章 :)
  • 我试着忘记那些让我夜不能寐的事情。这对认知和去甲肾上腺素/肾上腺素水平不利:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
  • 2016-06-30
  • 1970-01-01
相关资源
最近更新 更多