Author |
Message |
<dski.. #1 / 8
|
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.
-- "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.*-*-*.com/
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
Ing. Franz Glase #2 / 8
|
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
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
Bob Scho #3 / 8
|
I need help fixing this CASE Select code, please help
Quote:
> 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
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
Mike #4 / 8
|
I need help fixing this CASE Select code, please help
Quote:
> > 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
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
Osmo Ronkan #5 / 8
|
I need help fixing this CASE Select code, please help
Quote:
>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{*filter*}? Btw why not use numbers for both rows and columns (internally that is). Osmo
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
Osmo Ronkan #6 / 8
|
I need help fixing this CASE Select code, please help
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
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
the maveri #7 / 8
|
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.
... Insults are effective only where emotion is present.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
Frank Peel #8 / 8
|
I need help fixing this CASE Select code, please help
Quote:
>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.
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
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|