Skip to content

Latest commit

 

History

History

AlphaCa

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

[AlphaCa (AI Tic-Tac-Toe)]

Rage against the AlphaGo

  • Find the winner in more advanced way

2. Find the winner

k=86532; a.arrow[,,k]                                                   # winner : 2nd player (8-4-6 on the '\' line)

      [,1] [,2] [,3]
[1,]    8    5    1
[2,]    7    4    2
[3,]    9    3    6

# Get the sums of remainders that are 0(all even) or 3(all odd)
wl <- c()                                                               # wl(win/lose) : 0 (2nd player wins) / 1~2 (draw) / 3 (1st one wins)
for (i in 1:3) {                                                        # combine colums
  wl <- c(wl, sum(a.arrow[,i,k]%%2))
}
for (i in 1:3) {                                                        # combine rows
  wl <- c(wl, sum(a.arrow[i,,k]%%2))
}
wl <- c(wl, sum(diag(a.arrow[,,k])%%2))                                 # combine \ diagonal 
wl <- c(wl, sum(c(a.arrow[1,3,k],a.arrow[2,2,k],a.arrow[3,1,k])%%2))    # combine / diagonal
wl                                                                      # 7th element is 0 → 2nd player won

# mm : max and min value from wl; check easier if a winner exists
mm <- c(max(wl), min(wl))
mm

[1] 2 2 1 2 1 2 0 2
[1] 2 0

2.1. Find the winner when there are two or more winning lines

k=86537; a.arrow[,,k]

      [,1] [,2] [,3]
[1,]    5    3    7
[2,]    8    9    2
[3,]    4    1    6

# get wl.max with wl
wl <- c()
wl.max <- c()                                                           # wl.max : the max number of each line
for (i in 1:3) {
  wl <- c(wl, sum(a.arrow[,i,k]%%2))
  wl.max <- c(wl.max, max(a.arrow[,i,k]))
}
for (i in 1:3) {
  wl <- c(wl, sum(a.arrow[i,,k]%%2))
  wl.max <- c(wl.max, max(a.arrow[i,,k]))
}
wl <- c(wl, sum(diag(a.arrow[,,k])%%2))
wl.max <- c(wl.max, max(diag(a.arrow[,,k])))
wl <- c(wl, sum(c(a.arrow[1,3,k],a.arrow[2,2,k],a.arrow[3,1,k])%%2))
wl.max <- c(wl.max, max(a.arrow[1,3,k],a.arrow[2,2,k],a.arrow[3,1,k]))
wl                                                                      # 2, 4-th lines consist only of odd numbers
wl.max

# mm : max and min value from wl; check easier if a winner exists
mm <- c(max(wl), min(wl))
mm

[1] 1 3 1 3 1 1 2 2
[1] 8 9 7 7 9 6 9 9
[1] 3 1

# Find the final singular winner
wl.win.rank <- c(which(wl==3), which(wl==0)); wl.win.rank               # return 2, 4 where the winning lines are
wl.max.real <- min(wl.max[wl.win.rank]); wl.max.real                    # the min of the max values in 2, 4th lines is 7
wl.max.real.rank <- which(wl.max==wl.max.real); wl.max.real.rank        # 7 is the max value of 3, 4th lines

wl.mrr.freq <- table(c(wl.max.rank, wl.max.real.rank)); wl.mrr.freq     # {4} is the intersection of {2, 4} and {3, 4}

wl.rmr <- as.numeric(names(which(wl.mrr.freq==max(wl.mrr.freq))));wl.rmr# return 4
winner <- wl[wl.rmr]
winner                                                                  # the 4th line indicates '3' → 1st player won!

[1] 2 4
[1] 7
[1] 3 4

2 3 4
1 1 2

[1] 4
[1] 3

  • Generate randomized cases and find if the winner exists

0. The number of cases

factorial(361)                                              # Go-game : 19 * 19 = 361 points
exp(sum(log(1:361)))
sum(log(1:361, base=10))                                    # 768.1577
10^0.1577                                                   # → 1.437805 * 10^768

[1] Inf
[1] Inf
[1] 768.1577
[1] 1.437805

factorial(9)                                                # Tic-Tac-Toe : 362,880
factorial(9)/(2*2*2^4)                                      # eliminate symmetries of top and bottom(/2), left and right(/2), diagonals(/4) : 1/16 → 5,670

[1] 362880
[1] 5670

1. Generate randomized cases as array

a <- rank(runif(9), ties.method="random"); a
matrix(a, nrow=3, ncol=3)

[1] 2 9 7 5 1 3 8 6 4

      [,1] [,2] [,3]
[1,]    2    5    8
[2,]    9    1    6
[3,]    7    3    4

1.1 5 cases

set.seed(0307)                                              # the seed number works during the following for statement …… crazy!
k=5; aa <- c(); a.arr <- c()

for(i in 1:k) {
    a <- rank(runif(9), ties.method="random")
    aa <- c(aa, a)
    }

a.arr <- array(aa, c(3,3,k))
a.arr

, , 1

      [,1] [,2] [,3]
[1,]    6    3    2
[2,]    5    1    8
[3,]    4    7    9
……

1.1.1 100K cases

set.seed(0307)
k=10^5; aa <- c(); a.arr <- c()                             # I realized such numerous cases is not needed, when it was too late.

for(i in 1:k) {
    a <- rank(runif(9), ties.method="random")
    aa <- c(aa, a)
    }

a.arrow <- array(aa, c(3,3,k))
str(a.arrow)

int [1:3, 1:3, 1:100000] 6 5 4 3 1 7 2 8 9 3 ...

1.2 Find if each case has the winner

  (1) Fill number of 1~9 instead of O/X
       : The 1st Player puts (1, 3, 5, 7, 9) and the 2nd player does (2, 4, 6, 8).
  (2) It is the winner who puts only odd or only even numbers in a line including diagonal ones
       : If there are two or more such lines, the winner is who has the smaller max value(to be continued ……).

a.arrow[,,41562]                                            # winner : 1nd player (3-1-5 on the '\' line)

      [,1] [,2] [,3]
[1,]    3    9    6
[2,]    2    1    7
[3,]    4    8    5

a.arrow[,2,41562]
diag(a.arrow[,,41562])
n=41562; c(a.arrow[1,3,n],a.arrow[2,2,n],a.arrow[3,1,n])

[1] 9 1 8
[1] 3 1 5
[1] 6 1 4

a.arrow[,3,41562]%%2                                        # 0 : even number / 1 : odd number
diag(a.arrow[,,41562])%%2                                   # 1 1 1 : consists of only odd numbers

[1] 0 1 1
[1] 1 1 1