Greetings,
Relatively new to programming here, so I hope you'll go easy on me. Not sure what else to say for now, so I'll jump right into it.
Basically, I have to write a simple command window text game for my Intro to C++ class that utilizes some functions we wrote in an earlier phase. For my game, I decided to have the player try to shoot an oncoming zombie with a cannon. The player has three attempts to take the zombie down, and I made some ASCII art to visualize its approach. Players can also knock off limbs, or punch a hole in the zombie's torso without actually killing it (and just having it cut to the victory portion of the code), and so I tried to think of a clever way to display the zombie and his potential battle wounds. What I came up with is something along the lines of using a variable that records whether individual parts of the body are present or missing, and then using that variable to assign either an array with the ASCII for that part or an array with the equivalent in spaces to a separate array I used in the output statements.
Basically:
1 2 3 4 5 6 7 8 9 10
|
if( head == 1)
{
for( i = 0; i < strlen(head_intact); ++i ) // Not 100% on strlen
print_head[i] = head_intact[i];
}
else
{
for( i = 0; i < strlen(head_intact); ++i ) // Pretty sure it's right though?
print_head[i] = head_missing[i];
}
|
After I did that for all four limbs, the torso, and the head, I did something like:
1 2 3 4 5 6
|
for( i = 0; i < 4; ++i )
cout << print_leftarm[i] << print_head[i] << endl;
for( i = 5; i < 10; ++i )
cout << print_leftarm[i] << print_torso[i-5] << endl;
// The left arm (on the screen, from our point of view) is raised up, and that's
// why I print it with the head first. Just go down the entire zombie like this.
|
Where the problem is though (I believe), is how I've been doing the arrays. I was just trying to assign the strings to elements in a string (also tried char) array, but I think I'm approaching it wrong.
For example though, how I wanted to do it was something along the lines of:
1 2 3 4 5 6 7 8 9 10 11
|
string leftarm_intact[20];
leftarm_intact[0] = " #";
leftarm_intact[1] = " # ";
leftarm_intact[2] = "# ";
string leftarm_missing[20];
leftarm_missing[0] = " ";
leftarm_missing[1] = " ";
leftarm_missing[2] = " ";
//etc
|
When I used string as the type, and try to output an element it gives me the compile error:
1 2 3 4 5 6
|
1> h:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
|
a whole bunch of times, except the (653) changes to higher numbers as it goes.
When I used char as the type, and try to output an element it gives me the compile error:
1 2
|
1>[...]\source1.cpp(10) : error C2440: '=' : cannot convert from 'const char [4]' to 'char'
1> There is no context in which this conversion is possible
|
for every time I try to output an element.
After searching for solutions for an extended period of time, I think the answer may lie in storing the ASCII art in 2-dimensional arrays. The problem is I can't wrap my head around how the code should look to pull this off, especially since the characters in the ASCII art are varied and have no pattern. I'm on a windows machine using Visual Studio Express Edition. Any help would be greatly appreciated, and I'm clearly not the most experienced programmer so if you could keep explanations simple I'll be your best friend.
Oh, and if requested I can also post all of my actual code (was just trying to save space with simplified examples).
Thank you very much in advance!