HP Forums

Full Version: (85) Battleship game for HP-85
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following program is a battleship game for the HP-85. You can easily modify it to work with the HP-71B (by adding @ PAUSE after each DISP statement).

Code:
10 REM BATTLESHIP
20 OPTION BASE 1
30 CLEAR
40 DIM X(20,2),Y(20,2),N(2),R(2)
50 RANDOMIZE 997
60 DISP "HOW MANY SHIPS (5 TO 20)?";
70 INPUT M
80 REDIM X(M,2),Y(M,2)
90 N(1)=M
100 N(2)=M
110 R(1)=75 REM PC RADIUS
120 R(2)=50 REM USER'S RADIUS
130 FOR I=1 TO M
140 X(I,1)=INT(I*1000/M - 10)
150 Y(I,1)=INT(I*1000/M - 10)
160 X(I,2)=1+INT(1000*RND)
170 Y(I,2)=1+INT(1000*RND)
180 NEXT I
190 DISP "ENTER COORD FOR BOMBING"
200 INPUT X6,Y6
210 IF X6>1000 THEN GOSUB 650 @ GOTO 260
220 IF X6<0 THEN GOTO 640
230 IF X6>0 THEN GOTO 270
240 X6=1+INT(1000*RND)
250 Y6=1+INT(1000*RND)
260 DISP "DROPPED BOMB @ (";X6;",'"Y6;")"
270 K=2
280 GOSUB 460
290 DISP "PC HAS ";N(K);" SHIPS"
300 IF N(K)>0 THEN GOTO 330
310 DISP "YOU WIN!"
320 GOTO 640
330 X6=1+INT(1000*RND)
340 Y6=1+INT(1000*RND)
350 REM GIVE PC CHANGE TO CHEAR
360 IF RND>0.05 THEN GOTO 390
370 X6=X(1,1)
380 Y6=Y(1,1)
390 DISP "PC BOMBED (";X6;",";Y6;")"
400 K=1
410 GOSUB 460
420 DISP "YOU HAVE";N(K);" SHIPS"
430 IF N(K)>0 THEN GOTO 190
440 DISP "COMPUTER WINS"
450 GOTO 640
460 REM SUBROUTINE TO CALCULATE BOMBIMG
470 FOR I = 1 TO N(K)
480 D=SQR((Y(I,K)-Y6)^2+X(I,K)-X6)^2)
490 IF D>=R(K) THEN GOTO 480
500 IF K=1 THEN DISP "YOU WERE HIT @ (";X(I,K);",";Y(I,K);")"
510 IF K=2 THEN DISP "PC WAS HIT @ (";X(I,K);",";Y(I,K);")"
520 REM SWAP HIT POINT
530 IF N(K)=1 OR N(K)=M THEN GOTO 600
540 X0=X(I,N(K))
550 X(I,N(K))=X(I,K)
560 X(I,K)=X0
570 Y0=Y(I,N(K))
580 Y(I,N(K))=Y(I,K)
590 Y(I,K)=Y0
600 N(K)=N(K)-1
610 I=N(K)+1
620 NEXT I
630 RETURN
640 END
650 REM CHEAT SUB
660 DISP "PC COORDS"
670 FOR I=1 TO M
680 DISP X(I,2);",";Y(I,2)
690 NEXT I
700 X6=X(1,2)
710 Y6=Y(1,2)
720 RETURN

Notes

User input (X,Y) can be:

1) Numbers between 1 and 1000.
2) The values (0,0) makes the computer select random coordinates.
3) The values (-1,<any value>) ends the program.
4) The values (1001 or higher,<any number>) displays the coordinates of the PC's ship and bombs the ship at the first coordinates.

The X() and Y() matrix coordinates for player and PC are managed such that, the coordinate of a ship that is hit, is swapped with the last "valid" set of coordinates. The index of the last valid set of coordinates is N(K) and is decremented when a ship is hit.

The program has set in line 360 a 5% chance that the PC can cheat and bomb the coordinates of the player's first ship. By changing the value in the expression RND>0.05 you can alter the probability for the PC to cheat.

Enjoy!
Reference URL's