[C++] Console HangMan

Results 1 to 4 of 4
  1. #1
    Enthusiast dazee is offline
    MemberRank
    Mar 2008 Join Date
    33Posts

    [C++] Console HangMan

    Simple game!
    Have only 1 word!
    I build this for university..Homework...

    Source:
    Code:
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    //declaram functiile
    void game();
    void header();
    string stoupper(string s);
    void create_hint(string s,string& ss);
    void fill_hint(string l , string cw , string& wh,string& uc,int& gr);
    
    bool started = false;
    bool play = true;
    
    int main()
    {
    
        string x;
    
        while(play){
            system("cls");
            header();
            cout << "\"n\" ->new game"<<endl;
            cout << "\"x\" ->leave game"<<endl;
            cin >> x;
            if(x=="x"){
                play = false; //"x" iesim din joc
            }
            if(x=="n"){
                started = true; //"n" pornim un nou joc
            }
    
            if(started){
                game();
            }
    
        }
        return 0;
    }
    
    void game(){
        char stop;
    
        //initiam jocul
        int greseli;
        string cword;
        string hword;
        string uchars;
        string litera;
    
        litera ="";
        greseli = 0;
        uchars = "";
        cword = "hangover";
        cword = stoupper("hangover");
        create_hint(cword , hword);
    
    
        //porneste jocul
        while(started){
            //curatam consola si punem headerul
            system("cls");
            header();
    
            //now print word hint
            cout << "Hint:" << hword << endl;
    
            //now print used chars
            cout << "Used CHARS:"<< uchars << endl;
            cout << "Greseli:" << greseli<<endl;
    
            //now print yor caracter
    
            cout << "________" << endl;
            cout << "|     |"<<endl;
            cout << "|     ";
            if(greseli>0){cout<<"O";}
            cout<<endl;
            cout << "|     ";
            if(greseli>1){cout<<"|";}
            cout<<endl;
            cout << "|    ";
            if(greseli>2){cout<<"/";}
            if(greseli>3){cout<<"|";}
            if(greseli>4){cout<<"\\";}
            cout<<endl;
            cout << "|     ";
            if(greseli>3){cout<<"|";}
            cout<<endl;
            cout << "|    ";
            if(greseli>5){cout<<"/";}
            if(greseli>6){cout<<" \\";}
            cout <<endl;
            cout << "|"<<endl;
            cout << "|"<<endl;
            cout << "======================"<<endl;
            if(greseli > 6){
                cout<<"Ai pierdut!!! apasa orice tasta apoi ENTER"<<endl;
                cin>>stop;
                started = false;
            }else{
                if(hword == cword){
                    cout<<"Ai Castigat!!! apasa orice tasta apoi ENTER"<<endl;
                    cin>>stop;
                    started = false;
                }else{
                cout << "Introdu o litera:";
                cin >> litera ;
    
                litera = toupper(litera[0]);
    
                fill_hint(litera , cword , hword , uchars , greseli);
                }
            }
    
        }
    
    
    
    
    }
    
    void fill_hint(string l , string cw , string& wh,string& uc,int& gr){
        int len;
        string temps;
        int i=0;
        bool e = false;
        len = uc.length();
    
        while(i < len){
            if(l[0] == uc[i]){
                e=true;
            }
            i++;
        }
        i=0;
        if(e == false){
        uc += l;
        len = cw.length();
        temps = wh;
        while(i < len){
            if(l[0] == cw[i]){
                wh[i] = l[0];
            }
            i++;
        }
        if(temps == wh){
            gr++;
        }
        }
    }
    
    void create_hint(string s , string& ss){
        int len;
        len = s.length();
        int i=0;
        string temps;
        temps ="";
        while(i < len){
            temps += "*";
            i++;
        }
    	  ss = temps;
    }
    
    string stoupper(string s)
    	{
    	  int len;
    	  len = s.length();
    	  int i=0;
    	  string temps;
    	  temps ="";
    	  while(i < len){
            temps += toupper(s[i]);
            i++;
    	  }
    	  return temps;
    	}
    
    void header(){
        cout << "===================="<< endl << "Spanzuratoarea" << endl << "===================="<< endl;
    }


  2. #2
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [C++] Console HangMan

    Nice. When you said there was only one word I was worried that word would be embedded into the the game. As in (pseudo code),
    ' if( "w" in "word" ).' Luckily it's not. The word can easily be changed in the code. Just find where cword is declared and change the word to something else.

  3. #3
    The Gamma..? EliteGM is offline
    MemberRank
    Jul 2006 Join Date
    NandolandLocation
    4,078Posts

    Re: [C++] Console HangMan

    Nice! Damn, if this is college, I'm going to fricken ace it :P

  4. #4
    Enthusiast dazee is offline
    MemberRank
    Mar 2008 Join Date
    33Posts

    Re: [C++] Console HangMan

    Quote Originally Posted by EliteGM View Post
    Nice! Damn, if this is college, I'm going to fricken ace it :P
    this is only back code:D i had to do one graphical game 2d with sprites...but this is beging just:D
    thx for your sopinion s-p-n , actualy the game need to read words from a file ...it`s not finished:D



Advertisement