2016-11-15 2 views
0

그래서 제 과제를위한 포커 게임을 만들었습니다.
모두 잘 작동합니다 (적어도 그렇게 생각합니다). 간단하게하기 : 플레이어가 100 $의 잔액을 가지고 있고 손을 기준으로이기거나 돈을 잃습니다.
나는 균형이 < 0>보다 높으면 게임에서 플레이어에게 새로운 핸드를 계속 제공한다는 조건을 만들었습니다.
그러나 7,8 번 다시 시작한 후에 프로그램은 흰색을 표시하며 입력 한 내용이 무엇이든간에 영원히 계속 실행됩니다 ...프로그램이 실행되고 아무 이유없이 중단됩니다

다음 코드는 대부분 도움이 될 수 있기를 바랍니다.

package pkgtp2; 

import java.util.*; 

public class TP2 { 

static boolean straight; 
static boolean flush; 
static int paires; 
static int gains; 
static int balance = 100; 
static char[] cardsSymbols = new char[5]; 
static String[] cardsValues = new String[5]; 
static int hand[] = new int[5]; 
static boolean pack[] = new boolean[52]; 

public static void menu(int gains) { 
    System.out.println("**************   " + " AUCUNE COMBINAISON = -10$"); 
    System.out.println("*Jeu de Poker*   " + " 1 PAIRE = 0$"); 
    System.out.println("**************   " + " 2 PAIRES = 20$"); 
    System.out.println("       " + " BRELAN <3> = 35$"); 
    System.out.println("       " + " SUITE = 50$"); 
    System.out.println("       " + " FULL = 75$"); 
    System.out.println("       " + " COULEUR = 100$"); 
    System.out.println("       " + " CARRÉ = 150$"); 
    System.out.println("       " + " STRAIGHT FLUSH = 500$"); 
} // The Game Menu showing every possible hand 

public static void drawCards(int[] hand, boolean[] pack) { 

    Random give = new Random(); 

    for (int i = 0; i < hand.length; i++) { 
     do { 
      hand[i] = give.nextInt(52); 
     } while (pack[hand[i]]); 
     pack[hand[i]] = true; 
    } 
} // Gives the user 5 unique random numbers 

public static void cardSymbol(int[] cards, char[] cardssymbols) { 
    char symboles[] = {'♥', '♦', '♣', '♠'}; 
    int numOfSymbol; 
    for (int i = 0; i < cards.length; i++) { 
     numOfSymbol = cards[i]/13; 
     cardsSymbols[i] = symboles[numOfSymbol]; 
    } 

} // Converts the 5 numbers into values 

public static void cardValue(int[] cards, String[] cardsvalues) { 
    String valeurs[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; 
    int numOfValues; 
    for (int i = 0; i < cards.length; i++) { 
     numOfValues = cards[i] % 13; 
     cardsValues[i] = valeurs[numOfValues]; 
    } 
} // Converts the 5 numbers into actual cards values 

public static void printCards(char[] cardsymbols, String[] cardsvalues) { 
    System.out.println("Voici vos cartes : \n"); 
    for (int i = 0; i < hand.length; i++) { 
     System.out.print(cardsymbols[i]); 
     System.out.print(cardsvalues[i] + " "); 
    } 
} // Prints the Cards 

public static void changeCards(int[] cards, boolean[] pack) { 
    Scanner read = new Scanner(System.in); 
    int cardchange = 0; 
    Random give = new Random(); 
    System.out.println(); 

    for (int i = 0; i < 4; i++) { 
     do { 
      System.out.println(" Entrez des chiffres de 1 à 5 correspondant aux cartes que vous désirez changer (vous pouvez changer au plus 4 cartes)" 
        + " \n Si vous voulez conserver vos cartes, entrez 0 "); 
      while (!read.hasNextInt()) { 
       System.out.println("Veuillez entrer un chiffre "); 
       read.next(); 
      } 
      cardchange = read.nextInt(); 
     } while (cardchange < 0 || cardchange > 5); 

     if (cardchange == 0) { 
      i = 5; 
     } else { 
      System.out.print("\nLa carte " + cardchange + " va être changée\n "); 
      do { 
       cards[cardchange - 1] = give.nextInt(52); 
      } while (pack[cards[cardchange - 1]]); 
      pack[cards[cardchange - 1]] = true; 
     } 

     cardSymbol(cards, cardsSymbols); 
     cardValue(cards, cardsValues); 
     printCards(cardsSymbols, cardsValues); 

    } 

} // Lets the user change up to 4 cards, press 0 to not change any card and skip 

public static int checkPairs(char[] cardsSymbols, String[] cardsvalues) { 
    int paires = 0; 
    for (int i = 0; i < cardsvalues.length; i++) { 
     for (int j = i + 1; j < cardsvalues.length; j++) { 
      if (cardsvalues[i].equals(cardsvalues[j])) { 
       paires++; 
      } 
     } 
    } 
    return paires; 
} // Checks if the cards values match so the program can determine if the user has a pair, two pair, three of a kind, full house or quads 

public static boolean checkFlush(char[] cardsSymbols) { 
    boolean flush = false; 
    if (cardsSymbols[0] == cardsSymbols[1] && cardsSymbols[1] == cardsSymbols[2] && cardsSymbols[2] == cardsSymbols[3] && cardsSymbols[3] == cardsSymbols[4]) { 
     flush = true; 
    } 
    return flush; 
} // checks for a flush 

public static boolean checkStraight(String[] cardsvalues) { 
    boolean straight = false; 
    int numOfValues; 
    int[] straightTab = new int[5]; 
    for (int i = 0; i < hand.length; i++) { 
     numOfValues = hand[i] % 13; 
     straightTab[i] = numOfValues; 
    } 
    Arrays.sort(straightTab); 

    if (straightTab[0] + 1 == straightTab[1] && straightTab[1] + 1 == straightTab[2] && straightTab[2] + 1 == straightTab[3] && straightTab[3] + 1 == straightTab[4]) { 
     straight = true; 
    } 
    return straight; 
} // checks for a straight 

public static int combinaisons(int paires, boolean straight, boolean flush) { 
    int gains; 
    if (straight && flush) { 
     System.out.println("Vous avez une Quinte !"); 
     gains = 500; 
    } else if (flush) { 
     System.out.println("Vous avez une Couleur !"); 
     gains = 100; 
    } else if (straight) { 
     System.out.println("Vous avez une Suite !"); 
     gains = 50; 
    } else if (paires == 6) { 
     System.out.println("Vous avez un Carré !"); 
     gains = 150; 
    } else if (paires == 4) { 
     System.out.println("Vous avez un Full !"); 
     gains = 75; 
    } else if (paires == 3) { 
     System.out.println("Vous avez un Brelan !"); 
     gains = 35; 
    } else if (paires == 2) { 
     System.out.println("Vous avez Deux Paires !"); 
     gains = 20; 
    } else if (paires == 1) { 
     System.out.println("Vous avez une Paire !"); 
     gains = 0; 
    } else { 
     System.out.println("Vous n'avez Aucune Combinaison !"); 
     gains = -10; 
    } 
    return gains; 
} // gives the user money based on his hand strength 

public static int profit(int gains) { 
    balance = balance + gains; 
    System.out.println("Vos gains sont de " + gains + " $"); 
    System.out.println("Vous avez donc " + balance + " $"); 
    return gains; 
} // Gives the user his profit and his new balance 

public static void main(String[] args) { 
    do { 
     menu(gains); 
     drawCards(hand, pack); 
     cardSymbol(hand, cardsSymbols); 
     cardValue(hand, cardsValues); 
     printCards(cardsSymbols, cardsValues); 
     changeCards(hand, pack); 
     paires = checkPairs(cardsSymbols, cardsValues); 
     flush = checkFlush(cardsSymbols); 
     straight = checkStraight(cardsValues); 
     gains = combinaisons(paires, straight, flush); 
     profit(gains); 
    }while (balance > 0); 
    }// Here is my problem, the program just stops for a reason ... 

} 
+0

콘솔 오류가없는 것으로 간주 할 수 있습니다 ...? –

+0

나는 믿지 않는다. 나는 코드를 복사하여 붙여 넣기 만하면된다. – Adriann

+0

죄송합니다. 나는 이것이 자바 **가 아니 었음을 완전히 놓쳤다. ** Javascript. 내 잘못이야. –

답변

3

drawCards 방법, pack[hand[i] = true 결국 (후 10 정도 손) 모든 pack[hand[i]while (pack[hand[i]]); 원인 true에 무한 루프에 들어갈 설정 : 코드가 프랑스어로, 난 당신이 이해하는 데 도움이 영어로 방법 댓글을 달았습니다 .

pack[hand[i]] = true;에서 pack[hand[i]] = false;으로 변경하면 무한 루프가 방지되어 게임을 계속할 수 있습니다.

public static void reset_pack(){ 
    for(int i=0; i<pack.length;i++){ 
     pack[i] = false; 
    } 
} 

각 라운드 후이 메소드를 호출 의견 OP에 의해 제안

한 가지 방법은, 방법을 수행 할 수 있습니다 각 라운드 후 "팩"배열을 다시 설정하는 것입니다 :

... 
profit(gains); 
reset_pack(); 
+0

고마워, 그게 작동하지만 동일한 카드가 두 번 생성 될 수 있기 때문에 내 코드 버그를 만들 것입니다 ... "팩"배열을 매 라운드마다 초기화하는 방법에 대한 아이디어? – Adriann

+0

오, 좋은 지적이야! 내 논리를 테스트하지 않았으므로 지적 해 주셔서 감사합니다. 실제로 "팩"배열을 재설정하는 것이 더 나은 방법입니다. 아마도 "팩"배열을 재설정하는 방법이 효과가있을 것입니다. 내 대답을 편집하고 코드를 추가하여 배열을 재설정합니다. – davedwards

+0

신경 쓰지 마세요! 나는 당신의 도움을 위해 문제를 해결해 주셔서 감사합니다! – Adriann

2

감사는 무한 루프를 만들 것이라고 지적에 대해 하향 변속합니다. 여기
내가 추가하는 데 필요한 것입니다 :

for (int i=0;i<hand.length;i++){ 
pack[hand[i]] = false; 
} 

는 "changeCards"방법 후 프로그램이 늘 라운드의 나머지 더 이상 필요 "팩"배열을 재설정 할 수 있다는 조건을 추가. 즉, 프로그램에서 다음 라운드의 값을 다시 선택할 수 있으므로 8 회 또는 9 회 시도 후에도 멈추지 않습니다!
감사합니다.

관련 문제