Jump to content
  • Advertisement
Sign in to follow this  
Acidburn007

Hangman code

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm wondering whats the best way to implement some ASCII art. eg |- - -¬ | () | _|_ | /| ok not the best hangman picture but its a try. I thought of a 2d array of chars wondering if anyone as any better ideas?

Share this post


Link to post
Share on other sites
Advertisement
Just save it as a std::string.

Then all you have to do is std::cout<<myart; to show it.


std::ifstream fin("myart.txt");
std::string myart;
char ch;
while(fin.get(ch))
{
myart+=ch;
}
std::cout<<myart;


I haven't tested the code...but it should work;

//walle

[Edited by - walle on April 25, 2005 7:32:13 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
std::cout<<std::ifstream("myart.txt").rdbuf();


Wouldn't that create a bunch of unclsed handles as a by product(at least on some implementations)

Share this post


Link to post
Share on other sites
Hello, thanks for the information... I've managed to draw up the following code:


outfile.close();

ifstream infile;

infile.open("hangman.txt");
if (infile.fail())
{
cerr<< " error opening file"<<endl;
exit(1);
}

while(!infile.eof())
{
infile >>chars;
cout << chars;
}


however the printout doesnt print as specified in the document, new lines are not printed out therfore the entire text import, is imported on 1 line...

Thanks for the help so far mostly appricated

Share this post


Link to post
Share on other sites
Why load the ascii art from a file? Am I missing something? I'd just do it in the source:

void DrawMan(int failures){
switch(failures){
case 0:
cout << "|-----¬ "
<< "| "
<< "| "
<< "| "
<< "| "
<< "| ";
break;
case 1:
cout << "|-----¬ "
<< "| (:) "
<< "| "
<< "| "
<< "| "
<< "| ";
break;
case 2:
cout << "|-----¬ "
<< "| (:) "
<< "| | "
<< "| | "
<< "| "
<< "| ";
break;
case 3:
cout << "|-----¬ "
<< "| (:) "
<< "| _|_ "
<< "| | "
<< "| "
<< "| ";
break;
case 4:
cout << "|-----¬ "
<< "| (:) "
<< "| _|_ "
<< "| | "
<< "| / \ "
<< "| ";
break;
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by DerAnged
Quote:
Original post by smart_idiot
std::cout<<std::ifstream("myart.txt").rdbuf();


Wouldn't that create a bunch of unclsed handles as a by product(at least on some implementations)


No; the temporary ifstream will fall out of scope, and the dtor is required to close() it (or rather, to have the same effect).

Rob: Because data-driven design is a good thing :) If you put the data in a separate file, then you only have to write the printing logic once, instead of having the big switch statement and a bunch of extra quotes and <<'s and other garbage that doesn't have anything to do with ascii art. And remember, control-c is not your friend :)

Also, with data in a file, you don't have to recompile to change the art.

Also, with data in a file, if you later hire an artist to do the ASCII art for you (heh), you don't need to get him to edit your source code, or integrate his changes - he just sends you the updated text file.

Share this post


Link to post
Share on other sites
Cool. I could think of some code I think for the game. Hey I will try it and it won't work. Oh well, we learn from use don- I don't know strings yet. But thanks, I will be sure to save this code. I think I will use this, if only BASIC could do it like that, I could make a 100 question+ hangman in 1 hr 30 min in BASIC.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!