Pirate ship game question

Ok guys so i have a project to do for my c++ class and its supposed to be about two ships (one the player, the other one the computer) and they are on a 10x10 plane created by an array. I did the plane, now how do i get the ships on to their starting positions on the plane? I am really frustrated with this, please help!
This is what i have:
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
#include <cstdlib>
#include <iostream>
#include <iomanip>


using namespace std;

const int ROW = 10;
const int COL = 10;

void printBoard (int[][COL]);

int main(int argc, char *argv[])
{
    int gameboard[ROW][COL];
    
    printBoard(gameboard);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void printBoard (int [] [COL])
{
    for (int rows = 0; rows < ROW; rows++)
    {
        for (int collumns = 0; collumns < COL; collumns++)
        {
            cout << " *";
        }
        cout << endl; 
    }
}

I know im supposed to represent them as different characters so "P" would be for the players ship, and "C" is for the computers ship.
Hi Ive change your printBoard code slightly mine reads like this. Ive left drawing the Computer ship upto you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void printBoard (int [] [COL])
{
    for (int rows = 0; rows < ROW; rows++)
    {
        for (int collumns = 0; collumns < COL; collumns++)
        {
			if ((player / 10) == rows && (player % 10) == collumns)
			{
				cout << " P";
			}
			//else if statement here to check computer
			else 
			{
				cout << " *";
			}
        }
        cout << endl; 
    }
}


Ive also added two global variables
1
2
int player;
int computer;


and in main before printboard is called i set the player variable to a number between 0-99

I assume you will be drawing the positions randomly and you will need to use rand() & srand()
for that if you need further help just ask.

Hope this helped
Shredded

Ok shredded, you are the most helpful person today, thank you so much :D I was getting depressed because i was so stumped on this.

Anyways, this is what i have from the help you gave me, i made the computer to come out as well, i having a small problem though, there seems to be an outlying asterisk that prints out to the right of the board and its random it seems, what could that be?

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
#include <cstdlib>
#include <iostream>
#include <iomanip>


using namespace std;

const int ROW = 10;
const int COL = 10;
int player;
int computer;

void printBoard (int[][COL]);

int main(int argc, char *argv[])
{
    int gameboard[ROW][COL];
    
    srand(time(0));
    player = rand()%99+1;
    computer = rand()%99+1;
    printBoard(gameboard);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void printBoard (int [] [COL])
{
    for (int rows = 0; rows < ROW; rows++)
    {
        for (int collumns = 0; collumns < COL; collumns++)
        {
			if ((player / 10) == rows && (player % 10) == collumns)
			{
				cout << " P";
			}
			if ((computer / 10) == rows && (computer % 10) == collumns)
			{
				cout << " C";
			}
			//else if statement here to check computer
			else 
			{
				cout << " *";
			}
        }
        cout << endl; 
    }
}

    
Also, im supposed to make the player move his ship (up, left, right, NOT down), and when they move, the screen is supposed to refresh and re-draw he board with the moved ships. The computer's ship is supposed to move randomly though, the player doesn't control it.
hi again,

first the error from the extra asterisk is caused by the second <if> in printBoard just change it to an <else if> ( you might want to test if both players are on the same square as well.)

Ive also modified main function for keyboard input, youll see that i have only included the up key <w> and one case for the computer random movement.

You should be able to work the rest out.

Shredded

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
int main(int argc, char *argv[])
{
    int gameboard[ROW][COL];

	int	chrIn = 0x00;
	int	rndIn = 0x00;

	srand(time(0));
	player = rand() % 100;
	computer = rand() % 100;

    printBoard(gameboard);
	chrIn = _getch();

    while (chrIn != 'x')	//for exit
	{
		if (chrIn == 'w')	// if 'w' = UP 
		{
			if (player > 9)
			{
				player -= 10;
			}
		}

		rndIn = rand() % 3;

		switch (rndIn)
		{
		case	0:		// case UP

			if (computer > 9)
			{
				computer -= 10;
			}
			break;

		}

		cout << endl;		//line feed 

		printBoard(gameboard);
		chrIn = _getch();
	}

	system("PAUSE");
    return EXIT_SUCCESS;
}

Topic archived. No new replies allowed.