Advertisement
xerpi

N en raya alpha

Jun 27th, 2011
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.92 KB | None | 0 0
  1. functions={}
  2.  
  3. match={}
  4. match[1] = {n = 3,turn = 1,x = 100,y = 50, w = 20, h = 20, sel = {image = image.create(20, 20, color.new(255,255,255,125) ), x = 1, y = 1},J={}}
  5. match[1].table = {
  6. {"","",""},
  7. {"","",""},
  8. {"","",""}
  9. }
  10. match[1].J[1] = {string = "x", image = image.create(match[1].w, match[1].h, color.new(255,0,0) ) }
  11. match[1].J[2] = {string = "o", image = image.create(match[1].w, match[1].h, color.new(0,0,255) ) }
  12.  
  13. function functions.next_turn(array)
  14.     array.turn = array.turn + 1 if array.turn > #array.J then array.turn = 1 end
  15. end
  16.  
  17. function functions.draw_match(array)
  18.     for y = 1, #array.table do
  19.         for x = 1, #array.table[y] do
  20.             for i = 1, #array.J do
  21.                 if array.table[y][x] == array.J[i].string then
  22.                     array.J[i].image:blit(array.x + (x-1)*array.w, array.y + (y-1)*array.h)
  23.                 end
  24.             end
  25.         end
  26.     end
  27. end
  28.  
  29. function functions.draw_match_contorn(array,colore)
  30.     draw.rect( array.x, array.y , #array.table * array.w, #array.table[1] * array.h, ( array.colore or color.new(255,255,255) ) )
  31. end
  32.  
  33. function functions.move_selector(array)
  34.     if controls.press("up") then
  35.         array.sel.y = array.sel.y - 1; if array.sel.y < 1 then array.sel.y = #array.table end
  36.     end
  37.     if controls.press("down") then
  38.         array.sel.y = array.sel.y + 1; if array.sel.y > #array.table then array.sel.y = 1 end
  39.     end
  40.     if controls.press("left") then
  41.         array.sel.x = array.sel.x - 1; if array.sel.x < 1 then array.sel.x = #array.table[1] end
  42.     end
  43.     if controls.press("right") then
  44.         array.sel.x = array.sel.x + 1; if array.sel.x > #array.table[1] then array.sel.x = 1 end
  45.     end
  46. end
  47.  
  48. function functions.print_selector(array)
  49.     array.sel.image:blit( array.x + (array.sel.x - 1)*array.w, array.y + (array.sel.y - 1)*array.h)
  50. end
  51.  
  52. function fb(array,x,y)
  53.     if  x <= #array.table[1] and y <= #array.table
  54.     and x >= 1 and y >= 1 then return true else return false end
  55. end
  56.  
  57.  
  58.  
  59. function functions.check(array)
  60.     local cont = {}
  61.     cont.hor_right = 1;
  62.     cont.hor_left = 1;
  63.     cont.ver_up = 1;
  64.     cont.ver_down = 1;
  65.     cont.dia_up_inc = 1;
  66.     cont.dia_up_dec = 1;
  67.     cont.dia_down_inc = 1;
  68.     cont.dia_down_dec = 1;
  69.     local s = ""
  70.     for y = 1 , #array.table do
  71.         for x = 1, #array.table[y] do
  72.             if array.table[y][x] != "" then
  73.                 s = array.table[y][x]
  74.                 for i = 1, array.n do
  75.                     if fb(array,x+i,y) and array.table[y][x+i] == s then cont.hor_right = cont.hor_right+1 end
  76.                     if fb(array,x,y+i) and array.table[y+i][x] == s then cont.ver_down = cont.ver_down+1 end
  77.                     if fb(array,x-i,y) and array.table[y][x-i] == s then cont.hor_left = cont.hor_left+1 end
  78.                     if fb(array,x,y-i) and array.table[y-i][x] == s then cont.ver_up = cont.ver_up+1 end
  79.                     if fb(array,x+i,y-i) and array.table[y-i][x+i] == s then cont.dia_up_dec = cont.dia_up_dec + 1 end
  80.                     if fb(array,x-i,y-i) and array.table[y-i][x-i] == s then cont.dia_up_inc = cont.dia_up_inc + 1 end
  81.                     if fb(array,x+i,y+i) and array.table[y+i][x+i] == s then cont.dia_down_inc = cont.dia_down_inc + 1 end
  82.                     if fb(array,x-i,y+i) and array.table[y+i][x-i] == s then cont.dia_down_dec = cont.dia_down_dec+1 end                   
  83.                 end            
  84.                 if cont.hor_right >= array.n then return s end cont.hor_right = 1;
  85.                 if cont.hor_left >= array.n then return s end cont.hor_left = 1;                   
  86.                 if cont.ver_up >= array.n then return s end cont.ver_up = 1;
  87.                 if cont.ver_down >= array.n then return s end cont.ver_down = 1;                   
  88.                 if cont.dia_up_inc >= array.n then return s end cont.dia_up_inc = 1;               
  89.                 if cont.dia_down_inc >= array.n then return s end cont.dia_down_inc = 1;   
  90.                 if cont.dia_up_dec >= array.n then return s end cont.dia_up_dec = 1;               
  91.                 if cont.dia_down_dec >= array.n then return s end cont.dia_down_dec = 1;                   
  92.             end
  93.         end
  94.     end
  95. end
  96.  
  97.  
  98. function functions.check_winner(s,array)
  99. for i = 1, #array.J do
  100.     if array.J[i].string == s then return i end
  101. end
  102. return false
  103. end
  104.  
  105. function functions.fill_match(array)
  106.     local winner = functions.check(array)
  107.     if  winner != nil then
  108.         os.message("Player "..functions.check_winner(winner,array).." wins.")
  109.         reset()
  110.     end
  111.     if controls.press("cross") then
  112.         if array.table[array.sel.y][array.sel.x] == "" then
  113.             array.table[array.sel.y][array.sel.x] = array.J[array.turn].string
  114.             functions.next_turn(array)
  115.         end            
  116.     end
  117. end
  118.  
  119. ------
  120. function reset()
  121.     match[1].table = {
  122.         {"","",""},
  123.         {"","",""},
  124.         {"","",""}
  125.         }
  126. end
  127. while true do
  128. controls.read()
  129.  
  130. functions.draw_match(match[1])
  131. functions.draw_match_contorn(match[1], color.new(0,255,0))
  132. functions.move_selector(match[1])
  133. functions.print_selector(match[1])
  134. ----------
  135. match[1].J[match[1].turn].image:blit(5,5)
  136.  
  137.  
  138. if controls.r() then
  139.     reset()
  140. end
  141.  
  142. if math.abs(controls.analogx()) > 40 then match[1].x = match[1].x + controls.analogx()/40 end
  143. if math.abs(controls.analogy()) > 40 then match[1].y = match[1].y + controls.analogy()/40 end
  144. if controls.start() then match[1].x = 100 match[1].y = 50 end
  145. if controls.select() then a() end
  146. screen.flip()
  147. functions.fill_match(match[1])
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement