output file "Tic-Tac-Toe" _computer = 0 _human = 1 _window = 1 begin enum output 1 _one _two _three _four _five _six _seven _eight _nine _infoField _resetBtn end enum void local fn BuildWindow int i CGRect r = fn CGRectMake( 0, 0, 278, 348 ) window _window, @"Tic-Tac-Toe", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable r = fn CGRectMake( 20, 217, 80, 82 ) for i = _one to _three button i,,,@"", r, NSButtonTypeMomentaryLight, NSBezelStyleTexturedSquare, _window r = fn CGRectOffset( r, 79, 0 ) next r = fn CGRectMake( 20, 139, 80, 82 ) for i = _four to _six button i,,,@"", r, NSButtonTypeMomentaryLight, NSBezelStyleTexturedSquare, _window r = fn CGRectOffset( r, 79, 0 ) next r = fn CGRectMake( 20, 60, 80, 82 ) for i = _seven to _nine button i,,,@"", r, NSButtonTypeMomentaryLight, NSBezelStyleTexturedSquare, _window r = fn CGRectOffset( r, 79, 0 ) next for i = _one to _nine ControlSetFontWithName( i, @"Menlo", 60.0 ) ButtonSetTitle( i, @"" ) ViewSetClickGestureRecognizer( i ) next r = fn CGRectMake( 20, 306, 240, 24 ) textfield _infoField,,,r, _window TextFieldSetEditable( _infoField, NO ) TextFieldSetSelectable( _infoField, NO ) ControlSetAlignment( _infoField, NSTextAlignmentCenter ) TextFieldSetDrawsBackground( _infoField, NO ) TextFieldSetBordered( _infoField, NO ) TextFieldSetTextColor( _infoField, fn ColorRed ) ControlSetFontWithName( _infoField, @"Arial Bold", 18.0 ) r = fn CGRectMake( 155, 13, 109, 24 ) button _resetBtn,,,@"New Game", r, NSButtonTypeMomentaryLight, NSBezelStyleRounded, _window end fn void local fn LockBoard for int i = _one to _nine ButtonSetState( i, NSControlStateValueOn ) next end fn void local fn SetBtnTitleColor( tag1 as NSinteger, tag2 as NSinteger, tag3 as NSinteger, color as ColorRef ) ButtonSetTitleColor( tag1, color ) : ButtonSetTitleColor( tag2, color ) : ButtonSetTitleColor( tag3, color ) end fn void local fn SetColorOfWinningCells( cellsToColor as NSUInteger ) select cellsToColor // Rows 1-3 case 1 : fn SetBtnTitleColor( _one, _two, _three, fn ColorRed ) case 2 : fn SetBtnTitleColor( _four, _five, _six, fn ColorRed ) case 3 : fn SetBtnTitleColor( _seven, _eight, _nine, fn ColorRed ) // Columns 1-3 case 4 : fn SetBtnTitleColor( _one, _four, _seven, fn ColorRed ) case 5 : fn SetBtnTitleColor( _two, _five, _eight, fn ColorRed ) case 6 : fn SetBtnTitleColor( _three, _six, _nine, fn ColorRed ) // Diagonal _one to _nine and _three to _seven case 7 : fn SetBtnTitleColor( _one, _five, _nine, fn ColorRed ) case 8 : fn SetBtnTitleColor( _three, _five, _seven, fn ColorRed ) end select end fn void local fn DeclareWinner( whoseTurn as NSInteger, cellsToColor as NSUinteger ) fn SetColorOfWinningCells( cellsToColor ) if ( whoseTurn == _computer ) fn LockBoard ControlSetStringValue( _infoField, @"Macintosh won!" ) else fn LockBoard ControlSetStringValue( _infoField, @"You won!" ) end if end fn void local fn NewGame ControlSetStringValue( _infoField, @"" ) for int i = _one to _nine ButtonSetState( i, NSControlStateValueOff ) ButtonSetTitle( i, @"" ) ButtonSetTitleColor( i, fn ColorText ) next end fn BOOL local fn CheckCells( tag1 as NSInteger, mark1 as CFStringRef, tag2 as NSInteger, mark2 as CFStringRef, tag3 as NSInteger, mark3 as CFStringRef ) BOOL result = NO if fn StringIsEqual( fn ButtonTitle( tag1 ), mark1 ) && fn StringIsEqual( fn ButtonTitle( tag2 ), mark2 ) && fn StringIsEqual( fn ButtonTitle( tag3 ), mark3 ) then result = YES end fn = result BOOL local fn CheckForWinner( player as long ) as BOOL CFStringRef mark BOOL result = NO if player == _human then mark = @"X" else mark = @"O" // Check rows if fn CheckCells( _one, mark, _two, mark, _three, mark ) then result = YES : fn DeclareWinner( player, 1 ) : exit fn if fn CheckCells( _four, mark, _five, mark, _six, mark ) then result = YES : fn DeclareWinner( player, 2 ) : exit fn if fn CheckCells( _seven, mark, _eight, mark, _nine, mark ) then result = YES : fn DeclareWinner( player, 3 ) : exit fn // Check colums if fn CheckCells( _one, mark, _four, mark, _seven, mark ) then result = YES : fn DeclareWinner( player, 4 ) : exit fn if fn CheckCells( _two, mark, _five, mark, _eight, mark ) then result = YES : fn DeclareWinner( player, 5 ) : exit fn if fn CheckCells( _three, mark, _six , mark, _nine, mark ) then result = YES : fn DeclareWinner( player, 6 ) : exit fn // Check _one, _five, _nine diagonal if fn CheckCells( _one, mark, _five, mark, _nine, mark ) then result = YES : fn DeclareWinner( player, 7 ) : exit fn // Check _three, _five, _seven diagonal if fn CheckCells( _three, mark, _five, mark, _seven, mark ) then result = YES : fn DeclareWinner( player, 8 ) : exit fn end fn = result void local fn SetControls( tag as NSInteger ) ButtonSetTitle( tag, @"O" ) : ButtonSetState( tag, NSControlStateValueOn ) end fn void local fn ComputerMove NSUInteger i BOOL loop = YES /* 1 2 3 4 5 6 7 8 9 */ // Check row 1 for winning moves if fn CheckCells( _one, @"", _two, @"O" , _three, @"O" ) then fn SetControls( _one ) : exit fn if fn CheckCells( _one, @"O", _two, @"" , _three, @"O" ) then fn SetControls( _two ) : exit fn if fn CheckCells( _one, @"O", _two, @"O" , _three, @"" ) then fn SetControls( _three ) : exit fn // Check row 2 for winning moves if fn CheckCells( _four, @"", _five, @"O" , _six, @"O" ) then fn SetControls( _four ) : exit fn if fn CheckCells( _four, @"O", _five, @"" , _six, @"O" ) then fn SetControls( _five ) : exit fn if fn CheckCells( _four, @"O", _five, @"O" , _six, @"" ) then fn SetControls( _six ) : exit fn // Check row 3 for winning moves if fn CheckCells( _seven, @"", _eight, @"O" , _nine, @"O" ) then fn SetControls( _seven ) : exit fn if fn CheckCells( _seven, @"O", _eight, @"" , _nine, @"O" ) then fn SetControls( _eight ) : exit fn if fn CheckCells( _seven, @"O", _eight, @"O" , _nine, @"" ) then fn SetControls( _nine ) : exit fn // Check colmun 1 for winning moves if fn CheckCells( _one, @"", _four, @"O", _seven, @"O" ) then fn SetControls( _one ) : exit fn if fn CheckCells( _one, @"O", _four, @"", _seven, @"O" ) then fn SetControls( _four ) : exit fn if fn CheckCells( _one, @"O", _four, @"O", _seven, @"" ) then fn SetControls( _seven ) : exit fn // Check colmun 2 for winning moves if fn CheckCells( _two, @"", _five, @"O", _eight, @"O" ) then fn SetControls( _two ) : exit fn if fn CheckCells( _two, @"O", _five, @"", _eight, @"O" ) then fn SetControls( _five ) : exit fn if fn CheckCells( _two, @"O", _five, @"O", _eight, @"" ) then fn SetControls( _eight ) : exit fn // Check colmun 3 for winning moves if fn CheckCells( _three, @"", _six, @"O", _nine, @"O" ) then fn SetControls( _three ) : exit fn if fn CheckCells( _three, @"O", _six, @"", _nine, @"O" ) then fn SetControls( _six ) : exit fn if fn CheckCells( _three, @"O", _six, @"O", _nine, @"" ) then fn SetControls( _nine ) : exit fn // Check _one to _nine diagonal for winning moves if fn CheckCells( _one, @"", _five, @"O", _nine, @"O" ) then fn SetControls( _one ) : exit fn if fn CheckCells( _one, @"O", _five, @"", _nine, @"O" ) then fn SetControls( _five ) : exit fn if fn CheckCells( _one, @"O", _five, @"O", _nine, @"" ) then fn SetControls( _nine ) : exit fn // Check _three to _nine diagonal for winning moves if fn CheckCells( _three, @"", _five, @"O", _seven, @"O" ) then fn SetControls( _three ) : exit fn if fn CheckCells( _three, @"O", _five, @"", _seven, @"O" ) then fn SetControls( _five ) : exit fn if fn CheckCells( _three, @"O", _five, @"O", _seven, @"" ) then fn SetControls( _seven ) : exit fn // Check row 1 for blocking moves if fn CheckCells( _one, @"", _two, @"X", _three, @"X" ) then fn SetControls( _one ) : exit fn if fn CheckCells( _one, @"X", _two, @"", _three, @"X" ) then fn SetControls( _two ) : exit fn if fn CheckCells( _one, @"X", _two, @"X", _three, @"" ) then fn SetControls( _three ) : exit fn // Check row 2 for blocking moves if fn CheckCells( _four, @"", _five, @"X", _six, @"X" ) then fn SetControls( _four ) : exit fn if fn CheckCells( _four, @"X", _five, @"", _six, @"X" ) then fn SetControls( _five ) : exit fn if fn CheckCells( _four, @"X", _five, @"X", _six, @"" ) then fn SetControls( _six ) : exit fn // Check row 3 for blocking moves if fn CheckCells( _seven, @"", _eight, @"X", _nine, @"X" ) then fn SetControls( _seven ) : exit fn if fn CheckCells( _seven, @"X" ,_eight, @"", _nine, @"X" ) then fn SetControls( _eight ) : exit fn if fn CheckCells( _seven, @"X", _eight, @"X", _nine, @"" ) then fn SetControls( _nine ) : exit fn // Check colmun 1 for blocking moves if fn CheckCells( _one, @"", _four, @"X", _seven, @"X" ) then fn SetControls( _one ) : exit fn if fn CheckCells( _one, @"X", _four, @"", _seven, @"X" ) then fn SetControls( _four ) : exit fn if fn CheckCells( _one, @"X", _four, @"X", _seven, @"" ) then fn SetControls( _seven ) : exit fn // Check colmun 2 for blocking moves if fn CheckCells( _two, @"", _five, @"X", _eight, @"X" ) then fn SetControls( _two ) : exit fn if fn CheckCells( _two, @"X", _five, @"", _eight, @"X" ) then fn SetControls( _five ) : exit fn if fn CheckCells( _two, @"X", _five, @"X", _eight, @"" ) then fn SetControls( _eight ) : exit fn // Check colmun 3 for blocking moves if fn CheckCells( _three, @"", _six, @"X", _nine, @"X" ) then fn SetControls( _three ) : exit fn if fn CheckCells( _three, @"X", _six, @"", _nine, @"X" ) then fn SetControls( _six ) : exit fn if fn CheckCells( _three, @"X", _six, @"X", _nine, @"" ) then fn SetControls( _nine ) : exit fn // Check _one, _five, _nine diagonal for blocking moves if fn CheckCells( _one, @"", _five, @"X", _nine, @"X" ) then fn SetControls( _one ) : exit fn if fn CheckCells( _one, @"X", _five, @"", _nine, @"X" ) then fn SetControls( _five ) : exit fn if fn CheckCells( _one, @"X", _five, @"X", _nine, @"" ) then fn SetControls( _nine ) : exit fn // Check _three, _five, _nine diagonal for blocking moves if fn CheckCells( _three, @"", _five, @"X", _seven, @"X" ) then fn SetControls( _three ) : exit fn if fn CheckCells( _three, @"X", _five, @"", _seven, @"X" ) then fn SetControls( _five ) : exit fn if fn CheckCells( _three, @"X" , _five, @"X", _seven, @"" ) then fn SetControls( _seven ) : exit fn // If no winning or blocking moves found, select random empty button for computer move for i = _one to _nine if ( fn StringIsEqual( fn ButtonTitle(i), @"" ) ) while ( loop = YES ) i = rnd(9) if ( fn StringIsEqual( fn ButtonTitle(i), @"" ) ) ButtonSetTitle( i, @"O" ) : ButtonSetState( i, NSControlStateValueOn ) loop = NO end if wend end if next end fn BOOL local fn CheckForTie BOOL result = YES for int i = _one to _nine if ( fn StringIsEqual( fn ButtonTitle(i), @"" ) ) result = NO : exit fn end if next end fn = result void local fn Play( tag as long ) ButtonPerformClick( tag ) ButtonSetTitle( tag, @"X" ) : ButtonSetState( tag, NSControlStateValueOn ) if fn CheckForTie then fn LockBoard : ControlSetStringValue( _infoField, @"Tie Game!" ) if ( fn CheckForWinner( _human ) == NO ) then fn ComputerMove if ( fn CheckForWinner( _computer ) == NO ) if fn CheckForTie then fn LockBoard : ControlSetStringValue( _infoField, @"Tie Game!" ) else exit fn end if end fn void local fn DoAppEvent( ev as long ) select (ev) case _appWillFinishLaunching : fn BuildWindow end select end fn void local fn DoDialog( ev as long, tag as long, wnd as long ) select (ev) case _btnClick select ( tag ) case _resetBtn : fn NewGame end select case _gestureRecognizerClick if ( fn ButtonState( tag ) == NSControlStateValueOff ) then fn Play( tag ) case _windowWillClose : end end select end fn on appevent fn DoAppEvent on dialog fn DoDialog HandleEvents