2014-02-17 86 views
0

在Applescript中使用一個簡單的循環來創建將根據已聲明的預定義數組分配的變量。作業是隨機的。目標是創建一個變量,其中一個子集將數組的一部分分配給變量中的特定部分。我需要跟蹤該部分後來結束的位置並引用它。問題是變量的語法之一。 例如,如果我想洗牌的撲克牌,它會是這個樣子: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 

我知道我會擔心與隨機#發電機重複,一旦我得到的變量問題解決了,我可以做到這一點。但是我需要的是沿着子集的方向。例如(deckcard.1和deckcard.5)。我只是無法弄清楚如何讓它起作用。我試過並閱讀了所有我能找到的東西。

預先感謝您。

回答

0

我可以提出另一種方法。通過字符串數組來定義卡片並不是真正通過值來定義它們。如果你想比較遊戲中的牌或什麼的話會怎麼樣。假設你有2個黑桃和一個女王的心,你會如何定義哪一張牌擊敗對方,如果他們的顏色相同,甚至相同?你應該再次分割你的字符串,並將它們與很多if語句進行比較。

我的建議是使用兩個數字:一個是它的價值,一個是它的。然而,使用帶有兩個值的列表或者每個卡使用兩個鍵的記錄不會吸引我,所以我會將兩個數字一起存儲。卡類型有四種不同類型的值,因此按位只能使用00,01,10和11.這意味着我們可以將所有4個值存儲爲2位。所以我打算使用我的卡的前兩位作爲一種卡。其餘的位(最大隻需要4位)用於卡片值。

例如,二進制值10010是二進制卡片值100和卡片10的二進制類型。轉換爲十進制值:卡片種類2和卡片值4,其可以是5個俱樂部。爲了從價值中分離價值,我們可以使用mod和div。 Div 4會將這些位向右移動,移除前兩位(讀取的是一種卡),它只保留卡的值。 Mod 4將除了前2以外的所有位都設置爲0,它將刪除卡的值。通過這兩個運算符,我們可以從給定的整數中獲得正確的數據。這裏有一個示例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 
+0

感謝您的輸入!我原先想過分別將變量分配給卡片的花色和價值,所以很高興知道我的直覺是準確的。 – nebmuzik

+0

感謝您的輸入!我原先想過分別將變量分配給卡片的花色和價值,所以很高興知道我的直覺是準確的。我很確定我已經把我的頭圍繞在這個大部分上面,但是我對你的代碼有幾個問題。 1)爲什麼使用'(種子+ 1)'代替項目#種子作爲洗牌套牌的循環?因爲您使用0到51作爲您的原始循環?爲什麼不使用1-52並留下種子?再次感謝! – nebmuzik

+0

主要原因是從二進制角度而不是從邏輯人類角度或AppleScript的角度來看待卡片。因爲當使用2比特卡片類型(0 - 3)時,我認爲使用卡片值的相同起始值會更好。你可以改變你的喜好。記住,如果你想從1開始卡類型,並且你需要使用其他的比特迅雷(mod 8和div 8),因爲字節值100是第四種卡類型,所以你的卡值必須從4開始(bin 1000 = dec 8) –