user3674560
user3674560

Reputation:

Hold BattleShip hits and misses in BattleShip Game Program C++

I am trying to teach myself C++ so I am doing a Battleship program. I have a Ship, Board and BattleShip Driver class.

This version is fairly standard. The player enters the coordinates of a cell to try to hit a ship. The program stating if a ship is hit. If all cells occupied by a ship are hit, the program prints a message stating that that ship is sunk. After each attempt, the program prints the current state by showing the board with all successful attempts marked by "*" or "x"respectively.

I have a board for the Battleships

   a b c d e f g h i j
  +-------------------+
 0|                   |
 1|                   |
 2|                   |
 3|                   |
 4|                   |
 5|                   |
 6|                   |
 7|                   |
 8|                   |
 9|                   |
  +-------------------+

So my Board Constructor initializes my scores array with spaces.

char score[10][10] 

is array of char storing the current state of each cell of the board, with char 'x' and '*' representing unsuccessful and successful attempts respectively, and ' ' (space) in cells that were not hit.

Here is my Board Class:

#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;

}

Unfortunately my output is wrong no matter how much I change my functions I get output like the following: (As you can see the right vertical line is pushed out to the right every hit.

   a b c d e f g h i j
  +-------------------+
 0|*                   |
 1|                   |
 2|                   |
 3|   x                |
 4|                   |
 5|                   |
 6|                   |
 7|                   |
 8|                   |
 9|                   |
  +-------------------+

I tried to reimplement my void Board::print(void) , void Board::hit(char c, int i), and re-do my Board constructor, but nothing seems to be working the error keeps on persisting. My board keeps on getting pushed out to the right. I'm not sure how could this be fixed.

Ideally I would output like to produce output like following:

  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|                   |
  +-------------------+

Upvotes: 1

Views: 5503

Answers (1)

n-o-d
n-o-d

Reputation: 196

I guess score is a member variable. But in the constructor you hide it by declaring a local variable using the same name:

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

This way the member is not initialized.

in Board::print(void) the line
cout << " |" << endl;
should be
cout << "|" << endl;

I tested the print() method and it looked ok. Can't see any reason why the length of the output could increase.

Upvotes: 2

Related Questions