Notes {labels: 'NIL, viewFont: 10241} // nil=Unfiled, or specify a folder, e.g., 'Business. remove viewFont for current Styles default //ERASE! in order to erase existing entries in this folder first picoferm.nwt -- Sloup format 26 Mar 1998 Copyright 1993-98. S. Weyer. All Rights Reserved Worldwide. picoferm.nwt and PFB.pkg are freeware. Newt is shareware. For further info: http://members.bellatlantic.net/~sweyer/newton/index.htm mailto:weyer@kagi.com (optional) see picoferm.bit for Bitmaps: PFBIcon (optional) install MiscCnst.pkg for view constants (e.g., vfFillWhite) and ButtonBounds Newt source version of Pico Fermi Bagels ---------- init func() begin // several constants :DefConst('kTitle, "Pico Fermi Bagels"); :DefConst('kNewtsCapeSym, '|NewtsCape:NewtsCape|); end ---------- PFB //:doObj('build,'PFB) //:doObj('add,'PFB) //:doObj('remove,'PFB) {viewClass: clView, // add our own status&title rather than use _proto: protoApp viewBounds: NIL, // set in viewSetupFormScript declareSelf: 'base, // so close box will work viewFormat: 328017, // vfFillWhite + vfFrameBlack + vfPen*1 + vfInset*1 + vfShadow*1 viewFlags: 4, // vApplication viewJustify: 0, title: kTitle, allowZero: true, // set by ZeroCheck answer: nil, numDigits: 3, // set by DigitsPicker win: nil, winSound: @102, // ROM_funbeep newSound: @314, // ROM_poof guessExpando: NIL, // set as a child later viewSetupFormScript: func() begin answer := win := helpView := nil; self.PFBview := self; // for :newBounds in guessExpando local ap := GetAppParams(); // configure to full screen size self.viewBounds := RelBounds( ap.AppAreaLeft,ap.AppAreaTop, ap.AppAreaWidth,ap.AppAreaHeight); inherited:?viewSetupFormScript(); end, viewQuitScript: func() begin helpView:?close(); helpView := NIL; inherited:?viewQuitScript(); end, newBounds: func(view,loffset,toffset,roffset,boffset,xgrid,ygrid) begin local vb := view.viewBounds; local wid := vb.right+roffset-vb.left-loffset; local ht := vb.bottom+boffset-vb.top-toffset; self.viewBounds := RelBounds( loffset, toffset, if xgrid then Floor(wid/xgrid)*xgrid else wid, if ygrid then Floor( ht/ygrid)*ygrid else ht); end, _package: { shortTitle: "PFB 1.4", icon: :GetPictAsBits("Bitmaps","PFBIcon"), copyright: "\u00A9\u 1993-98, S. Weyer", compressed: true, devSignature: "mysig", //?? }, } ---------- PFB.helpRotateStuff~ {helpView: NIL, helpBook: NIL, // create/set in createHelpBook,setHelpBook // [or] :helpBookTemplate(), // copy data structure from Newt at development-time // actual help pages are added later, i.e., PFB.helpBook+page1, etc. // to support Rotate for Newton OS 2.x //reorienting: NIL, ReorientToScreen: func() begin //reorienting := TRUE; :SyncView(); :RedoChildren(); //reorienting := NIL; end, } ---------- PFB.gameStuff~ {generateAnswer: func() begin local i, rdig, z0 := if allowZero then 0 else 1, z9 := 9; answer := Array (numDigits, nil); for i:=0 to numDigits-1 do begin repeat rdig := Random (z0,z9) until not SetContains(answer,rdig); answer[i] := rdig; if rdig=z0 then z0:=z0+1 // squeeze range to minimize subsequent random gen else if rdig=z9 then z9:=z9-1; end; end, newGame: func() begin PlaySound(newSound); guessExpando:?reset(); answer := win := nil; end, showAnswer: func() begin if win then PlaySound(winSound); if answerGlance exists then begin answerGlance.text := if answer then (if win then "You won!" else "Too hard?") && "The answer is" && Stringer(answer) else "Hey! You haven't even guessed yet!!"; answerGlance:open(); end; end, } ---------- PFB.evaluateGuess func(guess) // separate from main PFB app frame for smaller note/easier editing begin local i, g, result, charnum; local zeroch := Ord($0), fermi := 0, pico := 0; local z := if allowZero then 0 else 1; // if we haven't generated an answer yet, do it now. if not answer then :generateAnswer(); // destructively remove extraneous spaces, decimal point StrReplace(guess," ", "", nil); StrReplace(guess,".", "", nil); // check length of guess if StrLen(guess) <> numDigits then result := ""; // check that guess is all digits, with none duplicated local aguess := Array(numDigits,nil); for i:=0 to min(numDigits,StrLen(guess))-1 do begin g := guess[i]; charnum := Ord(g)-zeroch; // conv char to digit if (charnum < z) or (charnum > 9) then result := result & $< & g && "not" && z & "-9>" else if SetContains(aguess,charnum) then result := result & $< & g && "duplicate>" else aguess[i]:=charnum; // save for duplicate comparison // check guess against answer if answer[i]=charnum then fermi := fermi+1 else if SetContains(answer,charnum) then pico := pico+1; end; if result // return error or feedback then return result else if fermi = 0 and pico = 0 then return "Bagels"; for i:=1 to fermi do result := result & "Fermi "; for i:=1 to pico do result := result & "Pico "; if fermi = numDigits then win := true; result; end ---------- ButtonBounds func(width) // to add application buttons to protoStatus at Newt development time begin // or install MiscCnst.pkg plug-in to obtain this method local left1:=25, top:=2, spacing:=6, bottom:=15; if width > 0 then SetBounds(spacing, top, spacing + width, bottom) // other buttons else SetBounds(left1, top, left1 - width, bottom); // first button end ---------- PFB+myStatus {_proto: protoStatus, } ---------- PFB.myStatus+01info~ {_proto: protoInfoButton, viewFlags: 515, viewBounds: :ButtonBounds(-13), //RelBounds(3, -16, 13, 13), viewJustify: 6, //vjCenterH + vjCenterH DoInfoHelp: func() if helpBook then begin local ttim := GetRoot().TinyTim; // help book reader if not helpView then helpView := BuildContext( {_proto: ttim._proto, bookRef: helpBook}); ttim:Close(); // in case system help is open helpView:openManual(helpBook); end, } ---------- PFB.myStatus+02giveup~ {_proto: protoTextButton, buttonClickScript: showAnswer, //func() :showAnswer(), // copy method ref text: "Give Up", viewBounds: :ButtonBounds(44), viewJustify: 8389670, // vjParentRightH + vjParentTopV + vjSiblingRightH + oneLineOnly + vjCenterH + vjCenterV } ---------- PFB+titleObj~ {_proto: protoTitle, title: kTitle, } ---------- PFB+ZeroCheck~ {_proto: protoCheckbox, text: "Allow Zero", valueChanged: func() begin allowZero := viewValue; if answer then :newGame(); end, viewBounds: RelBounds(8,17,72,14), viewSetupFormScript: func() begin viewValue := allowZero; inherited:?viewSetupFormScript(); end, viewValue: true, // initial default } ---------- PFB+NewGameButton~ {_proto: protoTextButton, buttonClickScript: newGame, //func() :newGame(), // copy method ref text: "New Game", viewBounds: RelBounds(0,20,60,10), viewJustify: 8388630, // vjCenterH + vjCenterV + oneLineOnly + vjParentCenterH } ---------- PFB+DigitsPicker~ {_proto: protoLabelPicker, labelCommands: ["2","3","4", "5"], text: "Digits", textSetup: func() NumberStr(numDigits), viewBounds: RelBounds(-66,19,66,14), viewJustify: 8388640, // vjLeftH + oneLineOnly + vjParentRightH labelActionScript: func(cmd) // assumes single digit begin local newDigits := Ord(labelCommands[cmd][0])-Ord($0); if numDigits <> newDigits then begin guessExpando:?setStyle(numDigits := newDigits); if answer then :newGame(); end; end, } ---------- PFB.myGuessEProto {_proto: protoTextExpando, entryFlags: 11009, // vVisible+vClickable+vGesturesAllowed+vNumbersAllowed+vSingleUnit +vNoKeys(16)??? viewJustify: 8388624, // vjParentCenterH+vjParentTopV + vjLeftH+vjTopV+oneLineOnly viewSetupDoneScript: func() begin entryLine.textFlags := 2; //vNoSpaces; inherited:?viewSetupDoneScript(); end, // label: "Guess N", // path: 'guessN, } ---------- PFB+GuessExpando {_proto: protoExpandoShell, editHeight: 46, indent: 60, lineHeight: 14, //lines: [], // set in viewSetupFormScript //numLines: 16, // set in viewSetupFormScript //target: NIL, // set in viewSetupFormScript //viewBounds: RelBounds(1,35,238,270), // set via :newBounds in viewSetupFormScript viewFormat: 336, viewSetupFormScript: func() begin local i, symslot; self.target := self; :newBounds(PFBview,1,35+editHeight,-1,-27,nil,lineHeight); self.numLines := (viewbounds.bottom-viewbounds.top) div lineHeight; viewbounds.top := 35; // viewbounds.top-editHeight editWidth := viewBounds.right - viewBounds.left - 20; :setStyle(numDigits); self.lines := Array(numLines,nil); for i:=1 to numLines do begin symslot := Intern("Guess" & i); lines[i-1]:= {_proto: myGuessEProto, // defined in app label: "Guess" && i, path: symslot, // e.g., 'Guess1 }; target.(symslot) := Clone(""); end; inherited:viewSetupFormScript() end, reset: func() begin local fr; :expandLine(0); // close current, open 1st field foreach fr in lines do SetValue(target, fr.path, Clone("")); :redoChildren(); // seems to be necessary end, setStyle: func(dig) self.textStyle := if dig <= 3 or editWidth > 240 // don't shrink for landscape then 0x00003001 // userFont12 else if dig=4 then 0x00002801 // userFont10 else 0x00002401, // userFont9 flushEdits: func() nil, //inherited:?flushEdits(), // seems to be needed CloseEdit: func(view) if view then begin local val := target.(view.path); if StrFilled(val) then begin SetValue(target, view.path, val && :evaluateGuess(val)); if win then :showAnswer(); end; end, } ---------- PFB+AnswerGlance {_proto: protoGlance, text: "Welcome to" && kTitle, // initial text viewFlags: 515, // vVisible must be on to avoid later problems... viewBounds: RelBounds(0,0,140,60), viewJustify: 86, // vjParentCenterH+vjParentCenterV + vjCenterH+vjCenterV } ---------- createHelpBook //:createHelpBook() func() GetRoot().(kNewtsCapeSym):?getURL( "Notes/" & kTitle && "Help", // assumes picoferm.htm with this in Notes {pageSize: 'help}) ---------- setHelpBook //PFB:setHelpBook() func() // assumes :createHelpBook run earlier (asynchronously) begin local newtscape := GetRoot().(kNewtsCapeSym); if newtscape then begin self._proto.helpBook := newtscape.curBook; newtscape:close(); // avoid problems after save end; end ---------- xPFB.helpBook+page0 .# first help book page. this is much easier than dealing with BookMaker! .subject 1 Implement Help Books .story This shows how you can implement a help book directly in Newton. The formatting is much more limited than with Newt's Cape. In order to include these pages, change 'xPFB' to 'PFB'. ---------- xPFB.helpBook+page1 .subject 1 Describe PFB .story PFB stands for Pico, Fermi, Bagels -- a number guessing game, similar to MasterMind or Jotto. For each guess, you will be told: - Fermi: a correct digit placed correctly - Pico: a correct digit placed incorrectly - Bagels: no digits are correct ---------- xPFB.helpBook+page2 .subject 1 Understand the Code .story This version of Pico Fermi Bagels (1.4) includes the Newt source; see: picoferm.txt. You could also register for the Newt Development Environment (see "Develop") to obtain a Newton programming manual and more NewtonScript application examples. ---------- xPFB.helpBook+page3 .subject 1 Distribute .story PFB is postcardware*. PFB may be distributed freely as long as all of the files are included and unmodified. You are free to make modifications for your own use. *Dedicated to Kristina and Ellie -- send email to Vader42@aol.com and EllieJelly@aol.com, or postcards or other valuable tokens of appreciation (address in "Contact Author"). Copyright 1993-98, S. Weyer. All Rights Reserved Worldwide. ---------- xPFB.helpBook+page4 .subject 1 Use .story \uFC0B\u Allow Zero: is zero a possible digit? [New Game]: start a new game \uFC01\u Digits: select number of digits (2-5) <write>: guesses <double-tap>: for numeric keypad <close/next label>: see result [Info]: this help book [Give Up]: see answer ---------- xPFB.helpBook+page5 .subject 1 Example .story If \uFC0B\u Allow Zero (checked) \uFC01\u Digits 3 PFB's number is 029 1. 445 <4 duplicate> 2. 4567 <not 3 digits> 3. 376 Bagels {no digits correct} 4. 914 Pico {1 digit correct but in wrong place} 5. 820 Fermi Pico {2 digits correct, but 1 in wrong place} . . . 092 Fermi Pico Pico {all digits correct, 2 in wrong places} 029 Fermi Fermi Fermi {win; sound} ---------- xPFB.helpBook+page6 .subject 1 Develop .story If you are interested in object-oriented programming, developing applications in NewtonScript and saving as packages directly on your Newton, try the "Newt" development environment. Registered users receive a 80+ pp. manual (Acrobat or paper) and 200+ examples. For further info, see pfb.txt or http://members.bellatlantic.net/~sweyer/newton/index.htm ---------- xPFB.helpBook+page7 .subject 1 Contact Author .story Steve Weyer 17 Timber Knoll Drive Washington Crossing, PA 18977-1052 Internet: weyer@kagi.com America Online,Compuserve: SteveWeyer http://members.bellatlantic.net/~sweyer/newton/index.htm ---------- BYE!