最終更新 1 month ago

capitalex's Avatar capitalex revised this gist 1 month ago. Go to revision

1 file changed, 123 insertions

tictactoe.io(file created)

@@ -0,0 +1,123 @@
1 + Cell := Object clone do (
2 + state := nil
3 + occupy := method(player,
4 + state = player
5 + )
6 + empty := method(state isNil)
7 + asString := method(
8 + if(state isNil, return "_")
9 + state
10 + )
11 + )
12 +
13 + Grid := Object clone do (
14 + init := method(
15 + self grid := list(
16 + list(Cell clone, Cell clone, Cell clone),
17 + list(Cell clone, Cell clone, Cell clone),
18 + list(Cell clone, Cell clone, Cell clone)
19 + )
20 + )
21 +
22 + init
23 + asString := method(
24 + grid map (row, " " .. row join(" | ")) join("\n---+---+---\n")
25 + )
26 +
27 + at := method(row, column, grid at(row) ?at(column))
28 +
29 + isInBounds := method(row, column, at(row, column) isNil not)
30 +
31 + isEmpty := method(row, column, at(row, column) empty)
32 +
33 + occupy := method(row, column, who, at(row, column) occupy(who))
34 +
35 + occupant := method(row, column, at(row, column) state)
36 +
37 + winnerInRow := method(row,
38 + if (occupant(row, 0) == occupant(row, 1) and occupant(row, 1) == occupant(row, 2)) \
39 + then(return occupant(row, 0))
40 + )
41 +
42 + winnerInColumn := method(col,
43 + if (occupant(0, col) == occupant(1, col) and occupant(1, col) == occupant(2, col)) \
44 + then(return occupant(0, col))
45 + )
46 +
47 + winnerDiagonal := method(
48 + if (occupant(0, 0) == occupant(1, 1) and occupant(1, 1) == occupant(2, 2)) \
49 + then (return occupant(0, 0))
50 + if (occupant(0, 2) == occupant(1, 1) and occupant(1, 1) == occupant(2, 0)) \
51 + then(return occupant(0, 2))
52 + )
53 + winner := method(
54 + if(winnerInRow(0), return winnerInRow(0))
55 + if(winnerInRow(1), return winnerInRow(1))
56 + if(winnerInRow(2), return winnerInRow(2))
57 + if(winnerInColumn(0), return winnerInColumn(0))
58 + if(winnerInColumn(1), return winnerInColumn(1))
59 + if(winnerInColumn(2), return winnerInColumn(2))
60 + if(winnerDiagonal, return winnerDiagonal)
61 + )
62 + )
63 +
64 + Game := Object clone do (
65 + init := method(
66 + self stdin := File standardInput
67 + self turn := "X"
68 + self turnsLeft := 9
69 + self state := "playing"
70 + self grid := Grid clone
71 + )
72 +
73 + init
74 + getWinner := method(
75 + if(grid winner, return grid winner)
76 + if(turnsLeft == 0, return "draw")
77 + nil
78 + )
79 +
80 + changeTurn := method(
81 + turnsLeft = turnsLeft - 1
82 + if(turn == "X") then(turn = "O") else(turn = "X")
83 + )
84 +
85 + takeTurn := method(
86 + loop(
87 + println
88 + "enter turn for " print ; turn print ; " > " print
89 + position := stdin readLine split map(asNumber)
90 +
91 + (position size == 2) ifFalse (
92 + "please enter two numbers seperated by spaces!" println
93 + continue
94 + )
95 +
96 + row := position at(0)
97 + column := position at(1)
98 +
99 + grid isInBounds(row, column) ifFalse (
100 + "space is out of bounds." println
101 + continue
102 + )
103 +
104 + grid isEmpty(row, column) ifFalse (
105 + "can't make move there." println
106 + continue
107 + )
108 +
109 + grid occupy(row, column, turn)
110 + break
111 + )
112 + changeTurn
113 + )
114 +
115 + play := method(
116 + while(turnsLeft > 0 and getWinner isNil, takeTurn)
117 + println
118 + if(getWinner) then(("The winner is " .. getWinner) println) else("It's a draw" println)
119 +
120 + )
121 +
122 + asString := method("Turn: " .. turn .. "\n" .. grid)
123 + )
Newer Older