2014-02-17 3 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 개와 하트 여왕 1 개를 가지고 있다고 가정 해 봅시다. 동일한 색상 또는 동일한 종류의 카드 인 경우 어떤 카드가 다른 카드를 상대로 어떻게 정의하겠습니까? 문자열을 다시 나눠서 많은 if 문과 비교해야합니다.

내 제안에는 두 개의 숫자가 사용됩니다. 하나는 가치이고 다른 하나는 친절합니다. 그러나 두 개의 값을 가진 목록이나 각 카드에 대해 두 개의 키가있는 레코드는 나를 호소하지 않으므로 두 숫자를 하나로 저장합니다. 카드 유형에는 네 가지 유형의 값이 있으므로 비트 단위로 00, 01, 10 및 11 만 사용합니다. 즉 4 개의 값을 모두 2 비트로 저장할 수 있습니다. 그래서 내 카드의 처음 두 비트는 카드 종류로 사용할 것입니다. 나머지 비트 (최대 4 비트 만 필요함)는 카드 값으로 사용됩니다.

예를 들어 이진 값 10010은 이진 카드 값 100과 이진 종류의 카드 10입니다. 10 진수 값으로 변환 : 카드 종류 2와 카드 값 4 (클럽 중 5 개가 될 수 있음). 값을 나누기 위해서는 mod와 div를 사용할 수 있습니다. Div 4는 비트를 오른쪽으로 이동시키고 처음 두 비트 (읽기 : 카드 종류)를 제거합니다.이 비트는 카드 값으로 만 남아 있습니다. 모드 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) 왜 갑판을 섞는 루프에 item # seed 대신 '(seed + 1)'을 사용합니까? 원래 루프로 0 ~ 51을 사용했기 때문에? 왜 1-52를 사용하지 않고 씨앗을 그대로 두십시오? 다시 한 번 감사드립니다! – nebmuzik

+0

주된 이유는 논리적 인간 지점이나 AppleScript 관점에서가 아니라 이진 관점에서 카드에 접근하는 것입니다. 왜냐하면 2 비트 (0-3)로 카드 유형을 사용할 때 카드 값에 동일한 시작 값을 사용하는 것이 더 좋을 것이라고 생각했기 때문입니다. 원하는대로 변경할 수 있습니다. 카드 값을 1에서 시작하려면 카드 값이 4 위치에서 시작해야하므로 바이트 값 100이 네 번째 카드 유형이므로 다른 비트 스위프트 (mod 8 및 div 8)를 사용해야 함을 기억하십시오 (bin 1000 = dec 8) –