Board index » delphi » I need help fixing this CASE Select code, please help

I need help fixing this CASE Select code, please help

Here is a snipit from a program I am making. It's a battle Ship game for
school. The purpose of this part of the code is to take the Column and row
and base on the direction U/D/L/R (up down left right) the user says, and
it will make sure that the last block is also legal and on the board....
The problem is that it always runs both the proper case constant and always
runs the else. here is the code

Case Direct of
      'U': If not (chr((ord(col1) - (ShipLength - 1))) in ['A'..'J'])
             Then Legal := False
             Else Col2 := chr((ord(col1) - (ShipLength - 1)));
      'D': If not (chr((ord(col1) + (ShipLength - 1))) in ['A'..'J'])
             Then Legal := False
             Else Col2 := chr((ord(col1) + (ShipLength - 1)));
      'L': If not (row1 - (ShipLength - 1) in [1..10])
             Then Legal := False
             Else row2 := (row1 - (ShipLength - 1));
      'R': If not (row1 + (ShipLength - 1) in [1..10])
             Then Legal := False
             Else Row2 := (row1 + (ShipLength - 1));
       else
         write ('This line always appears');
       end;{Case Direct of}

thanks for your help and please be sure to email the reponce.
dski...@erols.com

--

"I don't suffer from insanity, I enjoy every minute of it."
     - Mike
"Don't take life too seriously, you won't get out alive"
http://www.erols.com/dskippy

 

Re:I need help fixing this CASE Select code, please help


I never did the case construct this way and it normally should work properly.
Probably it will help to make begin end clauses around each case:
Case direct of
  'U' : begin
             if not ... then
             else
         end;
   'B' : begin
etc.
   else
      '....'
   End;

Cheers, Franz Glaser, Austria
http://members.eunet.at/meg-glaser

Re:I need help fixing this CASE Select code, please help


Quote
dski...@erols.com wrote:
> Here is a snipit from a program I am making. It's a battle Ship game for
> school. The purpose of this part of the code is to take the Column and row
> and base on the direction U/D/L/R (up down left right) the user says, and
> it will make sure that the last block is also legal and on the board....
> The problem is that it always runs both the proper case constant and always
> runs the else. here is the code

> Case Direct of
>       'U': If not (chr((ord(col1) - (ShipLength - 1))) in ['A'..'J'])
>              Then Legal := False
>              Else Col2 := chr((ord(col1) - (ShipLength - 1)));
>       'D': If not (chr((ord(col1) + (ShipLength - 1))) in ['A'..'J'])
>              Then Legal := False
>              Else Col2 := chr((ord(col1) + (ShipLength - 1)));
>       'L': If not (row1 - (ShipLength - 1) in [1..10])
>              Then Legal := False
>              Else row2 := (row1 - (ShipLength - 1));
>       'R': If not (row1 + (ShipLength - 1) in [1..10])
>              Then Legal := False
>              Else Row2 := (row1 + (ShipLength - 1));
>        else
>          write ('This line always appears');
>        end;{Case Direct of}

     I'm not sure how Borland Pascal handles the ELSE in the CASE statement.
This is
not Standard Pascal.  However, the following should almost certainly work (and
will be
100% Standard Pascal) --

  IF direct IN ['U', 'D', 'L', 'R']
   THEN
    CASE direct OF
     'U' : { your code here }
     'D' :
     'L' :
     'R' : { last code snippet }
    END
   ELSE { this line should never appear };

Bob Schor
Pascal Enthusiast

Re:I need help fixing this CASE Select code, please help


Quote
Bob Schor wrote:

> dski...@erols.com wrote:

> > Here is a snipit from a program I am making. It's a battle Ship game for
> > school. The purpose of this part of the code is to take the Column and row
> > and base on the direction U/D/L/R (up down left right) the user says, and
> > it will make sure that the last block is also legal and on the board....
> > The problem is that it always runs both the proper case constant and always
> > runs the else. here is the code

I think you might want to put begin/end pairs around each selector, as
shown below:

 Case Direct of
   'U' :
    begin
      If not (chr((ord(col1) - (ShipLength - 1))) in ['A'..'J']) then
      Legal := False
      Else
      Col2 := chr((ord(col1) - (ShipLength - 1)));
    end;

   'D' :
    begin
      If not (chr((ord(col1) + (ShipLength - 1))) in ['A'..'J']) then
      Legal := False
      Else
      Col2 := chr((ord(col1) + (ShipLength - 1)));
    end;

[...]

Best Regards,

Mike
CEO, Analog & Digital Design
Automated Production Test
  http://www.csolve.net/~add/home.htm

Hosting Jonathan Ramsey's Pascal TCP/IP for DOS:
  http://www.csolve.net/~add/zips/tcp.htm

Re:I need help fixing this CASE Select code, please help


Quote
In article <01bd0788$c01525c0$80f1accf@dskippy>,  <dski...@erols.com> wrote:
>Here is a snipit from a program I am making. It's a battle Ship game for
>school. The purpose of this part of the code is to take the Column and row
>and base on the direction U/D/L/R (up down left right) the user says, and
>it will make sure that the last block is also legal and on the board....
>The problem is that it always runs both the proper case constant and always
>runs the else. here is the code

You really should post a minimum test program that produces the error.
I tested your code and found no such behavior. Are you sure that you do
not call the case statement twice first with a correct value and then
with a wrong one. Have you used a de{*word*81}?

Btw why not use numbers for both rows and columns (internally that is).

Osmo

Re:I need help fixing this CASE Select code, please help


In article <349355EC.878B4...@vms.cis.pitt.edu>,
Bob Schor  <bsc...@vms.cis.pitt.edu> wrote:

Quote

>     I'm not sure how Borland Pascal handles the ELSE in the CASE statement.
>This is
>not Standard Pascal.'

Then why do you post in Borland Pascal group?

Quote
>  However, the following should almost certainly work (and
>will be
>100% Standard Pascal) --

>  IF direct IN ['U', 'D', 'L', 'R']
>   THEN
>    CASE direct OF
>     'U' : { your code here }
>     'D' :
>     'L' :
>     'R' : { last code snippet }
>    END
>   ELSE { this line should never appear };

The code is basically same. If the ELSE if done in the former it is done
in this as well.

Osmo

Re:I need help fixing this CASE Select code, please help


 ds> Case Direct of 'U': If not (chr((ord(col1) - (ShipLength - 1))) in

maybe try upcase(direct) if it is a character.

the maverick - m...@microserve.net - http://xf.home.ml.org

... Insults are effective only where emotion is present.

Re:I need help fixing this CASE Select code, please help


Quote
dski...@erols.com wrote in message <01bd0788$c01525c0$80f1accf@dskippy>...
>Here is a snipit from a program I am making. It's a battle Ship game for
>school. The purpose of this part of the code is to take the Column and row
>and base on the direction U/D/L/R (up down left right) the user says, and
>it will make sure that the last block is also legal and on the board....
>The problem is that it always runs both the proper case constant and always
>runs the else. here is the code
>[snip]
>thanks for your help and please be sure to email the reponce.
>dski...@erols.com

your case statement seems OK, I compiled it in BP7 and there are no "missing
semicolons" or suchlike.

Are you calling it in any sort of loop? because if so, you may be calling it
twice, the second time with an invalid "direct".

FP

Other Threads