这里有一些更深入的示例,应该可以帮助您构建该系统(之前的答案有一些错误)
local cards = {{name="one",count=0,flipped=false,matched=false},{name="two",count=0,flipped=false,matched=false},{name="three",count=0,flipped=false,matched=false},{name="four",count=0,flipped=false,matched=false},{name="five",count=0,flipped=false,matched=false},{name="six",count=0,flipped=false,matched=false}};
function getAvailableCards(Count)
local suitable_cards = (Count and {} or nil); -- create a table if you want to get keys of a valid cards in the cards table
local empty = true;
for key,data in pairs(cards) do
if (data.count < 2)then
if (Count) then -- add key if you want an output (table with valid keys)
table.insert(suitable_cards,key);
-- table.insert(suitable_cards,key); you can insert it again for higher range of numbers to select from, making math.random a bit more "random".
empty = false;
else
return true;
end
end
end
if (empty) then
return false;
end
return suitable_cards;
end
function getRandomCard()
local valid_cards = getAvailableCards(true); -- get a table with all valid keys of the cards table, to pick a random key
if (not valid_cards) then
return valid_cards; -- return false
end
local index = valid_cards[math.random(1,#valid_cards)]; -- select random key
cards[index].count = cards[index].count + 1; -- increase the count of key generation
return cards[index].name,index -- return card id and index from the cards table
end
function setupCards()
while getAvailableCards() do
local card_id,index = getRandomCard(); -- gets a random card id and it's index
-- setup the card in GUI and such,
-- like create a board and then create an image and load the image (based on card id)
-- As an example (no such functions, but you should create them or find functions similiar to them)
-- card = getBoard().NewImage();
-- card.id = index;
-- card.loadImage("hidden_card.png");
end
end
function TryToMatch()
local child1,child2 -- child tables that will be copied from the cards table, for later comparing
-- get all flipped but not matched cards
for key,child in pairs(cards) do
if (child.flipped and not child.matched) then
if (child1) then
child2 = key; -- get key
else
child1 = key; -- get key
end
end
end
if ( child1 and child2) then -- two revealed cards can be checked
if (child1.name == child2.name) then -- reveal
cards[child1].matched,cards[child2].matched = true,true;
TryToEnd();
else
--un-reveal it
end
end
end
function TryToEnd()
local matched = 0;
for key,child in pairs(cards) do
if (child.flipped and child.matched) then
matched = matched + 1;
end
end
if (#cards == matched) then
-- display that the game is finished
end
end
function Click(object)
-- When an object is clicked make it call this function with the object
if (object and object.id and cards[object.id] and not (cards[object.id].flipped and cards[object.id].matched)) then
cards[object.id].flipped = true;
-- You should load here the card image (actual card image, like you revealed the card)
-- Example
-- object.loadImage(cards[object.id].name.. ".png");
TryToMatch();
return true
end
return false
end
编辑:
我没有测试这个脚本也没有使用 Corona,它可能有一些错误,理论上它应该可以正常工作。