service{ const html Welcome = Welcome to the matches game!
Here are the rules:
Each player must in turn remove any number of matches from a single
pile. T he winner is the one that removes the last match.

N.B Here we could of adopted an optimal strategy wich consists of
starting with an even # of packs, let the player start. Always do
the exact move he previously did on another pack that has the same # of matches
that the pack he just manipulated
We always do an opposite move to the players last move and we will always win
We didn't do this here because we want the player to win. ; const html Illegal = Illegal move, game OVER ; const html Won = You won! ; const html Lost = You LOST! ; const html Pick = Who are aloud to pick in only one of these packs.
Pack1: <[pack1]>.
Pack2: <[pack2]>.
Pack3: <[pack3]>.
Pack4: <[pack4]>.
Pack5: <[pack5]>.

Please choose a pack with matches in it from 1 to 5:
Pack 1
Pack 2
Pack 3
Pack 4
Pack 5

Please choose the number of matches you want to get out of
the pack(smaller than the # in the pack and greater than zero: ; const html Fin = Thanks for playing this game!

Start

back home page

; int seed, total; int nextRandom(int x) { int current; if(x == 1) return(1); seed = ((25173*seed + 13849) % x) + 1; return(seed); } int Manipulate_Pack(int pack, int j) { if(j>pack || j<1){ show Illegal; exit Fin; } else pack = pack - j; if (pack == 0) total = total -1; return (pack); } void check(int x){if(x == 0) total = total - 1;} session Play() { int i, j; int pack1, pack2 , pack3, pack4, pack5; total = 5; pack1 = pack2 = pack3 = pack4 = pack5 = 10; show Welcome; seed = 7; while( pack1 !=0 || pack2 != 0 || pack3 != 0 || pack4 != 0 || pack5 != 0) { show plug Pick [pack1 = pack1, pack2 = pack2, pack3 = pack3, pack4 = pack4 , pack5 = pack5] receive[i = pack_choice, j = no_choice]; if(i==1){ pack1 = Manipulate_Pack(pack1, j);} if(i==2){ pack2 = Manipulate_Pack(pack2, j); } if(i==3){pack3 = Manipulate_Pack(pack3, j);} if(i==4){pack4 = Manipulate_Pack(pack4, j);} if(i==5){pack5 = Manipulate_Pack(pack5, j); } if(total == 0) {show Won; exit Fin;} if(total == 1){ if(pack1 != 0) pack1 = 0; if(pack2 != 0) pack2 = 0; if(pack3 != 0) pack3 = 0; if(pack4 != 0) pack4 = 0; if(pack5 != 0) pack5 = 0; } if(pack1 != 0){ pack1 = pack1 - nextRandom(pack1); check(pack1); } else if(pack2 != 0){ pack2 = pack2 - nextRandom(pack2); check(pack2); } else if(pack3 != 0){ pack3 = pack3 - nextRandom(pack3); check(pack3); } else if(pack4 != 0){ pack4 = pack4 - nextRandom(pack4); check(pack4); } else if(pack5 != 0){ pack5 = pack5 - nextRandom(pack5); check(pack5); } } show Lost; exit Fin; } }