Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Glitch on my game

  1. #1

    Glitch on my game

    Hello, i'm a beginner in the pascal language, knowing only the basics. I don't know any other language. I made this text based game some days ago and i was working on my code until i got a error to it... You can get the code here: http://pastebin.com/A5eQa7Hv . The problem is that the 'Continue or Menu?" phrase appears where it isnt supposed to be... i tried to check all the begin's and end's if they are at they'r place and i couldn't find any error... I just don't know why "Continue or Menu?" appears randomly and why it won't accept the input (small c or small m) due my key2 := upcase(key2) . If anyone doesnt mind compiling it and checking the error i'll be very thankful . Thank You!

    Ps: Don't mind the game for now, it's not finished, i'll fix the other "unfinished" parts of it but for now i need to get this fixed.

  2. #2
    Fixed the indents, it was really difficult to read. Don't know yet what the problem place is.
    Code:
    program project1;
     
    uses crt;
     
    var
      inc, rnd1 ,rnd2,jbc,sec, pris ,stats, bank, turn : integer;
      key,key1,key2,key3:char;
     
    procedure etc;
    begin
      writeln('Daily income: ',inc,' cash.');
      writeln('Bank: ',bank);
      writeln('--------------------------------------');
    end;
     
    procedure etc1;
    begin
      writeln('Starting day number ',turn,'.');
      writeln('Bank: ',bank);
      writeln('Prisoners: ',pris);
      writeln('Daily income: ',inc);
      writeln('Security level: ',sec);
    end;
     
    begin
      repeat
        sec := 0;
        jbc := 7;
        turn := 0;
        bank := 0;
        stats := 0;
        pris := 2;
        inc := 150;
        writeln('Hello and welcome to the JailBreak game made by Sami.');
        readln;
        writeln('Instructions: ');
        writeln('-Feed your prisoners day by day (if possible).');
        writeln('-If the unsatisfied prisoners count gets over 3 your');
        writeln('daily income will start to decrease.');
        writeln('-You can buy a new prisoner from Menu(M) or increase');
        writeln('the security system, thus decreasing canche of jailbreak');
        writeln('If you get -300 cash you will lose the game.');
        writeln('The goal of the game is to have 5 prisoners in less days.');
        readln;
        writeln('Ready to play?[press <ENTER> to start the first day.]');
        readln;
        {-Starting the routine-}
        begin
          repeat
            randomize;
            rnd1 := random(3);
            rnd2 := random(jbc);
    
            if (rnd1 = 1) then begin
              clrscr;
              turn := turn + 1;
              etc1;
              repeat
                write(pris,' prisoners are hungry. Feed them? (Y/N): ');
                readln(key);
                key := upcase(key);
              until (key = 'Y') or (key = 'N');
    
              if (key = 'N') then begin
                stats := stats - 2;
                writeln('--------------------------------------');
                writeln('The prisoners are not satisfied for today!');
                writeln('Unsatisfied prisoners count: ',stats);
              end;
    
              if (key = 'Y') then begin
                stats := stats + 2;
                writeln('--------------------------------------');
                writeln('Good job! the prisoners are fullfiled for today!');
                bank := bank - 150;
                writeln('-150 cash');
                writeln('Satisfied prisoners count: ',stats);
                bank := bank + inc;
                etc;
              end;
            end;
    
            if (rnd1 = 2) then begin
              turn := turn + 1;
              clrscr;
              etc1;
              repeat
                write(pris-1,' prisoner is hungry. Feed him? (Y/N): ');
                readln(key);
                key := upcase(key);
              until (key = 'Y') or (key = 'N');
    
              if (key = 'N') then begin
                stats := stats - 1;
                writeln('--------------------------------------');
                writeln('The prisoner is not satisfied for today!');
                writeln('Unsatisfied prisoners count: ',stats);
              end;
    
              if (key = 'Y') then begin
                stats := stats + 1;
                writeln('--------------------------------------');
                writeln('Good job! the prisoners are fullfiled for today!');
                bank := bank - 100;
                writeln('-100 cash');
                writeln('Satisfied prisoners count: ',stats);
                bank := bank + inc;
                etc;
              end;
            end;
    
            if (rnd1 = 3) then begin
              turn := turn + 1;
              clrscr;
              etc1;
              writeln('--------------------------------------');
              write('No prisoner is hungry today, you are lucky!');
              bank := bank + inc;
              etc;
            end;
            {-Ending the routine-}
    
            {--------------------}
    
            {-Deciding either -inc or staying the same-}
            if (stats < -3) then begin
              writeln('The prisoners are very unsatisfied , income - 25');
              inc := inc - 25;
            end;
    
            if (stats > -3) then begin
              inc := 150;
            end;
            {ending -inc routine}
    
            {-----------------------}
    
            {starting final question before next day}
            begin
              repeat
                key2 := ' ';
                key3 := ' ';
                writeln('Continue(C) or Menu(M)? : ');
                readln(key2);
                key2 := upcase(key2);
              until (key2 = 'C') or (key2 = 'M');
    
              {choice to continue or menu}
    
              if (key2 = 'M') then begin
                repeat
                  writeln('You entered the Menu, what you wish to do?');
                  writeln('--------------------------');
                  writeln('(B)uy new prisoner (-300 cash);');
                  writeln('(I)ncrease security (- 200 cash);');
                  writeln('(C)ontinue ( = cash);');
                  readln(key3);
                  key3 := upcase(key3);
                until (key3 = 'B') or (key3 = 'I') or (key3 = 'C');
              end;
            end;
            {ending menu choice}
    
            {starting custom choice}
            if (key3 = 'B') then begin
              writeln('You have bought a new prisoner!');
              writeln('-300 cash');
              writeln('Income : + 50');
              inc := inc + 50;
              bank := bank - 300;
              readln;
            end;
    
            if (key3 = 'I') then begin
              writeln('Security system upgraded, canche of escape decreased');
              writeln('-200 cash');
              bank := bank - 200;
              jbc := jbc + 2;
              sec := sec + 1;
              readln;
            end;
            {ending custom choice}
            {end day}
    
            {chance of escape at night}
            if (rnd2 = 1) then begin
              writeln('Bad Luck!One of your prisoner escaped!');
              pris := pris - 1;
              inc := inc - 50;
              readln;
            end;
            {end canche}
    
          until (pris = 5) or (bank < -300) or (pris = 0);
    
          if pris = 0 then begin
            writeln('You have no more prisoners, sorry, you lost the game!');
          end;
          if pris = 5 then begin
            writeln('Congratulations! You won the game in ',turn,' days with 5 prisoners!');
          end;
          if bank < -300 then
            writeln('Sorry, you lost the game (< -300 cash)');
    
        end;
    
        repeat
          writeln('Play again? (Y/N): ');
          readln(key1);
          key1 := upcase(key1);
        until (key = 'Y') or (key = 'N');
      until (key = 'N');
    end.
    And.. yes, it compiles with Lazarus

    And small sidenote that you shouldn't name variable "inc". There is a function inc() in system unit that is used to add number (default 1) to integer variable.
    Last edited by User137; 07-04-2012 at 12:34 AM.

  3. #3
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,108
    Blog Entries
    25
    Yeah, I'd just like to add that it's always best practice to avoid reusing ANY keywords or common language functions or you could end up having some crazy logic issues and errors in your programs. I try to think of nice alternatives myself.

    For example lets say you have a record that stores what "type" of object they are, well what do you call that variable to hold that value? Type would be terrible and would most likely cause an error preventing it from compiling. Instead I would try using "kind" (real life example!) as it does not conflict with the keyword type used to define different variable types.

    Oh and feel free to use the code button to post your code right in your post. It should work as long as it's not too long (long as in reallllllly long) User137's post is a good example of posting code of a decent size length.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  4. #4
    PGDCE Developer Carver413's Avatar
    Join Date
    Jun 2010
    Location
    Spokane,WA,Usa
    Posts
    206
    Quote Originally Posted by WILL View Post
    Oh and feel free to use the code button to post your code right in your post.
    What code button ? I alway's have to type all the bb codes in manually on this forum

  5. #5
    Thanks User137 and Will for your time , and i knew there was the function inc() but forgot bout it D:

  6. #6
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,108
    Blog Entries
    25
    Quote Originally Posted by Carver413 View Post
    What code button ? I alway's have to type all the bb codes in manually on this forum
    Ah then you never followed the directions for existing members who were migrated to the current vBulletin version of the site.

    Click here and see how you can turn on the WYSIWYG Editor for posting here on PGD! You are interested in what is under "Miscellaneous Options".

    Quote Originally Posted by smexhy View Post
    Thanks User137 and Will for your time , and i knew there was the function inc() but forgot bout it D:
    Not a problem. Starting out can seem a little daunting, but if you stick with it and don't always take the easy route you'll get all the ins and outs eventually. Taking a Computer Science class in your school will help greatly! It should cover most of the basic concepts of programming and should allow you to write code for just about any solution that can be thought up, save for the really advanced stuff.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  7. #7
    After changing Inc in your code to Income the game seems to work for me. I belive there is a bug in your game thou. Everytime I increased my security one of the prisoners escaped. I tried tracking this bug but had no luck. The reason for this is that your code is badly organized. Or atleast it sems to me likeway.
    So here are some recomendations for beter code organization.
    1. When creatung procedures, functions etc. try to use selfexplanatory names and not etc and etc1.
    2. Try to avoid writing same code in multiple parts of your program. You can do this by moving parts of code wich repeat several times into procedures. This wil make your code more readable and wil save you some time for not needint to type same code multiple types.
    3. When using if conditional clauses you can check for multiple parameters if needed. Below is part of your code where you are checking the value for rnd1 and if it is either 1 or 2 you then cal virtualy the same code.
    Code:
    if (rnd1 = 1) then
    begin
        clrscr;
        turn := turn + 1;
        etc1;
        repeat
            write(pris,
              ' prisoners are hungry. Feed them? (Y/N): ');
            readln(key);
            key := upcase(key);
        until (key = 'Y') or (key = 'N');    if (key = 'N') then
        begin
            stats := stats - 2;
            writeln('--------------------------------------');
            writeln('The prisoners are not satisfied for today!');
            writeln('Unsatisfied prisoners count: ', stats);
        end;
        if (key = 'Y') then
        begin
            stats := stats + 2;
            writeln('--------------------------------------');
            writeln('Good job! the prisoners are fullfiled for today!');
            bank := bank - 150;
            writeln('-150 cash');
            writeln('Satisfied prisoners count: ', stats);
            bank := bank + income;
            etc;
        end;
    end;
    if (rnd1 = 2) then
    begin
        turn := turn + 1;
        clrscr;
        etc1;
        repeat
            write(pris - 1,
              ' prisoner is hungry. Feed him? (Y/N): ');
            readln(key);
            key := upcase(key);
        until (key = 'Y') or (key = 'N');
        if (key = 'N') then
        begin
            stats := stats - 1;
            writeln('--------------------------------------');
            writeln('The prisoner is not satisfied for today!');
            writeln('Unsatisfied prisoners count: ', stats);
        end;
        if (key = 'Y') then
        begin
            stats := stats + 1;
            writeln('--------------------------------------');
            writeln('Good job! the prisoners are fullfiled for today!');
            bank := bank - 100;
            writeln('-100 cash');
            writeln('Satisfied prisoners count: ', stats);
            bank := bank + income;
            etc;
        end;
    end;
    This could be fixed by using one if clause and checking for two parameters like this:
    Code:
    if (rnd1 = 1) or (rnd1 = 2) then
    begin
        clrscr;
        turn := turn + 1;
        etc1;
        repeat
            write(pris,
              ' prisoners are hungry. Feed them? (Y/N): ');
            readln(key);
            key := upcase(key);
        until (key = 'Y') or (key = 'N');
        if (key = 'N') then
        begin
            stats := stats - 2;
            writeln('--------------------------------------');
            writeln('The prisoners are not satisfied for today!');
            writeln('Unsatisfied prisoners count: ', stats);
        end;
        if (key = 'Y') then
        begin
            stats := stats + 2;
            writeln('--------------------------------------');
            writeln('Good job! the prisoners are fullfiled for today!');
            bank := bank - 150;
            writeln('-150 cash');
            writeln('Satisfied prisoners count: ', stats);
            bank := bank + income;
            etc;
        end;
    end;
    EDIT:
    @Will
    The code button is only available in Advanced mode for posting reply and not for quick reply mode. Could this be changed?

    @smexhy
    You can acces advanced post mode first by clicking reply inder the post and then on Go Advanced button below the text box.
    Last edited by SilverWarior; 07-04-2012 at 08:32 AM.

  8. #8
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,108
    Blog Entries
    25
    Quote Originally Posted by SilverWarior View Post
    The code button is only available in Advanced mode for posting reply and not for quick reply mode. Could this be changed?
    Sadly no, but then again, I think that was the point of the difference between quick reply and advanced. at least all you need to do is square bracket the word "code".

    It may be possible to set yuorself to always Go Advanced. I can't be sure unless I look it up myself though.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  9. #9
    After searching a bit I found a thread that say it is posible to add aditional buttons to quick reply. So since this is a programer forum I think having CODE button would be useful.
    The lik to a thread: https://www.vbulletin.com/forum/show...ighlight=quick

  10. #10
    Thanks Silver and everyone for your time! luckily i fixed all the bugs by myself .
    I have a question about programming in general:
    So i have this readln(key) .
    and it := 'F' for example.
    and if i say
    if (key = 'F') and (variable > 0) then begin
    -syntax-
    end;
    it wont work like this... it will just jump to the other task even if i tell it
    else if (variable > 0) then begin
    writeln('You don have the enough variable');
    how is this a fix?
    If you don't understand me ill just post the code and you guys will see about what im talking ;\
    Last edited by smexhy; 08-04-2012 at 09:34 AM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Comodo SSL