i have two 2day array programs with systems to detect chars on the board, what is wrong with them?

they dont detect anything or they do detect everything...in one case in the bunny game i do have success in clearing the board of all 'm' and 'f' chars...however i have failed to successfully adapt it, ive ties enough to realize there is a simple flaw in my logic but i cant find it-potentially this is easy to solve but i have been over looking 'something' for a long time




first is the bunny code...i hope it still makes sense because it is only the function that scans through the array, all there was was a bunch of ms and fs moving randomly around a board


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
int makebabies (char brdray[10] [10])
{
    char x=' ';
    char f='f',F='F',m='m',M='M';

for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
       {
           int babies=0;
           int apos;
           int bneg;
           apos=a;
           bneg=b;

           apos=apos+1;
           if (brdray [a][b] == f&& brdray [apos][b])

            {
             babies=babies+1;
             cout <<"babies!!!"<<endl;

             return babies;
            }
           else if ((brdray [a][b]= f)&& (brdray[apos][b]=m)) //here is another 
                                         //many variations i tried
             {
             babies=babies+1;
             return babies;
            }
}
}
}


secondly i will give my whole code, you can presume the rest of the code for the one above is simlar,

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>


using namespace std;

int posx=10;
int posy=10;

char z='*';
char x=' ';
char b='^';
char u='!';

char brdray [20] [20] = {  {z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z}
                                          };

void move ();
void getboard ();
char proximity (char brdray [20][20]);

int main(){
brdray [10] [10] = b;
brdray [5] [5] = u;

    while (1)
{
 system ("cls");
getboard();
move();
proximity(brdray);
system ("pause");
}



}

void getboard ()
{
    for (int a=0;a<20;a++)
    {
        for (int b=0;b<20;b++)

            cout<<brdray [a][b];
            cout<<endl;

    }
}

void move ()
{

int enter;
int a,e,i,o;


brdray [posx] [posy] = x;

a=posx;
i=posx;
e=posy;
o=posy;
a=a+1;
e=e+1;
i=i-1;
o=o-1;


cout << "enter 1,2,3, and 4 to move" <<endl;
cin >> enter;
switch (enter)
{
case 1:
if(brdray [a][posy]!='*')
posx=posx+1;
break;
case 2:
if (brdray [posx][o]!='*')
posy=posy-1;
break;
case 3:
if (brdray[posx][e]!='*')
posy=posy+1;
break;
case 4:
if (brdray [i][posy]!='*')
posx=posx-1;
break;
}
brdray [posx] [posy] = b;
}

char proximity (char brdray[20][20])
{
    int n,m,k,l;
    int j =0;
    int s =0;
    for (s;s<20;s++)
    {
        for (j;j<20;j++)
        n=s;
        m=j;
        n=n+1;
        m=m+1;
        k=s;
        l=j;
        l=l-1;
        k=k-1;
        if (brdray [s] [j]=='^')
        {
            if (brdray [n][j]=='!')
            {
                cout << "something";
            }
            if (brdray [k][j]=='"')
            {
                cout<< "something else";
            }
        }

    }
}
Last edited on
camaaahnnnn
If you'd use proper indentation and names for you variables it'd be much easier for you to understand your own code.

What is this

if (brdray [a][b] == f&& brdray [apos][b])

supposed to do?


Do you know the difference between = and ==:
else if ((brdray [a][b]= f)&& (brdray[apos][b]=m)) //here is another
?
oh i messed with it so much that i left it it is supposed to be
 
if (brdray [a][b] == f&& brdray [apos][b]==m


i rebuilt it to post...i didnt do a verygood job of tidying...should have ran it got the error message and then pasted it...basicly im trying to show my thinking...but theres problems with copying and pasting
Add following functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool IsMale(char ch)
{
  return (ch == 'm') || (ch == 'M');
}
bool IsFemale(char ch)
{
  return (ch == 'f') || (ch == 'F');
}
bool IsBunny(char ch)
{
  return IsMale(ch) || IsFemale(ch);
}
bool IsOppositeGender(char ch1, char ch2)
{
  return (IsMale(ch1) && IsFemale(ch2)) || (IsMale(ch2) && IsFemale(ch1));
}
then you can easily check for babies:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int makebabies (char brdray[10] [10])
{
           int babies=0;

for (int a = 0;a<10 - 1;a++) // - 1 is imporant
 {
     for (int b =0;b<10 - 1;b++)
       {
           if (IsBunny(brdray [a][b]))
            {
             if(IsOppositeGender(brdray [a][b], brdray [a+1][b])) // because of +1 we need -1 in the loop above!
             babies=babies+1;
             if(IsOppositeGender(brdray [a][b], brdray [a+1][b+1]))
             babies=babies+1;
             if(IsOppositeGender(brdray [a][b], brdray [a][b+1]))
             babies=babies+1;
             cout <<"babies!!!"<<endl;
            }
}
}
             return babies;
}
BABIES XD

i will implement that and see how it works, thats clever, or at least it seems so
Topic archived. No new replies allowed.