// Hunt the Wumpus // https://rosettacode.org/wiki/Hunt_the_Wumpus# _w = 640 // window size width and height _h = 400 _window = 1 local fn BuildWindow CGRect r = fn cgrectmake(0,0,_w,_h) window _Window, @"Hunt the Wumpus", r, NSWindowStyleMaskTitled + NSWindowStyleMaskMiniaturizable windowcenter(_Window) WindowSetBackgroundColor(_Window,fn ColorBlack) WindowPrintViewSetTextInset( _window, 20) text ,14,fn colorWhite end fn fn BuildWindow //////////// Main Program ////////////// "Start" cls:restore data 7,13,19,12,18,20,16,17,19,11,14,18,13,15,18,9,14,16,1,15,17,10,16,20,6,11,19,8,12,17 data 4,9,13,2,10,15,1,5,11,4,6,20,5,7,12,3,6,8,3,7,10,2,4,5,1,3,9,2,8,14 data 1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1 uint32 i, j,tunnel(21,4),lost(7,4),targ,wump,player,bat1, bat2, pit1, pit2, d6, epi player = int(rnd(20)) uint32 arrows = 5 CFStringRef choice for i = 1 to 20 //set up rooms for j = 1 to 3 read tunnel(i,j) next j next i for i = 1 to 6 //set up list of permuatations of 1-2-3 for j = 1 to 3 read lost(i,j) next j next i //place wumpus, bats, and pits do wump = int(rnd(20)) until wump <> player do pit1 = int(rnd(20)) until pit1 <> player do pit2 = int(rnd(20)) until pit2 <> player && pit2 <> pit1 do bat1 = int(rnd(20)) until bat1 <> player && bat1 <> pit1 && bat1 <> pit2 do bat2 = int(rnd(20)) until bat2 <> player && bat2 <> pit1 && bat2 <> pit2 && bat2 <> bat1 do if player = wump text ,,fn colorRed print "You have been eaten by the Wumpus!" Print: WindowPrintViewScrollToBottom( _window ) goto "defeat" end if if player = pit1 || player = pit2 text ,,fn colorRed print "Aaaaaaaaaaa! You have fallen into a bottomless pit." Print: WindowPrintViewScrollToBottom( _window ) goto "defeat" end if if player = bat1 || player = bat2 text ,,fn colorOrange print "A bat has carried you into another empty room." text ,,fn colorWhite Print: WindowPrintViewScrollToBottom( _window ) do player = (rnd(20)) until player <> wump && player <> pit1 && player <> pit2 && player <> bat1 && player <> bat2 end if print text ,,fn colorGreen print "You are in room number" + str$(player) + ". There are tunnels to rooms"¬ + str$(tunnel(player,1)) + "," str$(tunnel(player,2)) + " and" +str$(tunnel(player,3)) if (tunnel(player,1)) + (tunnel(player,2)) + (tunnel(player,3)) = 0 then arrows = 5 : goto "Start" text ,,fn colorYellow print "You have" + str$(arrows) + " arrows left." text ,,fn colorWhite Print: WindowPrintViewScrollToBottom( _window ) d6 = int(rnd(6)) for i = 1 to 3 epi = tunnel(player,lost(d6,i)) if epi = wump text ,,fn colorRed print "You smell something terrible nearby." text ,,fn colorWhite Print: WindowPrintViewScrollToBottom( _window ) end if if epi = bat1 || epi = bat2 text ,,fn colorRed print "You hear a rustling." text ,,fn colorWhite Print: WindowPrintViewScrollToBottom( _window ) end if if epi = pit1 || epi = pit2 print "You feel a cold wind blowing from a nearby cavern." Print: WindowPrintViewScrollToBottom( _window ) end if next i "choices" print print "What would you like to do?" Print: WindowPrintViewScrollToBottom( _window ) choice = input %(_w/2 - 260 , _h + 20),@"Type A to shoot an arrow, or a number to move to another room. ( or Q to quit )" select case left(choice,1) case @"q", @"Q" , @"x" , @"X" end case @"a", @"A" CFStringRef targString targString = input %(_w/2 - 140 , _h + 20), @"Which room would you like to shoot into? " targ = fn StringIntegerValue(targString) print "You shot an arrow into room " + str$(targ) if targ = player text ,,fn colorRed print "You shot yourself. Why would you want to do such a thing?" Print: WindowPrintViewScrollToBottom( _window ) goto "defeat" end if text ,,fn colorGreen if targ = wump then goto "victory" if targ = tunnel(player,1) || targ = tunnel(player,2) || targ = tunnel(player,3) text ,,fn colorYellow print "The Wumpus awakes!" text ,,fn colorWhite Print: WindowPrintViewScrollToBottom( _window ) if rnd(100) <= 75 print "He moves to a nearby cavern." Print: WindowPrintViewScrollToBottom( _window ) short wumpRnd wumpRnd = Int(Rnd(3)) wump = Tunnel(wump,wumpRnd) else print "He goes back to sleep." Print: WindowPrintViewScrollToBottom( _window ) end if else text ,,fn colorRed print "You can't shoot that room from here." text ,,fn colorWhite Print: WindowPrintViewScrollToBottom( _window ) goto "choices" end if arrows -= 1 case @"0", @"1" ,@"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9" targ = fn StringIntegerValue(choice) if targ = player then print "You are already there." if targ = tunnel(player,1) || targ = tunnel(player,2) || targ = tunnel(player,3) print using "You walk to room ##"; targ player = targ else text ,,fn colorOrange print "You can't get there from here." text ,,fn colorWhite Print: WindowPrintViewScrollToBottom( _window ) end if case else text ,,fn colorRed print "You are making no sense." text ,,fn colorWhite Print: WindowPrintViewScrollToBottom( _window ) end select until arrows = 0 text ,,fn colorRed print "You have run out of arrows!" Print: WindowPrintViewScrollToBottom( _window ) "defeat" text ,,fn colorRed print "You lose! Better luck next time." Print: WindowPrintViewScrollToBottom( _window ) goto "End" "victory" text ,20,fn colorGreen print "You have slain the Wumpus!" print "You won!" Print: WindowPrintViewScrollToBottom( _window ) "End" text ,14,fn colorWhite choice = input %(_w/2 -140 , _h + 20),@"Hit Return for another. ( or Q to quit )" select case left(choice,1) case @"q", @"Q" , @"x" , @"X" end case else arrows = 5: goto "Start" end select handleevents