2017-03-24 2 views
-1

나는 내 자신을위한 자바를 배우고 있는데,이 문제가있다.왜 내 간단한 포커 프로그램에서 같은 카드를 계속 그립니다?

매번 AI와 나는 매 라운드마다 5 개의 Ace of Spades를 뽑는다. 이 문제를 어떻게 해결할 수 있습니까?

import java.io.IOException; 
import java.util.Scanner; 

public class poker { 
    static Scanner input = new Scanner(System.in); 
    public static void main(String[] args) throws IOException { 
     int score1 = 0,score2 = 0; 
     for (int i=0; i<10; i++) { 
      int[][] previous_cards = new int[0][2]; 
      int[][] hand1 = generate_hand(previous_cards); 
      int[][] sorted1 = sort_hand(hand1); 
      System.out.println("Round "+(i+1)+": "); 
      System.out.print(" Your hand: "); 
      print_hand(sorted1); 
      int identify_hand1 = identify_hand(sorted1); 
      System.out.print(" "); 
      print_identify_hand(identify_hand1); 
      System.out.println(); 
      int[][] hand2 = generate_hand(hand1); 
      int[][] sorted2 = sort_hand(hand2); 
      System.out.print(" Computer hand: "); 
      print_hand(sorted2); 
      int identify_hand2 = identify_hand(sorted2); 
      System.out.print(" "); 
      print_identify_hand(identify_hand2); 
      System.out.println(); 
      int compared = compare_hands(sorted1,sorted2); 
      if (compared==-1) 
       System.out.println(" You win this round!"); 
      else if (compared==1) 
       System.out.println(" The computer wins this round!"); 
      else System.out.println("Draw!"); 
      score1 += (compared<0)?1:0; 
      score2 += (compared>0)?1:0; 
      System.out.println(" Score: You:"+score1+" - Computer:"+score2); 
      input.nextLine(); 
     } 
     if (score1<score2) 
      System.out.println("The computer won with: "+score2+"-"+score1+"."); 
     else if (score1==score2) 
      System.out.println("Draw: "+score1+"-"+score2+"."); 
     else System.out.println("You won with: "+score1+"-"+score2+"."); 
    } 
    public static int[][] generate_hand(int[][] previous_cards) { 
     int[][] hand = new int[5][2]; 
     return hand; 
    } 
    public static int[] generate_card() { 
     int[] card = new int[2]; 
     card[0] = (int) (Math.random()*13 + 2); 
     card[1] = (int) (Math.random()*4 + 1); 
     return card; 
    } 
    public static int compare_2_cards(int[] card1, int[] card2) { 
     return 0; 
    } 
    public static void print_hand(int[][] hand) { 
     System.out.print(card_to_String(hand[0])+", "); 
     System.out.print(card_to_String(hand[1])+", "); 
     System.out.print(card_to_String(hand[2])+", "); 
     System.out.print(card_to_String(hand[3])+", "); 
     System.out.print(card_to_String(hand[4])); 
    } 
    public static String card_to_String(int[] c) { 
     String card = ""; 
     if (2<=c[0] && c[0]<=10) 
      card += c[0]; 
     else if (c[0]==11) card += "Jack"; 
     else if (c[0]==12) card += "Queen"; 
     else if (c[0]==13) card += "king"; 
     else card += "Ace"; 
     card += " of "; 
     if (c[1]==1) card += "hearts"; 
     else if (c[1]==2) card += "diamonds"; 
     else if (c[1]==2) card += "clubs"; 
     else card += "spades"; 
     return card; 
    } 
    public static int[][] sort_hand(int[][] hand) { 
     int[][] sorted = new int[5][2]; 
     return sorted; 
    } 
    public static void print_identify_hand(int identify_hand) { 
     if (identify_hand==1) 
      System.out.print("(straight flush)"); 
     else if (identify_hand==2) 
      System.out.print("(four of a kind)"); 
     else if (identify_hand==3) 
      System.out.print("(full house)"); 
     else if (identify_hand==3) 
      System.out.print("(four of a kind)"); 
     else if (identify_hand==4) 
      System.out.print("(flush)"); 
     else if (identify_hand==5) 
      System.out.print("(straight)"); 
     else if (identify_hand==6) 
      System.out.print("(three of a kind)"); 
     else if (identify_hand==7) 
      System.out.print("(two pairs)"); 
     else if (identify_hand==8) 
      System.out.print("(one pair)"); 
     else 
      System.out.print("(nothing - high hand comparison)"); 
    } 
    public static int compare_hands(int[][] hand1,int[][] hand2) { 
// IMPLEMENT: compare 2 cards 
     return 1; 
    } 
    public static int identify_hand(int[][] hand) { 
     if (hand[0][1]==hand[1][1] && hand[1][1]==hand[2][1] && hand[2][1]==hand[3][1] && hand[3][1]==hand[4][1] && // compare that they have the same suit 
       hand[0][0]+1==hand[1][0] && hand[1][0]+1==hand[2][0] && hand[2][0]+1==hand[3][0] && hand[3][0]+1==hand[4][0]) // compare card numbers 
      return 1; 
     if (hand[0][0]==hand[1][0] && hand[1][0]==hand[2][0] && hand[2][0]==hand[3][0]) // compare card numbers 
      return 2; 
     if (hand[1][0]==hand[2][0] && hand[2][0]==hand[3][0] && hand[3][0]==hand[4][0]) // compare card numbers 
      return 2; 
     return 9; 
    } 

} 
+4

디버거를 사용하는 방법을 배워야 할 필요가 있다고 생각합니다. –

+0

'new int [0] [2]'는 데이터를 절대 보유 할 수 없습니다. 귀하의 이름이 Java 명명 규칙을 위반합니다. @Berger의 대답은 다른 빈 배열 문제를 나열합니다. –

답변

3

int[][] hand 개체가 비어 있습니다.

generate_hand 메서드는 빈 배열을 반환하고 sort_hand은 다른 빈 배열을 반환하고 다른 메서드는 배열을 전혀 변경하지 않습니다.

당신은 다음 c[0] == 0c[1] == 0에 대한 결과를 표시 card_to_String에 빈 (int 배열에 대한, 즉 각 인덱스는 0 포함되어 있음을 의미) 배열을 전달 결국, 당신이 그것을 가지고 ...이 스페이드의 에이스입니다.

관련 문제