HP Forums
Battleship Game Program - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP-41C Software Library (/forum-11.html)
+--- Thread: Battleship Game Program (/thread-165.html)



Battleship Game Program - Namir - 12-19-2013 01:48 PM

Simple game of battleship where you and the computer have 5 ships located on a grid defined by the corners (1,1) and (100,100). The calculator will then prompt you for (X,Y) coordinates that should be in the range of (1,1) to (100,100). Each of you and the machine drop bombs at various locations in the game's grid. If the bomb is close enough to a ship, then the ship is hit. The first player that looses all 5 ships loses the game.

The program displays the coordinates of your bombing and it they are successful. Likewise, it displays where the machine strikes and the success of that strike.

This version uses flag 1 to fully automate the initialization and launch of the game. In addition, the program switches the (x,y) coordinates of a ship that is hit with the coordinates of the last operational ship. This scheme speeds up the game as ships are hit.

I have attached a ZIP file that contains the .raw file for this program so that you can easily load it and run it using any HP-41CX emulator that loads .raw files.

Memory Map

R00 = Random number
R01 = Index for X coordinates = 11.015 or 21.025
R02 = Index for Y coordinates = 16.02 or 26.03
R03 = Number of player's ships
R04 = Number of calculator's ships
R05 = prompting index for player's input
R06 = Charge drop X coordinate
R07 = Charge drop Y coordinate
R08 = radius of destruction (5 for player, 10 for calculator)
R09 = used
R10 =
R11 = X(1) player
R12 = X(2) player
...
R15 = X(5) player
R16 = Y(1) player
R17 = Y(2) player
...
R20 = Y(5) player
R21 = X(1) calculator
R22 = X(2) calculator
...
R25 = X(5) calculator
R26 = Y(1) calculator
R27 = Y(2) calculator
...
R30 = Y(5) calculator

Flags

F00 = Player entered no data, so program chooses random bomb coordinates. Relies on flag 22 and basically makes its effects a bit more robust.
F01 = Automatic game. Can be set or cleared by stopping program and manually changing the flag state.

Listing


Code:
1 LBL "SUB"
2 "A OR a"
3 PROMPT
4 RTN
5 LBL e        # Seed random number
6 XEQ 07
7 X^2
8 XEQ 07
9 X^2
10 +
11 1
12 X<=Y?
13 GTO e
14 X<>Y
15 LN
16 LASTX
17 /
18 -2
19 *
20 SQRT
21 RCL 00
22 *
23 75            # standard deviation
24 *
25 50            # mean
26 +
27 ABS
28 INT
29 101
30 MOD
31 X=0?
32 1
33 RTN
34 LBL E        # Random number between 1 and 100
35 RCL 00        # recall the last random number
36 PI
37 +
38 3
39 Y^X
40 FRC
41 STO 00        # update random number
42 100
43 *
44 1
45 +
46 INT        # generate uniform integer random number in the range (1, 100)
47 RTN
48 LBL A        # Manual input
49 1.005
50 STO 05
51 11.015
52 STO 01
53 16.02
54 STO 02        # Store indices to access the player's (X,Y) coordinates
55 LBL 00        # start input loop
56 FIX 0
57 "X<"
58 ARCL 05
59 "|->?"
60 PROMPT        # Prompt X<i>?
61 STO IND 01
62 "Y<"
63 ARCL 05
64 "|->?"
65 PROMPT        # Prompt Y<i>?
66 FIX 5
67 STO IND 02
68 ISG 01
69 STO X
70 ISG 02
71 STO X
72 ISG 05
73 GTO 00        # end loop
74 GTO B
75 LBL a        # Automatic selection
76 11.015
77 STO 01
78 16.02
79 STO 02
80 XEQ 01
81 LBL B        # automatic setup for calculator
82 21.025
83 STO 01
84 26.03
85 STO 02
86 XEQ 01
87 5
88 STO 03
89 STO 04
90 "C TO PLAY"
91 FS?01
92 GTO C            # Start the game automaticaly if flag 1 is set
93 BEEP
94 PROMPT
95 RTN
96 LBL 01        # start loop to store random (X,Y) coordinates
97 XEQ E
98 STO IND 01
99 XEQ E
100 STO IND 02
101 ISG 01
102 STO X
103 ISG 02
104 GTO 01        # end loop
105 RTN
106 LBL C        # Start playing the game
107 XEQ 10        # Show score
108 CF 00
109 CF 22        # clear keyboard input flag
110 "Y^X?"
111 FC?01        # is program in manual mode?
112 PROMPT
113 FS? 01        # is program in automatic mode?
114 PSE
115 FC? 22
116 SF 00        # set flag 00 because user did not make input
117 FS? 00
118 XEQ E        # get random x coordinate for bomb drop
119 STO 06
120 X<>Y
121 FS? 00
122 XEQ E        # get random y coordinate for bomb drop
123 STO 07
124 "YOU BOMBED"
125 AVIEW
126 FS? 01
127 PSE        # display message "YOU BOMBED" in automatic mode
128 FIX 0
129 "X="
130 ARCL 06
131 |-",Y="
132 ARCL 07
133 AVIEW
134 FS?C 00
135 PSE        # display player's bombing coordinates in automatic mode
136 FIX 5
137 5        # Set radius to 5 for non-automatic game
138 FS? 01        # Set radius to 7.5 for automatic game
139 7.5
140 STO 08        # Store value for the radious of destruction for player
141 21.02            # Store indices to access calculator's (X,Y) coordinates
142 RCL 04
143 1 E3
144 /
145 STO 09        @ store num subs/1000
146 +
147 STO 01
148 26.025
149 RCL 09
150 +
151 STO 02
152 LBL 02        # Scan computer's coordinates
153 RCL ID 01
154 RCL 06
155 -
156 X^2
157 RCL IND 02
158 RCL 07
159 -
160 X^2
161 +
162 SQRT
163 RCL 08
164 X<>Y
165 X<=Y?
166 GTO 03        # calculator's ship was hit
167 ISG 01
168 STO X
169 ISG 02
170 GTO 02        # end of loop
171 GTO 04        # go to calculator's turn
172 LBL 03        # Calculator was hit!
173 20
174 RCL 04
175 +
176 RCL IND X
177 STO IND 01    # copy x coordinate of the last operational sub in the one that was hit
178 25
179 RCL 04
180 +
181 RCL IND X
182 STO IND 02    # copy y coordinate of the last operational sub in the one that was hit
183 BEEP
184 "CALC HIT"
185 AVIEW
186 PSE
187 DSE 04        # decrement counter for calculator's ships
188 GTO 04
189 GTO 98        # end game, player wins
190 LBL 04        # do calculator's turn
191 10
192 STO 08        # Set radius of destruction for calculator
193 11.010
194 RCL 03
195 1E3
196 /
197 STO 09        # store number of subs /1000
198 +
199 STO 01
200 16.015
201 RCL 09
202 +
203 STO 02        # Store indices to access player's (X,Y) coordinates
204 XEQ E        # get random X location for charge drop for calculator
205 STO 06
206 XEQ E        # get random Y location for charge drop for calculator
207 STO 07
208 "CALC BOMBED"
209 AVIEW        # announce that it is the calculator's turn
210 PSE
211 "X="
212 FIX 0
213 ARCL 06
214 |-",Y="
215 ARCL 07
216 FIX 5
217 AVIEW        # display the coordinates of the bombing
218 PSE
219 LBL 05        # Start loop to check for a hit
220 RCL ID 01
221 RCL 06
222 -
223 X^2
224 RCL IND 02
225 RCL 07
226 -
227 X^2
228 +
229 SQRT
230 RCL 08
231 X<>Y
232 X<=Y?
233 GTO 06        # player's ship was hit
234 ISG 01
235 STO X
236 ISG 02
237 GTO 05        # end of loop
238 GTO C        # resume the next turn
239 LBL 06        # player was hit
240 10
241 RCL 03
242 +
243 RCL INDX X
244 STO IND 01        # copy x coordinate from last operational sub to one that was hit
245 15
246 RCL 03
247 +
248 RCL IND X 
249 STO IND 02        # copy y coordinate from last operational sub to one that was hit    
250 TONE 9
251 TONE 9
252 "YOU WERE HIT"
253 AVIEW
254 PSE
255 DSE 03        # decrement counter for player's ships
256 GTO C        # loop for next turn
257 GTO 99        # end game, calculator wins
258 LBL 99
259 BEEP
260 BEEP
261 "CALC WINS"
262 PROMPT
263 XEQ 10
264 RTN
265 LBL 98
266 BEEP
267 BEEP
268 "YOU WIN"
269 PROMPT
270 XEQ 10
271 RTN
272 LBL 10
273 FIX 0
274 "SCORE "
275 ARCL 03
276 |-:
277 ARCL 04
278 FIX 5
279 AVIEW
280 PSE
281 RTN

Usage

[A] Allows player to manually enter coordinates for his ships. When flag 1 is set, the program performs a special initialization automatically, using x(i) = 15*i and y(i) = 15*i.

[a] Randomly assigns coordinates for the player's ships.

[B] Randomly assigns coordinates for the calculator's ships.

[C] Play the sub hunt game. The calculator prompts you (in non-automatic mode) to enter the coordinates of the bombimg. If you just press R/S, the program selects random coordinates.

[e] Generate a nomerally distributed integer with a mean of 50 and standard deviation of 75. The routine generates random number in the range of (1, 100).

[E] Generate a random integer between 1 and 100.

Manually SF 01 to turn on automatic mode.
Manually CF 01 to turn ff automatic mode.


RE: Battleship Game Program - heymchr - 04-01-2016 01:30 PM

Thanks for sharing!
I noticed that the listing doesn't match what's in the raw file. Is the raw file a newer version?
Thx,
Christophe.