Advertisement
Guest User

checkers

a guest
Jun 7th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 36.61 KB | None | 0 0
  1. library(plotrix)
  2.  
  3. board <- list(
  4.     c(10, 1, 10, 1, 10, 1, 10, 1),
  5.     c(1, 10, 1, 10, 1, 10, 1, 10),
  6.     c(10, 1, 10, 1, 10, 1, 10, 1),
  7.     c(0, 10, 0, 10, 0, 10, 0, 10),
  8.     c(10, 0, 10, 0, 10, 0, 10, 0),
  9.     c(-1, 10, -1, 10, -1, 10, -1, 10),
  10.     c(10, -1, 10, -1, 10, -1, 10, -1),
  11.     c(-1, 10, -1, 10, -1, 10, -1, 10)
  12.    
  13.     # c(10, 0, 10, 1, 10, 0, 10, 1),
  14.     # c(1, 10, 1, 10, 1, 10, 1, 10),
  15.     # c(10, 1, 10, 0, 10, 1, 10, 0),
  16.     # c(0, 10, 1, 10, 0, 10, 0, 10),
  17.     # c(10, -1, 10, 0, 10, 0, 10, 0),
  18.     # c(-1, 10, -1, 10, -1, 10, -1, 10),
  19.     # c(10, -1, 10, -1, 10, -1, 10, -1),
  20.     # c(-1, 10, -1, 10, -1, 10, -1, 10)
  21.    
  22.     # c(10, 1, 10, 1, 10, 1, 10, 1),
  23.     # c(1, 10, 1, 10, 1, 10, 1, 10),
  24.     # c(10, 1, 10, 1, 10, 1, 10, 1),
  25.     # c(0, 10, 0, 10, 0, 10, 1, 10),
  26.     # c(10, 0, 10, -1, 10, -1, 10, 0),
  27.     # c(0, 10, 0, 10, 0, 10, -1, 10),
  28.     # c(10, -1, 10, -1, 10, -1, 10, -1),
  29.     # c(-1, 10, 0, 10, -1, 10, 0, 10)
  30.    
  31.     # c(10, 1, 10, 0, 10, 0, 10, 0),
  32.     # c(0, 10, 1, 10, 0, 10, 0, 10),
  33.     # c(10, 0, 10, 0, 10, 0, 10, 0),
  34.     # c(0, 10, 0, 10, 0, 10, 0, 10),
  35.     # c(10, 0, 10, 0, 10, 0, 10, 0),
  36.     # c(3, 10, 0, 10, 0, 10, 0, 10),
  37.     # c(10, 0, 10, 0, 10, 0, 10, -3),
  38.     # c(0, 10, 0, 10, 0, 10, 0, 10)
  39.    
  40. )
  41. checkersNum <- function(s) {
  42.     n <- 0
  43.     for (y in 1:8) {
  44.         for (x in 1:8) {
  45.             if (board[[y]][x] == s || board[[y]][x] == 3 * s)
  46.                 n <- n + 1
  47.         }
  48.     }
  49.     return (n)
  50. }
  51. white <- checkersNum(-1)
  52. black <- checkersNum(1)
  53.  
  54. board2 <<- list(
  55.     c(10, 1, 10, 1, 10, 1, 10, 1),
  56.     c(1, 10, 1, 10, 1, 10, 1, 10),
  57.     c(10, 1, 10, 1, 10, 1, 10, 1),
  58.     c(0, 10, 0, 10, 0, 10, 0, 10),
  59.     c(10, 0, 10, 0, 10, 0, 10, 0),
  60.     c(-1, 10, -1, 10, -1, 10, -1, 10),
  61.     c(10, -1, 10, -1, 10, -1, 10, -1),
  62.     c(-1, 10, -1, 10, -1, 10, -1, 10)
  63. )
  64. white2 <- 12
  65. black2 <- 12
  66. checkable2 <- list()
  67. checked2 <- NA
  68. side2 <- -1
  69. oneMore2 <- F
  70. queenIsChecked2 <- F
  71.  
  72. makeBackup <- function() {
  73.     board2 <<- board
  74.     white2 <<- white
  75.     black2 <<- black
  76.     checkable2 <<- checkable
  77.     checked2 <<- checked
  78.     side2 <<- side
  79.     oneMore2 <<- oneMore
  80.     queenIsChecked2 <<- queenIsChecked
  81. }
  82. backup <- function() {
  83.     board <<- board2
  84.     white <<- white2
  85.     black <<- black2
  86.     checkable <<- checkable2
  87.     checked <<- checked2
  88.     side <<- side2
  89.     oneMore <<- oneMore2
  90.     queenIsChecked <<- queenIsChecked2
  91. }
  92. checkBoard <- function() {
  93.     f <- F
  94.     checkable <<- list()
  95.     for (i in 1:8) {
  96.         for (j in 1:8){
  97.             if (board[[i]][j] == side) {
  98.                 if (check(c(i, j))) {
  99.                     f <- T
  100.                     checkable[[length(checkable) + 1]]<<- c(i, j)
  101.                 }
  102.             }
  103.             else if (board[[i]][j] == side * 3) {
  104.                 if (queenCheck(c(i, j))) {
  105.                     f <- T
  106.                     checkable[[length(checkable) + 1]]<<- c(i, j)
  107.                 }
  108.             }
  109.         }
  110.     }
  111.     return (f)
  112. }
  113. isCheckable <- function(pos) {
  114.     if (length(checkable) == 0)
  115.         return (T)
  116.     else {
  117.         x <- pos[2]
  118.         y <- pos[1]
  119.         for (i in 1:length(checkable)) {
  120.             if (y == checkable[[i]][1] && x == checkable[[i]][2])
  121.                 return (T)
  122.         }
  123.     }
  124.     return (F)
  125. }
  126. getCoordinates <- function(){
  127.     N = locator(n = 1)
  128.     for(i in 1 : 64)
  129.         if((N[1] > -40 + ((i - 1) %% 8) * 10) && (N[2] < 40 - ((i - 1) %/% 8) * 10) &&
  130.            (N[1] < -30 + ((i - 1) %% 8) * 10) && (N[2] > 30 - ((i - 1) %/% 8) * 10))
  131.             return(c((1 + (i - 1) %/% 8), 1 + ((i - 1) %% 8)))
  132.     if((N[1] < -40) && (N[1] > -50) && (N[2] < 50) && (N[2] > 40))
  133.         return (0)
  134.     if((N[1] > 40) && (N[1] < 50) && (N[2] < 50) && (N[2] > 40))
  135.         return (9)
  136.     if((N[1] > -38) && (N[1] < -34) && (N[2] < -40) && (N[2] > -50))
  137.         return (11)
  138.     if((N[1] >  34) && (N[1] <  38) && (N[2] < -40) && (N[2] > -50))
  139.     {}
  140. }
  141. drawBoard <- function(){
  142.     plot(0, 0, xlim = c(-60, 60), ylim = c(-60, 60), type = "n",
  143.          bty = "n", xaxt = "n", yaxt = "n", col.lab = "#FFFFFF")
  144.     for(i in 1 : 8)
  145.         for(j in 1 : 8)
  146.         {
  147.             Js = (j - 1) * 10
  148.             Is = (i - 1) * 10
  149.             Cell = board[[i]][j]
  150.             if(Cell == 10)
  151.             {
  152.                 rect(-40 + Js, 40 - Is,
  153.                      -30 + Js, 30 - Is, col = "#d4a550", border = "#d4a550")
  154.             }
  155.             else if(Cell == 0)
  156.             {
  157.                 rect(-40 + Js, 40 - Is,
  158.                      -30 + Js, 30 - Is, col = "#5F3405", border = "#5B3501")
  159.             }
  160.             else if(Cell == 1)
  161.             {
  162.                 rect(-40 + Js, 40 - Is,
  163.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  164.                 draw.circle(-35 + Js, 35 - Is, 4, col = "#090909", lwd = 2, border = "#252525")
  165.                 draw.circle(-35 + Js, 35 - Is, 3, col = "#090909", lwd = 2, border = "#252525")
  166.                 draw.circle(-35 + Js, 35 - Is, 2, col = "#101010", lwd = 2, border = "#252525")
  167.             }
  168.             else if(Cell == -1)
  169.             {
  170.                 rect(-40 + Js, 40 - Is,
  171.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  172.                 draw.circle(-35 + Js, 35 - Is, 4, col = "#A4A4A4", lwd = 2, border = "#D5D5D5")
  173.                 draw.circle(-35 + Js, 35 - Is, 3, col = "#A4A4A4", lwd = 2, border = "#D5D5D5")
  174.                 draw.circle(-35 + Js, 35 - Is, 2, col = "#B9B9B9", lwd = 2, border = "#D5D5D5")
  175.             }
  176.             else if(Cell == 2)
  177.             {
  178.                 rect(-40 + Js, 40 - Is,
  179.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  180.                 draw.circle(-35 + Js, 35 - Is, 4, col = "#090909", lwd = 2, border = "#252525")
  181.                 draw.circle(-35 + Js, 35 - Is, 3, col = "#090909", lwd = 2, border = "#252525")
  182.                 draw.circle(-35 + Js, 35 - Is, 2, col = "#101010", lwd = 2, border = "#252525")
  183.                 draw.circle(-35 + Js, 35 - Is, 4, lwd = 2, border = "#008500")
  184.             }
  185.             else if(Cell == -2)
  186.             {
  187.                 rect(-40 + Js, 40 - Is,
  188.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  189.                 draw.circle(-35 + Js, 35 - Is, 4, col = "#A4A4A4", lwd = 2, border = "#D5D5D5")
  190.                 draw.circle(-35 + Js, 35 - Is, 3, col = "#A4A4A4", lwd = 2, border = "#D5D5D5")
  191.                 draw.circle(-35 + Js, 35 - Is, 2, col = "#B9B9B9", lwd = 2, border = "#D5D5D5")
  192.                 draw.circle(-35 + Js, 35 - Is, 4, lwd = 2, border = "#008500")
  193.             }
  194.             else if(Cell == 3)
  195.             {
  196.                 rect(-40 + Js, 40 - Is,
  197.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  198.                 draw.circle(-35 + Js, 35 - Is, 4, col = "#101010", lwd = 2, border = "#303030")
  199.             }
  200.             else if(Cell == -3)
  201.             {
  202.                 rect(-40 + Js, 40 - Is,
  203.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  204.                 draw.circle(-35 + Js, 35 - Is, 4, col = "#A9A9A9", lwd = 2, border = "#D9D9D9")
  205.             }
  206.             else if(Cell == 4)
  207.             {
  208.                 rect(-40 + Js, 40 - Is,
  209.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  210.                 draw.circle(-35 + Js, 35 - Is, 4, col = "#101010", lwd = 2, border = "#303030")
  211.                 draw.circle(-35 + Js, 35 - Is, 4, lwd = 2, border = "#008500")
  212.             }
  213.             else if(Cell == -4)
  214.             {
  215.                 rect(-40 + Js, 40 - Is,
  216.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  217.                 draw.circle(-35 + Js, 35 - Is, 4, col = "#A9A9A9", lwd = 2, border = "#D9D9D9")
  218.                 draw.circle(-35 + Js, 35 - Is, 4, lwd = 2, border = "#008500")
  219.             }
  220.             else if((i == 9 - side || i == -1 * side) && !queenIsChecked && !is.na(checked))
  221.             {
  222.                 rect(-40 + Js, 40 - Is,
  223.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  224.                 draw.circle     (0 - 35 + Js,   0 + 35 - Is, 4,   col = "#d4a550", border = "#48036F", lwd = 2)
  225.                 draw.circle (-2.25 - 35 + Js, 1.3 + 35 - Is, 0.5, col = "#5B3501", border = "#5B3501")
  226.                 draw.circle (-0.85 - 35 + Js, 2.5 + 35 - Is, 0.5, col = "#5B3501", border = "#5B3501")
  227.                 draw.circle  (0.85 - 35 + Js, 2.5 + 35 - Is, 0.5, col = "#5B3501", border = "#5B3501")
  228.                 draw.circle  (2.25 - 35 + Js, 1.3 + 35 - Is, 0.5, col = "#5B3501", border = "#5B3501")
  229.                 polygon(c(-2.25 - 35 + Js,    0 - 35 + Js, -1.5 - 35 + Js),
  230.                         c(  1.3 + 35 - Is, -1.9 + 35 - Is, -1.9 + 35 - Is),
  231.                         col = "#5B3501", border = "#5B3501")
  232.                 polygon(c(-0.85 - 35 + Js,  0.7 - 35 + Js, -1.2 - 35 + Js),
  233.                         c(2.5  + 35 - Is, -1.9 + 35 - Is, -1.9 + 35 - Is),
  234.                         col = "#5B3501", border = "#5B3501")
  235.                 polygon(c(0.85 - 35 + Js,  1.2 - 35 + Js,  -0.7 - 35 + Js),
  236.                         c( 2.5 + 35 - Is, -1.9 + 35 - Is,  -1.9 + 35 - Is),
  237.                         col = "#5B3501", border = "#5B3501")
  238.                 polygon(c(2.25 - 35 + Js,   1.5 - 35 + Js,    0 - 35 + Js),
  239.                         c( 1.3  + 35 - Is, -1.9 + 35 - Is, -1.9 + 35 - Is),
  240.                         col = "#5B3501", border = "#5B3501")
  241.                 rect(-1.7 - 35 + Js, -2.4 + 35 - Is,
  242.                      1.7 - 35 + Js, -2.7 + 35 - Is, col = "#5B3501", border = "#5B3501")
  243.             }
  244.             else if(Cell == 5)
  245.             {
  246.                 rect(-40 + Js, 40 - Is,
  247.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  248.                 draw.circle     (0 - 35 + Js,  0 + 35 - Is, 4,   col = "#d4a550", border = "#008500", lwd = 2)
  249.                 draw.circle     (0 - 35 + Js, -1 + 35 - Is, 1.5, col = "#5B3501", border = "#5B3501")
  250.                 draw.circle (-2.25 - 35 + Js,  1 + 35 - Is, 0.5, col = "#5B3501", border = "#5B3501")
  251.                 draw.circle (-0.85 - 35 + Js,  2 + 35 - Is, 0.5, col = "#5B3501", border = "#5B3501")
  252.                 draw.circle  (0.85 - 35 + Js,  2 + 35 - Is, 0.5, col = "#5B3501", border = "#5B3501")
  253.                 draw.circle  (2.25 - 35 + Js,  1 + 35 - Is, 0.5, col = "#5B3501", border = "#5B3501")
  254.             }
  255.             else if(Cell == -5)
  256.             {
  257.                 rect(-40 + Js, 40 - Is,
  258.                      -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  259.                 draw.circle (0 - 35 + Js,   0 + 35 - Is, 4,   col = "#d4a550", border = "#A60000", lwd = 2)
  260.                 draw.circle (0 - 35 + Js, 0.8 + 35 - Is, 2.2, col = "#5B3501", border = "#5B3501")
  261.                 draw.circle(-1 - 35 + Js,   1 + 35 - Is, 0.8, col = "#d4a550", border = "#5B3501")
  262.                 draw.circle (1 - 35 + Js,   1 + 35 - Is, 0.8, col = "#d4a550", border = "#5B3501")
  263.                 draw.circle (0 - 35 + Js, -1.2 + 35 - Is, 1.5, col = "#5B3501", border = "#5B3501")
  264.                 segments(-0.5 - 35 + Js, -1.7 + 35 - Is,  0.5 - 35 + Js, -0.7 + 35 - Is, col = "#d4a550")
  265.                 segments (0.5 - 35 + Js, -1.7 + 35 - Is, -0.5 - 35 + Js, -0.7 + 35 - Is, col = "#d4a550")
  266.             }
  267.         }
  268.     Row = c("A", "B", "C", "D", "E", "F", "G", "H")
  269.     rect(-50,  50, -40, -50, col = "#d4a550", lwd = 3, border = "#5B3501")
  270.     rect( 40,  50,  50, -50, col = "#d4a550", lwd = 3, border = "#5B3501")
  271.     rect(-50,  50,  50,  40, col = "#d4a550", lwd = 3, border = "#5B3501")
  272.     rect(-50, -40,  50, -50, col = "#d4a550", lwd = 3, border = "#5B3501")
  273.     rect(-32,  -42, 32, -48, col = "#d4a550", lwd = 3, border = "#5B3501")
  274.     polygon(c(-37.5, -34.5, -34.5), c(-45, -42, -48), col = "#5B3501", border = "#d4a550")
  275.     polygon(c( 37.5,  34.5,  34.5), c(-45, -42, -48), col = "#5B3501", border = "#d4a550")
  276.     polygon(c(-36, -33, -33), c(-45, -42, -48), col = "#5B3501", border = "#d4a550")
  277.     polygon(c( 36,  33,  33), c(-45, -42, -48), col = "#5B3501", border = "#d4a550")  
  278.     segments(-40, 50, -40, -50, lwd = 3, col = "#5B3501")
  279.     segments( 40, 50,  40, -50, lwd = 3, col = "#5B3501")
  280.     for(i in 1 : 8)
  281.     {
  282.         text(-35 + 10 * (i - 1),  45, Row[i], font = 2)
  283.         text(-45, 35 - 10 * (i - 1), i, font = 2)
  284.         text( 45, 35 - 10 * (i - 1), i, font = 2)
  285.     }
  286.     text( 45, 45, "X", cex = 2, col = "#A60000")
  287.     text(-45, 45, "C", cex = 2, col = "#008500")
  288.     points(-43.5, 46, pch = 17, cex = 1.5, col = "#008500")
  289.     text(-45, -45, white, cex = 1.5, col = "#888888")        
  290.     text( 45, -45, black, cex = 1.5, col = "#888888")
  291.     if (nchar(mes) != 0)
  292.         text(0, -45, mes)
  293.     else
  294.         text(0, -45, "Checkers")
  295.     if (side == -1)
  296.         text(-45, -45, white, cex = 1.5, col = "#FFFFFF")
  297.     else
  298.         text( 45, -45, black, cex = 1.5, col = "#000000")
  299. }
  300.  
  301. move <- function(start, finish) {
  302.     if (finish[1] == 9 - side || finish[1] == -1 * side) { # && board[[finish[1]]][finish[2]] == side * 3)
  303.         des <- (finish - start) / 2
  304.         eatFlag <- F
  305.         if (board[[finish[1]]][finish[2]] == -5) {
  306.             eatFlag <- T
  307.             uncheck(start)
  308.             board[[start[1] + des[1]]][start[2] + des[2]] <- 0
  309.             if (side == 1)
  310.                 white <<- white - 1
  311.             else
  312.                 black <<- black - 1
  313.             moveAnimation(start, finish, T)
  314.         }
  315.         else {
  316.             uncheck(start)
  317.             moveAnimation(start, finish, F)
  318.         }
  319.        
  320.         board[[start[1]]][start[2]] <- 0
  321.         board[[finish[1]]][finish[2]] <- 3 * side
  322.         board <<- board
  323.         oneMore <<- F
  324.         checked <<- NA
  325.         if (!oneMore && queenCheck(finish) && eatFlag) {
  326.             side <<- side * -1
  327.             oneMore <<- T
  328.             checked <<- finish
  329.             queenIsChecked <<- T
  330.         }
  331.     }
  332.     else if (board[[finish[1]]][finish[2]] == 5) {
  333.         uncheck(start)
  334.         moveAnimation(start, finish, F)
  335.         board[[finish[1]]][finish[2]] <- board[[start[1]]][start[2]]
  336.         board[[start[1]]][start[2]] <- 0
  337.     }
  338.     else if (board[[finish[1]]][finish[2]] == -5) {
  339.         uncheck(start)
  340.         moveAnimation(start, finish, T)
  341.         des <- (finish - start) / 2
  342.         board[[finish[1]]][finish[2]] <- board[[start[1]]][start[2]]
  343.         board[[start[1] + des[1]]][start[2] + des[2]] <- 0
  344.         board[[start[1]]][start[2]] <- 0
  345.         if (board[[finish[1]]][finish[2]] == 1)
  346.             white <<- white - 1
  347.         else
  348.             black <<- black - 1
  349.         if (check(finish)) {
  350.             side <<- side * -1
  351.             oneMore <<- T
  352.             checked <<- finish
  353.         }
  354.         else{
  355.             oneMore <<- F
  356.             checked <<- NA
  357.         }
  358.     }
  359.     side <<- side * -1
  360.     board <<- board
  361. }
  362. check <- function(pos, changeBoard = F){
  363.     f <- F
  364.     aN <- length(checkable) == 0
  365.     s <- side
  366.     board[[pos[1]]][pos[2]] <- 2 * s
  367.     if (changeBoard)
  368.         checked <<- pos
  369.     x <- (pos + c(1, 1) * s)[2]
  370.     y <- (pos + c(1, 1) * s)[1]
  371.    
  372.     if (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0 && aN && !oneMore)
  373.         board[[y]][x] <- 5
  374.     else if (x > 1 && x < 8 && y > 1 && y < 8 &&
  375.              (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  376.              && board[[y + s]][x + s] == 0) {
  377.         board[[y + s]][x + s] <- -5
  378.         f <- T
  379.     }
  380.    
  381.    
  382.     x <- (pos + c(1, -1) * s)[2]
  383.     y <- (pos + c(1, -1) * s)[1]
  384.    
  385.     if (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0 && aN && !oneMore)
  386.         board[[y]][x] <- 5
  387.     else if (x > 1 && x < 8 && y > 1 && y < 8 &&
  388.              (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  389.              && board[[y + s]][x - s] == 0) {
  390.         board[[y + s]][x - s] <- -5
  391.         f <- T
  392.     }
  393.    
  394.     x <- (pos + c(-1, -1) * s)[2]
  395.     y <- (pos + c(-1, -1) * s)[1]
  396.    
  397.     if (x > 1 && x < 8 && y > 1 && y < 8 &&
  398.         (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  399.         && board[[y - s]][x - s] == 0) {
  400.         board[[y - s]][x - s] <- -5
  401.         f <- T
  402.     }
  403.    
  404.     x <- (pos + c(-1, 1) * s)[2]
  405.     y <- (pos + c(-1, 1) * s)[1]
  406.    
  407.     if (x > 1 && x < 8 && y > 1 && y < 8 &&
  408.         (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  409.         && board[[y - s]][x + s] == 0) {
  410.         board[[y - s]][x + s] <- -5
  411.         f <- T
  412.     }
  413.    
  414.     if (changeBoard)
  415.         board <<- board
  416.     return(f)
  417. }
  418. uncheck <- function(pos){
  419.     s <- side
  420.     board[[pos[1]]][pos[2]] <- board[[pos[1]]][pos[2]] - s
  421.     checked <<- NA
  422.     x <- (pos + c(1, 1) * s)[2]
  423.     y <- (pos + c(1, 1) * s)[1]
  424.     if (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 5)
  425.         board[[y]][x] <- 0
  426.     else if (x > 1 && x < 8 && y > 1 && y < 8 &&
  427.              (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  428.              && board[[y + s]][x + s] == -5)
  429.         board[[y + s]][x + s] <- 0
  430.    
  431.     x <- (pos + c(1, -1) * s)[2]
  432.     y <- (pos + c(1, -1) * s)[1]
  433.    
  434.     if (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 5)
  435.         board[[y]][x] <- 0
  436.     else if (x > 1 && x < 8 && y > 1 && y < 8 &&
  437.              (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  438.              && board[[y + s]][x - s] == -5)
  439.         board[[y + s]][x - s] <- 0
  440.    
  441.    
  442.     x <- (pos + c(-1, -1) * s)[2]
  443.     y <- (pos + c(-1, -1) * s)[1]
  444.    
  445.     if (x > 1 && x < 8 && y > 1 && y < 8 &&
  446.         (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  447.         && board[[y - s]][x - s] == -5)
  448.         board[[y - s]][x - s] <- 0
  449.    
  450.     x <- (pos + c(-1, 1) * s)[2]
  451.     y <- (pos + c(-1, 1) * s)[1]
  452.    
  453.     if (x > 1 && x < 8 && y > 1 && y < 8 &&
  454.         (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  455.         && board[[y - s]][x + s] == -5)
  456.         board[[y - s]][x + s] <- 0
  457.    
  458.    
  459.     board <<- board
  460. }
  461. queenCheck <- function(pos, changeBoard = F) {
  462.     board <- queenUncheck(pos, F)
  463.     board[[pos[1]]][pos[2]] <- 4 * side
  464.     directions <- data.frame(c(1, 1), c(1, -1), c(-1, 1), c(-1, -1))
  465.     f <- F
  466.     for (i in 1:4) {
  467.         x <- pos[2] + directions[1, i]
  468.         y <- pos[1] + directions[2, i]
  469.         while (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0) {
  470.             x <- x + directions[1, i]
  471.             y <- y + directions[2, i]
  472.         }
  473.        
  474.        
  475.         if (x > 0 && x < 9 && y > 0 && y < 9 &&
  476.             board[[y]][x] != side && board[[y]][x] != side * 3) {
  477.             x <- x + directions[1, i]
  478.             y <- y + directions[2, i]
  479.             while (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0) {
  480.                 f <- T
  481.                 board[[y]][x] <- -5
  482.                 x <- x + directions[1, i]
  483.                 y <- y + directions[2, i]
  484.             }
  485.         }
  486.     }
  487.     if (!f){
  488.         for (i in 1:4) {
  489.             x <- pos[2] + directions[1, i]
  490.             y <- pos[1] + directions[2, i]
  491.             while (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0) {
  492.                 board[[y]][x] <- 5
  493.                 x <- x + directions[1, i]
  494.                 y <- y + directions[2, i]
  495.             }
  496.         }
  497.     }
  498.     if (changeBoard) {
  499.         board <<- board
  500.         checked <<- pos
  501.         queenIsChecked <<- T
  502.     }
  503.     return(f)
  504. }
  505. queenUncheck <- function(pos, bugFlag = T) {
  506.     board[[pos[1]]][pos[2]] <- 3 * side
  507.     directions <- data.frame(c(1, 1), c(1, -1), c(-1, 1), c(-1, -1))
  508.    
  509.     for (i in 1:4) {
  510.         x <- pos[2] + directions[1, i]
  511.         y <- pos[1] + directions[2, i]
  512.         while (x > 0 && x < 9 && y > 0 && y < 9) {
  513.             if (abs(board[[y]][x]) == 5)
  514.                 board[[y]][x] <- 0
  515.             x <- x + directions[1, i]
  516.             y <- y + directions[2, i]
  517.         }
  518.     }
  519.     if (bugFlag) {
  520.         checked <<- NA
  521.         queenIsChecked <<- F
  522.         board <<- board
  523.     }
  524.     else
  525.         return (board)
  526. }
  527. queenMove <- function(start, finish) {
  528.     if (board[[finish[1]]][finish[2]] == -5) {
  529.         queenUncheck(start)
  530.         moveAnimation(start, finish, T)
  531.         direction <- sign(finish - start)
  532.         x <- start[2] + direction[2]
  533.         y <- start[1] + direction[1]
  534.        
  535.         while (board[[y]][x] == 0) {
  536.             x <- x + direction[2]
  537.             y <- y + direction[1]
  538.         }
  539.         board[[y]][x] <- 0
  540.         board[[finish[1]]][finish[2]] <- board[[start[1]]][start[2]]
  541.         board[[start[1]]][start[2]] <- 0
  542.         if (board[[finish[1]]][finish[2]] == 3)
  543.             white <<- white - 1
  544.         else
  545.             black <<- black - 1
  546.         board <<- board
  547.         if (queenCheck(finish)) {
  548.             side <<- side * -1
  549.             oneMore <<- T
  550.             queenIsChecked <<- T
  551.             checked <<- finish
  552.         }
  553.         else{
  554.             oneMore <<- F
  555.             checked <<- NA
  556.         }
  557.     }
  558.     else {
  559.         queenUncheck(start)
  560.         moveAnimation(start, finish, F)
  561.         board[[finish[1]]][finish[2]] <- board[[start[1]]][start[2]]
  562.         board[[start[1]]][start[2]] <- 0
  563.     }
  564.     side <<- side * -1
  565.     board <<- board
  566.    
  567. }
  568. newGame <- function() {
  569.     board <<- list(
  570.         c(10, 1, 10, 1, 10, 1, 10, 1),
  571.         c(1, 10, 1, 10, 1, 10, 1, 10),
  572.         c(10, 1, 10, 1, 10, 1, 10, 1),
  573.         c(0, 10, 0, 10, 0, 10, 0, 10),
  574.         c(10, 0, 10, 0, 10, 0, 10, 0),
  575.         c(-1, 10, -1, 10, -1, 10, -1, 10),
  576.         c(10, -1, 10, -1, 10, -1, 10, -1),
  577.         c(-1, 10, -1, 10, -1, 10, -1, 10)
  578.     )
  579.     white <<- 12
  580.     black <<- 12
  581.     checkable <<- list()
  582.     checked <- NA
  583.     side <<- -1
  584.     oneMore <<- F
  585.     queenIsChecked <<- F
  586. }
  587. moveAnimation <- function(start, finish, enemy) {
  588.     if (!animationFlag)
  589.         return (0)
  590.     y <- start[1]
  591.     x <- start[2]
  592.     type <- board[[start[1]]][start[2]]
  593.     if (enemy) {
  594.         if (abs(type) == 1) {
  595.             enemyX <- (start + (finish - start) / 2)[2]
  596.             enemyY <- (start + (finish - start) / 2)[1]
  597.         }
  598.         else {
  599.             direction <- sign(finish - start)
  600.             enemyY <- start[1] + direction[1]
  601.             enemyX <- start[2] + direction[2]
  602.            
  603.             while (board[[enemyY]][enemyX] == 0) {
  604.                 enemyX <- enemyX + direction[2]
  605.                 enemyY <- enemyY + direction[1]
  606.             }
  607.         }
  608.        
  609.     }
  610.     board[[y]][x] <<- 0
  611.     Is <- (y - 1) * 10
  612.     Js <- (x - 1) * 10
  613.     x_dir <- sign(finish - start)[2]
  614.     y_dir <- -1 * sign(finish - start)[1]
  615.     for (i in 0:10 * abs(start - finish)) {
  616.         drawBoard()
  617.         if (enemy && i > 5 * abs(start - c(enemyY, enemyX)) * sqrt(2))
  618.             board[[enemyY]][enemyX] <<- 0
  619.        
  620.         if(type == 1)
  621.         {
  622.             rect(-40 + Js, 40 - Is,
  623.                  -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  624.             draw.circle(-35 + Js + i * x_dir, 35 - Is + i * y_dir, 4, col = "#090909", lwd = 2, border = "#252525")
  625.             draw.circle(-35 + Js + i * x_dir, 35 - Is + i * y_dir, 3, col = "#090909", lwd = 2, border = "#252525")
  626.             draw.circle(-35 + Js + i * x_dir, 35 - Is + i * y_dir, 2, col = "#101010", lwd = 2, border = "#252525")
  627.         }
  628.         else if(type == -1)
  629.         {
  630.             rect(-40 + Js, 40 - Is,
  631.                  -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  632.             draw.circle(-35 + Js + i * x_dir, 35 - Is + i * y_dir, 4, col = "#A4A4A4", lwd = 2, border = "#D5D5D5")
  633.             draw.circle(-35 + Js + i * x_dir, 35 - Is + i * y_dir, 3, col = "#A4A4A4", lwd = 2, border = "#D5D5D5")
  634.             draw.circle(-35 + Js + i * x_dir, 35 - Is + i * y_dir, 2, col = "#B9B9B9", lwd = 2, border = "#D5D5D5")
  635.         }
  636.         else if(type == 3)
  637.         {
  638.             rect(-40 + Js, 40 - Is,
  639.                  -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  640.             draw.circle(-35 + Js + i * x_dir, 35 - Is + i * y_dir, 4, col = "#101010", lwd = 2, border = "#303030")
  641.         }
  642.         else if(type == -3)
  643.         {
  644.             rect(-40 + Js, 40 - Is,
  645.                  -30 + Js, 30 - Is, col = "#5B3501", border = "#5B3501")
  646.             draw.circle(-35 + Js + i * x_dir, 35 - Is + i * y_dir, 4, col = "#A9A9A9", lwd = 2, border = "#D9D9D9")
  647.         }
  648.         Sys.sleep(0.05)
  649.     }
  650.     if (enemy)
  651.         board[[enemyY]][enemyX] <<- side * -1
  652.     board[[y]][x] <<- type
  653. }
  654. action <- function(pos){
  655.     x <- pos[2]
  656.     y <- pos[1]
  657.     if (board[[y]][x] == side && is.na(checked) && isCheckable(pos))
  658.         check(pos, T)
  659.     else if (board[[y]][x] == side * 3 && is.na(checked) && isCheckable(pos))
  660.         queenCheck(pos, T)
  661.     else if (abs(board[[y]][x]) == 5) {
  662.         makeBackup()
  663.         if (queenIsChecked)
  664.             queenMove(checked, pos)
  665.         else
  666.             move(checked, pos)
  667.     }
  668.     else if (!is.na(checked) && !oneMore) {
  669.         if (queenIsChecked)
  670.             queenUncheck(checked)
  671.         else
  672.             uncheck(checked)
  673.     }
  674.     else if (board[[pos[1]]][pos[2]] != 0 && board[[pos[1]]][pos[2]] != 10){
  675.         if (oneMore)
  676.             mes <<- "You must eat!"
  677.         else if (sign(board[[pos[1]]][pos[2]]) != side)
  678.             mes <<- "Wrong side!"
  679.         else if (length(checkable) != 0)
  680.             mes <<- "There are checker, which able to eat!"
  681.     }
  682. }
  683.  
  684.  
  685. AI_Core <- function(board, depth, side) {
  686.     if (depth < 2) # influence of depth value on difficulty: 2 = easy, 4 = medium, 6 = hard, 8+ = impossible (to compute)
  687.     {
  688.         depth <- 2
  689.     }
  690.     else if (depth%%2 != 0)
  691.     {
  692.         depth <- depth - 1
  693.     }
  694.     move <- AI_Tree(board, depth, side)[[2]] # dont forget about checking if there is a possibility to make one more move
  695.     return(move)
  696. }
  697. AI_Tree <- function(board, depth, side) {
  698.     if (depth%%2 == 0)
  699.     {
  700.         bestvalue <- -1000
  701.         Board_Array <- AI_Generate(board, side)
  702.     }
  703.     else
  704.     {
  705.         bestvalue <- 1000
  706.         Board_Array <- AI_Generate(board, side*(-1))
  707.     }
  708.     n <- length(Board_Array)
  709.    
  710.     Layer <- list()
  711.     for (i in 1:n)
  712.     {
  713.         Layer[[i]] <- list(value = NULL, board = Board_Array[[i]])
  714.         if (depth > 1)
  715.         {
  716.             t <- AI_Evaluate(Layer[[i]]$board, side)
  717.             if ((t == 1000)||(t == -1000))
  718.                 Layer[[i]]$value <- t
  719.             else
  720.                 Layer[[i]]$value <- AI_Tree(Layer[[i]]$board, depth - 1, side)[[1]]
  721.         }
  722.         else
  723.         {
  724.             Layer[[i]]$value <- AI_Evaluate(Layer[[i]]$board, side)
  725.         }
  726.         if (depth%%2 == 0)
  727.         {
  728.             if (Layer[[i]]$value >= bestvalue)
  729.             {
  730.                 bestvalue <- Layer[[i]]$value
  731.                 bestboard <- Layer[[i]]$board
  732.             }
  733.         }
  734.         else
  735.         {
  736.             if (Layer[[i]]$value <= bestvalue)
  737.             {
  738.                 bestvalue <- Layer[[i]]$value
  739.                 bestboard <- Layer[[i]]$board
  740.             }
  741.         }
  742.     }
  743.    
  744.     return(list(bestvalue, bestboard))
  745. } # depth value should be divisible by 2
  746. generateEatMoves <- function(pos, board, s) {
  747.     possMoves <- list()
  748.     if (board[[pos[1]]][pos[2]] == s) {
  749.         x <- (pos + c(1, 1) * s)[2]
  750.         y <- (pos + c(1, 1) * s)[1]
  751.        
  752.         if (x > 1 && x < 8 && y > 1 && y < 8 &&
  753.             (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  754.             && board[[y + s]][x + s] == 0) {
  755.            
  756.             tempMoves <- generateEatMoves(c(y + s, x + s), replace(pos, c(y + s, x + s), board, T), s)
  757.             if (length(tempMoves) == 0)
  758.                 possMoves[[length(possMoves) + 1]] <- replace(pos, c(y + s, x + s), board, T)
  759.             else {
  760.                 for (i in tempMoves)
  761.                     possMoves[[length(possMoves) + 1]] <- i
  762.             }
  763.         }
  764.        
  765.        
  766.         x <- (pos + c(1, -1) * s)[2]
  767.         y <- (pos + c(1, -1) * s)[1]
  768.        
  769.         if (x > 1 && x < 8 && y > 1 && y < 8 &&
  770.             (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  771.             && board[[y + s]][x - s] == 0) {
  772.            
  773.             tempMoves <- generateEatMoves(c(y + s, x - s), replace(pos, c(y + s, x - s), board, T), s)
  774.             if (length(tempMoves) == 0)
  775.                 possMoves[[length(possMoves) + 1]] <- replace(pos, c(y + s, x - s), board, T)
  776.             else {
  777.                 for (i in tempMoves)
  778.                     possMoves[[length(possMoves) + 1]] <- i
  779.             }
  780.         }
  781.        
  782.         x <- (pos + c(-1, -1) * s)[2]
  783.         y <- (pos + c(-1, -1) * s)[1]
  784.        
  785.         if (x > 1 && x < 8 && y > 1 && y < 8 &&
  786.             (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  787.             && board[[y - s]][x - s] == 0) {
  788.            
  789.             tempMoves <- generateEatMoves(c(y - s, x - s), replace(pos, c(y - s, x - s), board, T), s)
  790.             if (length(tempMoves) == 0)
  791.                 possMoves[[length(possMoves) + 1]] <- replace(pos, c(y - s, x - s), board, T)
  792.             else {
  793.                 for (i in tempMoves)
  794.                     possMoves[[length(possMoves) + 1]] <- i
  795.             }
  796.         }
  797.        
  798.         x <- (pos + c(-1, 1) * s)[2]
  799.         y <- (pos + c(-1, 1) * s)[1]
  800.        
  801.         if (x > 1 && x < 8 && y > 1 && y < 8 &&
  802.             (board[[y]][x] == -1 * s || board[[y]][x] == -3 * s)
  803.             && board[[y - s]][x + s] == 0) {
  804.            
  805.             tempMoves <- generateEatMoves(c(y - s, x + s), replace(pos, c(y - s, x + s), board, T), s)
  806.             if (length(tempMoves) == 0)
  807.                 possMoves[[length(possMoves) + 1]] <- replace(pos, c(y - s, x + s), board, T)
  808.             else {
  809.                 for (i in tempMoves)
  810.                     possMoves[[length(possMoves) + 1]] <- i
  811.             }
  812.         }
  813.     }
  814.     else if (board[[pos[1]]][pos[2]] == s * 3) {
  815.         directions <- data.frame(c(1, 1), c(1, -1), c(-1, 1), c(-1, -1))
  816.         for (i in 1:4) {
  817.             x <- pos[2] + directions[1, i]
  818.             y <- pos[1] + directions[2, i]
  819.             while (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0) {
  820.                 x <- x + directions[1, i]
  821.                 y <- y + directions[2, i]
  822.             }
  823.            
  824.             if (x > 0 && x < 9 && y > 0 && y < 9 &&
  825.                 board[[y]][x] != s && board[[y]][x] != s * 3) {
  826.                 x <- x + directions[1, i]
  827.                 y <- y + directions[2, i]
  828.                 while (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0) {
  829.                     tempMoves <- generateEatMoves(c(y, x), replace(pos, c(y, x), board, T), s)
  830.                     if (length(tempMoves) == 0)
  831.                         possMoves[[length(possMoves) + 1]] <- replace(pos, c(y, x), board, T)
  832.                     else {
  833.                         for (b in tempMoves)
  834.                             possMoves[[length(possMoves) + 1]] <- b
  835.                     }
  836.                     x <- x + directions[1, i]
  837.                     y <- y + directions[2, i]
  838.                 }
  839.             }
  840.            
  841.         }
  842.     }
  843.    
  844.     return(possMoves)
  845. }
  846. generateMoves <- function(pos, board, s) {
  847.     possMoves <- list()
  848.     if (board[[pos[1]]][pos[2]] == s) {
  849.         x <- (pos + c(1, 1) * s)[2]
  850.         y <- (pos + c(1, 1) * s)[1]
  851.        
  852.         if (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0)
  853.             possMoves[[length(possMoves) + 1]] <- replace(pos, c(y, x), board, F)
  854.        
  855.        
  856.         x <- (pos + c(1, -1) * s)[2]
  857.         y <- (pos + c(1, -1) * s)[1]
  858.        
  859.         if (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0)
  860.             possMoves[[length(possMoves) + 1]] <- replace(pos, c(y, x), board, F)
  861.     }
  862.     else if (board[[pos[1]]][pos[2]] == 3 * s) {
  863.         directions <- data.frame(c(1, 1), c(1, -1), c(-1, 1), c(-1, -1))
  864.         for (i in 1:4) {
  865.             x <- pos[2] + directions[1, i]
  866.             y <- pos[1] + directions[2, i]
  867.             while (x > 0 && x < 9 && y > 0 && y < 9 && board[[y]][x] == 0) {
  868.                 possMoves[[length(possMoves) + 1]] <- replace(pos, c(y, x), board, F)
  869.                 x <- x + directions[1, i]
  870.                 y <- y + directions[2, i]
  871.             }
  872.         }
  873.     }
  874.    
  875.     return(possMoves)
  876. }
  877. AI_Generate <- function(board, side, checkStuck = F) {
  878.     moves <- list()
  879.     for (y in 1:8) {
  880.         for (x in 1:8) {
  881.             if (board[[y]][x] == side || board[[y]][x] == 3 * side) {
  882.                 tempMoves <- generateEatMoves(c(y, x), board, side)
  883.                 if (checkStuck && length(tempMoves) > 0)
  884.                     return (F)
  885.                 for (m in tempMoves)
  886.                     moves[[length(moves) + 1]] <- m
  887.             }
  888.         }
  889.     }
  890.     if (length(moves) == 0) {
  891.         for (y in 1:8) {
  892.             for (x in 1:8) {
  893.                 if (board[[y]][x] == side || board[[y]][x] == 3 * side) {
  894.                     tempMoves <- generateMoves(c(y, x), board, side)
  895.                     if (checkStuck && length(tempMoves) > 0)
  896.                         return (F)
  897.                     for (m in tempMoves)
  898.                         moves[[length(moves) + 1]] <- m
  899.                 }
  900.             }
  901.         }
  902.     }
  903.     if (checkStuck)
  904.         return (T)
  905.     return (moves)
  906. }
  907. replace <- function(start, finish, board, eat) {
  908.     if (eat) {
  909.         des <- sign(finish - start)
  910.         x <- start[2] + des[2]
  911.         y <- start[1] + des[1]
  912.         while (board[[y]][x] == 0) {
  913.             x <- x + des[2]
  914.             y <- y + des[1]
  915.         }
  916.         board[[y]][x] <- 0
  917.         board[[finish[1]]][finish[2]] <- board[[start[1]]][start[2]]
  918.         board[[start[1]]][start[2]] <- 0
  919.     }
  920.     else {
  921.         board[[finish[1]]][finish[2]] <- board[[start[1]]][start[2]]
  922.         board[[start[1]]][start[2]] <- 0
  923.     }
  924.     if ((finish[1] == 1 && sign(board[[finish[1]]][finish[2]]) == -1)
  925.         || (finish[1] == 8 && sign(board[[finish[1]]][finish[2]]) == 1))
  926.         board[[finish[1]]][finish[2]] <- sign(board[[finish[1]]][finish[2]]) * 3
  927.     return (board)
  928. }
  929. AI_Evaluate <- function(board, side) {
  930.     blacks <- 0
  931.     whites <- 0
  932.     blackQs <- 0
  933.     whiteQs <- 0
  934.     blackDist <- 0
  935.     whiteDist <- 0
  936.    
  937.     for (i in 1:8)
  938.     {
  939.         for (j in 1:8)
  940.         {
  941.             if (board[[i]][j] == 1)
  942.             {
  943.                 blacks <- blacks + 1
  944.                 if ((i > 3)&&(i < 8))
  945.                 {
  946.                     blackDist <- blackDist + i*0.1
  947.                 }
  948.             }
  949.             if (board[[i]][j] == -1)
  950.             {
  951.                 whites <- whites + 1
  952.                 if ((i < 6)&&(i > 1))
  953.                 {
  954.                     whiteDist <- whiteDist + (9-i)*0.1
  955.                 }
  956.             }
  957.             if (board[[i]][j] == 3)
  958.             {
  959.                 blackQs <- blackQs + 1
  960.             }
  961.             if (board[[i]][j] == -3)
  962.             {
  963.                 whiteQs <- whiteQs + 1
  964.             }
  965.         }
  966.     }
  967.     if (((blacks == 0)&&(blackQs == 0)) || AI_Generate(board, 1, T))
  968.     {
  969.         return(-1000*side)
  970.     }
  971.     else if (((whites == 0)&&(whiteQs == 0)) || AI_Generate(board, -1, T))
  972.     {
  973.         return(1000*side)
  974.     }
  975.     value <- (blacks - whites + blackDist - whiteDist + blackQs*10 - whiteQs*10)*side
  976.     return(value)
  977. }
  978.  
  979. AI_flag <- T
  980. AI_side <- 1
  981. level <- 2 * 1
  982. checkable <- list()
  983. checked <- NA
  984. side <- -1
  985. oneMore <- F
  986. queenIsChecked <- F
  987. mes <- ''
  988. animationFlag <- F
  989. while(white != 0 && black != 0) {
  990.     if (side == -1 * AI_side || !AI_flag) {
  991.         if (is.na(checked) && AI_Generate(board, side, T)) {
  992.             if (AI_side == -1 * 1)
  993.                 mes <- "White won"
  994.             else
  995.                 mes <- "Black won"
  996.             break
  997.         }
  998.         if (oneMore) {
  999.             if (queenIsChecked)
  1000.                 queenCheck(checked, T)
  1001.             else
  1002.                 check(checked, T)
  1003.         }
  1004.         checkBoard()
  1005.        
  1006.         drawBoard()
  1007.         mes <- ''
  1008.         coor <- getCoordinates()
  1009.         if (is.null(coor))
  1010.             next
  1011.         else if (coor == 11) {
  1012.             backup()
  1013.             next
  1014.         }
  1015.         else if (coor == 9)
  1016.             break
  1017.         else if (coor == 0){
  1018.             newGame()
  1019.             next
  1020.         }
  1021.         action(coor)
  1022.     }
  1023.     else {
  1024.         mes <- "AI is thinking..."
  1025.         drawBoard()
  1026.         mes <- ''
  1027.         if (AI_Generate(board, AI_side, T)) {
  1028.             if (AI_side == 1)
  1029.                 mes <- "White won"
  1030.             else
  1031.                 mes <- "Black won"
  1032.             break
  1033.         }
  1034.         board <- AI_Core(board, level, AI_side)
  1035.         if (AI_side == 1)
  1036.             white <- checkersNum(-1 * AI_side)
  1037.         else
  1038.             black <- checkersNum(-1 * AI_side)
  1039.         side <- side * -1
  1040.     }
  1041. }
  1042.  
  1043. if (black == 0)
  1044.     mes <- "White won"
  1045. if (white == 0)
  1046.     mes <- "Black won"
  1047.  
  1048. drawBoard()
  1049. print('end')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement