【问题标题】:Battleships game, ships drowned in C战舰游戏,船淹没在 C
【发布时间】:2015-12-23 16:30:21
【问题描述】:

我正在制作一个“战舰”游戏,在放置所有战舰、开始游戏等操作后,用户将输入“攻击坐标”,如果他击中了一艘战舰,则棋盘会显示“V”在他击中的坐标中,如果那里没有任何船只,则按“X”。

好吧,我做了那部分,这不是问题所在,我有 4 艘不同的船,每艘都有不同的大小,要淹死这艘船,你必须击中它的所有部分(它的大小)。

例子:

4|*
3|  *
2|    *
1|
0|* * * *
  - - - - -
  0 1 2 3 4
Enter coordinates for attack
(0,4)

4|V
3|  *
2|    *
1|
0|* * * *
  - - - - -
  0 1 2 3 4
Enter coordinates for attack
(1,3)

4|V
3|  V
2|    *
1|
0|* * * *
  - - - - -
  0 1 2 3 4
Enter coordinates for attack
(2,2)

4|V
3|  V
2|    V
1|
0|* * * *
  - - - - -
  0 1 2 3 4

the battleship of size 3 has drowned!

如果 4 艘战列舰被淹死,那么游戏就结束了!我所做的是检查 所有的船坐标都是'V',如果是这样,战舰已经淹死了,但我认为我的问题是有不止一艘船,如果我每次都检查这个,它总是说第一艘战舰已经淹死了。

我制作了两个包含船只位置的数组:

posX[] = {1,2,3,4}
posY[] = {1,2,3,4}

(表示船在位置 (1,1) (2,2) (3,3) (4,4) 大小为 4。)

我的做法是这样的:

for (int i = 0 l i < 4 ; i++)
{
if (board[posX[i]][posY[i] == 'V')
{
count++;
}
}
if ( count == 4 ) printf("the battleship of size 4 has drowned\n");

我尝试了一些熟悉的方法来检查所有坐标是否都是'V' printf,但它是相同的。

【问题讨论】:

  • 如果您也可以在问题中提供您的代码
  • 如果没有看到你的代码,绝对不可能说出什么是错误的。
  • 欢迎来到 SO。请阅读What topics can I ask aboutHow to ask a good question
  • @lan 我编辑了帖子,如果你能检查出来我会很高兴

标签: c drawing 2d-games coordinate-systems


【解决方案1】:

您需要为每艘战列舰维护一个状态,例如 isDead[4] 等。 init 为 0。一旦战舰被宣布死亡,你通知它已经死亡,将状态更改为 1 并稍后停止检查

【讨论】:

    【解决方案2】:

    此外,您不需要总是检查它。如果每次玩家击中一艘船,你检查船是否被淹死(它的所有坐标都被击中),你只需要增加一个 drowned_ships 计数器并检查该值是否已经等于停止/终止条件(drowned_ships = = 4)。

    #define DROWNED_SHIPS_STOP 4
    
    bool ship::isDrowned(){
        for (int i = 0 ; i < size() ; i++){
            //size() here would return 4 for your example -> it is the size of the ship
            if (board[posX[i]][posY[i] == 'V'){
                count++;
            }
        }
        return (count == size()) ? true:false;
    }
    
    (...)
    
    //Receive a x and y
    coordinates = get_player_input();
    
    Ship * ship;
    if (ship = isShip(coordinates.x,coordinates.y)){
        //test if a ship has been hit
        if( ship.isDrowned() ){
            //every position of that ship has been hit
            ships_drowned++;
            if (ships_drowned == DROWNED_SHIPS_STOP)
                DisplayGameTermination();
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 2018-01-05
      • 2018-01-05
      • 2022-01-02
      • 1970-01-01
      • 2021-03-05
      相关资源
      最近更新 更多