Advertisement
xerpi

IA tests

Jul 10th, 2011
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1. IA = {}
  2.  
  3. function IA.random(mode)
  4.     if mode == "3D" then
  5.         y = math.random(1,board.height)
  6.         x = math.random(1,board.width)
  7.         d = math.random(1,board.depth)
  8.         while true do
  9.             y = math.random(1,board.height)
  10.             x = math.random(1,board.width)
  11.             d = math.random(1,board.depth)
  12.             if board.table[d][y][x] == "" then
  13.                 board.table[d][y][x] = board.player[board.turn].st
  14.                 return true;
  15.             end
  16.         end
  17.     elseif mode == "C4" then
  18.         y = board.height
  19.         d = math.random(1,board.depth)
  20.         x = math.random(1,board.width)
  21.         while true do
  22.             d = math.random(1,board.depth)
  23.             x = math.random(1,board.width)
  24.             for i = board.height, 1, -1 do
  25.                 if board.table[d][i][x] == "" then
  26.                     board.table[d][i][x] = board.player[board.turn].st
  27.                     return true;
  28.                 end
  29.             end
  30.         end
  31.     end
  32. end
  33.  
  34. function IA.impossible(mode)
  35. local cont = {horizontal = 1,vertical = 1,dia_up = 1,dia_down = 1,order={}};
  36.         local t = "";
  37.         for n = board.row-1, 1, -1 do
  38.             for d = 1, board.depth do
  39.                 for y = 1,board.height do
  40.                     for x = 1, board.width do
  41.                         cont = {horizontal = 1,vertical = 1,dia_up = 1,dia_down = 1,order={}};
  42.                         t = board.table[d][y][x]
  43.                         if t != "" then --The current chip is not empty.
  44.                             for r = 1, n do
  45.                                 if board.inside(x+r,y) and board.table[d][y][x+r] == t then cont.horizontal = cont.horizontal +1 end    --Horizontal
  46.                                 if board.inside(x,y+r) and board.table[d][y+r][x] == t then cont.vertical = cont.vertical +1 end    --Vertical
  47.                                 if board.inside(x+r,y-r) and board.table[d][y-r][x+r] == t then cont.dia_up = cont.dia_up +1 end    --Diagonal up
  48.                                 if board.inside(x+r,y+r) and board.table[d][y+r][x+r] == t then cont.dia_down = cont.dia_down +1 end    --Diagonal down            
  49.                             end
  50.                            
  51.                             for i = 1, 4 do --Sort the cont.order table by size
  52.                                 if cont.horizontal > cont.vertical and cont.horizontal > cont.dia_up and cont.horizontal > cont.dia_down then cont.order[i] = "hor" end
  53.                                 if cont.vertical > cont.horizontal and cont.vertical > cont.dia_up and cont.vertical > cont.dia_down then cont.order[i] = "ver" end
  54.                                 if cont.dia_up > cont.vertical and cont.dia_up > cont.horizontal and cont.dia_up > cont.dia_down then cont.order[i] = "dia_up" end
  55.                                 if cont.dia_down > cont.vertical and cont.dia_down > cont.dia_up and cont.dia_down > cont.horizontal then cont.order[i] = "dia_down" end                           
  56.                             end
  57.                            
  58.                             for i = 1, 4 do --Do...
  59.                                     if cont.order[i] == "hor" then
  60.                                                 if cont.horizontal >= n then --Horizontal
  61.                                                         if board.inside(x+n,y) and board.table[d][y][x+n] == "" then
  62.                                                             board.table[d][y][x+n] = board.player[board.turn].st
  63.                                                             return true
  64.                                                         end
  65.                                                         if board.inside(x-1,y) and board.table[d][y][x-1] == "" then
  66.                                                             board.table[d][y][x-1] = board.player[board.turn].st
  67.                                                             return true
  68.                                                         end
  69.                                                 end                                
  70.                                     elseif cont.order[i] == "ver" then
  71.                                                 if cont.vertical >= n then --Vertical
  72.                                                             if board.inside(x,y+n) and board.table[d][y+n][x] == "" then
  73.                                                                 board.table[d][y+n][x] = board.player[board.turn].st
  74.                                                                 return true
  75.                                                             end
  76.                                                             if board.inside(x,y-1) and board.table[d][y-1][x] == "" then
  77.                                                                 board.table[d][y-1][x] = board.player[board.turn].st
  78.                                                                 return true
  79.                                                             end
  80.                                                 end                                
  81.                                     elseif cont.order[i] == "dia_up" then
  82.                                                 if cont.dia_up >= n then --Diagonal up
  83.                                                         if board.inside(x+n,y-n) and board.table[d][y-n][x+n] == "" then
  84.                                                             board.table[d][y-n][x+n] = board.player[board.turn].st
  85.                                                             return true
  86.                                                         end
  87.                                                         if board.inside(x-1,y+1) and board.table[d][y+1][x-1] == "" then
  88.                                                             board.table[d][y+1][x-1] = board.player[board.turn].st
  89.                                                             return true
  90.                                                         end
  91.                                                 end                            
  92.                                     elseif cont.order[i] == "dia_down" then                        
  93.                                                 if cont.dia_down >= n then  --Diagonal down
  94.                                                         if board.inside(x+n,y+n) and board.table[d][y+n][x+n] == "" then
  95.                                                             board.table[d][y+n][x+n] = board.player[board.turn].st
  96.                                                             return true
  97.                                                         end
  98.                                                         if board.inside(x-1,y-1) and board.table[d][y-1][x-1] == "" then
  99.                                                             board.table[d][y-1][x-1] = board.player[board.turn].st
  100.                                                             return true
  101.                                                         end                                                    
  102.                                                 end                            
  103.                                     end
  104.                             end                    
  105.                         end
  106.                     end
  107.                 end
  108.             end
  109.         end
  110. end
  111.  
  112. function IA.play(str,mode)
  113. local x;
  114. local y;
  115. local d;
  116. local number = math.random(1,100)
  117. if str == "very easy" then  -- Easy mode, everything is random
  118.     IA.random(mode)
  119. elseif str == "easy" then
  120.     if number <20 then IA.impossible(mode) else IA.random(mode) end --20% of times is impossible
  121. elseif str == "medium" then
  122.     if number <40 then IA.impossible(mode) else IA.random(mode) end --40% of times is impossible
  123. elseif str == "hard" then
  124.     if number <70 then IA.impossible(mode) else IA.random(mode) end --70% of times is impossible
  125. elseif str == "extreme" then
  126.     if number <90 then IA.impossible(mode) else IA.random(mode) end --90% of times is impossible
  127. elseif str == "impossible" then
  128.     IA.impossible(mode) --100% of times is impossible
  129. end
  130. return false;
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement