Recent

Author Topic: Is my compiler bugged or anything?  (Read 1451 times)

anbadaothcsts@gmail.com

  • New Member
  • *
  • Posts: 18
Is my compiler bugged or anything?
« on: March 29, 2020, 04:43:17 am »
i see absolutely nothing wrong with this code but my compiler said
Code: Pascal  [Select][+][-]
  1. main.pas(106,28) Fatal: Syntax error, ";" expected but "(" found
which is the line
Code: Pascal  [Select][+][-]
  1.      #72,'W','w': Direction(Up);
i don't understand why? Is it my compiler is bugging or anything :///
Code: Pascal  [Select][+][-]
  1. uses crt;
  2.  
  3. type CharDirection = (Up, Down, Left, Right, None);
  4.  
  5. var GoalX     : Integer      ;
  6.     GoalY     : Integer      ;
  7.     CharX     : Integer      ;
  8.     CharY     : Integer      ;
  9.     Stage     : String       ;
  10.     Key       : Char         ;
  11.     Direction : CharDirection;
  12.  
  13. procedure SetGoal;
  14.     begin
  15.       Randomize;
  16.       GoalX:=Random(80)+1;
  17.       Randomize;
  18.       GoalY:=Random(24)+2;
  19.     end;
  20.  
  21. procedure InfStage;
  22.     begin
  23.       GotoXY(1,1);
  24.       write(Stage,'     ');
  25.     end;
  26.  
  27. procedure CheckWin;
  28.     begin
  29.       if (CharX = GoalX) and (CharY = GoalY) and (Key = #13) then
  30.         begin
  31.           clrscr;
  32.           write('You won!');
  33.         end;
  34.     end;
  35.  
  36. procedure CheckChar;
  37.     begin
  38.         if not CharX in [1..WindMaxX] then
  39.             begin
  40.               if CharX < 1 then CharX := 1
  41.               else if CharX > WindMaxX then CharX := WindMaxX;
  42.               if Direction in [Left, Right] then
  43.               Direction := None;
  44.             end;
  45.         if not CharY in [2..WindMaxY] then
  46.             begin
  47.               if CharY <= 2 then CharY := 2
  48.               else if CharY > WindMaxY then CharY := WindMaxY;
  49.               if Direction in [Up, Down] then
  50.               Direction := None;
  51.             end;
  52.     InfStage;
  53.     end;
  54.  
  55. procedure CheckCoord;
  56.     begin
  57.       if (CharX in [1..WindMaxX]) and (CharY in [2..WindMaxY]) then Stage := 'Freezing! D:';
  58.       if (CharX in [GoalX-12..GoalX+12]) and (CharY in [GoalY-12..GoalY-12]) then Stage := 'So Cold :<';
  59.       if (CharX in [GoalX-8..GoalX+8]) and (CharY in [GoalY-8..GoalY+8]) then Stage := 'Cool :o';
  60.       if (CharX in [GoalX-5..GoalX+5]) and (CharY in [GoalY-5..GoalY+5]) then Stage := 'Warm :)';
  61.       if (CharX in [GoalX-3..GoalX+3]) and (CharY in [GoalY-3..GoalY+3]) then Stage := 'Hot hot :D';
  62.       if (CharX in [GoalX-1..GoalX+1]) and (CharY in [GoalY-1..GoalY+1]) then Stage := 'Warm >:D';
  63.      
  64.       InfStage;
  65.     end;
  66.  
  67. procedure WriteO;
  68.     begin
  69.       CheckChar;
  70.       CheckCoord;
  71.       write(' ');
  72.       gotoxy(CharX,CharY);
  73.       write('o');
  74.       gotoxy(CharX,CharY);
  75.       InfStage;
  76.     end;
  77.  
  78. procedure UpdatePosition;
  79.     begin
  80.       case Direction of
  81.          Up: Dec(CharY);
  82.          Down: Inc(CharY);
  83.          Left: Dec(CharX);
  84.          Right: Inc(CharX);
  85.       end;
  86.     end;
  87.    
  88. procedure SetDirection(NewDirection: CharDirection);
  89. begin
  90.   if Direction <> NewDirection then
  91.     Direction := NewDirection;
  92. end;
  93.  
  94. begin
  95.   ClrScr;
  96.   CharX:=Random(80)+1;
  97.   CharY:=Random(24)+2;
  98.   WriteO;
  99.   Direction := None;
  100.   SetGoal;
  101.   repeat
  102. CheckChar;
  103. CheckCoord;
  104.     key:=readkey;
  105.     case key of
  106.      #72,'W','w': Direction(Up);
  107.      #75,'A','a': Direction(Left) ;
  108.      #80,'S','s': Direction(Down) ;
  109.      #77,'D','d': Direction(Right);
  110.     else Direction := None;
  111.     end;
  112. UpdatePosition;
  113. InfStage;
  114. CheckWin;
  115.   until key = #27;
  116. end.
  117.  
p/s 1: yes, this is a Hot & Cold game, this is a demo tho, i will improve it later.
p/s 2: i'm using OnlineGBD for Pascal so i can use the variable "WindMaxX","WindMaxY"
here's the link btw >https://www.onlinegdb.com/online_pascal_compiler
« Last Edit: March 29, 2020, 04:50:00 am by anbadaothcsts@gmail.com »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is my compiler bugged or anything?
« Reply #1 on: March 29, 2020, 05:36:53 am »
i see absolutely nothing wrong with this code but my compiler said
Syntactically no, but semantically yes. Check what you declare Direction as (at the start of the program) and how you use it (in the offending line).

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Is my compiler bugged or anything?
« Reply #2 on: March 29, 2020, 05:49:49 am »
@anbadaothcsts

Those lines should be:
Code: Pascal  [Select][+][-]
  1.     case key of
  2.      #72,'W','w': Direction := Up;
  3.      #75,'A','a': Direction := Left;
  4.      #80,'S','s': Direction := Down;
  5.      #77,'D','d': Direction := Right;
  6.     else Direction := None;
  7.     end;

anbadaothcsts@gmail.com

  • New Member
  • *
  • Posts: 18
Re: Is my compiler bugged or anything?
« Reply #3 on: March 29, 2020, 07:29:09 am »
@anbadaothcsts

Those lines should be:
Code: Pascal  [Select][+][-]
  1.     case key of
  2.      #72,'W','w': Direction := Up;
  3.      #75,'A','a': Direction := Left;
  4.      #80,'S','s': Direction := Down;
  5.      #77,'D','d': Direction := Right;
  6.     else Direction := None;
  7.     end;
Ahh it’s all make senses now! Thank you

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Is my compiler bugged or anything?
« Reply #4 on: March 29, 2020, 01:49:15 pm »
I want to add some value to this situation …

Use UpCase(ReadKey);

That way you don't need to code the upper and lower cases for the test.

Also while in CRT mode/DOS the readkey will return a #0 if you hit a special key, when receiving one of these you should call ReadKey again to get the special key code that follows.

 so if you are inclined to use the arrow keys for example this is where they work..

 If Inner case statement works for this too.  :D
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Is my compiler bugged or anything?
« Reply #5 on: March 29, 2020, 02:05:00 pm »
Code: Pascal  [Select][+][-]
  1. Key := Upcase(ReadKey);
  2.     case key of
  3.      'W': Direction := Up;
  4.      'A': Direction := Left;
  5.      'S': Direction := Down;
  6.      'D': Direction := Right;
  7.      #0 :
  8.            Case ReadKey Of
  9.             #72: Direction := Up;
  10.             #75: Direction := Left;
  11.             #80: Direction := Down;
  12.             #77: Direction := Right;
  13.           end;
  14.     else Direction := None;
  15.     end;
  16.  
  17.  

Not tested but looks good from where I sit  :D
« Last Edit: March 29, 2020, 02:08:42 pm by jamie »
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018