Batnum

 The game starts with an imaginary pile of objects, coins for example. You and your opponent (the computer) alternately remove objects from the pile. You specify in advance the minimum and maximum number of objects that can be taken on each turn. You also specify in advance how winning is defined: 1. To take the last object or 2. To avoid taking the last object. You may also determine whether you or the computer goes first. The strategy of this game is based on modulo arithmetic. If the maximum number of objects a player may remove in a turn is M, then to gain a winning position a player at the end of his turn must leave a stack of 1 modulo (M+1) coins. If you don't understand this, play the game 23 Matches first, then BATNUM, and have fun! BATNUM is a generalized version of a great number of manual remove-the-object games. The original computer version was written by one of the two originators of the BASIC language, John Kemeny of Dartmouth College. Code Listing (Small Basic File: batnum.sb): ``TextWindow.CursorLeft = 33 TextWindow.WriteLine("BATNUM") TextWindow.CursorLeft = 15 TextWindow.WriteLine("BIBLEBYTE BOOKS, MAPLE VALLEY, WASHINGTON") TextWindow.WriteLine("") TextWindow.WriteLine("") TextWindow.WriteLine("") TextWindow.WriteLine("THIS PROGRAM IS A 'BATTLE OF NUMBERS' GAME, WHERE THE") TextWindow.WriteLine("COMPUTER IS YOUR OPPONENT.") TextWindow.WriteLine("") TextWindow.WriteLine("THE GAME STARTS WITH AN ASSUMED PILE OF OBJECTS. YOU") TextWindow.WriteLine("AND YOUR OPPONENT ALTERNATELY REMOVE OBJECTS FROM THE PILE.") TextWindow.WriteLine("WINNING IS DEFINED IN ADVANCE AS TAKING THE LAST OBJECT OR") TextWindow.WriteLine("NOT. YOU CAN ALSO SPECIFY SOME OTHER BEGINNING CONDITIONS.") TextWindow.WriteLine("DON'T USE ZERO, HOWEVER, IN PLAYING THE GAME.") TextWindow.WriteLine("ENTER A NEGATIVE NUMBER FOR NEW PILE SIZE TO STOP PLAYING.") TextWindow.WriteLine("") Goto LN330 LN220: For I=1 To 10 TextWindow.WriteLine("") EndFor LN330: TextWindow.Write("ENTER PILE SIZE ") N = TextWindow.ReadNumber() If N>=1 Then Goto LN370 EndIf Goto LN330 LN370: If N<>Math.Floor(N) Then Goto LN220 EndIf If N<1 Then Goto LN220 EndIf LN390: TextWindow.Write("ENTER WIN OPTION - 1 TO TAKE LAST, 2 TO AVOID LAST ") M = TextWindow.ReadNumber() If M=1 Then Goto LN430 EndIf If M<>2 Then Goto LN390 EndIf LN430: TextWindow.Write("ENTER MIN ") A = TextWindow.ReadNumber() TextWindow.Write("ENTER MAX ") B = TextWindow.ReadNumber() If A>B Then Goto LN430 EndIf If A<1 Then Goto LN430 EndIf If A<>Math.Floor(A) Then Goto LN430 EndIf If B<>Math.Floor(B) Then Goto LN430 EndIf LN490: TextWindow.Write("ENTER START OPTION - 1 COMPUTER FIRST, 2 YOU FIRST ") S = TextWindow.ReadNumber() TextWindow.WriteLine("") TextWindow.WriteLine("") If S=1 Then Goto LN530 EndIf If S<>2 Then Goto LN490 EndIf LN530: C=A+B If S=2 Then Goto LN570 EndIf LN550: Sub600() If W=1 Then Goto LN220 EndIf LN570: Sub810() If W=1 Then Goto LN220 EndIf Goto LN550 Sub Sub600 Q=N If M=1 Then Goto LN630 EndIf Q=Q-1 LN630: If M=1 Then Goto LN680 EndIf If N>A Then Goto LN720 EndIf W=1 TextWindow.WriteLine("COMPUTER TAKES "+N+" AND LOSES.") goto LeaveSub600 LN680: If N>B Then Goto LN720 EndIf W=1 TextWindow.WriteLine("COMPUTER TAKES "+N+" AND WINS.") goto LeaveSub600 LN720: P=Q-C*Math.Floor(Q/C) If P>=A Then Goto LN750 EndIf P=A LN750: If P<=B Then Goto LN770 EndIf P=B LN770: N=N-P TextWindow.WriteLine("COMPUTER TAKES "+P+" AND LEAVES "+N) W=0 LeaveSub600: EndSub Sub Sub810 TextWindow.WriteLine("") TextWindow.Write("YOUR MOVE ") LN820: P = TextWindow.ReadNumber() If P<>0 Then Goto LN870 EndIf TextWindow.WriteLine("I TOLD YOU NOT TO USE ZERO! COMPUTER WINS BY FORFEIT.") W=1 Goto LeaveSub810 LN870: If P<>Math.Floor(P) Then Goto LN920 EndIf If P>=A Then Goto LN910 EndIf If P=N Then Goto LN960 EndIf Goto LN920 LN910: If P<=B Then Goto LN940 EndIf LN920: TextWindow.Write("ILLEGAL MOVE, REENTER IT ") Goto LN820 LN940: N=N-P If N<>0 Then Goto LN1030 EndIf LN960: If M=1 Then Goto LN1000 EndIf TextWindow.WriteLine("TOUGH LUCK, YOU LOSE.") W=1 Goto LeaveSub810 LN1000: TextWindow.WriteLine("CONGRATULATIONS, YOU WIN.") W=1 Goto LeaveSub810 LN1030: If N>=0 Then Goto LN1060 EndIf N=N+P Goto LN920 LN1060: W=0 LeaveSub810: EndSub`` This chapter is adapted from the book Basic Computer Games Small Basic Edition published by Kidware Software. To purchase this book in its entirety, please see the Computer Science For Kids web site.

Sample Run:

Next Chapter > >

Excerpt © Copyright 2010-2013 By Kidware Software LLC All Rights Reserved. Computer Science For Kids, the Computer Science For Kids logo, and related trade dress are trademarks or registered trademarks of Kidware Software LLC. Philip Conrod & Lou Tylee have co-authored dozens of books and tutorials for beginning Microsoft Basic, Small Basic, Visual Basic, and Visual C# developers of all ages for over 25 years.