' *** NIM Game *** ,by prof. Daniele Mazza ( mazzad50@gmail.com )
'
'This software is presented as is, with no
'warrantees expressed or implied.
'This software is FREEWARE. You may use it
'any way you want, including in commercial
'applications. A credit to the author is
'not necessary, but can be included if you like.
dim bin(3,5),n(3)
global s1
mainwin 110 30
' 3 random number between 1 and 16 are assigned to the three Pots
' (they represent the coins contained in each pot at the beginning of the game)
for i=1 to 3
n(i) = 1 + int(rnd(1)*16)
next i
' preparing pots and coins
locate 24,1:print "**** NIM GAME ****"
print " Two simple rules :"
print " 1- You can take as many coins as you wish, but only from one container a time."
print " 2- The winner is who takes the last coin available. First move is always to You."
g1$ = "| |"
g2$ = "|($) ($) ($) ($)|"
for j = 8 to 14 step 2
for i = 20 to 72 step 24:locate i,j: print g1$;:next i
for i = 20 to 72 step 24:locate i,j+1: print g2$;:next i
next j
locate 20,16: print "|_______1_______| |_______2_______| |_______3_______|"
' game starts
for k = 1 to 15 ' max no. of moves
call ShowCoins
call PlayerMove
call ShowCoins
call ComputerMove
next k
end
'-------------------------------- sub's --------------------------------
Sub NIMaddition ' it is a binary format addition without carryover (i.e. 1+1+0=0 1+1+1=1 etc.)
for j = 1 to 3
a = n(j)
for i = 4 to 0 step -1
b = 2^i
if b <= a then
a = a - b:bin(j,i) = 1
else
bin(j,i) = 0
end if
next i
next j
s1 = 0
for i = 0 to 4
s = 0
for j = 1 to 3
s = s + bin(j,i)
next j
s1 = s1 + s mod 2
next i
End Sub
Sub ShowCoins ' displays the coins in the three containers
for j = 1 to 3
for i = 16 to n(j)+1 step -1
l1 = 15 - 2*int((i - 0.5)/4)
locate j*24 + 9 - 4*((16-i) mod 4),l1:print " ";
next i
next j
for i = 1 to 3
locate 4 + i*24,6
print n(i);" "
next i
End Sub
Sub ComputerMove
locate 14,24:print "Thinking about .......";space$(30)
locate 14,25:print space$(50)
timer 2500,[go]:wait
[go]
timer 0
for nPot = 1 to 3
k1 = n(nPot)
for nCoin = 0 to k1 - 1 ' computer tries to find out how to reach a regular position (NIM sum = 0)
n(nPot) = nCoin:Call NIMaddition
if s1 = 0 then
n(nPot) = nCoin
locate 14,25:print "Computer took ";k1 - nCoin;" coin(s) from pot ";nPot;space$(20)
s2 = 0
for i = 1 to 3:s2 = s2 + n(i):next i
if s2 = 0 then locate 20,20:print " THE COMPUTER WINS THE GAME , SORRY FOR YOU !":call ShowCoins:end
exit sub
end if
next nCoin
n(nPot) = k1
next nPot
' in case the computer cannot reach a regular position (with NIM sum = 0)
[guess]
nPot = int(rnd(1)*3) + 1
if n(nPot) = 0 then [guess] ' cannot take away, pot is empty !
nCoin = int(rnd(1)*n(nPot)) ' takes a random number of coins from 1 to n(nPot)
locate 14,25:print "Computer took ";n(nPot) - nCoin;" coin(s) from pot ";nPot;space$(20)
n(nPot) = nCoin
End Sub
Sub PlayerMove
locate 40,24:print "( 1 to 3)"
locate 14,24:input "Choose a pot please .. ";nPot
if nPot<1 or nPot>3 then notice "Wrong number of Pot !!":end
if n(nPot) = 0 then notice "Pot is empty !!":end
locate 40,25:print " ( 1 to ";n(nPot);" )"
locate 14,25:input "Number of coins to withdraw .. ";nCoin
if nCoin>n(nPot) then nCoin = n(nPot)
if nCoin = 0 then nCoin = 1
n(nPot) = n(nPot) - nCoin
s2 = 0
for i = 1 to 3:s2 = s2 + n(i):next i
if s2 = 0 then locate 20,20:print " THE HUMAN WINS THE GAME , SORRY FOR THE MACHINE !":call ShowCoins:end
End Sub