Tråd: Hello World.
View Single Post
Da var mitt bidrag ferdig. Jeg har ikke kodet noe på flere år, så det er VELDIG hacky og stygt, men det funker i alle fall.

Du skriver et input på 11 tegn, og spillet gir deg et output. Prøv å få det til å spytte ut "Hello World". Om du vil spille spillet, ikke les koden, for da blir det ikke så veldig puzzle. Men når jeg tenker meg om er kanskje koden så rotete og rar at det blir en større utfordring.

Kodet for windows i C++ (bruker system("cls"); blant annet), og den kompilede exefilen kan lastes ned her: http://rapidshare.com/files/198861740/test.exe.html

Kode:

Kode

// Declarations & Includes

#include <cstdlib> 
#include <iostream>

void Mixit();
char out[128];
char in[128] = "Mongo Spill";
int offset[128];
int win = 0;

// Main

using namespace std;
int main()
{
    srand((unsigned)time(0));
    Mixit();
    for(int i=0;i<=10;i++)
    offset[i]=(rand()%8);
    
    cout << "This game generates output depending on your input and other hidden factors.\n"
         << "Try to make it show the right output!\n\n"
         << "Probably a bit buggy, but should work fine if you only enter letters and\n"
         << "spaces. If you enter space or a number as the first character, the game exits.\n\n";
    system("pause");
    system("cls");
    
    
    while(win==0)
    {
        for(int i=0;i<=10;i++)
        offset[i] ++;
        for(int i=0;i<=10;i++)
        {
                if(in[i]<97 && i != 5 && in[i]!=' ')
                in[i] = in[i] + 32; //Sett alt til lowercase
        }
        
        if(in[0]>=97 && in[0]<=122)
        in[0] = in[0] - 32;             //Adjust first letter uppercase (in)
        
        if(in[6]>=97 && in[6]<=122)
        in[6] = in[6] - 32;             //Adjust first letter uppercase (in)
        
        
        for(int i=0;i<=10;i++)
        {
                if(in[i]==' ')
                out[i]=' ';
                else
                {
                    out[i] = in[i] + offset[i];
                    while(out[i]>122 && out[i]!=' ')
                    out[i] = out[i] - 26;
                    while(out[i]<97 && out[i]!=' ')
                    out[i] = out[i] + 26;
                }
        }
        
        if(out[0]>=97 && out[0]<=122)
        out[0] = out[0] - 32;             //Adjust first letter uppercase (out)
        
        if(out[6]>=97 && out[6]<=122)
        out[6] = out[6] - 32;             //Adjust first letter uppercase (out)
        

        
        out[5] = ' ';
                
        cout << "Input : ";
        for(int i=0;i<=10;i++)
        cout << in[i];
        cout << "\n";
        
        cout << "Output: ";
        for(int i=0;i<=10;i++)
        cout << out[i];
        cout << "\n";
        
        cout << "Next  : ";
        cin.getline(in,128);
        
        if(  out[0]=='H' 
         &&  out[1]=='e' 
         &&  out[2]=='l' 
         &&  out[3]=='l' 
         &&  out[4]=='o' 
         &&  out[5]==' ' 
         &&  out[6]=='W' 
         &&  out[7]=='o'
         &&  out[8]=='r'
         &&  out[9]=='l'
         && out[10]=='d')
         {
         win = 1;
         cout << "\n\n    Congratulations, you won!\n\n";
         }
         
         if( in[0]<60 )
         {
         win = -1;
         cout << "\n\n    Game exiting. Bye!\n\n";
         }
    }
    
    system("pause");
    return 0;
}

// Functions

void Mixit()
{      
      for(int i=0;i<=10;i++)
      {
           out[i] = (rand()%26)+97;
      }
};