使用Applescript中的简单循环创建一个变量,该变量将根据已声明的预定义数组进行分配。作业是随机的。目标是创建一个带有子集的变量,该子集将数组的一部分分配给变量中的特定部分。我需要跟踪部件最后的位置并引用它。问题是变量的语法之一。 例如,如果我想要洗牌一副牌,它看起来像这样:
set cardlist to {"Ace of Spades", "2 of Spades", "3 of Spades", "4 of Spades", "5 of Spades", "6 of Spades", "7 of Spades", "8 of Spades", "9 of Spades", "10 of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", "Ace of Diamonds", "2 of Diamonds", "3 of Diamonds", "4 of Diamonds", "5 of Diamonds", "6 of Diamonds", "7 of Diamonds", "8 of Diamonds", "9 of Diamonds", "10 of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds", "Ace of Hearts", "2 of Hearts", "3 of Hearts", "4 of Hearts", "5 of Hearts", "6 of Hearts", "7 of Hearts", "8 of Hearts", "9 of Hearts", "10 of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts", "Ace of Clubs", "2 of Clubs", "3 of Clubs", "4 of Clubs", "5 of Clubs", "6 of Clubs", "7 of Clubs", "8 of Clubs", "9 of Clubs", "10 of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"}
-- shuffle deck
set deckcount to 1
set deckset to random number from 1 to 52
repeat 52 times
set deskset to random number from 1 to 52
set deckcard(subset) to item deckset in cardlist
set deckcount to deckcount + 1
set deckset to random number from 1 to 52
end repeat
我知道我将不得不担心随机#generator的重复,一旦我解决了变量问题,我就可以做到。但我需要的是与子集相关的东西。例如(deckcard.1和deckcard.5)。我只是无法弄清楚如何让它工作。我试过并阅读了我能找到的一切。
提前谢谢你。
答案 0 :(得分:0)
我可以建议另一种方法。通过字符串数组定义卡并不是真正按值定义它们。如果您想比较游戏中的牌或其他什么,该怎么办?假设你有2个黑桃和一个女王的心,如果它们是相同的颜色甚至是同一种颜色,你如何定义哪些卡击败另一个?您应该再次拆分字符串并将它们与许多if语句进行比较。
我的建议是使用两个数字:一个用于它的值,一个用于它的类型。但是,对于每张卡使用带有两个值的列表或带有两个键的记录不会吸引我,所以我将两个数字存储为一个。卡类型有四种不同类型的值,因此按位只使用00,01,10和11.这意味着我们可以将所有4个值存储为2位。所以我的卡片的前两位我将用作卡片。然后将剩余的比特(最多只需要4比特)用作卡值。
例如二进制值10010是二进制卡值100和二进制卡10。转换为十进制值:卡类型2和卡值4,可以是5个球杆。要从它的种类和价值中分割出价值,我们可以使用mod和div。 Div 4将向右移位,删除前两位(读取:卡的类型),这仍然只是卡值。 Mod 4将所有位设置为0,除了前2位,它将删除卡值。使用这两个运算符,我们可以从给定的整数中获取正确的数据。这是一个可以解释的AppleScript示例。
-- We're going to use the first 2 bits to define the kind of the card (Heart, Spade, Club or Diamond)
-- The remaining bits we're going to define the card value
-- to get the card value we simply divide the value by 4 to remove the first two bits
-- to get kind of card we simply mod the value by 4 to keep the first two bits
-- create a deck
set deck to {}
repeat with x from 0 to 51
set end of deck to x
end repeat
-- The deck is ordered; first by card value then by it's color (normally by color and then by value)
-- now we're going to shuffle the deck by picking randomly an item from the ordered list.
-- the picked item will be removed, so it won't be picked again and copied to the shuffled list
set shuffledDeck to {}
repeat until integers of deck = {}
set seed to some integer of deck
set end of shuffledDeck to seed
set item (seed + 1) of deck to missing value
end repeat
-- now if we want a presentational form of the deck we can simply do this
set the cardValues to {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}
set the cardColors to {"Spades", "Hearts", "Clubs", "Diamonds"}
set stringsForShuffledDeck to {}
repeat with card in shuffledDeck
set end of stringsForShuffledDeck to (item (card div 4 + 1) of cardValues) & " of " & (item (card mod 4 + 1) of cardColors)
end repeat
-- the whole point of using number values instead of an array of number
-- is to check which cards beats the other by value and or color
-- we're going to use the first 2 cards of the shuffled deck
set firstCard to item 1 of shuffledDeck
set secondCard to item 2 of shuffledDeck
if firstCard div 4 > secondCard div 4 then
log item 1 of stringsForShuffledDeck & " beats " & item 2 of stringsForShuffledDeck
else if firstCard div 4 = secondCard div 4 then
log item 1 of stringsForShuffledDeck & " equals " & item 2 of stringsForShuffledDeck
else
log item 1 of stringsForShuffledDeck & " is less than " & item 2 of stringsForShuffledDeck
end if
-- Now compare card 3 and 4 of shuffled deck by color
set thirdCard to item 3 of shuffledDeck
set FourthCard to item 4 of shuffledDeck
if thirdCard mod 4 = FourthCard mod 4 then
log item 3 of stringsForShuffledDeck & " is of the same kind as " & item 4 of stringsForShuffledDeck
else
log item 3 of stringsForShuffledDeck & " is other kind as " & item 4 of stringsForShuffledDeck
end if
-- We have set the even numbers for black colored cards and odd number for red colors
-- Now we can determine very easily it's colors.
if thirdCard mod 2 = FourthCard mod 2 then
log item 3 of stringsForShuffledDeck & " has the same color as " & item 4 of stringsForShuffledDeck
else
log item 3 of stringsForShuffledDeck & " has other color as " & item 4 of stringsForShuffledDeck
end if