2014-11-24 19 views
0

對不起,標題的可怕措辭。我在Processing Programming for Programming課程中創建了一個「War」遊戲。我需要更改我的代碼,以便每個使用的牌從甲板/陣列中移除。我偶然發現一些帖子和谷歌的結果提到「ArrayList」,但我仍然有點無知。(處理)刪除陣列中以前使用的對象

以下代碼顯示兩個單獨的隨機卡,並在單擊鼠標時顯示兩個新隨機卡。

(第一個標籤 '戰爭')

void draw(){ 
image(card[imageIndex],40,150); 
image(card2[imageIndex2],340,150); 
} 

void mousePressed(){ 
imageIndex = int(random(card.length)); 
imageIndex2 = int(random(card2.length)); 
} 

(第二個選項卡 '卡')

PImage[] card = new PImage[13]; 
PImage[] card2 = new PImage[13]; 
int imageIndex = int(random(0,12)), 
imageIndex2 = int(random(0,12)); 

void setup(){ 
size(500,500); 
card[0] = loadImage("2_of_clubs.jpg"); 
card[1] = loadImage("3_of_clubs.jpg"); 
card[2] = loadImage("4_of_clubs.jpg"); 
card[3] = loadImage("5_of_clubs.jpg"); 
card[4] = loadImage("6_of_clubs.jpg"); 
card[5] = loadImage("7_of_clubs.jpg"); 
card[6] = loadImage("8_of_clubs.jpg"); 
card[7] = loadImage("9_of_clubs.jpg"); 
card[8] = loadImage("10_of_clubs.jpg"); 
card[9] = loadImage("jack_of_clubs.jpg"); 
card[10] = loadImage("queen_of_clubs.jpg"); 
card[11] = loadImage("king_of_clubs.jpg"); 
card[12] = loadImage("ace_of_clubs.jpg"); 

card2[0] = loadImage("2_of_clubs.jpg"); 
card2[1] = loadImage("3_of_clubs.jpg"); 
card2[2] = loadImage("4_of_clubs.jpg"); 
card2[3] = loadImage("5_of_clubs.jpg"); 
card2[4] = loadImage("6_of_clubs.jpg"); 
card2[5] = loadImage("7_of_clubs.jpg"); 
card2[6] = loadImage("8_of_clubs.jpg"); 
card2[7] = loadImage("9_of_clubs.jpg"); 
card2[8] = loadImage("10_of_clubs.jpg"); 
card2[9] = loadImage("jack_of_clubs.jpg"); 
card2[10] = loadImage("queen_of_clubs.jpg"); 
card2[11] = loadImage("king_of_clubs.jpg"); 
card2[12] = loadImage("ace_of_clubs.jpg"); 
} 
+0

也許這篇文章可以幫助。它已被移動,我只能找到這個不是形式版本。無論如何值得閱讀;)http://archive-org.com/page/4420950/2014-08-17/http://wiki.processing.org/index.php?title=Why_use_ArrayList_instead_of_array_with_append%28%29%3F&action=edit – 2014-11-24 12:31:56

+0

是的,你應該爲此使用ArrayList。當你搜索「ArrayList教程」時發生了什麼?你可以使用一個數組,並將你想要移除的索引設置爲null。 – 2014-11-24 13:45:54

回答

0

使用ArrayList中看起來或多或少是:

// gonna use strings instead of images 
// just to show the idea. I don't have all this images... 
ArrayList<String> card = new ArrayList<String>(); 
ArrayList<String> card2; 

int imageIndex, imageIndex2; 

String display1, display2; 

void setup() { 
    size(500, 500); 
    card.add("2_of_clubs.jpg"); 
    card.add("3_of_clubs.jpg"); 
    card.add("4_of_clubs.jpg"); 
    card.add("5_of_clubs.jpg"); 
    card.add("6_of_clubs.jpg"); 
    card.add("7_of_clubs.jpg"); 
    card.add("8_of_clubs.jpg"); 
    card.add("9_of_clubs.jpg"); 
    card.add("10_of_clubs.jpg"); 
    card.add("jack_of_clubs.jpg"); 
    card.add("queen_of_clubs.jpg"); 
    card.add("king_of_clubs.jpg"); 
    card.add("ace_of_clubs.jpg"); 

    card2 = new ArrayList<String>(card); 


    imageIndex = int(random(card.size())); 
    imageIndex2 = int(random(card2.size())); 

    display1 = card.get(imageIndex); 
    display2 = card2.get(imageIndex2); 

    card.remove(imageIndex); 
    card2.remove(imageIndex2); 

    println("\ncard draw from card : "+ display1); 
    println("card draw from card2: "+ display2 + "\n"); 
} 

void draw() { 
} 

void mousePressed() { 
    if (card.size() > 0) { 
    imageIndex = int(random(card.size())); 
    imageIndex2 = int(random(card2.size())); 

    display1 = card.get(imageIndex); 
    display2 = card2.get(imageIndex2); 

    card.remove(imageIndex); 
    card2.remove(imageIndex2); 

    println("\ncard draw from card : "+ display1); 
    println("card draw from card2: "+ display2 + "\n"); 
    } else { 
    println("The deck is empty..."); 
    } 
} 
0

如果你必須使用數組那麼你將不得不做類似於下面的代碼:

PImage[] removeCardFromDeck(PImage deck, int index){ 
    PImage[] newdeck = new PImage[deck.length -1]; 
    int count=0; 
    for(int i =0 ; i<newdeck.length;i++){ 
    if(count==index) count++; 
    newdeck[i] = deck[count]; 
    count++;  
    } 
    return newdeck; 
} 

但是對於這項任務,最好使用ArrayLists。