5 Replies - 360 Views - Last Post: 29 November 2011 - 08:53 PM Rate Topic: -----

#1 ironelf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-November 11

battleship program: outputting the grid does not seem to want to work

Posted 29 November 2011 - 07:49 PM

okay so i just started learning programming and in our class we have to create a battleship program. we are using netbeans and after working on it for a while, whenever i output the grid it is filled with crazy symbols. We have not learned a whole lot but any advice on what i am doing wrong would be great.

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

int rows = 10;

// Function prototypes
bool isgameover(char [][10], char [][10], int);
bool testmove(int, int, char [][10], int);
int playermovement(char [][10], char [][10], int);
int computermovement(char [][10], char [][10], int);
void printBoard(char [][10], int);
int placePiece(char, int, char [][10], int);
int intializeBoard(char [][10], int);
void clearscreen();

int main() {

    const char battleship = 'B';
    const char aircraftcarrier = 'A';
    const char cruiser = 'C';
    const char submarine = 'M';
    const char destroyer = 'D';
    int battleshipsize = 4;
    int aircraftcarriersize = 5;
    int cruisersize = 3;
    int submarinesize = 3;
    int destroyersize = 2;
    char pSB[rows][10];
    char pHB[rows][10];
    char cSB[rows][10];
    char cHB[rows][10];
    intializeBoard(pSB, rows);
    intializeBoard(cSB, rows);
    intializeBoard(cHB, rows);
    intializeBoard(pHB, rows);
    placePiece(battleship, battleshipsize, cSB, rows);
    placePiece(aircraftcarrier, aircraftcarriersize, cSB, rows);
    placePiece(cruiser, cruisersize, cSB, rows);
    placePiece(submarine, submarinesize, cSB, rows);
    placePiece(destroyer, destroyersize, cSB, rows);
    placePiece(battleship, battleshipsize, pSB, rows);
    placePiece(aircraftcarrier, aircraftcarriersize, pSB, rows);
    placePiece(cruiser, cruisersize, pSB, rows);
    placePiece(submarine, submarinesize, pSB, rows);
    placePiece(destroyer, destroyersize, pSB, rows);
    while(isgameover(pSB, cHB, rows) != true || isgameover(cSB, pHB, rows)){
        clearscreen();
        cout << "-----------Player Ship Board----------\n";
        printBoard(pSB, rows);
        cout << "-----------Player Hit Board-----------\n";
        printBoard(pHB, rows);
        playermovement(cSB, pHB, rows);
    }
    return 0;
}

void clearscreen() {
    printf("\e[1;1H\e[2J");
}

int intializeBoard(char array[][10], int rows) {
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
        return array[i][j] = 'S';
        }
    }
}

int placePiece(char ship, int size, char array[][10], int rows){
    int xstart = 0;
    int ystart = 0;
    while (xstart == 0 || ystart == 0){
        xstart = rand() % 10;
        ystart = rand() % 10;
        
    }while (array[xstart][ystart] == 'S'){
        array[xstart][ystart] = ship;
    }
    int direction = 0;
    direction = rand() % 2;
    if (direction == 1){
        for (int i = 1; i < size; i++){
            if (array[xstart+i][ystart] == 'S'){
            array[xstart+i][ystart] = ship;
            }}
    }else{
        for (int i = 1; i < size; i++){
            if (array[xstart][ystart+i] == 'S'){
            array[xstart][ystart+i] = ship;
        }}
    }
}

void printBoard(char array[][10], int rows){
    for (int i = 0; i < rows; i++){
        cout << (i+1) << " |";
        for (int j = 0; j < rows; j++){
            cout << array[i][j] << " | ";
        }
        cout << endl;
   }
}

int playermovement(char array[][10], char array2[][10], int rows){
    cout << "It's your turn to attack!\n";
    int xpoint, ypoint = 0;
    cout << "Select a x coordinate to attack:\n";
    cin >> xpoint;
    cout << "select a y coordinate to attack:\n";
    cin >> ypoint;
    if (array[xpoint][ypoint] != 'S'){
        array2[xpoint][ypoint] = 'X';
    }else if (array[xpoint][ypoint] == 'S'){
        array2[xpoint][ypoint] = 'O';
    }
    testmove(xpoint, ypoint, array2, rows);
}

int computermovement(char array[][10], char array2[][10], int rows){
    cout << "Its the computers turn!\n";
    int xattack, yattack = 0;
    xattack = rand() % 10;
    yattack = rand() % 10;
    if (array[xattack][yattack] != 'S'){
        array2[xattack][yattack] = 'X';
    }else if (array[xattack][yattack] == 'S'){
        array2[xattack][yattack] = 'O';
    }
}

bool testmove (int xpoint, int ypoint, char array2[][10], int rows){
    if (array2[xpoint][ypoint] == 'X' || array2[xpoint][ypoint] == 'O'){
        return true;
    }else{
        return false;
    }
}

bool isgameover (char array[][10], char array2[][10], int rows){
    for (int i = 0; i < 10; i++){
        for (int k = 0; k < 10; k++){
            if (array[i][k] != 0){
                if (array2[i][k] == 'X'){
                    return true;
                }else{
                    continue;
                }
            }else{
                continue;
            }
        }
    }
}





Is This A Good Question/Topic? 0
  • +

Replies To: battleship program: outputting the grid does not seem to want to work

#2 ironelf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-November 11

Re: battleship program: outputting the grid does not seem to want to work

Posted 29 November 2011 - 08:09 PM

i think it has something to do with the place piece function but im not entirely sure
Was This Post Helpful? 0
  • +
  • -

#3 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,185
  • Joined: 05-May 05

Re: battleship program: outputting the grid does not seem to want to work

Posted 29 November 2011 - 08:14 PM

You have a syntax error.

 return array[i][j] = 'S';



That's not legal C++ syntax.

Where have you defined initializeBoard(char, int)?

Add

int intializeBoard(char  , int){}


to the end of your file.

When I run the program, the opponents board has question marks in each box, which means your trying to display an undefined character. The reason this is happening is because your printing a character that never even got a value.

void printBoard(char array , int rows)
{
    for (int i = 0; i < rows; i++) {
        cout << (i+1) << " |";
        for (int j = 0; j < rows; j++) {
            cout << array  << " | ";
        }
        cout << endl;
    }
}



You call that function with:

printBoard(pSB, rows);


There's no point in the code where you give pSB a value. So you when you display it in printBoard, you get gibberish. You can simply initialize pSB to see what I mean. Try this:

char pSB  = 'A';

This post has been edited by blackcompe: 29 November 2011 - 08:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 ironelf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-November 11

Re: battleship program: outputting the grid does not seem to want to work

Posted 29 November 2011 - 08:19 PM

i deleted the return array part and it cleared up the problem. but now when it places the pieces they run off the screen or do not get fully placed. thanks for the help is there anything else i need to do?
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,185
  • Joined: 05-May 05

Re: battleship program: outputting the grid does not seem to want to work

Posted 29 November 2011 - 08:41 PM

View Postironelf, on 29 November 2011 - 08:19 PM, said:

i deleted the return array part and it cleared up the problem. but now when it places the pieces they run off the screen or do not get fully placed. thanks for the help is there anything else i need to do?


I know you probably don't want to re-design your application, but really you ought to be using a 2D array of characters to represent the board. Here's an example of how it should be done.
Was This Post Helpful? 0
  • +
  • -

#6 ironelf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-November 11

Re: battleship program: outputting the grid does not seem to want to work

Posted 29 November 2011 - 08:53 PM

what if i dont know if it is going to be true or false?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1