Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- functions={}
- n=2
- scenery={}
- scenery[1] = {mode = "2D",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={}}
- scenery[1].table = {
- {"","",""},
- {"","",""},
- {"","",""}
- }
- scenery[1].J[1] = {string = "x", image = image.create(scenery[1].w, scenery[1].h, color.new(255,0,0) ) }
- scenery[1].J[2] = {string = "o", image = image.create(scenery[1].w, scenery[1].h, color.new(0,0,255) ) }
- -----------------------------------------------------------------------
- scenery[2] = {mode = "3D",difx=0,dify=0,n = 3,turn = 1,ang = 0,pos={x = 1,y = -6,z=-43},rot={x=33,y=0,z=0}, w = 10, h = 1.64, sel = {object = model.load("models/selec.obj"), x = 1, y = 1},J={}}
- scenery[2].table={
- {"o","x",""},
- {"x","o","o"},
- {"o","o","x"}
- }
- scenery[2].pos.x = 0- scenery[2].w-scenery[2].difx
- scenery[2].J[1] = {string = "x", object = model.load("models/cross.obj") }
- scenery[2].J[2] = {string = "o", object = model.load("models/circle.obj") }
- function functions.draw_scenery(array)
- for y = 1, #array.table do
- for x = 1, #array.table[y] do
- for i = 1, #array.J do
- if array.mode == "2D" then
- 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
- elseif array.mode == "3D" then
- if array.table[y][x] == array.J[i].string then
- array.J[i].object:position(array.pos.x +((x-1)*array.w +(x-1)*array.difx), array.pos.y +math.sin(math.rad(-array.rot.x))*((y-1)*array.w + (y-1)*array.dify),array.pos.z + math.cos(math.rad(-array.rot.x))*((y-1)*array.w+ (y-1)*array.dify))
- array.J[i].object:rotation(math.rad(array.rot.x), math.rad(array.rot.y) ,math.rad(array.rot.z))
- array.J[i].object:blit()
- end
- end
- end
- end
- end
- end
- function functions.draw_scenery_contorn(array,colore)
- if array.mode == "2D" then
- draw.rect( array.x, array.y , #array.table * array.w, #array.table[1] * array.h, ( array.colore or color.new(255,255,255) ) )
- elseif array.mode == "3D" then
- draw.rect(array.pos.x, array.pos.y, array.pos.y +math.sin(math.rad(-array.rot.x))*((#array.table-1)*array.w + (#array.table-1)*array.dify) , array.pos.z + math.cos(math.rad(-array.rot.x))*((#array.table[1]-1)*array.w+ (#array.table[1]-1)*array.dify), color.new(255,0,0) )
- end
- end
- function functions.print_selector(array)
- if array.mode == "2D" then
- array.sel.image:blit( array.x + (array.sel.x - 1)*array.w, array.y + (array.sel.y - 1)*array.h)
- elseif array.mode == "3D" then
- array.sel.object:position(array.pos.x +((array.sel.x-1)*array.w +(array.sel.x-1)*array.difx), array.pos.y +math.sin(math.rad(-array.rot.x))*((array.sel.y-1)*array.w + (array.sel.y-1)*array.dify),array.pos.z + math.cos(math.rad(-array.rot.x))*((array.sel.y-1)*array.w+ (array.sel.y-1)*array.dify))
- array.sel.object:rotation(math.rad(array.rot.x), math.rad(array.rot.y) ,math.rad(array.rot.z))
- array.sel.object:blit()
- end
- end
- function functions.next_turn(array)
- array.turn = array.turn + 1 if array.turn > #array.J then array.turn = 1 end
- 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 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 = {hor_right = 1,hor_left = 1,ver_up = 1,ver_down = 1,dia_up_inc = 1,dia_up_dec = 1,dia_down_inc = 1,dia_down_dec = 1};
- local completed = true;
- local s = "";
- for y = 1 , #array.table do
- for x = 1, #array.table[y] do
- if array.table[y][x] == "" then completed = false; end --board completed
- if array.table[y][x] != "" then
- s = array.table[y][x]
- 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;
- cont = {hor_right = 1,hor_left = 1,ver_up = 1,ver_down = 1,dia_up_inc = 1,dia_up_dec = 1,dia_down_inc = 1,dia_down_dec = 1};
- end
- end
- end
- return completed;
- 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_scenery(array)
- local winner = functions.check(array)
- if winner != nil then
- if type(winner) == "boolean" and winner then
- os.message("Tie.")
- reset()
- elseif type(winner) == "string" then
- os.message("Player "..functions.check_winner(winner,array).." wins.")
- reset()
- end
- end
- 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
- end
- end
- ------
- function reset()
- scenery[n].table = {
- {"","",""},
- {"","",""},
- {"","",""}
- }
- scenery[2].table={
- {"o","x",""},
- {"x","o","o"},
- {"o","o","x"}
- }
- end
- while true do
- controls.read()
- functions.draw_scenery(scenery[n])
- functions.draw_scenery_contorn(scenery[n], color.new(0,255,0))
- functions.move_selector(scenery[n])
- functions.print_selector(scenery[n])
- ----------
- if scenery[n].mode == "3D" then
- if controls.r() then scenery[n].pos.z = scenery[n].pos.z-1 end
- if controls.l() then scenery[n].pos.z = scenery[n].pos.z+1 end
- if controls.start() then
- if controls.down() then scenery[n].pos.y = scenery[n].pos.y-1 end
- if controls.up() then scenery[n].pos.y = scenery[n].pos.y+1 end
- if controls.right() then scenery[n].pos.x = scenery[n].pos.x+1 end
- if controls.left() then scenery[n].pos.x = scenery[n].pos.x-1 end
- if math.abs(controls.analogy()) > 40 then scenery[n].rot.x = scenery[n].rot.x + controls.analogy()/40 end
- --if math.abs(controls.analogx()) > 40 then scenery[n].rot.y = scenery[n].rot.y + controls.analogx()/40 end
- end
- else
- if math.abs(controls.analogx()) > 40 then scenery[n].x = scenery[n].x + controls.analogx()/40 end
- if math.abs(controls.analogy()) > 40 then scenery[n].y = scenery[n].y + controls.analogy()/40 end
- end
- if controls.start() and controls.r() and controls.l() then
- reset()
- scenery[2].pos={x = 1,y = -6,z=-43} scenery[2].rot={x=0,y=0,z=0}
- end
- if controls.l() and controls.r() and controls.right() then n = 2 end
- if controls.l() and controls.r() and controls.left() then n = 1 end
- if controls.select() then a() end
- screen.print(5,5,"Turn: "..scenery[n].turn.." S: "..n.." Mode: "..scenery[n].mode)
- screen.print(5,25,"Pos.x: "..scenery[2].pos.x.." Pos.y: "..scenery[2].pos.y.." Pos.z: "..scenery[2].pos.z.." rot.x"..scenery[2].rot.x)
- screen.flip()
- functions.fill_scenery(scenery[n])
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement