Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 12-18-2004, 01:00 AM   #1
Member (9 bit)
 
colecifer's Avatar
 
Join Date: Jan 2004
Location: Kansas City(westwood), KS
Posts: 458
Problem with my java program

I'm very bored and decided to start a little project over winter break. I'm working on a texas hold 'em program. Right now the program is very simple and i plan on making it much better later. right now it doesn't even get to the flop, just deals a starting hand. The problem is my random is not working, if i create a new object my program uses the same values that it got last time. I was thinking it wouldn't do this because its Objects and the should be seperate. Here is my code please check it out, its making me a little crazy
Code:
import java.awt.*;
//import java2.swing.*;
import java.util.Random;

	 
public class Poker
{
	public static void main(String args[])
	{
	Cards play = new Cards();
	play.dealHand();
	System.out.println(play.getHand());
	Cards r = new Cards();
	r.dealHand();
	System.out.println(r.getHand());
	}
}
class Cards
{

private String hand;
private int card1;
private int card2;
private String han1;
private String han2;
private int card1suit;
private int card2suit;
private String car1suit;
private String car2suit;



public Cards()
{
System.out.println("shuffling cards...");
hand = " ";
card1 = 0;
card2 = 0;
han1 = " ";
han2 = " ";
card1suit = 0;
card2suit = 0;
car1suit = " ";
car2suit = " ";

}

public String dealHand()
{
  
    Random gen = new Random();
	card1 = gen.nextInt(12)+1;
	card2 = gen.nextInt(12)+1;
	card1suit = gen.nextInt(4)+1;
	card2suit = gen.nextInt(4)+1;	

		

	
	switch (card1suit)
	{
		case 1 :
			car1suit = " of spades";
			break;
		case 2 :
			car1suit = " of clubs";
			break;
		case 3 :
			car1suit = " of hearts";
			break;
		case 4 :
			car1suit = " of diamonds";
			break;
	}

	
	switch (card2suit)
	{
		case 1 :
			car2suit = " of spades";
			break;
		case 2 :
			car2suit = " of clubs";
			break;
		case 3 :
			car2suit = " of hearts";
			break;
		case 4 :
			car2suit = " of diamonds";
			break;
	}
	han1 = card1 + car1suit;
	han2 = card2 + car2suit;	
	if (card1 == 1)
		{
		han1 = "Ace" + car1suit;
	
		}
	if (card2 == 1)
		{
	
		han2 = "Ace" + car2suit;
		}
	if (card1 == 11)
		{
		han1 = "Jack" + car1suit;
	
		}
	if (card2 == 11)
		{
	
		han2 = "Jack" + car2suit;
		}
	if (card1 == 12)
		{
		han1 = "Queen" + car1suit;
	
		}
	if (card2 == 12)
		{

		han2 = "Queen" + car2suit;
		}
	if (card2 == 13)
		{
	
		han2 = "King" + car2suit;
		}
	if (card1 == 12)
		{
		han1 = "King" + car1suit;
	
		}
	
	return hand = han1 +" " + han2;
	
	}	 
public String getHand()
{
	
	return dealHand();
}	
	
	}
colecifer is offline   Reply With Quote
Old 12-18-2004, 07:02 AM   #2
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
Call Random.setSeed before calling Random.nextInt() :
Code:
Random gen = new Random();
gen.setSeed(System.currentTimeMillis());
aym is offline   Reply With Quote
Old 12-18-2004, 12:43 PM   #3
Member (9 bit)
 
colecifer's Avatar
 
Join Date: Jan 2004
Location: Kansas City(westwood), KS
Posts: 458
I'm still having the same problem, thanks for the tip though. I never knew you could use the time as your seed.

EDIT
I threw a loop into the program to slow it down and that for whatever reason worked. Now i'm working on making the program more advanced.

Last edited by colecifer; 12-18-2004 at 10:43 PM.
colecifer is offline   Reply With Quote
Old 12-19-2004, 09:00 PM   #4
Member (9 bit)
 
colecifer's Avatar
 
Join Date: Jan 2004
Location: Kansas City(westwood), KS
Posts: 458
sorry about double post, but i can no longer edit my earlier one.
Ok so i got the program working well. It deals out multiple hands and it does the flop, turn, and river. The only problem, i can not think of a way to make it so it won't deal the same card to people. I just can't think of a good way. There is probably some easy method i just haven't learned yet.
here is the code
Code:
import java.awt.*;
//import java2.swing.*;
import java.util.Random;

	 
public class Poker
{
	public static void main(String args[])
	{
//	int x;
//	int y = 0;	
	Cards play = new Cards(5);
	
	
	play.dealHand();
	System.out.println();
	play.dealFlop();
	System.out.println();
	play.dealTurn();
	System.out.println();
	play.dealRiver();
	Cards r = new Cards(3);
	r.dealHand();
//	System.out.println(r.getHand());
//	Cards whatever = new Cards();
//	System.out.println(whatever.getHand());
	}
}
class Cards
{

private String hand;
private int card1;
private int card2;
private String han1;
private String han2;
private int card1suit;
private int card2suit;
private String car1suit;
private String car2suit;
private int x;
private int y = 0;
private int numHands;
private int a;
private int b;
private int card3;
private int card3suit;
private String han3;
private String car3suit;

public Cards(int n1)
{
System.out.println("shuffling cards...");
numHands = n1;
	
}

public void dealHand()
{
 for(a=1;a<=numHands;a++) 
 {
 
    Random gen = new Random();
    gen.setSeed(System.currentTimeMillis());
	card1 = gen.nextInt(12)+1;
	card2 = gen.nextInt(12)+1;
	card1suit = gen.nextInt(4)+1;
	card2suit = gen.nextInt(4)+1;	
	for(x=1;x<=100000000;x++)
   	 	y++;
		

	
	switch (card1suit)
	{
		case 1 :
			car1suit = " of spades";
			break;
		case 2 :
			car1suit = " of clubs";
			break;
		case 3 :
			car1suit = " of hearts";
			break;
		case 4 :
			car1suit = " of diamonds";
			break;
	}

	
	switch (card2suit)
	{
		case 1 :
			car2suit = " of spades";
			break;
		case 2 :
			car2suit = " of clubs";
			break;
		case 3 :
			car2suit = " of hearts";
			break;
		case 4 :
			car2suit = " of diamonds";
			break;
	}

	han1 = card1 + car1suit;
	han2 = card2 + car2suit;
		
	if (card1 == 1)
		{
		han1 = "Ace" + car1suit;
	
		}
	if (card2 == 1)
		{
	
		han2 = "Ace" + car2suit;
		}
	if (card1 == 11)
		{
		han1 = "Jack" + car1suit;
	
		}
	if (card2 == 11)
		{
	
		han2 = "Jack" + car2suit;
		}
	if (card1 == 12)
		{
		han1 = "Queen" + car1suit;
	
		}
	if (card2 == 12)
		{

		han2 = "Queen" + car2suit;
		}
	if (card2 == 13)
		{
	
		han2 = "King" + car2suit;
		}
	if (card1 == 13)
		{
		han1 = "King" + car1suit;
		}
	
		
	hand = han1 + " " +han2;
	System.out.println(hand);

	}
	
	}	 
	
public void dealFlop()
	{
	
    Random gen = new Random();
    gen.setSeed(System.currentTimeMillis());
	card1 = gen.nextInt(12)+1;
	card2 = gen.nextInt(12)+1;
	card3 = gen.nextInt(12)+1;
	card1suit = gen.nextInt(4)+1;
	card2suit = gen.nextInt(4)+1;	
	card3suit = gen.nextInt(4)+1;
	
	for(x=1;x<=100000000;x++)
   	 	y++;
		

	
	switch (card1suit)
	{
		case 1 :
			car1suit = " of spades";
			break;
		case 2 :
			car1suit = " of clubs";
			break;
		case 3 :
			car1suit = " of hearts";
			break;
		case 4 :
			car1suit = " of diamonds";
			break;
	}

	
	switch (card2suit)
	{
		case 1 :
			car2suit = " of spades";
			break;
		case 2 :
			car2suit = " of clubs";
			break;
		case 3 :
			car2suit = " of hearts";
			break;
		case 4 :
			car2suit = " of diamonds";
			break;
	}
	switch (card3suit)
	{
		case 1 :
			car3suit = " of spades";
			break;
		case 2 :
			car3suit = " of clubs";
			break;
		case 3 :
			car3suit = " of hearts";
			break;
		case 4 :
			car3suit = " of diamonds";
			break;
	}
	han1 = card1 + car1suit;
	han2 = card2 + car2suit;
	han3 = card3 + car3suit;	
	if (card1 == 1)
		{
		han1 = "Ace" + car1suit;
	
		}
	if (card2 == 1)
		{
	
		han2 = "Ace" + car2suit;
		}
	if (card1 == 11)
		{
		han1 = "Jack" + car1suit;
	
		}
	if (card2 == 11)
		{
	
		han2 = "Jack" + car2suit;
		}
	if (card1 == 12)
		{
		han1 = "Queen" + car1suit;
	
		}
	if (card2 == 12)
		{

		han2 = "Queen" + car2suit;
		}
	if (card2 == 13)
		{
	
		han2 = "King" + car2suit;
		}
	if (card1 ==13)
		han1 = "King" + car1suit;
	if (card3 ==1)
		han3 = "Ace" + car3suit;
	if (card3 ==11)
		han3 = "Jack" + car3suit;
	if (card3 ==12)
		han3 = "Queen" + car3suit;			
	if (card3 ==13)
		han3 = "King" + car3suit;
	hand = han1 + " " +han2 + " " + han3;
	System.out.println("Flop..." +hand);
	
	}
public void dealTurn()
	{
	Random gen = new Random();
    gen.setSeed(System.currentTimeMillis());
	card1 = gen.nextInt(12)+1;
	card1suit = gen.nextInt(4)+1;
	switch (card1suit)
	{
		case 1 :
			car1suit = " of spades";
			break;
		case 2 :
			car1suit = " of clubs";
			break;
		case 3 :
			car1suit = " of hearts";
			break;
		case 4 :
			car1suit = " of diamonds";
			break;
	}
	han1 = card1 + car1suit;
	if (card1 == 1)
		{
		han1 = "Ace" + car1suit;
		}
	if (card1 == 13)
		{
		han1 = "King" + car1suit;
		}
	if (card1 == 11)
		{
		han1 = "Jack" + car1suit;
	if (card1 == 12)
		{
		han1 = "Queen" + car1suit;
		}
		}
	System.out.println("Turn..." + han1);				
	}
public void dealRiver()
	{
	Random gen = new Random();
    gen.setSeed(System.currentTimeMillis());
	card1 = gen.nextInt(12)+1;
	card1suit = gen.nextInt(4)+1;
	switch (card1suit)
	{
		case 1 :
			car1suit = " of spades";
			break;
		case 2 :
			car1suit = " of clubs";
			break;
		case 3 :
			car1suit = " of hearts";
			break;
		case 4 :
			car1suit = " of diamonds";
			break;
	}
	han1 = card1 + car1suit;
	if (card1 == 1)
		{
		han1 = "Ace" + car1suit;
		}
	if (card1 == 13)
		{
		han1 = "King" + car1suit;
		}
	if (card1 == 11)
		{
		han1 = "Jack" + car1suit;
	if (card1 == 12)
		{
		han1 = "Queen" + car1suit;
		}
		}
	System.out.println("River..." + han1);				
	}	
	}
I know the code is long, but this is really driving me crazy.
colecifer is offline   Reply With Quote
Old 12-21-2004, 04:33 AM   #5
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
In the long run, you need to store info about cards in one place, a class that has an array for all cards, can be a boolean array for now, if a card is dealt, set its variable in the array to true.

Later, when dealing another card, you need to check its var in the array and make sure it hasn't been dealt before.
aym is offline   Reply With Quote
Old 12-21-2004, 01:23 PM   #6
Member (9 bit)
 
colecifer's Avatar
 
Join Date: Jan 2004
Location: Kansas City(westwood), KS
Posts: 458
Quote:
Originally Posted by aym_7
In the long run, you need to store info about cards in one place, a class that has an array for all cards, can be a boolean array for now, if a card is dealt, set its variable in the array to true.

Later, when dealing another card, you need to check its var in the array and make sure it hasn't been dealt before.
I was thinking of something like that, but haven't learned arrays. Why i am thinking about doing is first of changing the way i generate cards and combining the card and suit generation. Then i'll get seperate variables for the flop turn and river. That way i can check them against the hands. I'm also going to make a maximum number of players, probably 10 because in a real game of hold 'em you woudln't want more then that anyway. This way they'll all have seperate variables and i can check them against each other.
colecifer is offline   Reply With Quote
Old 12-21-2004, 03:39 PM   #7
Member (13 bit)
 
Floppyman's Avatar
 
Join Date: Mar 1999
Posts: 6,791
I'm gonna agree with aym_7 on this one. You're gonna want an array or other type of datastructure to keep track of what cards have been dealt. Arrays in java are actually pretty simple to implement and use. Search on google and it will probably take you no more than 10 minutes to learn how to use them. HTH
Floppyman is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:14 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2