"プログラミングのお題スレ Part10
>>779"
"Squeak/Pharo Smalltalk"
| N M empty board rest total rock rockAt disp currRock input pos dirs units check |
N := 15. M := 5.
empty := $. .
rock := 'ox'.
board:= (Array new: N withAll: (String new: N withAll: empty)) deepCopy.
rockAt := [:pt | (board at: pt y ifAbsent: #()) at: pt x ifAbsent: empty].
Transcript open.
disp := [:msg | Transcript cr; cr; show: board asStringWithCr; cr; show: msg].
currRock:= nil.
input := [
| posStr |
disp value: currRock asString, '? '.
[ posStr := UIManager default request: currRock asString, '?'.
(posStr isNil or: [posStr isEmpty]) ifTrue: [^self].
Transcript show: posStr; space.
posStr size = 2
and: [1 asPoint <= (pos := posStr second digitValue @ posStr first digitValue)
and: [pos <= N asPoint
and: [(rockAt value: pos) == empty]]]
] whileFalse
].
dirs := 0 asPoint eightNeighbors.
units := (dirs first: 4) collect: #r.
check := [
| limits lens |
limits := dirs * 0 + pos.
M-1 timesRepeat: [
limits := dirs with: limits collect: [:dir :head |
(rockAt value: head + dir) == currRock ifTrue: [head + dir] ifFalse: [head]
]
].
lens := (((limits last: 4) - (limits first: 4) collect: #r) / units) rounded.
"Transcript cr; show: lens."
lens anySatisfy: [:len | len >= (M-1)].
].
rest := total := N squared.
[rest > 0] whileTrue: [
currRock := rock atWrap: total - rest + 1.
input value.
(board at: pos y) at: pos x put: currRock.
check value ifTrue: [disp value: currRock asString, ' win'. ^self].
rest := rest - 1.
].
disp value: 'draw'