Data update

This commit is contained in:
Ingy döt Net 2025-02-27 18:35:13 -05:00
parent 8e4e15fa56
commit 72eb4943cb
1853 changed files with 35514 additions and 9441 deletions

View file

@ -0,0 +1,303 @@
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

View file

@ -5,4 +5,4 @@ move=. pos ($:@][echo@'no')`(-@turn@], turn@]`[`(board@])})@.open ]
outcome=. 'tie'"_`(' wins',~ {&'.XO'@-@turn)@.won
show=. [ ''echo@, '',~ (,' '&,)/"1@({&'.XO')@(3 3$board)
won=. [: +./ 3 = [: | +/"1@(],|:,(<@0 1|:]),:<@0 1|:|.)@(3 3$board)
ttt=: [: outcome [: [F.(show@move[_2:Z:won+.full) 10&{.@_1
ttt=: [: outcome [: show@move^:(won-.@+.full)^:_. (10&{.@_1)

View file

@ -1 +1 @@
Until=. {{[F.(u[_2:Z:v)}} NB. apply u until condition v is true
Until=. {{u^:(-.@v)^:_.}} NB. apply u until condition v is true

View file

@ -1 +0,0 @@
{{u^:(-.@:v)^:_}}

View file

@ -0,0 +1,268 @@
MODULE TicTacToe;
FROM STextIO IMPORT
WriteLn, WriteString, ReadString, SkipLine;
FROM SWholeIO IMPORT
WriteInt, ReadCard;
FROM RandomNumbers IMPORT
Rnd;
TYPE
TWinPos = ARRAY [0 .. 7], [0 .. 2] OF INTEGER; (* Winning positions *)
TMover = (Human, Computer, Nobody);
CONST
WinPos = TWinPos{{0, 1, 2}, {3, 4, 5},
{6, 7, 8}, {0, 3, 6},
{1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}};
VAR
Board : ARRAY [0 .. 8] OF CHAR;
BestMove,
T : INTEGER;
MyPiece,
HisPiece : CHAR;
CompFirst: BOOLEAN;
Mover : TMover;
Answ : ARRAY [0 .. 1] OF CHAR;
MyWinsCnt,
HisWinsCnt,
DrawsCnt,
MovesCnt,
I : CARDINAL;
PROCEDURE WriteStringLn(S: ARRAY OF CHAR);
BEGIN
WriteString(S);
WriteLn
END WriteStringLn;
PROCEDURE SpacesFilled(): BOOLEAN;
VAR
I: CARDINAL;
BEGIN
FOR I := 0 TO 8 DO
IF Board[I] = " " THEN
RETURN FALSE;
END
END;
RETURN TRUE
END SpacesFilled;
PROCEDURE DisplayNumberedBoard;
VAR
I : CARDINAL;
Row: ARRAY [0 .. 10] OF CHAR;
BEGIN
Row := " * | * | * ";
FOR I := 0 TO 8 BY 3 DO
Row[1] := CHR(I + 1 + ORD("0"));
Row[5] := CHR(I + 2 + ORD("0"));
Row[9] := CHR(I + 3 + ORD("0"));
WriteStringLn(Row);
IF I <> 6 THEN
WriteStringLn("---+---+---");
END;
END
END DisplayNumberedBoard;
PROCEDURE DisplayPiecedBoard;
VAR
I : CARDINAL;
Row: ARRAY [0 .. 10] OF CHAR;
BEGIN
Row := " * | * | * ";
FOR I := 0 TO 8 BY 3 DO
Row[1] := Board[I];
Row[5] := Board[I + 1];
Row[9] := Board[I + 2];
WriteStringLn(Row);
IF I <> 6 THEN
WriteStringLn("---+---+---");
END;
END;
END DisplayPiecedBoard;
PROCEDURE Evaluate(Me: CHAR; Him: CHAR): INTEGER;
(* Recursive algorithm *)
VAR
I : CARDINAL;
SafeMove,
V,
LoseFlag: INTEGER;
BEGIN
IF Win(Me) THEN
RETURN 1
END;
IF Win(Him) THEN
RETURN -1
END;
IF SpacesFilled() THEN
RETURN 0
END;
LoseFlag := 1;
I := 0;
WHILE I <= 8 DO
IF Board[I] = " " THEN
Board[I] := Me; (* Try the move. *)
V := Evaluate(Him, Me);
Board[I] := " "; (* Restore the empty space. *)
IF V = -1 THEN
BestMove := I;
RETURN 1
END;
IF V = 0 THEN
LoseFlag := 0;
SafeMove := I
END;
END;
I := I + 1;
END;
BestMove := SafeMove;
RETURN -LoseFlag
END Evaluate;
PROCEDURE Win(Piece: CHAR): BOOLEAN;
VAR
I: CARDINAL;
BEGIN
FOR I := 0 TO 7 DO
IF (Board[WinPos[I, 0]] = Piece) AND
(Board[WinPos[I, 1]] = Piece) AND
(Board[WinPos[I, 2]] = Piece) THEN
RETURN TRUE
END;
END;
RETURN FALSE
END Win;
PROCEDURE ClearBoard;
VAR
I: CARDINAL;
BEGIN
FOR I := 0 TO 8 DO
Board[I] := " "
END;
END ClearBoard;
PROCEDURE WriteSummary(What: ARRAY OF CHAR; Cnt: CARDINAL);
BEGIN
WriteString(What);
WriteInt(Cnt, 1);
WriteString(" game");
IF Cnt <> 1 THEN
WriteStringLn("s.");
ELSE
WriteStringLn(".");
END;
END WriteSummary;
BEGIN
MyWinsCnt := 0;
HisWinsCnt := 0;
DrawsCnt := 0;
CompFirst := TRUE; (* It be reversed, so in fact human goes first *)
WriteLn;
WriteStringLn(" TIC-TAC-TOE");
WriteLn;
WriteStringLn("In this version, X always goes first.");
WriteStringLn("The board is numbered:");
REPEAT
CompFirst := NOT CompFirst; (* reverse who goes first *)
MovesCnt := 0;
WriteLn;
DisplayNumberedBoard;
WriteLn;
IF CompFirst THEN
WriteStringLn("I go first.");
ELSE
WriteString("You go first.");
END;
WriteLn;
ClearBoard;
IF CompFirst THEN
MyPiece := "X";
HisPiece := "O"
ELSE
MyPiece := "O";
HisPiece := "X"
END;
IF CompFirst THEN
Mover := Computer
ELSE
Mover := Human
END;
WHILE Mover <> Nobody DO
CASE Mover OF
| Computer:
IF MovesCnt = 0 THEN
BestMove := Rnd(9)
ELSIF MovesCnt = 1 THEN
IF Board[4] <> " " THEN
BestMove := Rnd(2) * 6 + Rnd(2) * 2 (* 0, 2, 6, or 8 *)
ELSE
BestMove := 4
END
ELSE
T := Evaluate(MyPiece, HisPiece)
END;
Board[BestMove] := MyPiece;
MovesCnt := MovesCnt + 1;
WriteLn;
DisplayPiecedBoard;
WriteLn;
IF Win(MyPiece) THEN
MyWinsCnt := MyWinsCnt + 1;
WriteStringLn("I win!");
Mover := Nobody
ELSIF SpacesFilled() THEN
DrawsCnt := DrawsCnt + 1;
WriteStringLn("It's a draw. Thank you.");
Mover := Nobody
ELSE
Mover := Human
END
| Human:
LOOP
WriteString("Where do you move? ");
ReadCard(I);
SkipLine;
IF (I < 1) OR (I > 9) THEN
WriteString("Illegal! ");
ELSIF Board[I - 1] <> " " THEN
WriteString("Place already occupied. ");
ELSE
EXIT
END
END;
Board[I - 1] := HisPiece;
MovesCnt := MovesCnt + 1;
WriteLn;
DisplayPiecedBoard;
WriteLn;
IF Win(HisPiece) THEN
HisWinsCnt := HisWinsCnt + 1;
WriteStringLn("You beat me! Good game.");
Mover := Nobody
ELSIF SpacesFilled() THEN
DrawsCnt := DrawsCnt + 1;
WriteStringLn("It's a draw. Thank you.");
Mover := Nobody
ELSE
Mover := Computer
END;
END (* CASE *)
END; (* WHILE *)
WriteLn;
WriteString("Another game (y/n)? ");
ReadString(Answ);
SkipLine
UNTIL CAP(Answ[0]) <> "Y";
WriteLn;
WriteStringLn("Final score:");
WriteSummary("You won ", MyWinsCnt);
WriteSummary("I won ", MyWinsCnt);
WriteSummary("We tied ", DrawsCnt);
WriteStringLn("See you later!");
END TicTacToe.