Comment
==================================================================

The Game of NIM. CopyRight 1989 by Marc Perkel * All rights reserved.

In order to demonstrate the power of the MarxMenu language, I
have written this game in the menu language.

This is an example of a complex menu system. It shows how the
commands are used. Print this out and study it.

MarxMenu is the most powerful menu system and job control language for
DOS that exists. This is but a small sample of what it can do.

The MarxMenu language is available from:

Computer Tyme
411 North Sherman, Suite 300
Springfield Mo. 65802
(417) 866-1222 * (800) 548-5353

This game may be distributed for free as long this file is included.
Enjoy and have fun.

=========================================================
EndComment

Var
   ForeColor BackColor ClearChar Matches MatchRow MatchCol ChoiceRange
   ChoiceCount UserChoice MyChoice MyLastChoice YourTurn X YesNo
   MyResponse UserKey

Const
   MatchesPerRow = 14
   PauseTime = 100

if ColorScreen
  ForeColor = Yellow
  BackColor = Mag
  ClearChar = 32
else
  ForeColor = Black
  BackColor = Grey
  ClearChar = 177
endif

Explode On
Shadow Off
BlockBox
BoxHeaderColor ForeColor BackColor
BoxBorderColor Green Blue
BoxInsideColor Yellow Blue
InverseColor Yellow Red
BlankTime 3
BlankMessage 'The Game of NIM'

TextColor Red Red
ClearScreen ClearChar

TextColor ForeColor BackColor
ClearLine
Write '  * The MarxMenu System *'

GotoXY(1,25)
ClearLine
WriteCenter 'Copyright 1989 by Marc Perkel * All Rights Reserved'

ClockColor ForeColor BackColor
ClockPos 35 1

BoxBorderColor Yellow Mag
BoxInsideColor Mag Mag
DrawBox 10 4 61 17
BoxBorderColor LGreen Blue
BoxInsideColor Yellow Blue
Shadow On

DrawBox 35 11 11 3
WriteCenter 'The'
Loop 5
   MoveWindow -4 -1
   Wait 3
EndLoop

DrawBox 35 11 11 3
WriteCenter 'Game'
Loop 5
   MoveWindow 4 -1
   Wait 3
EndLoop

DrawBox 35 11 11 3
WriteCenter 'Of'
Loop 5
   MoveWindow -4 1
   Wait 3
EndLoop

DrawBox 35 11 11 3
WriteCenter 'Nim'
X = 0
Loop 5
   MoveWindow 4 1
   Wait 3
EndLoop

Wait 20
DrawBox 25 11 31 3
WriteCenter 'Marx Menu Demonstration'
Wait 250
RollWindow 4
EraseTopWindow
Wait 20
RollWindow 3
EraseTopWindow
Wait 20
RollWindow 2
EraseTopWindow
Wait 20
RollWindow 1
EraseTopWindow
Wait 20
EraseTopWindow
EraseTopWindow
BoxBorderColor Green Blue
BoxInsideColor Yellow Blue
Shadow Off

ChoiceRange = 4 + (Random mod 3)

Matches = 30 + (Random mod 9)
If Matches mod (ChoiceRange + 1) = 0 Then Matches = Matches + 1

ExplodeDelay = 40
Loop Matches
   MatchCol = (pred(LoopIndex) mod MatchesPerRow) * 5 + 6
   MatchRow = (pred(LoopIndex) / MatchesPerRow) * 6 + 3
   DrawBox(MatchCol,MatchRow,3,5)
EndLoop
ExplodeDelay = 15

BoxBorderColor Yellow Green
BoxInsideColor White Green
BoxHeader '  The Game of Nim  '
DrawBox 10 7 60 12
TextColor LCyan Green
ClearLine 205
TextColor White Green
Writeln
Writeln ' There are ' Matches ' matches on the screen.'
Writeln ' You will choose a number between 1 and ' ChoiceRange ' which
Writeln ' will remove that many matches from the screen.
Writeln ' Then I will remove between 1 and ' ChoiceRange ' matches.'
Writeln
Writeln ' Who ever takes the last match wins.'
Writeln
Write   ' Do you wish to play? '
YesNo = ReadKey
If UpperCase(YesNo) = 'Y'
   Write 'Yes '
   Wait PauseTime
   EraseTopWindow
   Repeat
      YourTurn = 1
      BoxBorderColor LCyan Green
      BoxInsideColor Yellow Green
      DrawBox 20 21 30 3
      Write ' There are ' Matches ' matches left.
      BoxHeader ' Your Turn '
      DrawBox 64 (21 - ChoiceRange) 15 (ChoiceRange + 3)
      ChoiceCount = 1
      TextColor LCyan Green
      ClearLine 205
      TextColor Yellow Green
      Writeln
      Repeat
         WriteCenter ChoiceCount
         If ChoiceCount < ChoiceRange Then Writeln
         ChoiceCount = succ(ChoiceCount)
      Until ChoiceCount > ChoiceRange
      UseArrows
      Repeat
         UserKey = ReadKey
         If UserKey = Char(27)
            ExitMenu
         Else
            UserChoice = Value(UserKey)
         Endif
      Until (UserChoice > 0) and (UserChoice <= ChoiceRange)
      EraseTopWindow
      EraseTopWindow
      X = 0
      While X < UserChoice
         EraseTopWindow
         X = X + 1
         Wait 20
      EndWhile
      Matches = Matches - UserChoice
      Wait PauseTime
      If Matches > 0
         YourTurn = 0
         BoxBorderColor Yellow Green
         BoxInsideColor White Green
         DrawBox 10 21 30 3
         Write ' There are ' Matches ' matches left.
         DrawBox 45 21 26 3
         MyChoice = Matches mod (ChoiceRange + 1)
         If MyChoice = 0
            If Matches = (ChoiceRange + 1)
               MyChoice = 1
            Else
               MyChoice = Random mod Pred(ChoiceRange) + 1
            Endif
         Endif
         MyResponse = 'I will take ' + Str(MyChoice) + ' match'
         If MyChoice <> 1 Then MyResponse = MyResponse + 'es'
         MyResponse = MyResponse + '.'
         WriteCenter MyResponse
         Wait PauseTime * 2
         EraseTopWindow
         EraseTopWindow
         X = 0
         While X < MyChoice
            EraseTopWindow
            X = X + 1
            Wait 20
         EndWhile
         Matches = Matches - MyChoice
         Wait PauseTime
      Endif
   Until Matches = 0
   BoxBorderColor Yellow Mag
   BoxInsideColor Mag Mag
   DrawBox 20 9 44 7
   BoxBorderColor LGreen Blue
   BoxInsideColor Yellow Blue
   DrawBox 25 11 34 3
   If YourTurn = 1
      WriteCenter 'Congratulations! You have WON!'
   Else
      WriteCenter 'Too bad! You lost!'
   Endif
   Wait 300
Else
   Write 'No'
   Wait PauseTime
EndIf

