All i get for ouput is 1's and 0's -Arrays

So I'm writing code for a virtual battleship game. The ships need to be placed randomly and can not overlap. The ships are placed by their "bow" and then are incremented or decremented in a random direction and do not go out of bounds and are represented by the char 'O'. I wrote a function to check if the chars that represent the ships in the arrays are where the program is trying to place them with a bool check. If check is false the program should keep trying to place until true is returned. When I encapsulated my place ship functions into do while loops to accomplish this my program would compile and not execute. When i only encapsulate one function I get an output of 1's and 0's. I don't know if the problem is in my loop or my check function. Both are listed below. Thanks.
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

 void ship::placeAircraftCarrier(board & boardA)
{
	
	int roll1=0;
	char bow='O';
	length=5;
do{	
	front=(rand()%10);
	roll1=(rand()%2);
	if(roll1==0)
	{
		orientation=0;
		x=(rand()%10);
		boardA.boardA[front][x]=bow;
		check(boardA);     // call check function
		
		if(front<5)
		{	
			
			for(int i=0;i<5;i++)
			boardA.boardA[front+i][x]='O';
		}
		if(front>=5)
		{
			for(int i=0;i<5;i++)
			boardA.boardA[front-i][x]='O';
		}
	}	
	else
	{
		orientation=1;
		x=(rand()%10);
		boardA.boardA[x][front]=bow;
		check(boardA);   //call check function
		
		if(front<5)
		{
			for(int i=0;i<5;i++)
			boardA.boardA[x][front+i]='O';
		}
		if(front>=5)
		{
			for(int i=0;i<5;i++)
			boardA.boardA[x][front-i]='O';
		}
	
	}
	}while(check(boardA)==false);
	
}

bool ship::check(board & boardA)
{
	bool checkFlag=true;
	for(int i=0;i<length;i++)
	{
		if(orientation==0)
		{
			if(front<5)
			{
				if(boardA.boardA[front+i][x]=='O')
				{
					checkFlag=false;
				}
			}
			if(front>=5)
			{
				if(boardA.boardA[front-i][x]=='O')
				{
					checkFlag=false;
				}
			}
		}
		if(orientation==1)
		{
			if(front<5)
			{
				if(boardA.boardA[x][front+i]=='O')
				{
					checkFlag=false;
				}
			}
			if(front>=5)
			{
				if(boardA.boardA[x][front-i]=='O')
				{
					checkFlag=false;
				}
			}
		}
	}
	return checkFlag;
}
Last edited on
Topic archived. No new replies allowed.