Storing Life Form Details in a Class

So for an assignment, I have to create this Life Form class. The instructions are... (skip the big blob for a summed up version)

Create a "Life Form" class that contains a name, symbol (like a C or A), and (x, y) location. After, create a function in Main to displays your 20x20 char array. Adjust the code so that before writing any character, the function checks to see if it is about to write the location of the life form. If it is at the life form's function, it should write the life form's symbol instead. Finish off by making a step function that picks a direction randomly and calls the function to make that movement (the display has to be refreshed each movement). Then add a little menu with options like step (move once), auto step 10 (move 10 spots) and quit.

So, in simpler terms, we need to randomize a location in a 20x20 array with a specific character ('C' will do) and display it. The creature/character has to be in a "lifeform" class that contains a name, symbol (like a C or A), and (x, y) location (this location will be randomized). Then, code a function that picks a random direction and moves the symbol through the array, in a function called step, along with a "step 10" function that moves it 10 times.

I have already done something similar, but it's not in a class and it doesn't have the step function. This program initializes a 15x15 array with all 0's, then adds in the character in a random spot

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
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

void find(char array[][10]){ //used to find the creature
	for (int a = 0; a < 10; a++){
		for (int b = 0; b < 10; b++){
			if (array[a][b] != '0'){
				cout << "Found a " << array[a][b] << " creature in row " << a + 1 << ", column " << b + 1 << "." << endl;
			}
		}
	}
}

void initlize(char array[][10]){
	for (int a = 0; a < 10; a++){ //initilize array to all 0's
		for (int b = 0; b < 10; b++){
				array[a][b] = '0';
			}
		}

		cout << "Initilzing Array..." << endl;
		for (int a = 0; a < 10; a++){ //display the array with all 0's
			for (int b = 0; b < 10; b++){
				cout << array[a][b] << " ";
			}
			cout << endl;
		}

		system("pause");
		system("cls"); //now remove all 0's and add in a creature...
}

int main(){
	srand((unsigned) time(0)); //randomize
	char array[10][10];
	string n;
	int x, y;
	bool tf;

	do{ //using do to repeat
		initlize(array);

		cout << "Adding in a creature..." << endl;
		x = (rand() % 8) + 1; //random position in x
		y = (rand() % 8) + 1; //random position in y
		array[x][y] = 'C'; //set that position to C

		find(array); //find that position

		for (int a = 0; a < 10; a++){ //display the array
			for (int b = 0; b < 10; b++){
				cout << array[a][b] << " ";
			}
			cout << endl;
		}

		cout << "Would you like to re-run? (Y/N): ";
		cin >> n;

		if (n == "Y" || n == "y"){
			tf = true;
			system("cls");
			x = 0, y = 0;
		}else{
			tf = false;
		}
	}while(tf == true);

	system("PAUSE");
}


Go ahead and test it out to see how it works. (I know it's messy, still kinda new to C++).

The hardest part for me is to put the 'life form' into a class, so any help with that would be great! If you're confused with my long explanation, please ask!
Last edited on
Topic archived. No new replies allowed.