Battleships

I am completely stuck now on my void setupships, bool check position, and the play game. I know that the setting up ships requires the players to choose the coordinates of their five ships, the check position will be the computer(if I am correct) checking whether or not a ship is there, if so an S will be placed on the grid. The play game requires the players to choose coordinates and then a hit a miss will come up. Any help is much appreciated. Thanks



#include <iostream>
using namespace std;

void initializeBoard();//Starting the board
void displayBoard();//Displays p1 and p2 board
void setupShips();//Seting the chips up, choosing the locations of the ships
bool checkPosition(int xpos, int ypos, int player);//checking the players position if the position is correct then it is a hit, if not it is a miss
void playGame();

char p1board[10][10];//player 1 board
char p2board[10][10];//player 2 board

int main()//my main functions
{
initializeBoard();
displayBoard();
setupShips();
// checkPosition(xpos, ypos, player);//x and y positions of each pleayer
// playGame();
return 0;
} // close main function


// Initialize Board function.
void initializeBoard()
{
for (int i = 0; i<10; i++)
{
for (int j = 0; j < 10; j++)
{
p1board[i][j] = '0';
p2board[i][j] = '0';
} // close inner loop.
} // close outer loop.
} // close initializeBoard function.

// Display Board function.
void displayBoard()
{
cout << "\n1 2 3 4 5 6 7 8 9\n";
for (int i = 0; i < 10; i++)
{
if(i<9)
{
cout << i + 1 << " ";
}
else
{
cout << "10 ";
}
for (int j = 0; j < 10; j++)
{
cout << p1board[i][j];

} // close inside loop.
cout << endl;
} // close loop.
cout << "\n\n";
cout << "\n1 2 3 4 5 6 7 8 9\n";
for (int i = 0; i < 10; i++)
{
if(i<9)
{
cout << i + 1 << " ";
}
else
{
cout << "10 ";
}
for (int j = 0; j < 10; j++)
{

cout << p2board[i][j];
} // close inside loop.
cout << endl;
} // close loop.
cout << "\n\n";
} // close displayBoard function.

// Setup Ships function
void setupShips()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
p1board[i][j] = '*'; //This is where the players will choose where...
p2board[i][j] = '*';//..they want their ships to be placed on grid..
} // close inner for loop.
} // close outer for loop.
} // close setupShips function.
// bool CheckPosition goes here.
bool checkPosition(int xpos, ypos)
{ //This is where the player will choose coordinates and if there is a ship it will..
//say S

if (p1board[xpos][ypos] = '*') & (p1board[xpos][ypos] = 's')
{
return true;
} // close if block.
return false;
else if (p2board[xpos][ypos] = '*') & (p2board[xpos][ypos] = 's')
{
return true;
} // close else if block.
return false;
} // close checkPosition function.

// playGame function goes here.// Pretty stuck here
// Declare variables.
char miss = 'm';
char hit = 'h';
char mark = '*';
int xpos, ypos, player;
bool validMove;
char winner = '?';
First I took the liberty of cleaning up the code format a little bit. (Anytime you post code you should click the 'pound' symbol to insert code tags.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

#include <iostream>
using namespace std;

void initializeBoard();//Starting the board
void displayBoard();//Displays p1 and p2 board
void setupShips();//Seting the chips up, choosing the locations of the ships
bool checkPosition(int xpos, int ypos, int player);//checking the players position if the position is correct then it is a hit, if not it is a miss
void playGame();

char p1board[10][10];//player 1 board
char p2board[10][10];//player 2 board

int main()//my main functions
{
    initializeBoard();
    displayBoard();
    setupShips();
    // checkPosition(xpos, ypos, player);//x and y positions of each pleayer
    // playGame();
    return 0;
} // close main function


// Initialize Board function.
void initializeBoard()
{
    for (int i = 0; i<10; i++)
        {
            for (int j = 0; j < 10; j++)
                {
                    p1board[i][j] = '0';
                    p2board[i][j] = '0';
                } // close inner loop.
        } // close outer loop.
} // close initializeBoard function.

// Display Board function.
void displayBoard()
{
    cout << "\n1 2 3 4 5 6 7 8 9\n";
    for (int i = 0; i < 10; i++)
        {
            if(i<9)
                {
                    cout << i + 1 << " ";
                }
            else
                {
                    cout << "10 ";
                }
            for (int j = 0; j < 10; j++)
                {
                    cout << p1board[i][j];

                } // close inside loop.
            cout << endl;
        } // close loop.
    cout << "\n\n";
    cout << "\n1 2 3 4 5 6 7 8 9\n";
    for (int i = 0; i < 10; i++)
        {
            if(i<9)
                {
                    cout << i + 1 << " ";
                }
            else
                {
                    cout << "10 ";
                }
            for (int j = 0; j < 10; j++)
                {

                    cout << p2board[i][j];
                } // close inside loop.
            cout << endl;
        } // close loop.
    cout << "\n\n";
} // close displayBoard function.

// Setup Ships function
void setupShips()
{
    for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
                {
                    p1board[i][j] = '*'; //This is where the players will choose where...
                    p2board[i][j] = '*';//..they want their ships to be placed on grid..
                } // close inner for loop.
        } // close outer for loop.
} // close setupShips function.

// bool CheckPosition goes here.
bool checkPosition(int xpos, ypos)
{
     //This is where the player will choose coordinates and if there is a ship it will..
    //say S

    if (p1board[xpos][ypos] = '*') & (p1board[xpos][ypos] = 's')
        {
            return true;
        } // close if block.
    return false;
    else if (p2board[xpos][ypos] = '*') & (p2board[xpos][ypos] = 's')
        {
            return true;
        } // close else if block.
    return false;
} // close checkPosition function.

// playGame function goes here.// Pretty stuck here
// Declare variables.
char miss = 'm';
char hit = 'h';
char mark = '*';
int xpos, ypos, player;
bool validMove;
char winner = '?';
First thing I noticed is you your bool checkPosition function declaration is different than your function definition header.
You have bool checkPosition(int xpos, int ypos, int player); as a declaration but: bool checkPosition(int xpos, ypos) as a header.


Try changing to:
bool checkPosition(int xpos, int ypos);
&
bool checkPosition(int xpos, int ypos) respectively. You can add the int player to both as needed.

Secondly plboard is not defined. If it is a class definition post it so we can help.

edit: oh sorry my bad. thats p1board.
Last edited on
1
2
3
4
5
6
7
8
9
10
    if (p1board[xpos][ypos] = '*') & (p1board[xpos][ypos] = 's')
        {
            return true;
        } // close if block.
    return false;
    else if (p2board[xpos][ypos] = '*') & (p2board[xpos][ypos] = 's')
        {
            return true;
        } // close else if block.
    return false;


wtf, can you do me a favor and re-read that? the problem is obvious. :)
Ok I fixed the bool declaration. I don't think I want it as a class definition. elvenspike, if you see the problem can you share that with me so I can fix it. As I said I am pretty stuck. Thanks
Topic archived. No new replies allowed.