fork download
  1. BlockClosure extend [ valueWithExit [ self value: [^self] ] ]
  2.  
  3. | board showBoard patterns get message |
  4.  
  5. board := #(('.' '.' '.') ('.' '.' '.') ('.' '.' '.')) deepCopy.
  6. showBoard := [board do: [:line | line do: [:cell | cell display]. stdout nl]].
  7.  
  8. patterns := board,
  9. ({[:i | 1]. [:i | 2]. [:i | 3]. [:i | i]. [:i | 4-i]} collect: [:spec |
  10. (1 to: 3) collect: [:idx | (board at: idx) at: (spec value: idx)]]).
  11.  
  12. get := [:xy |
  13. | v |
  14. [(xy, '?') display. (v := stdin nextLine asInteger) displayNl between: 1 and: 3] whileFalse.
  15. v
  16. ].
  17.  
  18. message := '[draw]'.
  19. [:exit |
  20. | rest |
  21. [(rest := (board gather: [:line | line]) count: [:each | each = '.']) > 0] whileTrue: [
  22. | player x y |
  23. player := #('x' 'o') at: rest \\ 2 + 1.
  24. ('[<', player asString, '>''s turn]') displayNl.
  25. showBoard value.
  26. [ x := get value: 'x'.
  27. y := get value: 'y'.
  28. ((board at: y) at: x) ~= '.'] whileTrue.
  29. ((board at: y) at: x) at: 1 put: player first.
  30. (patterns anySatisfy: [:pat | (pat count: [:each | each = player]) = 3]) ifTrue: [
  31. message := '[<', player asString, '> won!]'.
  32. exit value].
  33. ]
  34. ] valueWithExit.
  35. message displayNl.
  36. showBoard value.
  37. '[game over]' displayNl.
  38.  
Success #stdin #stdout 0s 171456KB
stdin
1
1
2
2
3
3
2
1
2
3
1
3
3
1
1
2
3
2
stdout
[<o>'s turn]
...
...
...
x?1
y?1
[<x>'s turn]
o..
...
...
x?2
y?2
[<o>'s turn]
o..
.x.
...
x?3
y?3
[<x>'s turn]
o..
.x.
..o
x?2
y?1
[<o>'s turn]
ox.
.x.
..o
x?2
y?3
[<x>'s turn]
ox.
.x.
.oo
x?1
y?3
[<o>'s turn]
ox.
.x.
xoo
x?3
y?1
[<x>'s turn]
oxo
.x.
xoo
x?1
y?2
[<o>'s turn]
oxo
xx.
xoo
x?3
y?2
[<o> won!]
oxo
xxo
xoo
[game over]