Java - Problema con la creación de un programa de Poker

 
Vista:

Problema con la creación de un programa de Poker

Publicado por Alejandro (3 intervenciones) el 24/03/2017 15:06:26
Hola buenas, tengo creado un programa para jugar al poker en java. El problema es que tanto la IA como yo siempre robamos Ases de picas (las 5 cartas de cada uno son ases de picas en todas las rondas). Os dejo las líneas de código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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;
}
}

Espero que puedan ayudarme porque estoy intentando aprender por mi cuenta, y con esto, por mucho que le dé vueltas no lo veo. Gracias!!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Problema con la creación de un programa de Poker

Publicado por Renzo (232 intervenciones) el 24/03/2017 16:34:58
Hola
veo que tienes un metodo llamado generate_card que nunca usas. Dicho método es el encargado de generar las cartas aleatorias.
Creo que debería usarla dentro del método sort_hand y debería quedar así:


1
2
3
4
5
6
7
8
9
public static int[][] sort_hand(int[][] hand) {
	int[][] sorted = new int[5][2];
	for(int i=0;i<5;i++){
		int[] cardRnd = generate_card();
		sorted[i][0]= cardRnd[0];
		sorted[i][1]= cardRnd[1];
	}
	return sorted;
}

Prueba y nos comentas


Saludos

Renzo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Problema con la creación de un programa de Poker

Publicado por Alejandro (3 intervenciones) el 24/03/2017 16:58:08
He estado usando lo que me dijiste y funciona, pero hay veces que se repiten las cartas en la misma mano (por jemplo tener en la mano dos veces el as de diamantes). Además, por mucho que lo reinicie, la IA gana siempre jajaja.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Si pero no jajaja

Publicado por Alejandro (3 intervenciones) el 24/03/2017 17:00:47
He estado usando lo que me dijiste y funciona, pero hay veces que se repiten las cartas en la misma mano (por jemplo tener en la mano dos veces el as de diamantes). Además, por mucho que lo reinicie, la IA gana siempre jajaja.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Si pero no jajaja

Publicado por Renzo (232 intervenciones) el 24/03/2017 17:30:45
Hola
ambos jugadores usan la misma baraja cierto ? significa que si al jugador le toca el rey de corazones, a la IA no le debería tocar. Estoy en lo correcto.
Si es así deberías reformular tu método genera hand para que no repita las cartas. Se me ocurre que tengas un array para toda la clase que sirva para controlarlo.

Renzo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Si pero no jajaja

Publicado por Renzo (232 intervenciones) el 24/03/2017 17:51:48
Tu hiciste el programa ? Creo que debes reformular tu metodo de valoración de manos también.

Renzo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar