Hangman code
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?
Just save it as a std::string.
Then all you have to do is std::cout<<myart; to show it.
I haven't tested the code...but it should work;
//walle
[Edited by - walle on April 25, 2005 7:32:13 AM]
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]
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)
Hello, thanks for the information... I've managed to draw up the following code:
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
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
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; }}
Quote:Original post by DerAngedQuote: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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement