首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在BattleShip游戏程序C++中保留BattleShip命中和未命中

在BattleShip游戏程序C++中保留BattleShip命中和未命中
EN

Stack Overflow用户
提问于 2014-05-27 16:55:50
回答 1查看 5.1K关注 0票数 1

我正在尝试自学C++,所以我正在做一个战舰项目。我有一个船,董事会和BattleShip的司机类。

这个版本是相当标准的。玩家输入一个单元的坐标,试图击中一艘船。说明是否有船被击中的程序。如果一艘船占据的所有单元格都被击中,程序会打印一条消息,说明这艘船已经沉没。每次尝试后,程序通过分别以"*“或”x“标记所有成功尝试的显示板来打印当前状态。

我有一块战舰的冲浪板

代码语言:javascript
复制
   a b c d e f g h i j
  +-------------------+
 0|                   |
 1|                   |
 2|                   |
 3|                   |
 4|                   |
 5|                   |
 6|                   |
 7|                   |
 8|                   |
 9|                   |
  +-------------------+

所以我的Board构造器用空格初始化我的分数数组。

代码语言:javascript
复制
char score[10][10] 

是存储电路板每个单元的当前状态的字符数组,其中字符'x‘和'*’分别表示不成功和成功的尝试,而‘’(空格)存储在未命中的单元中。

这是我的Board Class:

代码语言:javascript
复制
#include "Board.h"
#include "Ship.h"
#include <iostream>
using namespace std;
#include <vector>
#include <string.h>
#include <stdexcept>


//member function definitions

Board::Board(void)

{
    char score[10][10] = {' '};
    for (int i = 0; i < 10; i ++) {
        for (int j = 0; j < 10; j++) {
            score[i][j] = ' ';
        }
}

}

void Board::addShip(char type, int x1, int y1, int x2, int y2) 
{
    if(shipList.size()<10)
        {
            shipList.push_back(Ship::makeShip(type,x1,y1,x2,y2));
        }
}

void Board::print(void){

 cout<< "   a b c d e f g h i j"<< endl;
    cout <<"  +-------------------+"<< endl;

    for (int i = 0; i < 10; i++) {
        // print the first character as part of the opener.
        cout<< " " << i << "|" << score[i][0];
        for (int j = 1; j < 10; j++) {
           // only add spaces for subsequent characters.
           cout << " " << score[i][j];
        }
        cout << "          |" << endl;
    }
    cout <<"  +-------------------+"<< endl;
}

void Board::hit(char c, int i){

    if (c<'a' || c>'j' || i > 9 || i<0){
        throw invalid_argument("invalid input");
    }

    Ship* ship = shipAt(i, c-'a');


    if (ship) {
        score[i][c-'a']= '*';
    }
    else{
        score[i][c-'a']= 'x';

    }

}

Ship* Board::shipAt(int x, int y)
{
  for(Ship* ship : shipList){
    if(ship->Ship::includes(x, y)){
        return ship;
    }
    else{
        return NULL;
    }
    }

}

int Board::level(void)
{
    int lev = 0;
    std::vector<Ship *>::iterator iter = shipList.begin();
    std::vector<Ship *>::iterator end = shipList.end();
    for ( ; iter != end; ++iter )
    {
       lev += (*iter)->level();
    }

    return lev;

}

不幸的是,我的输出是错误的,无论我如何改变我的函数,我得到的输出如下:(正如你所看到的,每次点击时,右边的竖线被推到右边。

代码语言:javascript
复制
   a b c d e f g h i j
  +-------------------+
 0|*                   |
 1|                   |
 2|                   |
 3|   x                |
 4|                   |
 5|                   |
 6|                   |
 7|                   |
 8|                   |
 9|                   |
  +-------------------+

我试图重新实现我的void Board::print(void)void Board::hit(char c, int i),并重新执行我的Board构造函数,但似乎什么都不起作用,错误仍然存在。我的冲浪板一直被推到右边。我不确定这是怎么解决的。

理想情况下,我会输出如下所示的输出:

代码语言:javascript
复制
  a b c d e f g h i j
  +-------------------+
 0|                   |
 1|                   |
 2|                   |
 3|                   |
 4|                   |
 5|                   |
 6|                   |
 7|                   |
 8|          x        |
 9|                   |
  +-------------------+
   a b c d e f g h i j
  +-------------------+
 0|                   |
 1|                   |
 2|                   |
 3|                   |
 4|                   |
 5|                   |
 6|                   |
 7|                   |
 8|          x   *    |
 9|                   |
  +-------------------+
EN

回答 1

Stack Overflow用户

发布于 2014-05-27 17:14:45

我猜score是一个成员变量。但在构造函数中,您可以通过使用相同的名称声明一个局部变量来隐藏它:

Board::Board(void) { char score[10][10] = {' '};

这样,成员就不会被初始化。

在Board::中打印(无效)行

cout << " |" << endl;

应该是

cout << "|" << endl;

我测试了print()方法,它看起来没问题。我看不出为什么输出的长度会增加。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23885041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档