600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 游戏老虎吃绵羊 -- lua

游戏老虎吃绵羊 -- lua

时间:2020-08-09 15:17:15

相关推荐

游戏老虎吃绵羊 -- lua

简介:

游戏双方分别使用绵羊(S)和老虎(T)

图 1

图中6S代表当前为位置上有6只绵羊

除了如图所示为6S的点外,其他点只能同时有一个动物,6S点可容纳任意数量的动物,但必须是同一种类,两只老虎不能同时出现在同一位置上

绵羊每次可以沿着连线移动一个位置,老虎每次也可以仅移动一个位置,但如果该直线方向上相邻是绵羊,且距离为2的点为空时,老虎可以跨过绵羊跳到该位置,并吃掉绵羊

当绵羊数为0时老虎胜,老虎无法移动时绵羊胜,如图2,(3, 2)位置上的老虎跳到(5, 2)吃掉(4,2)上的一只绵羊

图 2

绵羊和老虎轮流移动一个棋子,每局开始以如图1所示开始,且老虎先走。

游戏操作:

提示:It's turn to tigger

输入:老虎所在行 (回车)老虎所在列(回车)

提示: Input move target

输入:要移动到的位置行(回车),列(回车)

完成后切换到羊

提示: It's turn to sheep

输入:羊所在行(回车),列(回车)

如果该位置有多只羊

提示Select piece,并列出该位置上羊的索引如1 2 3 4 5 6

输入:羊的索引 ,如1(回车)

提示:Input move target

输入:要移动到的位置行(回车),列(回车)

完成羊的移动,切换到老虎,交换操作直到游戏结束,如

Sheep win

源代码:

下载连接

/detail/piaobotudou/7718661

Array类:

--class array --Array = {nextIdx = 1,}function Array:new(o)o = o or {}setmetatable(o, self)self.__index = selfo.content = {}for i=1, #o doif o[i] theno:push_back(o[i])endendreturn o;endfunction Array:count()return self.nextIdx - 1endfunction Array:toIndex(idx)i = tonumber(idx)if i and i>0 and i<=self:count() thenreturn iendreturn nilendfunction Array:push_back(a)self.content[self.nextIdx] = aself.nextIdx = self.nextIdx + 1endfunction Array:pop_back()if self:count() > 0 thenself.nextIdx = self.nextIdx - 1res = self.content[self.nextIdx]self.content[self.nextIdx] = nilreturn resendreturn nilendfunction Array:cleanup()for i=1, #self.content doself.content[i] = nilendself.nextIdx = 1endfunction Array:find(item)for i=1, self:count() doif self.content[i] == item thenreturn iendendreturn nilendfunction Array:remove_item(item)idx = self:find(item)if idx thenself:remove(idx)endendfunction Array:insert(idx, a)idx = self:toIndex(idx)if idx thenlocal cpllocal cp2cp1 = afor i=idx, self:count() docp2 = self.content[i]self.content[i] = cp1cp1 = cp2endself:push_back(cp1)endendfunction Array:getAt(idx)i = self:toIndex(idx)if i thenreturn self.content[idx]endreturn nilendfunction Array:remove(idx)idx = self:toIndex(idx)if idx thenfor i=idx, self:count()-1 doself.content[i] = self.content[i+1]endself:pop_back()endend--Insert at 1function Array:push_front(a)self:insert(1, a)endfunction Array:pop_front()res = self.content[1]self:remove(1)return resendfunction Array:print()local s = "";if self:count() > 0 thens = s..self:getAt(1)endfor i=2, self:count() dos = s.." ".. self:getAt(i)endprint(s)end

core.lua

--Chess board class-----Include Line & Node --PIECETYPE = {none = 0,tigger = 1,sheep = 2}--piece define--Piece = {}Piece.name = "unnamed piece"Piece.r = -1 --rowPiece.c = -1 --colPiece.idx = -1Piece.dead = falsePiece.type = none function Piece:new(o)o = o or {}setmetatable(o, self)self.__index = selfreturn oendPieceTigger = Piece:new()PieceTigger.name = "Tigger"Piece.type = PIECETYPE.tiggerPieceSheep = Piece:new()PieceSheep.name = "Sheep"PieceSheep.type = PIECETYPE.sheepPieceManager = {tiggers=nil,sheeps=nil}function PieceManager:new(o)o = o or {}setmetatable(o, self)self.__index = self;o.tiggers = {}o.sheeps = {}return oendfunction PieceManager:makealive()for i=1,#self.tiggers doself.tiggers[i].dead = falseendfor i=1,#self.sheeps doself.sheeps[i].dead = falseendendfunction PieceManager:getpiece(idx)if idx < 25 thenreturn self.sheeps[idx]elseif idx<27 thenreturn self.tiggers[idx - 24]elsereturn nilendendfunction PieceManager:init(tn, sn)for i=1, tn doself.tiggers[i] = PieceTigger:new()self.tiggers[i].idx = i + snself.tiggers[i].name = "tigger "..(i-sn)endfor i=1,sn doself.sheeps[i] = PieceSheep:new()self.sheeps[i].idx = iself.sheeps[i].name = "sheep "..iendend--class Line--Line = {}Line.nodes = nilfunction Line:new(o)o = o or {}setmetatable(o, self)self.__index = selfo.nodes = {}return oendfunction Line:append(node)self.nodes[(#(self.nodes)) + 1] = nodeend----class Node----dofile("array.lua")Node = {}Node.lines = nilNode.pieces = nil Node.capacity = 1function Node:new(o)o = o or {}setmetatable(o, self)self.__index = selfo.pieces = Array:new()o.lines = {}return oendfunction Node:canaddpiece()return self.pieces:count() < self.capacityendfunction Node:empty()return self.pieces:count() == 0endfunction Node:firstpiece()if self:empty() thenreturn nilelsereturn self.pieces:getAt(1);endendfunction Node:add(l, i)self.lines[#self.lines + 1] = {line = l, idx = i}endfunction Node:isonline(lidx)for i = 1, #self.lines doif self.lines[i].line == lidx thenreturn trueendendreturn falseendfunction Node:getidxofline(lidx)for i=1, #self.lines doif self.lines[i].line == lidx thenreturn self.lines[i].idxendendreturn nilendfunction Node:printlines()print( "LinesCount: "..#self.lines)str = ""for i=1, #self.lines dostr = str .. self.lines[i].line.." "endprint(str)endfunction Node:getline(otherNode)--self:printlines()--otherNode:printlines()for i=1, #self.lines doif otherNode:isonline(self.lines[i].line) thenreturn self.lines[i].lineendendreturn nilend--Board----[[1 2 34 5 --------/-------.,------/-------.6|`.|,`|`|,`| | `. | ,' | `. | ,' | |', | - | ', | - | |`|,'|`|,'| --------|---------------|-------- 7| ,' | `. | ,' | `. | | ,- | `, | ,- | `, | | /|',| /|',| -`------|-------|-------|-------. 811 |`.|,`|`.|,`| 16| `. | ,' | `. | ,' | | ', | - | ', | - | |`|,'|`|,'| --------|---------------|-------- 9| ,' | `. | ,' | `. | | ,- | `, | ,- | `, | | /|',| /|',| -`------\-------'`------\-------' 101213 14 15----]]Board = {}Board.nodes = nilBoard.lines = nilfunction Board:new(o)o = o or {}setmetatable(o, self)self.__index = selfo.nodes = {}o.lines = {}return oendfunction rowcolToIndex(r, c)return (r-1)*5 + cendfunction indexToRow(idx)return math.ceil(idx/5)endfunction indexToCol(idx)return idx - (indexToRow(idx) - 1)*5endfunction Board:appendNodeToLine(lidx, r, c)self.lines[lidx]:append(rowcolToIndex(r, c))self.nodes[r][c]:add(lidx, #self.lines[lidx].nodes)endfunction Board:Init()for i=1, 5 doself.nodes[i] = {}for j=1, 5 doself.nodes[i][j] = Node:new()endendself.nodes[2][2].capacity = 100self.nodes[4][2].capacity = 100self.nodes[2][4].capacity = 100self.nodes[4][4].capacity =100for i=1,16 doself.lines[i] = Line:new()endfor i=1, 5 dofor k=1,5 doself:appendNodeToLine(i, k, i)self:appendNodeToLine(i+5, i, k)endendself:appendNodeToLine(11, 3, 1)self:appendNodeToLine(11, 2, 2)self:appendNodeToLine(11, 1, 3)self:appendNodeToLine(12, 5, 1)self:appendNodeToLine(12, 4, 2)self:appendNodeToLine(12, 3, 3)self:appendNodeToLine(12, 2, 4)self:appendNodeToLine(12, 1, 5)self:appendNodeToLine(13, 5, 3)self:appendNodeToLine(13, 4, 4)self:appendNodeToLine(13, 3, 5)self:appendNodeToLine(14, 3, 1)self:appendNodeToLine(14, 4, 2)self:appendNodeToLine(14, 5, 3)self:appendNodeToLine(15, 1, 1)self:appendNodeToLine(15, 2, 2)self:appendNodeToLine(15, 3, 3)self:appendNodeToLine(15, 4, 4)self:appendNodeToLine(15, 5, 5)self:appendNodeToLine(16, 1, 3)self:appendNodeToLine(16, 2, 4)self:appendNodeToLine(16, 3, 5)end--GAME--Game = {}Game.board = nil Game.pieces = nil--PieceManager:new()Game.turn = PIECETYPE.tiggerGame.curpick = -1function Game:new(o)o = o or {}setmetatable(o, self)self.__index = selfo.board = Board:new()o.pieces = PieceManager:new()return oendfunction Game:AddPieceToBoard(r, c, idx, bt)self.board.nodes[r][c].pieces:push_back(idx)--print(r.." "..c.." "..idx)if bt thenself.pieces.tiggers[idx-24].r = rself.pieces.tiggers[idx-24].c = celseself.pieces.sheeps[idx].r = rself.pieces.sheeps[idx].c = cendendfunction Game:restart()for i=1, 5 dofor j=1,5 doself.board.nodes[i][j].pieces:cleanup()endend--for i, v in ipairs(self.board.nodes) do--endself.pieces:makealive()for i=1,6 doself:AddPieceToBoard(2, 2, i, false)self:AddPieceToBoard(2, 4, i+6, false)self:AddPieceToBoard(4, 2, i+12, false)self:AddPieceToBoard(4, 4, i+18, false)end--self.board.nodes[2][2].pieces:print()--self.board.nodes[2][4].pieces:print()--for i,v in ipairs(self.pieces.sheeps) do--print(v.name.." "..v.r.." "..v.c)--if v.r<0 then--break--end--endself:AddPieceToBoard(3, 2, 25, true)self:AddPieceToBoard(3, 4, 26, true)self.turn = PIECETYPE.tiggerself.curpick = -1endfunction Game:init()self.board:Init()self.pieces:init(2, 24)self:restart()end--1:25 2:26--function Game:getsheepmoveablenodeidx(idx)local curpiece = self.pieces:getpiece(idx)local curnode = self.board.nodes[curpiece.r][curpiece.c]local lidx = 0local nidx = 0local line = nillocal tr = 0local tc = 0local tn = nilres = {}for i=1, #curnode.lines dolidx = curnode.lines[i].linenidx = curnode.lines[i].idxline = self.board.lines[lidx]if nidx - 1 > 0 thentr = indexToRow(line.nodes[nidx - 1])tc = indexToCol(line.nodes[nidx - 1])tn = self.board.nodes[tr][tc]if tn:empty() or (tn:canaddpiece() and self.pieces:getpiece(tn:firstpiece()).type == PIECETYPE.sheep) thenres[#res + 1] = line.nodes[nidx - 1]endendif nidx + 1 <= #line.nodes thentr = indexToRow(line.nodes[nidx + 1])tc = indexToCol(line.nodes[nidx + 1])tn = self.board.nodes[tr][tc]if tn:empty() or (tn:canaddpiece() and self.pieces:getpiece(tn:firstpiece()).type == PIECETYPE.sheep) thenres[#res + 1] = line.nodes[nidx + 1]endendendreturn resendfunction Game:gettiggermoveablenodeidx(idx)local idx = idx + 24local curpiece = self.pieces:getpiece(idx)local curnode = self.board.nodes[curpiece.r][curpiece.c]local lidx = 0local nidx = 0local line = nillocal tr = 0local tc = 0local tn = nilres = {}for i=1, #curnode.lines dolidx = curnode.lines[i].linenidx = curnode.lines[i].idxline = self.board.lines[lidx]--ss = "Line index: " .. lidx.."nIdx: "..curnode.lines[i].idx--print(ss)--ss = ""--for i=1, #line.nodes do--ss = ss.." "..line.nodes[i]--end--print(ss)if nidx - 1 > 0 thentr = indexToRow(line.nodes[nidx - 1])tc = indexToCol(line.nodes[nidx - 1])tn = self.board.nodes[tr][tc]if tn:empty() thenres[#res + 1] = line.nodes[nidx - 1]elseif nidx - 2 > 0 and self.pieces:getpiece(tn:firstpiece()).type == PIECETYPE.sheep thentr = indexToRow(line.nodes[nidx - 2])tc = indexToCol(line.nodes[nidx - 2])tn = self.board.nodes[tr][tc]if tn:empty() thenres[#res + 1] = line.nodes[nidx - 2]endendendif nidx + 1 <= #line.nodes thentr = indexToRow(line.nodes[nidx + 1])tc = indexToCol(line.nodes[nidx + 1])--print("tr "..tr.."tc: "..tc)tn = self.board.nodes[tr][tc]if tn:empty() thenres[#res + 1] = line.nodes[nidx + 1]elseif nidx + 2 <= #line.nodes and self.pieces:getpiece(tn:firstpiece()).type == PIECETYPE.sheep thentr = indexToRow(line.nodes[nidx + 2])tc = indexToCol(line.nodes[nidx + 2])tn = self.board.nodes[tr][tc]if tn:empty() thenres[#res + 1] = line.nodes[nidx + 2]endendendendreturn resendfunction Game:istiggerunmoveable(idx)res = self:gettiggermoveablenodeidx(idx)return #res == 0endfunction Game:pickpiece(idx)if self.turn == PIECETYPE.tigger thenif idx< 25 or idx>26 thenself.curpick = -1;return falseelseself.curpick = idxreturn trueendelse if idx<1 or idx>24 thenself.curpick = -1;return falseelseself.curpick = idxreturn trueendendendfunction Game:movepieceto(r, c, pieceIdx)curPiece = self.pieces:getpiece(pieceIdx)self.board.nodes[curPiece.r][curPiece.c].pieces:remove_item(pieceIdx)self.board.nodes[r][c].pieces:push_back(pieceIdx)curPiece.r = rcurPiece.c = cif curPiece.type == PIECETYPE.sheep thenself.turn = PIECETYPE.tiggerelseself.turn = PIECETYPE.sheepend--print("It's turn to "..self.turn)endfunction Game:eatsheepinnode(node)idx = node.pieces:pop_back()self.pieces:getpiece(idx).dead = trueendfunction Game:moveto(r, c)if self.curpick < 1 or self.curpick > 26 or r < 1 or r > 5 or c< 1 or c > 5 thenprint("curpick or r, c error")return falseendlocal curPiece = self.pieces:getpiece(self.curpick)local curNode = self.board.nodes[curPiece.r][curPiece.c]local tarNode = self.board.nodes[r][c]local lidx = curNode:getline(tarNode)local midNode--not same lineif not lidx thenprint("Can't get line")return falseendlocal curidx = curNode:getidxofline(lidx)local taridx = tarNode:getidxofline(lidx)print("Line: "..lidx.." Move from: "..curidx.." To: "..taridx)if curidx == taridx or not curidx or not taridx thenreturn falseendlocal step = math.abs(curidx - taridx)if step == 1 then--Move one step, move if target node can hold--if curPiece.type == PIECETYPE.tigger thenif tarNode:empty() thenprint("Tigger move one step")self:movepieceto(r, c, self.curpick)elseprint("Target node is not empty")endelseif tarNode:canaddpiece() and (not tarNode:firstpiece() or self.pieces:getpiece(tarNode:firstpiece()).type == curPiece.type) thenprint("Sheep move one step")self:movepieceto(r, c, self.curpick)elseprint("Can't move to target")endendelseif step == 2 and curPiece.type == PIECETYPE.tigger and tarNode:empty() thenmididx = (curidx + taridx)/2midNode = self.board.nodes[indexToRow(self.board.lines[lidx].nodes[mididx])][indexToCol(self.board.lines[lidx].nodes[mididx])]if midNode:firstpiece() and self.pieces:getpiece(midNode:firstpiece()).type == PIECETYPE.sheep thenprint("Trigger move and eat sheep in node "..mididx)self:movepieceto(r, c, self.curpick)self:eatsheepinnode(midNode)elseprint("Trigger can't jump");endendreturn falseendfunction Game:getpiecesonnode(r, c)local res = {}--print(r.." "..c.." ")--print(self.board.nodes[r][c])for i=1, self.board.nodes[r][c].pieces:count() dores[#res+1] = self.board.nodes[r][c].pieces:getAt(i)endreturn resendfunction Game:whowin()if self.turn == PIECETYPE.tigger thenif self:istiggerunmoveable(1) and self:istiggerunmoveable(2) thenreturn PIECETYPE.sheependelselocal bres = truefor i=1,24 doif not self.pieces.sheeps[i].dead thenbres = falsebreakendendif bres thenreturn PIECETYPE.tiggerendendreturn nil end--no sheeps or tigger can't movefunction Game:isover()type = self:whowin()if type thenreturn trueelsereturn falseendend

game.lua

#!dofile("board.lua")function getnodestr(game, node)if node.pieces:count() > 0 then--print(game.pieces:getpiece(node.pieces:getAt(1)).name);if game.pieces:getpiece(node.pieces:getAt(1)).type == PIECETYPE.sheep thenreturn node.pieces:count().."S"elsereturn node.pieces:count().."T"endendreturn "0 "endfunction drawnodeonline(i, game, board)local str = ""for j=1,4 dostr = str ..getnodestr(game, board.nodes[i][j]).." ----\t"endstr = str .. getnodestr(game, board.nodes[i][5])print(str)endfunction drawchessboard(game, board)drawnodeonline(1, game, board)print("| \\ | / | \\ | / |")print("|\\ | / |\\ | / |")drawnodeonline(2, game, board)print("|/ | \\ |/ | \\ |")print("| / | \\ | / | \\ |")drawnodeonline(3, game, board)print("| \\ | / | \\ | / |")print("|\\ | / |\\ | / |")drawnodeonline(4, game, board)print("|/ | \\ |/ | \\ |")print("| / | \\ | / | \\ |")drawnodeonline(5, game, board)endfunction printturn(g)if g.turn == 1 thenprint("It's turn to Tigger")elseprint("It's turn to Sheep")endendgame = Game:new{}game:init()drawchessboard(game,game.board)printturn(game)winner = nilwhile not winner dorow = io.read("*number")col = io.read("*number")pics = game:getpiecesonnode(row, col)local str = " "if #pics > 1 thenprint("Select piece: ")for i=1, #pics dostr = str..pics[i].." "endprint(str)sel = io.read("*number")bok = falsefor i=1,#pics doif sel == pics[i] thenbok = true;breakendendif bok and game:pickpiece(sel) thenprint("Input move target")row = io.read("*number")col = io.read("*number")game:moveto(row, col)elseprint("Wrong")endelseif #pics == 1 thenif game:pickpiece(pics[1]) thenprint("Select piece "..pics[1])print("Input move target")row = io.read("*number")col = io.read("*number")game:moveto(row, col)elseprint("Wrong piece")endelseprint("No piece available")enddrawchessboard(game,game.board)printturn(game)winner = game:whowin()endif winner == PIECETYPE.tigger thenprint("Tigger win")elseprint("Sheep win")end

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。