Any Browser Campaign

Matrix Game for HP-41CX
deutsche Version

Game theory is a fascinating field and one topic are the so-called two person zero-sum games. A game is called a zero-sum game, if the gains of one player is the loss of the other one and vice versa. If the game is played sufficiently - infinity times - often, loss and gains of both players will become zero. The stimulus now is, that the game isn't played infinity but only finite times. In this case there is a winner and a looser.

The playing field here is a matrix, whose elements are covered with different positive and negative numbers, which are generated with a random number generator. Now the player looks for a row, which is "advantageous" to him, and the HP-41CX calculates a column, respectively. If the so defined element is positive the player wins this round. In case it is negative the HP-41CX is the winner. If the element equals zero, the game ends drawn.

In the simple method used by me the game can have a "value" different from zero. Now, if you would play this game ad infinitum, one of the players will win, and from this fact the "value" of the game is determined. Ideally no one would win - this would be a "zero"-sum game.

An Example For Better Understanding :

Let the following matrix be given :

c1
c2
r1
1
0
r2
1
-1

In this case row 1 would be advantageous to the player, because one time he wins ([r1, c1] = 1) and in the other the game would end in a draw ([r1, c2] = 0). If he chooses row 2 he would win if his opponent chooses column 1 ([r2, c1] = 1), but he looses if the other one chooses column 2 ([r2, c2] = -1).

From the point of view of the other player column 2 is the right one, for the game ends drawn ([r1, c2] = 0) or he wins ([r2, c2] = -1). Column 1 is not acceptable for him, because he looses in both cases.

In the above example for the first player it's advantageous to choose row 1, and for the other one column 2 is favourable. If both players make their choices concerning these considerations the game will end in a draw ([r1, c2] = 0). This matrix is more advantageous to the first player, for he wins in two cases, looses in one and in one other the game ends drawn - correspondingly the other player is in disadvantage.

The above matrix is a very simple one. It's of more interest and more challenging, if the matrix becomes larger in size and the number's values increase. Then it's no longer easy to decide which row or column is the most advantageous or which chooses the other player !

Interestingly, there exists an optimal strategy for solving this problem (and the program masters it, of course ;-) ) ! Noteworthy is, that a choice is possible, which on first sight looks "unfavourable". But, of course, one has to take into account the possible opponent's choices and the arising consequences ! ;-)

I don't want to go into detail here concerning the theory of two person zero-sum games. (In case I should have much time I will write some lines. ;-) ) There is plenty of literature available, for example G. Owen, Game Theory, Springer Verlag.

Course of Game :

Starting the program the first matrix is generated. In the display appears "1. MATRIX" and then "1. ROW:". Now the elements of this row are displayed separated by commas These numbers must be written on a sheet of paper by the player, because the HP-41CX's display cannot contain the whole matrix but only one row per time. ;-) If the matrix is fully constructed "READY" is displayed.

Now the HP-41CX calculates the column which is the most favourable for it and the player makes his choice of a row, respectively. After a new BEEP the row must be entered. Then it is displayed, which column was computed by the HP-41CX and afterwards the element determined by row and column. If the value of this element is positive the player wins this round. In case the value is zero "DRAW" is displayed. The HP-41CX wins, if the value is negative. In the last case the game ends at once and the number of tries and the degree of difficulty are displayed.

In the first or second case the player is asked, whether he wants to continue. In the display appears "CONTIN ? Y/N". Now "Y" must be pressed to continue the game, otherwise "N". If the last round ended in a draw, a new matrix of same level is generated. If the player had won the last round, the difficulty is increased. There are 21 levels (degrees of difficulty) for the matrices.

ERWSP must be the first program in extended memory (in CAT 4 ERWSP must be first displayed). There is no need for jumping by hand to any addresses !

After ERWSP is copied into extended memory the main program GAME9 can be loaded. Before starting the program SIZE 122 must be executed. Additionally, GAME9 creates the data file "DATA" with size 28 registers in extended memory, which is removed at program termination.

Program Listing ERWSP :

  01 LBL "ERWSP"
  02  RCL 04
  03  2
  04  XY?   not equal
  05  GTO 01
  06  ISG 04
  07  ""          (NOP)
  08  GTO "HP"
  09 LBL 01
  10  X<>Y
  11  3
  12  XY?   not equal
  13  GTO 01
  14  RCL 07
  15  2
  16  X=Y?
  17  GTO 02
  18  ISG 04
  19  ""          (NOP)
  20  GTO "HP"
  21 LBL 02
  22  ISG 07
  23  ""          (NOP)
  24  5
  25  STO 06
  26  GTO "HP"
  27 LBL 01
  28  X<>Y
  29  4
  30  XY?   not equal
  31  GTO 01
  32  RCL 07
  33  3
  34  X=Y?
  35  GTO 02
  36  ISG 04
  37  ""          (NOP)
  38  5
  39  STO 06
  40  3
  41  STO 07
  42  GTO "HP"
  43 LBL 02
  44  ISG 07
  45  ""          (NOP)
  46  7
  47  STO 06
  48  GTO "HP"
  49 LBL 01
  50  X<>Y
  51  5
  52  XY?   not equal
  53  GTO 01
  54  RCL 07
  55  3
  56  X=Y?
  57  GTO 02
  58  ISG 04
  59  ""          (NOP)
  60  3
  61  STO 07
  62  5
  63  STO 06
  64  GTO "HP"
  65 LBL 02
  66  ISG 07
  67  ""          (NOP)
  68  7
  69  STO 06
  70  GTO "HP"
  71 LBL 01
  72  X<>Y
  73  6
  74  XY?   not equal
  75  GTO 11
  76  RCL 07
  77  3
  78  X=Y?
  79  GTO 01
  80  X<>Y
  81  4
  82  X=Y?
  83  GTO 02
  84  ISG 04
  85  ""          (NOP)
  86  3
  87  STO 07
  88  5
  89  STO 06
  90  GTO "HP"
  91 LBL 02
  92  ISG 07
  93  ""          (NOP)
  94  9
  95  STO 06
  96  GTO "HP"
  97 LBL 01
  98  ISG 07
  99  ""          (NOP)
100  7
101  STO 06
102  GTO "HP"
103 LBL 11
104  X<>Y
105  7
106  XY?   not equal
107  GTO 11
108  RCL 07
109  3
110  X=Y?
111  GTO 01
112  X<>Y
113  4
114  X=Y?
115  GTO 02
116  ISG 04
117  ""          (NOP)
118  4
119  STO 07
120  7
121  STO 06
122  GTO "HP"
123 LBL 02
124  ISG 07
125  ""          (NOP)
126  9
127  STO 06
128  GTO "HP"
129 LBL 01
130  ISG 07
131  ""          (NOP)
132  7
133  STO 06
134  GTO "HP"
135 LBL 11
136  X<>Y
137  8
138  XY?   not equal
139  GTO 01
140  RCL 07
141  4
142  X=Y?
143  GTO 02
144  ISG 04
145  ""          (NOP)
146  4
147  STO 07
148  7
149  STO 06
150  GTO "HP"
151 LBL 02
152  ISG 07
153  ""          (NOP)
154  9
155  STO 06
156  GTO "HP"
157 LBL 01
158  X<>Y
159  9
160  XY?   not equal
161  GTO 11
162  RCL 07
163  4
164  X=Y?
165  GTO 01
166  X<>Y
167  5
168  X=Y?
169  GTO 02
170  ISG 04
171  ""          (NOP)
172  4
173  STO 07
174  7
175  STO 06
176  GTO "HP"
177 LBL 02
178  ISG 07
179  ""          (NOP)
180  11
181  STO 06
182  GTO "HP"
183 LBL 01
184  ISG 07
185  ""          (NOP)
186  9
187  STO 06
188  GTO "HP"
189 LBL 11
190  RCL 07
191  4
192  X=Y?
193  GTO 01
194  X<>Y
195  5
196  X=Y?
197  GTO 02
198  TONE 0
199  TONE 3
200  TONE 5
201  TONE 7
202  TONE 9
203  "GRATULATION"
204  AVIEW
205  PSE
206  PSE
207  GTO "ENDE"
208 LBL 02
209  ISG 07
210  ""          (NOP)
211  11
212  STO 06
213  GTO "HP"
214 LBL 01
215  ISG 07
216  ""          (NOP)
217  9
218  STO 06
219  GTO "HP"
220  END

Programmlisting SPIEL9 (Hauptprogramm) :

  01 LBL "SPIEL9"
  02  "MATRIX GAME"
  03  AVIEW
  04  CLRG
  05  CF 05
  06  "DATA"
  07  28
  08  CRFLD
  09   E
  10  SEEKPT
  11  11
  12  SAVEX
  13  X<>Y
  14  STO 05
  15  ISG X
  16  ""          (NOP)
  17  STO 04
  18  STO 07
  19  ISG X
  20  ""          (NOP)
  21  STO 06
  22  FIX 0
  23  CF 29
  24  26
  25  SEEKPT
  26  TIME
  27   E2
  28  /
  29  SAVEX
  30 LBL "HP"
  31  2,025
  32  STO 10
  33 LBL 14
  34  RCL 10
  35  SEEKPT
  36  ,
  37  SAVEX
  38  ISG 10
  39  GTO 14
  40  27
  41  SEEKPT
  42  GETX
  43  ISG X
  44  ""          (NOP)
  45  X<>Y
  46  SEEKPT
  47  X<>Y
  48  SAVEX
  49  SIGN
  50  STO 08
  51  RCL 05
  52  CLA
  53  " "          (1 space)
  54  ARCL X
  55  ". MATRIX :"
  56  AVIEW
  57  RCL 04
  58  X^2
  59  LASTX
  60  +
  61   E1
  62  +
  63   E3
  64  /
  65  11
  66  +
  67  STO 09
  68  RCL 04
  69   E3
  70  /
  71  ISG X
  72  STO 10
  73 LBL 03
  74  RCL 08
  75  " "         (1 space)
  76  ARCL X
  77  ". ROW :"
  78  AVIEW
  79  CLA
  80 LBL 00
  81  XEQ 16
  82  RCL 06
  83  MOD
  84  ISG X
  85  ""          (NOP)
  86  STO IND 09
  87  RCL 07
  88  -
  89  ARCL X
  90  LASTX
  91  +
  92  DSE X
  93  ""          (NOP)
  94  RCL 08
  95  15
  96  +
  97  ,
  98  SEEKPT
  99  X<>Y
100  SAVEX
101  RCL Z
102  RCL 10
103  INT
104  XEQ 18
105  ISG 10
106  GTO 01
107  GTO 02
108 LBL 01
109  ","
110  ISG 09
111  GTO 00
112 LBL 02
113  TONE 5
114  AVIEW
115  ISG 08
116  ""          (NOP)
117  ISG 09
118  SIGN
119  STO IND 09
120  RCL 04
121  ST- 10
122  STOP
123  ISG 09
124  GTO 03
125  BEEP
126  "   READY"
127  AVIEW
128  SIGN
129  STO 00
130  1,003
131  CLRGX
132 LBL 30
133  ISG 02
134 LBL 31
135  ISG 03
136  ""          (NOP)
137  RCL 04
138  ISG X
139  ""          (NOP)
140  RCL 03
141  X>Y?
142  GTO 40
143  RCL 04
144  RCL 02
145  X>Y?
146  GTO 40
147  RCL 03
148  XEQ 37
149  X<> Z
150  XEQ 37
151   E3
152  /
153  +
154  RCL 04
155  ISG X
156  ""          (NOP)
157   E5
158  /
159  +
160  XEQ 39
161  RCL IND M
162  ST* 00
163  X=0?
164  GTO 31
165  1/X
166  RCL M
167  INT
168  XEQ 36
169  RDN
170  STO 01
171  XEQ 33
172  RCL 01
173  ST- 01
174  RCL 02
175  X=Y?
176  GTO 32
177  XEQ 35
178  RCL 00
179  CHS
180  STO 00
181 LBL 32
182  ISG 01
183  ""          (NOP)
184  RCL 04
185  RCL 01
186  X>Y?
187  GTO 30
188  RCL 02
189  X=Y?
190  GTO 32
191  RCL 01
192  RCL 03
193  XEQ 37
194  RDN
195  RCL IND T
196  CHS
197  XEQ 34
198  GTO 32
199 LBL 33
200  XEQ 38
201  X<>Y
202 LBL 04
203  ST* IND Y
204  ISG Y
205  GTO 04
206  RTN
207 LBL 34
208  STO M
209  RDN
210  XEQ 38
211  X<>Y
212  XEQ 38
213  RCL M
214  SIGN
215 LBL 05
216  RDN
217  RCL IND Y
218  LAST X
219  *
220  ST+ IND Y
221  ISG Y
222  ""          (NOP)
223  ISG Z
224  GTO 05
225  RTN
226 LBL 35
227  XEQ 38
228  X<>Y
229  XEQ 38
230  INT
231   E3
232  /
233  X<>Y
234  INT
235  +
236  RCL 04
237  ISG X
238  ""          (NOP)
239   E6
240  /
241  +
242  REGSWAP
243  RTN
244 LBL 38
245  RCL 04
246  ISG X
247  ""          (NOP)
248  *
249  11
250  +
251  RCL X
252  RCL 04
253  ISG X
254  ""          (NOP)
255  ST- Z
256  SIGN
257  -
258   E3
259  /
260  +
261  RTN
262  LBL 36
263  11
264  -
265  RCL 04
266  ISG X
267  ""          (NOP)
268  X<>Y
269  STO O       (character "O" - not zero)
270  X<>Y
271  MOD
272  ST- O
273  LASTX
274  ST/ O
275  CLX
276  X<> O
277  y<>Y
278  ISG Y
279  ""          (NOP)
280  ISG X
281  ""          (NOP)
282  RTN
283 LBL 37
284  X<> 04
285  ISG X
286  ""          (NOP)
287  ST- 04
288  *
289  ST+ 04
290  X<> L
291  DSE X
292  X<> 04
293   E1
294  +
295  RTN
296 LBL 39
297  STO M
298  STO N
299  STO O
300  RCL IND X
301  ABS
302  ENTER^
303  ENTER^
304  RDN
305 LBL 07
306  CLX
307  RCL IND Z
308  ABS
309  X>Y?
310  GTO 01
311  R^
312  X>Y?
313  GTO 02
314  RDN
315 LBL 06
316  ISG Z
317  GTO 07
318  X<>Y
319  R^
320  RTN
321 LBL 01
322  X<>Y
323  CLX
324  RCL Z
325  STO M
326  GTO 06
327 LBL 02
328  CLX
329  RCL T
330  STO N
331  X<>Y
332  RDN
333  GTO 06
334 LBL 40
335  CLD
336  RCL 04
337  RCL X
338   E3
339  /
340  ISG X
341  STO 10
342  ISG Y
343  ""          (NOP)
344  X<>Y
345  STO 09
346 LBL 08
347  RCL 09
348  RCL 10
349  INT
350  *
351   E1
352  +
353  RCL IND X
354  RCL 10
355  2
356  +
357  SEEKPT
358  X<>Y
359  SAVEX
360  LASTX
361  XEQ 19
362  +
363  2
364  SEEKPT
365  X<>Y
366  SAVEX
367  ISG 10
368  GTO 08
369  RCL 04
370  ST- 10
371 LBL 09
372  RCL 10
373  2
374  +
375  XEQ 19
376  LASTX
377  XEQ 19
378  X0?          not equal
379  1/X
380  *
381  RCL 10
382  2
383  +
384  SEEKPT
385  X<>Y
386  SAVEX
387  ISG 10
388  GTO 09
389  RCL 04
390  ST- 10
391  3
392  SEEKPT
393  GETX
394  GETX
395  GETX
396  GETX
397  XEQ 15
398  STO M
399  RDN
400  STO N
401  RDN
402  4
403  RCL 04
404  X<=Y?
405  GTO 02
406  -
407  CHS
408   E3
409  /
410  ISG X
411  STO 10
412  RDN
413  RCL N
414  RCL M
415 LBL 10
416  GETX
417  XEQ 15
418  ISG 10
419  GTO 10
420  GTO 01
421 LBL 02
422  R^
423  R^
424  RCL N
425  RCL M
426 LBL 01
427   E
428  STO 09
429  RDN
430  13
431  SEEKPT
432  RDN
433  SAVEX
434  RDN
435  SAVEX
436  RDN
437  SAVEX
438  XEQ 16
439  9
440  MOD
441  2
442  X<=Y?
443  GTO 01
444  15
445  SEEKPT
446  GETX
447  X<=0?
448  GTO 11
449  GTO 02
450 LBL 01
451  X<>Y
452  5
453  X<=Y?
454  GTO 01
455 LBL 11
456  14
457  SEEKPT
458  GETX
459  X>0?
460  GTO 02
461 LBL 01
462  13
463  SEEKPT
464  GETX
465 LBL 02
466  3
467  SEEKPT
468  X<>Y
469 LBL 12
470  GETX
471  X=Y?
472  GTO 13
473  RDN
474  ISG 09
475  ""           (NOP)
476  GTO 12
477 LBL 13
478  BEEP
479  "  ROW ?"
480  PROMPT
481  STO 10
482  FC?C 22
483  GTO 13
484  X<=0?
485  GTO 13
486  RCL 04
487  X<Y?
488  GTO 13
489  X<>Y
490  15
491  +
492  ,
493  SEEKPT
494  X<>Y
495  SAVEX
496  " HP-41CX:"
497  AVIEW
498  PSE
499  " COLUMN "
500  ARCL 09
501  AVIEW
502  PSE
503  RCL 09
504  XEQ 17
505  ISG X
506  ""           (NOP)
507  RCL 07
508  -
509  " ["
510  ARCL 10
511  ","
512  ARCL 09
513  "] = "
514  ARCL X
515  AVIEW
516  PSE
517  PSE
518  X<0?
519  GTO 20
520  BEEP
521  X=0?
522  GTO 01
523  " CONGRATULATE"
524  GTO 02
525 LBL 01
526  "   DRAW"
527  SF 05
528 LBL 02
529  AVIEW
530  PSE
531  AON
532  "CONTIN ? Y/N"
533  PROMPT
534  FS? 49
535  GTO 21
536  78
537  AOFF
538  ATOX
539  X=Y?
540  GTO "ENDE"
541  FS?C 05
542  GTO "HP"
543  ISG 05
544  ""           (NOP)
545  " "          (BE)
546  ASTO b
547 LBL 16
548  26
549  SEEKPT
550  GETX
551  R-D
552  FRC
553  R-D
554  FRC
555  FS? 06
556  E^X
557  FRC
558  26
559  SEEKPT
560  X<>Y
561  SAVEX
562   E6
563  *
564  INT
565  RTN
566 LBL 17
567  DSE X
568  ""           (NOP)
569   E
570  XEQ 19
571  X<>y
572  Y^X
573  ,
574  XEQ 19
575  XEQ 19
576  X<>Y
577  ST/ Y
578  X<>Y
579  INT
580   E
581  XEQ 19
582  MOD
583  RTN
584 LBL 18
585  XEQ 17
586  X<>Y
587  ST* Z
588  *
589  ,
590  XEQ 19
591  XEQ 19
592  X<>Y
593  -
594  +
595  ,
596  XEQ 19
597  SEEKPT
598  X<>Y
599  SAVEX
600  RTN
601 LBL 19
602  SEEKPT
603  CLX
604  GETX
605  RTN
606 LBL 21
607  BEEP
608  "  BATTERY"
609  SF 11
610  OFF
611  AVIEW
612  STOP
613 LBL 15
614  X<Y?
615  X<>Y
616  R^
617  X<Y?
618  X<>Y
619  R^
620  X<Y?
621  X<>Y
622  RDN
623  X<Y?
624  X<>Y
625  RDN
626  X<Y?
627  X<>Y
628  R^
629  X<Y?
630  X<>Y
631  R^
632  RTN
633 LBL 20
634  TONE 8
635  TONE 7
636  TONE 5
637  TONE 3
638  TONE 2
639  TONE 0
640  TONE 10
641  TONE 1
642  "  SORRY,"
643  PSE
644  AVIEW
645  PSE
646 LBL "ENDE"
647  "END OF GAME"
648  AVIEW
649  27
650  SEEKPT
651  GETX
652  CLA
653  ARCL X
654   E
655  X=Y?
656  GTO 01
657  " TRIES"
658  GTO 02
659 LBL 01
660  " TRY"
661 LBL 02
662  RCL 05
663  PSE
664  AVIEW
665  " "          (1 space)
666  ARCL X
667  ". MATRIX"
668  PSE
669  AVIEW
670  FIX 4
671  SF 29
672  "DATA"
673  PURFL
674  CF 06
675  CLA
676  CLRG
677  CLST
678  END


Back to HP-41CX Selected (Game) Programs

Back to Natural Sciences and Mathematical Games

Back to the starting page

Kai Schröder, 6.6.2001