基礎実習　MA0２

```select.act <- function(state){
poss.act = which(state=="N")
if (length(poss.act)==1){
act = poss.act
} else {
act = sample(poss.act, 1)
}
return(act)
}

ck.term <- function(state) {
term = F
result = "undecided"
term.idx = matrix(c(1,2,3,4,5,6,7,8,9,1,4,7,
2,5,8,3,6,9,1,5,9,3,5,7),ncol=3,byrow=T)
if (sum(state == "N")==0) {
term=T
result = "tie"
}
for (i.ck in 1:8) {
O.won = all(state[term.idx[i.ck,]]=="O")
X.won = all(state[term.idx[i.ck,]]=="X")
if (O.won ==1 ) {
term= T
result = "O won"
break
}
if (X.won ==1){
term=T
result = "X won"
break
}
}
return(list(term = term, result=result))
}

ck.term2 <- function(state) {
term = F
result = "undecided"
term.idx = matrix(c(1,2,3,4,5,6,7,8,9,1,4,7,
2,5,8,3,6,9,1,5,9,3,5,7),ncol=3,byrow=T)
if (sum(state == "N")==0) {
term=T
result = "tie"
}
O.won = apply(term.idx,1,function(x) all(state[x]=="O"))
X.won = apply(term.idx,1,function(x) all(state[x]=="X"))
if (any(O.won) == T ) {
term= T
result = "O won"
} else if (any(X.won) ==1){
term=T
result = "X won"
}
return(list(term = term, result=result))
}

tictactoe <-function(){
coord = matrix(c(1,3,1,2,1,1,2,3,2,2,2,1,3,3,
3,2,3,1), nrow = 9, byrow=T)
plot(0,0, type="n", xlim= c(0.5,3.5), ylim=c(0.5,3.5))
abline(h = 1.5); abline(h = 2.5)
abline(v = 1.5); abline(v = 2.5)
state = rep("N",9); marker = c("O","X")
repeat {
for (i.player in 1:2) {
act = select.act(state)
state[act] = marker[i.player]
text(coord[act,1],coord[act,2], marker[i.player], cex=4)
res = ck.term2(state)
if (res\$term == T) { break }
Sys.sleep(0.5)
}
if (res\$term == T) {
print(paste("result:", res\$result))
break
}
}
}

# 実行例
> tictactoe()
[1] "result: X won"

# 対戦版　- 無作為に動くので非常に弱いです。
# 上のselect.actを変更することでマシになると思います。
play.tictactoe.R <-function(){
coord = matrix(c(1,3,1,2,1,1,2,3,2,2,2,1,3,3,
3,2,3,1), nrow = 9, byrow=T)
plot(coord, pch=paste(1:9), col='gray', cex=3, xlim= c(0.5,3.5), ylim=c(0.5,3.5))
abline(h = 1.5); abline(h = 2.5)
abline(v = 1.5); abline(v = 2.5)
state = rep("N",9);
repeat {
for (i.player in 1:2){
if (i.player == 1){
act = as.numeric(readline(prompt="Enter box ID: "))
} else {act = select.act(state)}
state[act] = marker[i.player]
text(coord[act,1],coord[act,2], marker[i.player], cex=4)
res = ck.term2(state)
if (res\$term == T) { break }
Sys.sleep(0.5)
}
if (res\$term == T) {
print(paste("result:", res\$result))
break
}
}
}

# 実行例：
> play.tictactoe.R()
Enter box ID: 5
Enter box ID: 1
Enter box ID: 9
[1] "result: O won"
```