2012-03-19 4 views
0

그래서 저는 카드 게임 전쟁을 구현해야합니다. 갑판 (arraylist) 중 하나의 크기에 도달하면 내 루프 중지를 제외하고는 모두 잘 진행됩니다. 내가 원하는 것은 갑판 중 하나가 비어있을 때까지 반복하는 것입니다. 그리고 내가 그것을 어떻게 할 수 있는지에 대한 조언? 코드가 짧아 질 수는 있지만 지금은 걱정할 필요가 없습니다. 여기 내 코드는 다음과 같습니다.자바 카드 게임. (전쟁의 게임)

import java.util.*; 
import java.lang.*; 

public class lab9_11{ 
    public static void main(String args[]){ 

     System.out.printf("\n"); 
     System.out.printf("\n"); 
     System.out.printf("\\  //\\ |\\ |\n"); 
     System.out.printf(" \\/\\//__\\ |/ |\n"); 
     System.out.printf(" \\/ \\// \\ |\\ o\n"); 
     System.out.printf("\n"); 
     System.out.printf("Lets play a game of war.\n"); 
     System.out.printf("Type 1 to begin or 0 to exit.\n"); 
     Scanner keyboard = new Scanner(System.in); 

     int initial = keyboard.nextInt(); 

     if (initial > 1){ 
      System.out.printf("You can't read, so you shouldn't play this.\n"); 
      System.exit(-1); 
     } 

     if (initial == 0){ 
      System.exit(-1); 
     } 

     else { 
      System.out.printf("Okay, lets play!\n"); 
      System.out.printf("\n"); 
     } 
     System.out.printf("Here is a deck of 52 cards -> []:\n"); 

     int deck[] = new int[52]; 

     deck[0] = 2; //0-12 = hearts 
     deck[1] = 3; 
     deck[2] = 4; 
     deck[3] = 5; 
     deck[4] = 6; 
     deck[5] = 7; 
     deck[6] = 8; 
     deck[7] = 9; 
     deck[8] = 10; 
     deck[9] = 11; //jack of hearts 
     deck[10] = 12; //queen of hearts 
     deck[11] = 13; //king of hearts 
     deck[12] = 14; //ace of hearts 

     deck[13] = 2; //13-25 = spades 
     deck[14] = 3; 
     deck[15] = 4; 
     deck[16] = 5; 
     deck[17] = 6; 
     deck[18] = 7; 
     deck[19] = 8; 
     deck[20] = 9; 
     deck[21] = 10; 
     deck[22] = 11; //jack of spades 
     deck[23] = 12; //queen of spades 
     deck[24] = 13; //king of spades 
     deck[25] = 14; //ace of spades 

     deck[26] = 2; //0-12 = diamonds 
     deck[27] = 3; 
     deck[28] = 4; 
     deck[29] = 5; 
     deck[30] = 6; 
     deck[31] = 7; 
     deck[32] = 8; 
     deck[33] = 9; 
     deck[34] = 10; 
     deck[35] = 11; //jack of diamonds 
     deck[36] = 12; //queen of diamonds 
     deck[37] = 13; //king of diamonds 
     deck[38] = 14; //ace of diamonds 

     deck[39] = 2; //39-51 = clubs 
     deck[40] = 3; 
     deck[41] = 4; 
     deck[42] = 5; 
     deck[43] = 6; 
     deck[44] = 7; 
     deck[45] = 8; 
     deck[46] = 9; 
     deck[47] = 10; 
     deck[48] = 11; //jack of clubs 
     deck[49] = 12; //queen of clubs 
     deck[50] = 13; //king of clubs 
     deck[51] = 14; //ace of clubs 

     System.out.printf("First, lets shuffle it into 2 decks of 26.\n"); 

     Random rangen = new Random(); //Random number generator 
     for (int i=0; i < deck.length; i++) { //shuffles the deck 
      int randomPosition = rangen.nextInt(deck.length); 
      int temp = deck[i]; 
      deck[i] = deck[randomPosition]; 
      deck[randomPosition] = temp; 
     } 

     int[] player1 = new int[26]; //initializes 1st player's deck 
     int[] player2 = new int[26]; //initializes 2nd player's deck 

     System.arraycopy(deck, 0, player1, 0, player1.length); //takes 26 cards and puts into player1's deck. 
     System.arraycopy(deck, player1.length, player2, 0, player2.length);//takes the other 26 and puts into player2's deck. 
     System.out.printf(".\n"); 
     System.out.printf(".\n"); 
     System.out.printf("Here's your deck. --> [?]\n"); 
     System.out.printf(".\n"); 
     System.out.printf(".\n"); 
     System.out.printf("Here's your opponent's deck. --> [?]\n"); 
     System.out.printf(".\n"); 
     System.out.printf(".\n"); 
     System.out.printf("Okay, lets begin!\n"); 
     System.out.printf("\n"); 
     System.out.printf("\n"); 
     System.out.printf("Type 1 to flip your cards.\n"); 
     System.out.printf("Which ever player holds all the cards by the end wins.\n"); 
     int q = keyboard.nextInt(); 
     if (q == 1){ 
      System.out.printf("Lets see...\n"); 
     } 
     if (q != 1){ 
      System.out.printf("That wasn't one... type the number '1' or else the game will end because you are stupid.\n"); 
      int q2 = keyboard.nextInt(); 
       if (q != 1) { 
       System.exit(-1); 
      }  
     } 
     List<Integer> player1List = new ArrayList<Integer>();//converts array1 into arraylist1 
     for (int i = 0; i < player1.length; i++) 
     { 
      player1List.add(player1[i]); 
     } 
     System.out.println(player1List); 
     List<Integer> player2List = new ArrayList<Integer>();//converts array2 into arraylist2 
     for (int i = 0; i < player2.length; i++) 
     { 
      player2List.add(player2[i]); 
     } 
     System.out.println(player2List); 


     for (int i = 0; i < player1List.size(); i++){ 
      if (player1List.get(i) < player2List.get(i)){ 
       System.out.printf("Player 1: %d\n", player1List.get(i)); 
       System.out.printf("Player 2: %d\n", player2List.get(i)); 
       System.out.printf("Player 2 wins round!\n"); 
       player2List.add(player1List.get(i)); 
       player1List.remove(player1List.get(i)); 
      } 
      if (player1List.get(i) < player2List.get(i)){ 
       System.out.printf("Player 1: %d\n", player1List.get(i)); 
       System.out.printf("Player 2: %d\n", player2List.get(i)); 
       System.out.printf("Player 1 wins round!\n"); 
       player1List.add(player2List.get(i)); 
       player2List.remove(player2List.get(i)); 
      } 
      if (player1List.get(i) == player2List.get(i)){ 
       System.out.printf("Player 1: %d\n", player1List.get(i)); 
       System.out.printf("Player 2: %d\n", player2List.get(i)); 
       System.out.printf("It's a tie, cards return to your deck.\n"); 
      } 
     } 
     if (player1List.isEmpty() || player2List.isEmpty()){ 
      if (player1List.isEmpty()) { 
       System.out.printf("Player 2 WINS THE GAME!!!!!!\n"); 
      } 
      if (player2List.isEmpty()) { 
       System.out.printf("Player 1 WINS THE GAME!!!!!!\n"); 
      } 
     } 


     System.out.println(player1List); 

     System.out.println(player2List); 


     /* 
     System.out.printf("test.\n"); 
     System.out.printf("test1111.\n"); 
     for(int i = 0; i < player1.length; i++){ 
      System.out.println(player1[i]); 
     } 
     System.out.printf("test.\n"); 
     System.out.printf("test222.\n"); 
     for(int i = 0; i < player2.length; i++){ 
      System.out.println(player2[i]); 
     } 
     System.out.printf("test.\n"); 
     System.out.printf("test11.\n"); 
     for(int i = 0; i < win1.length; i++){ 
      System.out.println(win1[i]); 
     } 
     System.out.printf("test.\n"); 
     System.out.printf("test22.\n"); 
     for(int i = 0; i < win2.length; i++){ 
      System.out.println(win2[i]); 
     } 
     */ 

     System.out.printf("\n"); 
     System.out.printf("\n"); 
    } 
} 

여기 내 for 루프가 좁혀졌습니다. 나는 arraylists 중 하나가 비어 때까지 그것은 반복 그래서 그것을 필요합니다.

+1

숙제와 비슷하기 때문에 숙제 태그를 추가했습니다. 그렇지 않은 경우 태그를 제거하십시오. –

+0

추한 코드, 더 많은 방법으로 헤어 졌어. –

+0

알다시피, 이것은 프로그램을위한 나의 기반 일뿐입니다. 정리가 끝나고 –

답변

0

발생하는 문제는 반복되는 ArrayList를 변경했기 때문에 발생한다고 생각합니다.

예 : 위의 코드에서

for (int i = 0; i < player1List.size(); i++){ 

    player1List.remove(i); 

} 

는 루프 들어가면, i0로 설정 player1List.size()는 2를 반환한다 - 따라서 루프가 입력된다. 이 루프 동안 요소가 ArrayList에서 제거되고 루프의 두 번째 반복을 확인하면 player1List.size()가 이제 1을 반환합니다 (요소가 제거되었으므로). 그러면 루프 검사가 실패하고 그렇지 않습니다. 두 번째로 돌아 가라.

반복하는 목록을 변경하는 것을 포함하지 않는 방법을 찾아야합니다. (아마도 clone()는? 아니면 임시 배열에 요소를 복사?)

HTH

1

당신의 한면에 (제공) 카드를 둘 필요가 나는, 당신의 갑판을위한 Queue를 사용하여 제안을 (아래쪽) 그리고 (이 게임에 대한 위키피디아 항목에서 알 수있는 한) 상단에서 카드 (poll)를 가져옵니다. A LinkedListQueue을 잘 구현 한 것입니다. 코드는 다음과 같이 갈 수 :

while(!deck1.isEmpty() && !deck2.isEmpty()) { 
    int card1 = deck1.poll(); 
    int card2 = deck2.poll(); 

    if(card1 < card2) { 
     deck2.offer(card2); 
     deck2.offer(card1); 
    } 

    if(card2 < card1) { 
     deck1.offer(card1); 
     deck1.offer(card2); 
    } 

    if(card1 == card2) { 
     deck1.offer(card1); 
     deck2.offer(card2); 
    } 
} 
0

헤이 대신 하나 의 두 개의 카운터를 사용할 필요는 (i = 0 int로, 내가 < player1List.size(); 내가 ++) // 하나 개의 카운터를 어떤 카드가 끝났는 지 예측할 수 없다! 위한 (INT I = 0, J = 0, I < player1List.size() & & J < player2List.size(); 나는 ++, J를 ++) 다른 실수가이

같이 // : 만약 (player1List.get (i) < player2List.get (i)) // for 조건 for for 루프