Description: in this game, the players take turns to remove 1, 2, or 3 counters from a pile of counters, the loser being the player taking the last counter.
Strategy: at the end of each go, aim to leave 4n + 1 counters, where n=0,1, 2. To implement this the computer needs to divide the current no. of counters by 4 and look at the remainder, r. (I seem to remember r being provided by the calculator, but now can't see how that can happen, so here it is calculated by the program). Before running the program, the user would need to manually set the number of decimal places to 0; to determine r during the computer's go: divide the current no. of counters by 4, multiply the result by 4 and then subtract that number from the current no. of counters:
if r=1, take 1 counter (this is arbitrary) - as the winning strategy CANNOT be followed
if r=2, take away 1 counter - winning strategy can be followed
if r=3, take away 2 counters - winning strategy can be followed
if r=0, take away 3 counters - winning strategy can be followed
Input, start: no. of counters; whose go - 0: user, 1: computer . Note: <= 0 for user, >= 1 for computer also works!
User and computer 'input', each go: number to take (1, 2, or 3)
Output, each go: no. of counters remaining, current no. of counters
Output, end: winner's ID (0: user, 1: computer)
Memory registers used:
0 - number of counters at start
1 - current no. of counters
2 - who starts (le 0: user, ge 1: computer)
3 - user's number of counters to remove
4 - temp store - user input minus number remaining (<=0)
5 - complement of whose go is it, to identify winner (0:user, 1: computer)
Program registers used: P0 to P12
Target machines: aimed at the Diehl Combitron-S and Combitronic. However, it currently uses too many program registers. Should be possible to adapt it to the Algotronic, which has more program registers available, as well as more flexible conditional jump commands.
Notes:
in the listing below, 'snd' is the V symbol & 'rcv' is the same, but inverted, no modern symbol existing for it.
if the number of counters were fixed, most of the steps in P0 could be removed. Also, the need for perhaps up to 3 program registers would go if we were to remove the validation of the number of counters taken by the user in P2 to P5. Not ideal, but I think the program would then just about fit in the space available.
Instruction Comments
P 0 INITIALIZE
<number> input number of counters at start
#
CHANGE SIGN validate > 0
CJ input <= 0
0
CHANGE SIGN end validate
snd number of counters at start
0
snd initialize current no. of counters
1
P 1 continue
<number> input who starts?
#
snd
2
1
-
*
CJ computer goes if input >= 1
7
P 2 USER'S GO. *can we do this somehow in P 1? waste of prog register****
1 set 'complement of whose go is it?' to 1 (i.e. to computer)
snd
5
<number> user inputs no. of counters to remove
#
VALIDATE - take 1, 2, or 3 counters
snd
3
+
4
-
P 3 continue
* take 4 from user's go, clear register
CJ If input > 3, invalid. *print error code?*
2
CHANGE SIGN
+
4
-
* take 4 from user's go, clear register
CJ If input <= 0, invalid. *print error code?*
2
VALIDATE - input can't exceed current no. of counters
P 4 continue
rcv
3
+
rcv
1
-
Diamond
snd store user's input minus current no. of counters (<= 0)
4
- +/- register: user's input minus current no. of counters (>= 0)
P 5 continue
1
-
*
CJ input exceeds current no. of counters *print error code?*
2
USER INPUT - END VALIDATE
rcv user's input minus current no. of counters (<= 0)
4
CJ to END current no. of counters =0.
12
CHANGE SIGN
P6 continue
# print updated current no. of counters
snd update current no. of counters
1
(
= clear x/: ?
* clear +/- ?
)
END USER'S GO
P 7 COMPUTER'S GO
0 set 'complement of whose go is it?' to 0 (i.e. to user)
snd
5
REMAINDER
rcv
1
+
:
4
=
x
P 8 continue
4
= current no. of counters/4 remainder (0,1,2,3)
END REMAINDER
-
1
-
Diamond remainder -1 = no. of counters to remove (-1, 0, 1, 2), but -1 makes no sense, so add 4!
CJ
9 If >=0
If < 0
4
+
P 9 no. of counters to remove (3, 0, 1, 2) , but 0 not allowed, so arbitrarily make it 1!
1
-
Diamond no. of counters to remove - 1 (2, -1, 0, 1)
CJ If >=0
10
1
+
P 10
1
+
* no. of counters to remove (3, 1, 1, 2)
# print no. of counters to remove - computer
+
rcv
1
-
* no. of counters to remove minus current no. of counters (<= 0)
P 11 continue.
CJ to END. current no. of counters =0.
12
CHANGE SIGN
# print updated current no. of counters
snd update current no. of counters
1
(
= clear x/: ?
* clear +/- ?
)
J END COMPUTER'S GO
2
P 12 END
# print number remaining - should be 0!
rcv complement of whose go? (0: user, 1: computer)
5
# print winner's ID
J BACK FOR NEW GAME
0