Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- functions={}
- match={}
- 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={}}
- match[1].table = {
- {"","",""},
- {"","",""},
- {"","",""}
- }
- match[1].J[1] = {string = "x", image = image.create(match[1].w, match[1].h, color.new(255,0,0) ) }
- match[1].J[2] = {string = "o", image = image.create(match[1].w, match[1].h, color.new(0,0,255) ) }
- function functions.next_turn(array)
- array.turn = array.turn + 1 if array.turn > #array.J then array.turn = 1 end
- end
- function functions.draw_match(array)
- for y = 1, #array.table do
- for x = 1, #array.table[y] do
- for i = 1, #array.J do
- if array.table[y][x] == array.J[i].string then
- array.J[i].image:blit(array.x + (x-1)*array.w, array.y + (y-1)*array.h)
- end
- end
- end
- end
- end
- function functions.draw_match_contorn(array,colore)
- draw.rect( array.x, array.y , #array.table * array.w, #array.table[1] * array.h, ( array.colore or color.new(255,255,255) ) )
- end
- function functions.move_selector(array)
- if controls.press("up") then
- array.sel.y = array.sel.y - 1; if array.sel.y < 1 then array.sel.y = #array.table end
- end
- if controls.press("down") then
- array.sel.y = array.sel.y + 1; if array.sel.y > #array.table then array.sel.y = 1 end
- end
- if controls.press("left") then
- array.sel.x = array.sel.x - 1; if array.sel.x < 1 then array.sel.x = #array.table[1] end
- end
- if controls.press("right") then
- array.sel.x = array.sel.x + 1; if array.sel.x > #array.table[1] then array.sel.x = 1 end
- end
- end
- function functions.print_selector(array)
- array.sel.image:blit( array.x + (array.sel.x - 1)*array.w, array.y + (array.sel.y - 1)*array.h)
- end
- function fb(array,x,y)
- if x <= #array.table[1] and y <= #array.table
- and x >= 1 and y >= 1 then return true else return false end
- end
- function functions.check(array)
- local cont = {}
- cont.hor_right = 1;
- cont.hor_left = 1;
- cont.ver_up = 1;
- cont.ver_down = 1;
- cont.dia_up_inc = 1;
- cont.dia_up_dec = 1;
- cont.dia_down_inc = 1;
- cont.dia_down_dec = 1;
- local s = ""
- local loop_ver = true;
- local winner;
- --hor & vert
- for y = 1 , #array.table do
- for x = 1, #array.table[y] do
- if array.table[y][x] != "" then
- s = array.table[y][x]
- --horizontal
- for i = 1, array.n do
- if fb(array,x+i,y) and array.table[y][x+i] == s then cont.hor_right = cont.hor_right+1 end
- if fb(array,x,y+i) and array.table[y+i][x] == s then cont.ver_down = cont.ver_down+1 end
- if fb(array,x-i,y) and array.table[y][x-i] == s then cont.hor_left = cont.hor_left+1 end
- if fb(array,x,y-i) and array.table[y-i][x] == s then cont.ver_up = cont.ver_up+1 end
- 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
- 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
- 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
- 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
- end
- if cont.hor_right >= array.n then return s end cont.hor_right = 1;
- if cont.hor_left >= array.n then return s end cont.hor_left = 1;
- if cont.ver_up >= array.n then return s end cont.ver_up = 1;
- if cont.ver_down >= array.n then return s end cont.ver_down = 1;
- if cont.dia_up_inc >= array.n then return s end cont.dia_up_inc = 1;
- if cont.dia_down_inc >= array.n then return s end cont.dia_down_inc = 1;
- if cont.dia_up_dec >= array.n then return s end cont.dia_up_dec = 1;
- if cont.dia_down_dec >= array.n then return s end cont.dia_down_dec = 1;
- end
- end
- end
- end
- function functions.check_winner(s,array)
- for i = 1, #array.J do
- if array.J[i].string == s then return i end
- end
- return false
- end
- function functions.fill_match(array)
- local winner, bool;
- if controls.press("cross") then
- if array.table[array.sel.y][array.sel.x] == "" then
- array.table[array.sel.y][array.sel.x] = array.J[array.turn].string
- functions.next_turn(array)
- end
- winner = functions.check(match[1])
- if winner != nil then
- os.message("Player "..functions.check_winner(winner,match[1]).." wins.")
- reset()
- end
- end
- end
- ------
- function reset()
- match[1].table = {
- {"","",""},
- {"","",""},
- {"","",""}
- }
- end
- while true do
- controls.read()
- functions.fill_match(match[1])
- functions.draw_match(match[1])
- functions.draw_match_contorn(match[1], color.new(0,255,0))
- functions.move_selector(match[1])
- functions.print_selector(match[1])
- ----------
- match[1].J[match[1].turn].image:blit(5,5)
- if controls.r() then
- reset()
- end
- if math.abs(controls.analogx()) > 40 then match[1].x = match[1].x + controls.analogx()/40 end
- if math.abs(controls.analogy()) > 40 then match[1].y = match[1].y + controls.analogy()/40 end
- if controls.start() then match[1].x = 100 match[1].y = 50 end
- if controls.select() then a() end
- screen.flip()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement