Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 03-23-2005, 10:30 AM   #1 (permalink)
zen_tom
Guest
 
[java] Contiguous Array Test

I'm writing a client-server poker-tournament game (which I later want to extend to playing other games) and have started on the part where the system analyses the hand a given player has.

I have a jCard object that represents a card. A player's hand is represented by a jHand object that contains a Stack, which contains a set of jCards.

jCard properties are:
String suit; (Text version of suit, i.e. "Clubs", "Diamonds" etc)
String code; (2 char code 1st Char = suit, 2nd char = value - i.e. HK = King of Hearts)
int value; 1-13 (Aces through Kings)

So, I've another object called a jHandAnalyser that has a number of methods for checking what a hand might be worth. The main property of this object is a jHand reference called myHand.

Calling the constructor of jHandAnalyser passes in the hand you want to analyse. e.g.

jHandAnalyser aPlayer = new jHandAnalyser(aHand);

This jHandAnalyser needs to look at the jHand object it's given and decide what the hand is worth.

So far I have a method called isFlush() that returns true if the cards are all of the same suit, but am wondering how to implement the isStrait (sp?) method. i.e. I want to check that all the cards are in a sequential run. Has anyone got any ideas how I can do this nice and cleanly?
 
Old 03-23-2005, 11:12 AM   #2 (permalink)
Once upon a time...
 
you could make sure the cards are always sorted,
then check the run length from [0] to length.
__________________
--
Man Alone
=======
Abstainer: a weak person who yields to the temptation of denying himself a pleasure.
Ambrose Bierce, The Devil's Dictionary.
manalone is offline  
Old 03-23-2005, 11:31 AM   #3 (permalink)
zen_tom
Guest
 
OK, yes, I've got that working fine - except where Aces being hi, low or Hi+Low - I converted the Stack(I want to preserve the order of dealing) to a list sorted by value and checked that after the lowest card, that the value increments by 1 each iteration.
 
Old 03-23-2005, 11:34 AM   #4 (permalink)
a-j
Tilted
 
I would say if you sort the hand it may be easier, but here is a non-sorted method I came up with, in honor of the IOCCC -- meaning it isn't obvious what I'm doing, maybe you can figure it out.

This is assuming a 5 card hand, if you are doing other sized hands You'll need to modify the check phase steps 7+. I haven't tested this, but I think it is correct

Code:
1. int val = 0;
2. int lowest = Integer.MAX_VALUE;
3. for each card value {
4.   val = val | (1 << card.value);
5.   lowest = Math.min(card.value, lowest);
6. }
7. if (val == 0x3C02) return true;
8. val = val >> lowest;
9. return val == 0x1F;
a-j is offline  
Old 03-23-2005, 11:35 AM   #5 (permalink)
a-j
Tilted
 
If I recall a ace cannot be high and low in a straight. Q K A 2 3 is not a valid straight.
a-j is offline  
Old 03-23-2005, 11:45 AM   #6 (permalink)
zen_tom
Guest
 
it can't be high and low in a straight, but it can be high or low in a hand - so A2345 and TJQKA could both be valid straights.

If an ace is given value 1 (i.e. aces are low), then i can start increment-testing my card-values from 1, it it's high, then I can assume a value of 14, and expect it at the end of any straights - but if it's simultaneously 1 and 14, I'm going to need to do some special coding for it (which I'd rather avoid, but which looks unavoidable at the mometn)

a-j is that bit-shifting you're doing there? That's not a bad idea at all - any hand could be held as a 52 bit bitmask. The only problem is squeezing in cards of the same value but different suits...
 
Old 03-23-2005, 12:03 PM   #7 (permalink)
zen_tom
Guest
 
I've just gone away and had a think about this, and think that using a bitmask is genius!
so each card value is assigned a bit i.e.
Code:
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][1] = Ace
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][1][ ] = Duece
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][1][ ][ ] = 3
[ ][ ][ ][ ][ ][ ][ ][ ][ ][1][ ][ ][ ] = 4
[ ][ ][ ][ ][ ][ ][ ][ ][1][ ][ ][ ][ ] = 5
[ ][ ][ ][ ][ ][ ][ ][1][ ][ ][ ][ ][ ] = 6
[ ][ ][ ][ ][ ][ ][1][ ][ ][ ][ ][ ][ ] = 7
[ ][ ][ ][ ][ ][1][ ][ ][ ][ ][ ][ ][ ] = 8
[ ][ ][ ][ ][1][ ][ ][ ][ ][ ][ ][ ][ ] = 9
[ ][ ][ ][1][ ][ ][ ][ ][ ][ ][ ][ ][ ] = 10
[ ][ ][1][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] = Jack
[ ][1][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] = Queen
[1][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] = King

Then, all the cards in a hand are ORed into a hand array

[ ][ ][ ][ ][ ][ ][1][ ][ ][ ][ ][ ][ ] = 7
[ ][ ][ ][ ][ ][1][ ][ ][ ][ ][ ][ ][ ] = 8
[ ][ ][ ][ ][1][ ][ ][ ][ ][ ][ ][ ][ ] = 9
[ ][ ][ ][1][ ][ ][ ][ ][ ][ ][ ][ ][ ] = 10
[ ][ ][1][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] = Jack

[ ][ ][1][1][1][1][1][ ][ ][ ][ ][ ][ ] = All Cards

Now by finding the first 1 and the last one, and filling in the gaps, I should be able to make a mask.

[ ][ ][1][1][1][1][1][ ][ ][ ][ ][ ][ ] = Mask

Now if I XOR the mask and the card array, I should always get zero if there is a straight, or something else if not.
It also solves the roaming Ace problem too.
 
Old 03-23-2005, 12:04 PM   #8 (permalink)
zen_tom
Guest
 
What's the significance of the number 0x3C02 ?
 
Old 03-23-2005, 12:23 PM   #9 (permalink)
a-j
Tilted
 
I special cased the the A high/low problem.

In binary it is 11 1100 0000 0010
Using the value of the card as the offset in the bitmask (A=1, K=13), the above number represents K Q J T A.
a-j is offline  
Old 03-23-2005, 12:31 PM   #10 (permalink)
zen_tom
Guest
 
I like it - thanks for the idea - again, genius!
 
Old 03-24-2005, 09:40 AM   #11 (permalink)
zen_tom
Guest
 
OK, I've got it to work and put in an acesHiLow variable (-1 for low, 0 for either or 1 for high) and this should work as a test to see if all the cards in a given hand are in a run.

Where aces are high or low, the thing only works for 5-card hands, but hopefully I'll work out a way to fix it so it will work for any number of cards.

Code:
public boolean isStrait(int acesHiLow){
		boolean isstrait = false;
		int maskval = 1;
		int val = 0;
		int lowest = Integer.MAX_VALUE;
		for (int i = 0; i< myHand.size();i++) {
			if (acesHiLow == -1 || myHand.card(i).value != 1){
			val = val | (1 << myHand.card(i).value);
			maskval = maskval << 1;
			lowest = Math.min(myHand.card(i).value, lowest);}
			if (acesHiLow == 0 && myHand.card(i).value == 1){
				val = val | (1 << myHand.card(i).value);
				val = val | (1 << 14);
				maskval = maskval << 1;
				maskval = maskval << 1;
				lowest = Math.min(myHand.card(i).value, lowest);}
			if (acesHiLow == 1 && myHand.card(i).value == 1){
				val = val | (1 << 14);
				maskval = maskval << 1;
				lowest = Math.min(14, lowest);}			
			}
		val = val >> lowest;

		if (val + 1 == maskval){return true;}
		if ((acesHiLow == 0 && val == 8223) || (acesHiLow == 0 && val == 15873)){return true;}
		// System.out.println(val + " " + maskval );
		return false;
	}
Thanks again.
 
 

Tags
array, contiguous, java, test


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 Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 07:45 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360