Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 2 [3]
  ignore  |  Print  
  Name that Poker hand - Tiny Code Challenge  (Read 25937 times)
0 Members and 1 Guest are viewing this topic.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #60 - Posted 2014-10-07 22:12:21 »

?

Quote
AC 8C QA JH TC: high card
AC 8C QA JH TC: one pair
Exception in thread "main" java.lang.IllegalStateException
   at Poker.main(Poker.java:39)
                                         
Offline saucymeatman
« Reply #61 - Posted 2014-10-07 22:43:35 »


Different algorithm - not sure how to count but this is about 40 chars fewer than the most recent one. Possibly buggy but handles the official test data so this way seems viable at least.

About 320:
1  
2  
3  
4  
5  
6  
7  
int h(char[]g) {
  int i=0,f=0,k=0,s=0,p=0,t=0,q=0,n=0;
  for(;i<17;i++){n=0;for(char x:g)if(x=="A23456789TJQKCDHS".charAt(i))n++;
  if(i<13){if(n>0&&i==12)k++;if(n==1)s++;else if(s<5)s=1;if(n==2)p++;if(n==3)t++;if(n==4)q++;}
  else if(n==5)f=1;}if(s<5)s=0;for(i=0;new int[]{f&s&k,f&s,q,t&p,f,s,t,p-1,p,1}[i]<=0;i++);
  return 99-i;
}

Cant you omit the "=0" when declaring the int "n"? n gets defined anyways at the beginning of the for loop.
Offline richierich
« Reply #62 - Posted 2014-10-07 22:45:05 »

?

Quote
AC 8C QA JH TC: high card
AC 8C QA JH TC: one pair
Exception in thread "main" java.lang.IllegalStateException
   at Poker.main(Poker.java:39)
                                         

Lol, OK actually in my test data I "fixed" the invalid card QA! Not sure why you got an IllegalStateException though - when I run it it just reports the QA hand as a pair because of 2 'A's (which is wrong of course).

Do we actually have to handle invalid inputs, or just that particular odd one? My solution definitely has other bugs if it has to handle anything.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline richierich
« Reply #63 - Posted 2014-10-07 22:49:35 »

Cant you omit the "=0" when declaring the int "n"? n gets defined anyways at the beginning of the for loop.

Yep looks like it - nice one!
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #64 - Posted 2014-10-07 23:01:47 »

Changed that 'QA' (lol, can't believe we didn't see that) to a 'QS':

Passed the initial battery, but the permutation generator is the real test, which it failed quickly:

Quote
AH 3H 4H 5H 6H
   Riven: flush
   Richy: straight flush
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #65 - Posted 2014-10-07 23:25:35 »

Made a more "extensible" test framework using Riven's permutation gen with our 3 submissions:

http://pastebin.java-gaming.org/0a2155d9e0717

EDIT: fixed duplicate test

Offline richierich
« Reply #66 - Posted 2014-10-08 00:24:36 »

Ah cool - didn't see the permutation tester Smiley

This version gets much further, and is a few characters longer, 325 or 330 I think.

1  
2  
3  
4  
5  
6  
7  
8  
int h(char[]g) {
  int i=0,f=0,r=0,s=0,p=0,t=0,q=0,n;
  for(;i<18;i++){n=0;for(char x:g)if(x=="A23456789TJQKACDHS".charAt(i))n++;
  if(i<14){if(n==1){s++;if(i==13&&s==5)r=1;}else if(s<5)s=0;
  if(i<13){if(n==2)p++;if(n==3)t++;if(n==4)q++;}}
  if(n==5)f=1;}s=(s<5)?0:1;for(i=0;new int[]{f&s&r,f&s,q,t&p,f,s,t,p-1,p,1}[i]<=0;i++);
  return 99-i;
}

Reaches this:
1  
2  
3  
4  
2/52, 3/52
Exception in thread "main" 2H 3H 4H 5H 6H
   Riven: royal flush
   Richy: straight flush

Which seems like a good time to quit for the night Smiley
Will try out the new test framework tomorrow....
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #67 - Posted 2014-10-08 03:08:39 »

I don't know which version of Riven's you're testing against, but that's definitely a straight flush, and your new code verifies successfully on the tester. Congratz!

EDIT: golf'd it a bit, was 325, now 304. No algorithmic changes.

1  
2  
3  
4  
5  
6  
7  
8  
int h(char[]g){
   int i=-1,f=0,r=0,s=0,p=0,t=0,q=0,n;for(;i++<17;){n=0;
   for(int x:g)if(x=="A23456789TJQKACDHS".charAt(i))n++;
   s=i<14&n>0?s==4&i==13?s+(r=1):s+1:s<5?0:s;
   if(i<13){if(n==2)p++;if(n==3)t++;if(n==4)q++;}f=n==5?1:f;}
   s=s<5?0:1;for(i=0;new int[]{f&s&r,f&s,q,t&p,f,s,t,p-1,p,1}[i]<=0;i++);
   return 99-i;
}
Offline moogie

JGO Ninja


Medals: 16
Projects: 6
Exp: 10 years


Java games rock!


« Reply #68 - Posted 2014-10-08 05:37:35 »

I WILL NOT BE DENIED



Yay i also achived 7.0.

I would be interested in seeing how our implementations differ...

my solution:

1  
class S{public static void main(String[]a){for(int i=0;i<100;)System.out.println((++i%3<1?"Fizz":"")+(i%5>0?i%3>0?i:"":"Buzz"));}}

Java4k RIP 2014
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #69 - Posted 2014-10-08 06:11:46 »

1  
2  
-   new int[]{...}[i]<=0
+   new int[]{...}[i]<1


1  
2  
-   s=s<5?0:1;for(i=0;new int[]{f&s&r,f&s,q,t&p,f,s,t,p-1,p,1}[i]<1;i++);
+   s=s<5?0:1;for(i=0;new int[]{f&s&r,f&s,q,t&p,f,s,t,p-1,p,1}[i++]<1;);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pjt33

« JGO Spiffy Duke »


Medals: 40
Projects: 4
Exp: 7 years



« Reply #70 - Posted 2014-10-08 08:39:16 »

I don't think it would take many changes to get something which can be posted to http://codegolf.stackexchange.com/questions/6494/name-the-poker-hand
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #71 - Posted 2014-10-08 13:19:27 »

@moogie
You're just going to give away the answer like that?

I actually feel kind of dumb because I had that solution (a thing or two moved around) for the longest time (the 6.3 pt solution), but simply didn't realize the class didn't have to be named 'Solution' like many of the other challenges.  Emo
Offline moogie

JGO Ninja


Medals: 16
Projects: 6
Exp: 10 years


Java games rock!


« Reply #72 - Posted 2014-10-08 20:57:47 »

yep Smiley

it frustrated me that I could not look at other submissions to see if there is any other "tricks" I could utilise! I try to share my insights to help others avoid similar frustration.

Java4k RIP 2014
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #73 - Posted 2014-10-08 21:00:36 »

it frustrated me that I could not look at other submissions to see if there is any other "tricks" I could utilise!

That's kind of the point  Tongue

I take it then that our solutions are very similar then

Pretty sure that configuration is optimal, a few things can be switched, e.g. where i is incremented, etc.
Offline moogie

JGO Ninja


Medals: 16
Projects: 6
Exp: 10 years


Java games rock!


« Reply #74 - Posted 2014-10-08 21:06:10 »

That's kind of the point  Tongue

Ah, if that is the case then I think I may not have the "correct" competition psyche... I personally like arriving at a solution in a collaborative manner.

If it offends I can remove the solution.

Java4k RIP 2014
Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #75 - Posted 2014-10-08 22:09:33 »

I used pretty much the same solution as moogie, except with a few of the ternary operators switched around.

What I find funny is that we all called the class 'S'.

Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #76 - Posted 2014-10-09 14:41:45 »

I wonder if a recursive solution might be smaller.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #77 - Posted 2014-10-09 14:45:03 »

For fizzbuzz? No, because the method declaration would be too large. It's such a tiny challenge there is no room for any fanciness.
The only one I used a recursive solution for was the sudoku solver.
Offline richierich
« Reply #78 - Posted 2014-10-09 18:44:55 »

EDIT: golf'd it a bit, was 325, now 304. No algorithmic changes.
Lol - nice one! I thought I'd golf'd it down pretty hard from the pre-obfuscated version.

Quote
I wonder if a recursive solution might be smaller.
For fizzbuzz it would surely have to recurse on main() if anything, for the reason BurntPizza said. But is there any way to get anything useful into and then out of the String[] parameter without lots of syntax? Null or not null could be used I suppose...

For poker (recurse once per card or something?) gut feeling is it would either need global variables or so many arguments the function signature would be unreasonably long.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #79 - Posted 2014-10-09 19:51:26 »

Quick attempt, not going to get much smaller, and 27 chars more than the iterative:

1  
class R{public static void main(String[]a){int i=a.length;if(i<101){System.out.println((i%3<1?"Fizz":"")+(i%5<1?"Buzz":i%3<1?"":i));main(new String[i+1]);}}}
Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #80 - Posted 2014-10-09 20:14:39 »

For fizzbuzz? No, because the method declaration would be too large. It's such a tiny challenge there is no room for any fanciness.
The only one I used a recursive solution for was the sudoku solver.

Nah, for the Tiny Poker challenge.

Tbh I hadn't given it much thought, just emphasizing that micro-optimisation can only get you so far - an algorithmic change might give much more significant gains.
Though if the algorithm doesn't benefit from a virtually free & unbounded stack, it's unlikely recursion will offer a more efficient solution.

I did wonder if Java 8's lambda syntax might be able to give significant gains ( e.g. "myList.forEach(System.out::println)"), unfortunately the package locations of even the most basic class requirement (java.util.Arrays) sinks that idea pretty quickly.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #81 - Posted 2014-10-09 20:18:48 »

Yeah I can't really think of how recursion could benefit h() off the top of my stack head.
Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #82 - Posted 2014-10-09 20:31:32 »

If they were using an older version of Java (circa Java 1.5), you'd be able to completely destroy the Java FizzBuzz highscore with this:

1  
class S{static{for(int i=0;i<100;)System.out.println((++i%3<1?"Fizz":"")+(i%5>0?i%3>0?i:"":"Buzz"));}}


Back then the static initializer would execute before the main thread threw the "No Main method" error.
As that error is delivered to stderr, not stdout, it'd still fulfill the competition's requirements.

Alas, not to be.
Offline ra4king

JGO Kernel


Medals: 506
Projects: 3
Exp: 5 years


I'm the King!


« Reply #83 - Posted 2014-10-09 21:22:48 »

Yup, I tried using that static initializer instead of a main method but it didn't work Sad

Pages: 1 2 [3]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!