2d Array Determine where chars change
Nov 23, 2011 at 8:13am UTC
Ok honestly had no idea how to word the title. I need help on this quick as it's already past due and I've been struggling with it for a while now.
Basically I am generating a yard, then I place bones randomly in that yard, then a dog digs random holes in the yard, anywhere where the dog digs a hole and finds a bone an "x" is placed, otherwise an "H" is placed. I have everything done but no matter what I do I cannot figure out how to determine when a bone is found and replace the "B" with an "x"...
Here is the code so far:
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
#include <iostream>
#include <iomanip>
using namespace std;
const int R = 12;
const int C = 22;
// Prototypes
void Yard(char yard[R][C]);
void fence(char yard[R][C]);
void display(char yard[R][C]);
void bones(char yard[R][C]);
void holes(char yard[R][C]);
void detemine(char yard[R][C]);
int main()
{
char yard[R][C];
//int a = 0;
//char *target;
Yard(yard);
fence(yard);
cout << "\nMy Yard" << endl;
display(yard);
bones(yard);
cout << endl;
display(yard);
cout << endl;
holes(yard);
cout << "\n After my dog found bons" << endl;
display(yard);
/* Attempt to get return from void detemine and place "+"s and "H"s, void determine was int determine, changed to void just as a test, probably needs to be int, however does not work either way
if (a == 1)
{
*target == '+';
}
else
{
*target == 'H';
}
*/
return 0;
}
void Yard(char yard[R][C]) // Make Yard
{
for (int row = 0; row < 12; row++)
{
for (int colum = 0; colum < 22; colum++)
{
yard[row][colum] = '.' ;
}
}
}
void fence(char yard[R][C]) // Make fence around yard
{
for (int c = 0; c < 12; c++)
{
yard[c][0] = '|' ;
}
for (int c = 0; c < 12; c++)
{
yard[c][21] = '|' ;
}
for (int c = 1; c < 21; c++)
{
yard[0][c] = '-' ;
}
for (int c = 1; c < 21; c++)
{
yard[11][c] = '-' ;
}
}
void display(char yard[R][C]) // Display functions
{
for (int row = 0; row < 12; row++)
{
for (int colum = 0; colum < 22; colum++)
{
cout << yard[row][colum] << " " ;
}
cout << endl;
}
}
void bones(char yard[R][C]) // Place bones randomly
{
srand(time(0));
for (int c = 0; c < 20; c++)
{
yard[rand() % 8 + 2][rand() % 18 + 2] = 'B' ;
}
}
void holes(char yard[R][C]) // Dig holes randomly
{
srand(time(0));
for (int c = 0; c < 20; c++)
{
detemine(yard);
yard[rand() % 10 + 1][rand() % 20 + 1] = 'H' ;
}
}
void detemine(char yard[R][C]) // Determine if bone is found
{
if (yard[R][C] = 'B' )
{
yard[R][C] = '+' ;
}
else
{
yard[R][C] = 'H' ;
}
}
Nov 23, 2011 at 1:11pm UTC
On line 118: if (yard[R][C] = 'B' )
is an assignment. Change it to if (yard[R][C] == 'B' )
for comparison
Topic archived. No new replies allowed.