• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Vortez

Poker monster

3 posts in this topic

I've wrote this code to solve project euler problem #54. It started innocently enough, hard coding winning hands conditions for the sake of 'simplicity'... Things got a little out of hand in the SolveXXXTie() functions laugh.png .

#include "Windows.h"
#include "stdio.h"

#include <Vector>
using namespace std;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

const int NumPlayers = 2;
const int NumCardsPerHand = 5;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define TIE     -1
#define PLAYER1  0
#define PLAYER2  1

enum CardSuit {
	SPADES, HEARTS, DIAMONDS, CLUBS,
};

enum CardValue {
	TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE,
};

enum HandResult {
	HIGH_CARD, ONE_PAIR, TWO_PAIRS, THREE_OF_A_KIND, STRAIGHT, FLUSH, FULL_HOUSE, FOUR_OF_A_KIND, STRAIGHT_FLUSH, ROYAL_FLUSH, 
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct CardStruct {
	CardValue v;
	CardSuit  s;
};

struct HandStruct {
	CardStruct Cards[NumCardsPerHand];
};

vector<HandStruct> PlayersCards[NumPlayers];

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CardValue CharToCardValue(char c)
{
	switch(c)
	{
	default:
	case '2': return TWO;
	case '3': return THREE;
	case '4': return FOUR;
	case '5': return FIVE;
	case '6': return SIX;
	case '7': return SEVEN;
	case '8': return EIGHT;
	case '9': return NINE;
	case 'T': return TEN;
	case 'J': return JACK;
	case 'Q': return QUEEN;
	case 'K': return KING;
	case 'A': return ACE;
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CardSuit CharToCardSuit(char c)
{
	switch(c)
	{
	default:
	case 'S': return SPADES;
	case 'H': return HEARTS;
	case 'D': return DIAMONDS;
	case 'C': return CLUBS;
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int LoadFromFile(char *fname)
{
	int NumRows = 0;

	const int bufsize = 64;
	char Buffer[bufsize];

	CardStruct cs;
	HandStruct hs;

	FILE *f = fopen(fname, "rt");
	if(f){
	
		while(1){
			ZeroMemory(Buffer, bufsize);	
			if(fgets(Buffer, bufsize, f) == NULL)
				break;

			int k = 0;
			for(int p = 0; p < NumPlayers; p++){
				for(int i = 0; i < NumCardsPerHand; i++){

					char v,s;
					v = Buffer[k++];
					s = Buffer[k++];

					cs.v = CharToCardValue(v);
					cs.s = CharToCardSuit(s);

					hs.Cards[i] = cs;

					k++;
				}
				PlayersCards[p].push_back(hs);
			}
			NumRows++;
		}
	
		fclose(f);
	}

	return NumRows;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void SwapCard(CardStruct *c1, CardStruct *c2)
{
	CardStruct temp = *c1;
	*c1 = *c2;
	*c2 = temp;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void SortCards(HandStruct *hs)
{
	bool swapped;

	do 
	{     
		swapped = false;

		for(int i = 1; i < NumCardsPerHand; i++){
			if(hs->Cards[i-1].v > hs->Cards[i].v){
				SwapCard(&hs->Cards[i-1], &hs->Cards[i]);
				swapped = true;
			}
		}
	} 
	while(swapped);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool RoyalFlush(HandStruct *hs)
{
	CardStruct *c = &hs->Cards[0];

	if((c[0].v == TEN   && 
		c[1].v == JACK  && 
		c[2].v == QUEEN && 
		c[3].v == KING  && 
		c[4].v == ACE) == false)
	{
		return false;
	}

	if((c[0].s == c[1].s && 
		c[1].s == c[2].s && 
		c[2].s == c[3].s && 
		c[3].s == c[4].s && 
		c[4].s == c[0].s) == false)
	{
		return false;
	}

	return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool StraightFlush(HandStruct *hs)
{
	CardStruct *c = &hs->Cards[0];

	if((c[1].v == c[0].v+1 && 
		c[2].v == c[1].v+1 && 
		c[3].v == c[2].v+1 && 
		c[4].v == c[3].v+1) == false)
	{
		return false;
	}

	if((c[0].s == c[1].s && 
		c[1].s == c[2].s && 
		c[2].s == c[3].s && 
		c[3].s == c[4].s && 
		c[4].s == c[0].s) == false)
	{
		return false;
	}

	return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool FourOfaKind(HandStruct *hs)
{
	CardStruct *c = &hs->Cards[0];

	bool Found = false;

	if((c[0].v == c[1].v && 
		c[1].v == c[2].v && 
		c[2].v == c[3].v))
	{
		Found = true;
	}

	if((c[1].v == c[2].v && 
		c[2].v == c[3].v && 
		c[3].v == c[4].v))
	{
		Found = true;
	}

	return Found;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool FullHouse(HandStruct *hs)
{
	CardStruct *c = &hs->Cards[0];

	bool Found = false;

	if((c[0].v == c[1].v  && 
		c[1].v == c[2].v) && 
	   (c[3].v == c[4].v))
	{
		Found = true;
	}

	if((c[0].v == c[1].v) && 
	   (c[2].v == c[3].v  && 
	    c[3].v == c[4].v))
	{
		Found = true;
	}

	return Found;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool Flush(HandStruct *hs)
{
	CardStruct *c = &hs->Cards[0];

	if((c[0].s == c[1].s && 
		c[1].s == c[2].s && 
		c[2].s == c[3].s && 
		c[3].s == c[4].s && 
		c[4].s == c[0].s) == false)
	{
		return false;
	}

	return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool Straight(HandStruct *hs)
{
	CardStruct *c = &hs->Cards[0];

	if((c[1].v == c[0].v+1 && 
		c[2].v == c[1].v+1 && 
		c[3].v == c[2].v+1 && 
		c[4].v == c[3].v+1) == false)
	{
		return false;
	}

	return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool ThreeOfaKind(HandStruct *hs)
{
	CardStruct *c = &hs->Cards[0];

	if(((c[0].v == c[1].v && c[1].v == c[2].v) && (c[0].v != c[3].v && c[0].v != c[4].v)) || 
	   ((c[1].v == c[2].v && c[2].v == c[3].v) && (c[1].v != c[0].v && c[1].v != c[4].v)) || 
	   ((c[2].v == c[3].v && c[3].v == c[4].v) && (c[2].v != c[0].v && c[2].v != c[1].v)))
	{
		return true;	
	}

	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool TwoPairs(HandStruct *hs)
{
	CardStruct *c = &hs->Cards[0];

	if((c[0].v == c[1].v && c[2].v == c[3].v && c[4].v != c[3].v) || 
	   (c[1].v == c[2].v && c[3].v == c[4].v && c[0].v != c[1].v) || 
	   (c[0].v == c[1].v && c[3].v == c[4].v && c[2].v != c[1].v && c[2].v != c[3].v))
	{
		return true;	
	}

	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool OnePair(HandStruct *hs)
{
	CardStruct *c = &hs->Cards[0];

	if((c[0].v == c[1].v && c[2].v != c[1].v && c[3].v != c[1].v && c[4].v != c[1].v) || 
	   (c[1].v == c[2].v && c[3].v != c[2].v && c[4].v != c[2].v && c[0].v != c[2].v) || 
	   (c[2].v == c[3].v && c[4].v != c[3].v && c[0].v != c[3].v && c[1].v != c[3].v) || 
	   (c[3].v == c[4].v && c[0].v != c[4].v && c[1].v != c[4].v && c[2].v != c[4].v))
	{
		return true;	
	}

	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int AnalyseHand(int p, int i)
{
	HandStruct hs = PlayersCards[p][i];
	SortCards(&hs);

	if(RoyalFlush(&hs))
		return ROYAL_FLUSH;

	if(StraightFlush(&hs))
		return STRAIGHT_FLUSH;

	if(FourOfaKind(&hs))
		return FOUR_OF_A_KIND;

	if(FullHouse(&hs))
		return FULL_HOUSE;

	if(Flush(&hs))
		return FLUSH;

	if(Straight(&hs))
		return STRAIGHT;

	if(ThreeOfaKind(&hs))
		return THREE_OF_A_KIND;

	if(TwoPairs(&hs))
		return TWO_PAIRS;

	if(OnePair(&hs))
		return ONE_PAIR;

	return HIGH_CARD;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int SolveStraightFlushTie(HandStruct *hs1, HandStruct *hs2)
{
	CardStruct *c1 = &hs1->Cards[0];
	CardStruct *c2 = &hs2->Cards[0];

	CardValue HighestP1 = TWO;
	CardValue HighestP2 = TWO;

	for(int i = 0; i < NumCardsPerHand; i++){
		if(c1[i].v > HighestP1)
			HighestP1 = c1[i].v;
		if(c2[i].v > HighestP2)
			HighestP2 = c2[i].v;
	}

	if(HighestP1 > HighestP2)
		return PLAYER1;

	if(HighestP1 < HighestP2)
		return PLAYER2;

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int SolveFourOfaKindTie(HandStruct *hs1, HandStruct *hs2)
{
	CardStruct *c1 = &hs1->Cards[0];
	CardStruct *c2 = &hs2->Cards[0];

	CardValue HighestP1 = TWO;
	CardValue HighestP2 = TWO;

	CardValue QuadValP1 = c1[2].v;
	CardValue QuadValP2 = c2[2].v;

	if(c1[0].v != c1[1].v){
		HighestP1 = c1[0].v;
	} else {
		HighestP1 = c1[4].v;
	}

	if(c2[0].v != c2[1].v){
		HighestP2 = c2[0].v;
	} else {
		HighestP2 = c2[4].v;
	}

	if(QuadValP1 > QuadValP2)
		return PLAYER1;

	if(QuadValP1 < QuadValP2)
		return PLAYER2;

	if(HighestP1 > HighestP2)
		return PLAYER1;

	if(HighestP1 < HighestP2)
		return PLAYER2;

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int SolveFullHouseTie(HandStruct *hs1, HandStruct *hs2)
{
	CardStruct *c1 = &hs1->Cards[0];
	CardStruct *c2 = &hs2->Cards[0];

	CardValue PairValP1 = TWO;
	CardValue PairValP2 = TWO;

	CardValue TriValP1 = TWO;
	CardValue TriValP2 = TWO;

	if(c1[0].v == c1[1].v && c1[1].v == c1[2].v){
		TriValP1  = c1[0].v;
		PairValP1 = c1[4].v;
	} else {
		TriValP1  = c1[4].v;
		PairValP1 = c1[0].v;
	}

	if(c2[0].v == c2[1].v && c2[1].v == c2[2].v){
		TriValP2  = c2[0].v;
		PairValP2 = c2[4].v;
	} else {
		TriValP2  = c2[4].v;
		PairValP2 = c2[0].v;
	}

	if(TriValP1 > TriValP2)
		return PLAYER1;

	if(TriValP1 < TriValP2)
		return PLAYER2;

	if(PairValP1 > PairValP2)
		return PLAYER1;

	if(PairValP1 < PairValP2)
		return PLAYER2;

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int SolveFlushTie(HandStruct *hs1, HandStruct *hs2)
{
	CardStruct *c1 = &hs1->Cards[0];
	CardStruct *c2 = &hs2->Cards[0];

	for(int i = NumCardsPerHand-1; i >= 0; i--){
		if(c1[i].v > c2[i].v){
			return PLAYER1;
		} else if(c1[i].v < c2[i].v){	
			return PLAYER2;
		}
	}

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int SolveStraightTie(HandStruct *hs1, HandStruct *hs2)
{
	CardStruct *c1 = &hs1->Cards[0];
	CardStruct *c2 = &hs2->Cards[0];

	if(c1[4].v > c2[4].v){
		return PLAYER1;
	} else if(c1[4].v < c2[4].v){	
		return PLAYER2;
	}

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int SolveThreeOfaKindTie(HandStruct *hs1, HandStruct *hs2)
{
	CardStruct *c1 = &hs1->Cards[0];
	CardStruct *c2 = &hs2->Cards[0];

	CardValue TriValP1 = TWO;
	CardValue TriValP2 = TWO;

	CardValue HighValP1[2] = {TWO, TWO};
	CardValue HighValP2[2] = {TWO, TWO};

	if(c1[0].v == c1[1].v && c1[1].v == c1[2].v){
		TriValP1 = c1[0].v;
		HighValP1[0] = c1[4].v;
		HighValP1[1] = c1[3].v;
	} else if(c1[1].v == c1[2].v && c1[2].v == c1[3].v){
		TriValP1 = c1[1].v;
		HighValP1[0] = c1[4].v;
		HighValP1[1] = c1[0].v;
	} else if(c1[2].v == c1[3].v && c1[3].v == c1[4].v){
		TriValP1 = c1[2].v;
		HighValP1[0] = c1[1].v;
		HighValP1[1] = c1[0].v;
	}

	if(c2[0].v == c2[1].v && c2[1].v == c2[2].v){
		TriValP2 = c2[0].v;
		HighValP2[0] = c2[4].v;
		HighValP2[1] = c2[3].v;
	} else if(c2[1].v == c2[2].v && c2[2].v == c2[3].v){
		TriValP2 = c2[1].v;
		HighValP2[0] = c2[4].v;
		HighValP2[1] = c2[0].v;
	} else if(c2[2].v == c2[3].v && c2[3].v == c2[4].v){
		TriValP2 = c2[2].v;
		HighValP2[0] = c2[1].v;
		HighValP2[1] = c2[0].v;
	}

	if(TriValP1 > TriValP2)
		return PLAYER1;

	if(TriValP1 < TriValP2)
		return PLAYER2;

	if(HighValP1[0] > HighValP2[0])
		return PLAYER1;

	if(HighValP1[0] < HighValP2[0])
		return PLAYER2;

	if(HighValP1[1] > HighValP2[1])
		return PLAYER1;

	if(HighValP1[1] < HighValP2[1])
		return PLAYER2;

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int SolveTwoPairsTie(HandStruct *hs1, HandStruct *hs2)
{
	CardStruct *c1 = &hs1->Cards[0];
	CardStruct *c2 = &hs2->Cards[0];

	CardValue PairValP1[2] = {TWO, TWO};
	CardValue PairValP2[2] = {TWO, TWO};

	CardValue HighValP1 = TWO;
	CardValue HighValP2 = TWO;

	if(c1[0].v == c1[1].v && c1[2].v == c1[3].v && c1[4].v != c1[3].v){
		PairValP1[0] = c1[2].v;
		PairValP1[1] = c1[0].v;
		HighValP1 = c1[4].v;
	} else if(c1[1].v == c1[2].v && c1[3].v == c1[4].v && c1[0].v != c1[1].v){
		PairValP1[0] = c1[3].v;
		PairValP1[1] = c1[1].v;
		HighValP1 = c1[0].v;
	} else if(c1[0].v == c1[1].v && c1[3].v == c1[4].v && c1[2].v != c1[1].v && c1[2].v != c1[3].v){
		PairValP1[0] = c1[3].v;
		PairValP1[1] = c1[0].v;
		HighValP1 = c1[2].v;
	}	
	
	if(c2[0].v == c2[1].v && c2[2].v == c2[3].v && c2[4].v != c2[3].v){
		PairValP2[0] = c2[2].v;
		PairValP2[1] = c2[0].v;
		HighValP2 = c2[4].v;
	} else if(c2[1].v == c2[2].v && c2[3].v == c2[4].v && c2[0].v != c2[1].v){
		PairValP2[0] = c2[3].v;
		PairValP2[1] = c2[1].v;
		HighValP2 = c2[0].v;
	} else if(c2[0].v == c2[1].v && c2[3].v == c2[4].v && c2[2].v != c2[1].v && c2[2].v != c2[3].v){
		PairValP2[0] = c2[3].v;
		PairValP2[1] = c2[0].v;
		HighValP2 = c2[2].v;
	}	
	
	if(PairValP1[1] > PairValP2[1])
		return PLAYER1;

	if(PairValP1[1] < PairValP2[1])
		return PLAYER2;

	if(PairValP1[0] > PairValP2[0])
		return PLAYER1;

	if(PairValP1[0] < PairValP2[0])
		return PLAYER2;

	if(HighValP1 > HighValP2)
		return PLAYER1;

	if(HighValP1 < HighValP2)
		return PLAYER2;

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int SolveOnePairTie(HandStruct *hs1, HandStruct *hs2)
{
	CardStruct *c1 = &hs1->Cards[0];
	CardStruct *c2 = &hs2->Cards[0];

	CardValue PairValP1 = TWO;
	CardValue PairValP2 = TWO;

	CardValue HighValP1[3] = {TWO, TWO, TWO};
	CardValue HighValP2[3] = {TWO, TWO, TWO};

	if(c1[0].v == c1[1].v && c1[2].v != c1[1].v && c1[3].v != c1[1].v && c1[4].v != c1[1].v){
		PairValP1 = c1[0].v;
		HighValP1[0] = c1[4].v;
		HighValP1[1] = c1[3].v;
		HighValP1[2] = c1[2].v;
	} else if(c1[1].v == c1[2].v && c1[3].v != c1[2].v && c1[4].v != c1[2].v && c1[0].v != c1[2].v){
		PairValP1 = c1[1].v;
		HighValP1[0] = c1[4].v;
		HighValP1[1] = c1[3].v;
		HighValP1[2] = c1[0].v;
	} else if(c1[2].v == c1[3].v && c1[4].v != c1[3].v && c1[0].v != c1[3].v && c1[1].v != c1[3].v){
		PairValP1 = c1[2].v;
		HighValP1[0] = c1[4].v;
		HighValP1[1] = c1[1].v;
		HighValP1[2] = c1[0].v;
	} else if(c1[3].v == c1[4].v && c1[0].v != c1[4].v && c1[1].v != c1[4].v && c1[2].v != c1[4].v){
		PairValP1 = c1[3].v;
		HighValP1[0] = c1[2].v;
		HighValP1[1] = c1[1].v;
		HighValP1[2] = c1[0].v;
	}

	if(c2[0].v == c2[1].v && c2[2].v != c2[1].v && c2[3].v != c2[1].v && c2[4].v != c2[1].v){
		PairValP2 = c2[0].v;
		HighValP2[0] = c2[4].v;
		HighValP2[1] = c2[3].v;
		HighValP2[2] = c2[2].v;
	} else if(c2[1].v == c2[2].v && c2[3].v != c2[2].v && c2[4].v != c2[2].v && c2[0].v != c2[2].v){
		PairValP2 = c2[1].v;
		HighValP2[0] = c2[4].v;
		HighValP2[1] = c2[3].v;
		HighValP2[2] = c2[0].v;
	} else if(c2[2].v == c2[3].v && c2[4].v != c2[3].v && c2[0].v != c2[3].v && c2[1].v != c2[3].v){
		PairValP2 = c2[2].v;
		HighValP2[0] = c2[4].v;
		HighValP2[1] = c2[1].v;
		HighValP2[2] = c2[0].v;
	} else if(c2[3].v == c2[4].v && c2[0].v != c2[4].v && c2[1].v != c2[4].v && c2[2].v != c2[4].v){
		PairValP2 = c2[3].v;
		HighValP2[0] = c2[2].v;
		HighValP2[1] = c2[1].v;
		HighValP2[2] = c2[0].v;
	}

	if(PairValP1 > PairValP2)
		return PLAYER1;

	if(PairValP1 < PairValP2)
		return PLAYER2;

	if(HighValP1[0] > HighValP2[0])
		return PLAYER1;

	if(HighValP1[0] < HighValP2[0])
		return PLAYER2;

	if(HighValP1[1] > HighValP2[1])
		return PLAYER1;

	if(HighValP1[1] < HighValP2[1])
		return PLAYER2;

	if(HighValP1[2] > HighValP2[2])
		return PLAYER1;

	if(HighValP1[2] < HighValP2[2])
		return PLAYER2;

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int SolveHighCardTie(HandStruct *hs1, HandStruct *hs2)
{
	CardStruct *c1 = &hs1->Cards[0];
	CardStruct *c2 = &hs2->Cards[0];

	for(int i = NumCardsPerHand-1; i >= 0; i--){
		if(c1[i].v > c2[i].v){
			return PLAYER1;
		} else if(c1[i].v < c2[i].v){	
			return PLAYER2;
		}
	}

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int AnalyseHands(int row)
{
	int r1 = AnalyseHand(PLAYER1, row);
	int r2 = AnalyseHand(PLAYER2, row);

	if(r1 > r2)
		return PLAYER1;

	if(r1 < r2)
		return PLAYER2;

	// Tie...
	HandStruct hs1 = PlayersCards[PLAYER1][row];
	HandStruct hs2 = PlayersCards[PLAYER2][row];
	SortCards(&hs1);
	SortCards(&hs2);

	if(r1 == STRAIGHT_FLUSH)
		return SolveStraightFlushTie(&hs1, &hs2);

	if(r1 == FOUR_OF_A_KIND)
		return SolveFourOfaKindTie(&hs1, &hs2);

	if(r1 == FULL_HOUSE)
		return SolveFullHouseTie(&hs1, &hs2);

	if(r1 == FLUSH)
		return SolveFlushTie(&hs1, &hs2);

	if(r1 == STRAIGHT)
		return SolveStraightTie(&hs1, &hs2);

	if(r1 == THREE_OF_A_KIND)
		return SolveThreeOfaKindTie(&hs1, &hs2);

	if(r1 == TWO_PAIRS)
		return SolveTwoPairsTie(&hs1, &hs2);

	if(r1 == ONE_PAIR)
		return SolveOnePairTie(&hs1, &hs2);

	if(r1 == HIGH_CARD)
		return SolveHighCardTie(&hs1, &hs2);

	return TIE;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/*void PrintResult(int res)
{
	switch(res)
	{
	case ROYAL_FLUSH:	  printf("Royal Flush\n");     break;
	case STRAIGHT_FLUSH:  printf("Straight Flush\n");  break;
	case FOUR_OF_A_KIND:  printf("Four of a kind\n");  break;
	case FULL_HOUSE:	  printf("Full House\n");      break;
	case FLUSH:			  printf("Flush\n");           break;
	case STRAIGHT:		  printf("Straight\n");        break;
	case THREE_OF_A_KIND: printf("Three of a kind\n"); break;
	case TWO_PAIRS:		  printf("Two Pairs\n");       break;
	case ONE_PAIR:		  printf("One Pair\n");        break;
	case HIGH_CARD:		  printf("High Card\n");       break;
	}
}*/

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main()
{
	int NumRows = LoadFromFile("poker.txt");

	int Tie = 0;
	int P1Wins = 0;
	int P2Wins = 0;

	for(int row = 0; row < NumRows; row++){

		int res = AnalyseHands(row);

		if(res == PLAYER1){
			printf("Player 1 Win\n");
			P1Wins++;
		} else if(res == PLAYER2){
			printf("Player 2 Win\n");
			P2Wins++;
		} else {
			printf("Tie\n");
			Tie++;
		}
	}

	printf("\nWin: %d, Lose: %d, Tie: %d\n\n", P1Wins, P2Wins, Tie);

	return 0;
}

SolveOnePairTie() is the worst of all...

 

Still, to my surprise, it worked the first shoot!

 

 

2

Share this post


Link to post
Share on other sites

Re-posted with linecounts for everyone's amusement.

001#include "Windows.h"
002#include "stdio.h"
003
004#include <Vector>
005using namespace std;
006
007////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
008////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
009////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
010
011const int NumPlayers = 2;
012const int NumCardsPerHand = 5;
013
014////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
015
016#define TIE     -1
017#define PLAYER1  0
018#define PLAYER2  1
019
020enum CardSuit {
021	SPADES, HEARTS, DIAMONDS, CLUBS,
022};
023
024enum CardValue {
025	TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE,
026};
027
028enum HandResult {
029	HIGH_CARD, ONE_PAIR, TWO_PAIRS, THREE_OF_A_KIND, STRAIGHT, FLUSH, FULL_HOUSE, FOUR_OF_A_KIND, STRAIGHT_FLUSH, ROYAL_FLUSH, 
030};
031
032////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
033
034struct CardStruct {
035	CardValue v;
036	CardSuit  s;
037};
038
039struct HandStruct {
040	CardStruct Cards[NumCardsPerHand];
041};
042
043vector<HandStruct> PlayersCards[NumPlayers];
044
045////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
046////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
047////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
048
049CardValue CharToCardValue(char c)
050{
051	switch(c)
052	{
053	default:
054	case '2': return TWO;
055	case '3': return THREE;
056	case '4': return FOUR;
057	case '5': return FIVE;
058	case '6': return SIX;
059	case '7': return SEVEN;
060	case '8': return EIGHT;
061	case '9': return NINE;
062	case 'T': return TEN;
063	case 'J': return JACK;
064	case 'Q': return QUEEN;
065	case 'K': return KING;
066	case 'A': return ACE;
067	}
068}
069
070////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
071
072CardSuit CharToCardSuit(char c)
073{
074	switch(c)
075	{
076	default:
077	case 'S': return SPADES;
078	case 'H': return HEARTS;
079	case 'D': return DIAMONDS;
080	case 'C': return CLUBS;
081	}
082}
083
084////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
085
086int LoadFromFile(char *fname)
087{
088	int NumRows = 0;
089
090	const int bufsize = 64;
091	char Buffer[bufsize];
092
093	CardStruct cs;
094	HandStruct hs;
095
096	FILE *f = fopen(fname, "rt");
097	if(f){
098	
099		while(1){
100			ZeroMemory(Buffer, bufsize);	
101			if(fgets(Buffer, bufsize, f) == NULL)
102				break;
103
104			int k = 0;
105			for(int p = 0; p < NumPlayers; p++){
106				for(int i = 0; i < NumCardsPerHand; i++){
107
108					char v,s;
109					v = Buffer[k++];
110					s = Buffer[k++];
111
112					cs.v = CharToCardValue(v);
113					cs.s = CharToCardSuit(s);
114
115					hs.Cards[i] = cs;
116
117					k++;
118				}
119				PlayersCards[p].push_back(hs);
120			}
121			NumRows++;
122		}
123	
124		fclose(f);
125	}
126
127	return NumRows;
128}
129
130////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
131////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
132////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
133
134void SwapCard(CardStruct *c1, CardStruct *c2)
135{
136	CardStruct temp = *c1;
137	*c1 = *c2;
138	*c2 = temp;
139}
140
141////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
142
143void SortCards(HandStruct *hs)
144{
145	bool swapped;
146
147	do 
148	{     
149		swapped = false;
150
151		for(int i = 1; i < NumCardsPerHand; i++){
152			if(hs->Cards[i-1].v > hs->Cards[i].v){
153				SwapCard(&hs->Cards[i-1], &hs->Cards[i]);
154				swapped = true;
155			}
156		}
157	} 
158	while(swapped);
159}
160
161////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
163////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
164
165bool RoyalFlush(HandStruct *hs)
166{
167	CardStruct *c = &hs->Cards[0];
168
169	if((c[0].v == TEN   && 
170		c[1].v == JACK  && 
171		c[2].v == QUEEN && 
172		c[3].v == KING  && 
173		c[4].v == ACE) == false)
174	{
175		return false;
176	}
177
178	if((c[0].s == c[1].s && 
179		c[1].s == c[2].s && 
180		c[2].s == c[3].s && 
181		c[3].s == c[4].s && 
182		c[4].s == c[0].s) == false)
183	{
184		return false;
185	}
186
187	return true;
188}
189
190////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
191
192bool StraightFlush(HandStruct *hs)
193{
194	CardStruct *c = &hs->Cards[0];
195
196	if((c[1].v == c[0].v+1 && 
197		c[2].v == c[1].v+1 && 
198		c[3].v == c[2].v+1 && 
199		c[4].v == c[3].v+1) == false)
200	{
201		return false;
202	}
203
204	if((c[0].s == c[1].s && 
205		c[1].s == c[2].s && 
206		c[2].s == c[3].s && 
207		c[3].s == c[4].s && 
208		c[4].s == c[0].s) == false)
209	{
210		return false;
211	}
212
213	return true;
214}
215
216////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
217
218bool FourOfaKind(HandStruct *hs)
219{
220	CardStruct *c = &hs->Cards[0];
221
222	bool Found = false;
223
224	if((c[0].v == c[1].v && 
225		c[1].v == c[2].v && 
226		c[2].v == c[3].v))
227	{
228		Found = true;
229	}
230
231	if((c[1].v == c[2].v && 
232		c[2].v == c[3].v && 
233		c[3].v == c[4].v))
234	{
235		Found = true;
236	}
237
238	return Found;
239}
240
241////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
242
243bool FullHouse(HandStruct *hs)
244{
245	CardStruct *c = &hs->Cards[0];
246
247	bool Found = false;
248
249	if((c[0].v == c[1].v  && 
250		c[1].v == c[2].v) && 
251	   (c[3].v == c[4].v))
252	{
253		Found = true;
254	}
255
256	if((c[0].v == c[1].v) && 
257	   (c[2].v == c[3].v  && 
258	    c[3].v == c[4].v))
259	{
260		Found = true;
261	}
262
263	return Found;
264}
265
266////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
267
268bool Flush(HandStruct *hs)
269{
270	CardStruct *c = &hs->Cards[0];
271
272	if((c[0].s == c[1].s && 
273		c[1].s == c[2].s && 
274		c[2].s == c[3].s && 
275		c[3].s == c[4].s && 
276		c[4].s == c[0].s) == false)
277	{
278		return false;
279	}
280
281	return true;
282}
283
284////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
285
286bool Straight(HandStruct *hs)
287{
288	CardStruct *c = &hs->Cards[0];
289
290	if((c[1].v == c[0].v+1 && 
291		c[2].v == c[1].v+1 && 
292		c[3].v == c[2].v+1 && 
293		c[4].v == c[3].v+1) == false)
294	{
295		return false;
296	}
297
298	return true;
299}
300
301////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
302
303bool ThreeOfaKind(HandStruct *hs)
304{
305	CardStruct *c = &hs->Cards[0];
306
307	if(((c[0].v == c[1].v && c[1].v == c[2].v) && (c[0].v != c[3].v && c[0].v != c[4].v)) || 
308	   ((c[1].v == c[2].v && c[2].v == c[3].v) && (c[1].v != c[0].v && c[1].v != c[4].v)) || 
309	   ((c[2].v == c[3].v && c[3].v == c[4].v) && (c[2].v != c[0].v && c[2].v != c[1].v)))
310	{
311		return true;	
312	}
313
314	return false;
315}
316
317////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
318
319bool TwoPairs(HandStruct *hs)
320{
321	CardStruct *c = &hs->Cards[0];
322
323	if((c[0].v == c[1].v && c[2].v == c[3].v && c[4].v != c[3].v) || 
324	   (c[1].v == c[2].v && c[3].v == c[4].v && c[0].v != c[1].v) || 
325	   (c[0].v == c[1].v && c[3].v == c[4].v && c[2].v != c[1].v && c[2].v != c[3].v))
326	{
327		return true;	
328	}
329
330	return false;
331}
332
333////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
334
335bool OnePair(HandStruct *hs)
336{
337	CardStruct *c = &hs->Cards[0];
338
339	if((c[0].v == c[1].v && c[2].v != c[1].v && c[3].v != c[1].v && c[4].v != c[1].v) || 
340	   (c[1].v == c[2].v && c[3].v != c[2].v && c[4].v != c[2].v && c[0].v != c[2].v) || 
341	   (c[2].v == c[3].v && c[4].v != c[3].v && c[0].v != c[3].v && c[1].v != c[3].v) || 
342	   (c[3].v == c[4].v && c[0].v != c[4].v && c[1].v != c[4].v && c[2].v != c[4].v))
343	{
344		return true;	
345	}
346
347	return false;
348}
349
350////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
351////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
352////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
353
354int AnalyseHand(int p, int i)
355{
356	HandStruct hs = PlayersCards[p][i];
357	SortCards(&hs);
358
359	if(RoyalFlush(&hs))
360		return ROYAL_FLUSH;
361
362	if(StraightFlush(&hs))
363		return STRAIGHT_FLUSH;
364
365	if(FourOfaKind(&hs))
366		return FOUR_OF_A_KIND;
367
368	if(FullHouse(&hs))
369		return FULL_HOUSE;
370
371	if(Flush(&hs))
372		return FLUSH;
373
374	if(Straight(&hs))
375		return STRAIGHT;
376
377	if(ThreeOfaKind(&hs))
378		return THREE_OF_A_KIND;
379
380	if(TwoPairs(&hs))
381		return TWO_PAIRS;
382
383	if(OnePair(&hs))
384		return ONE_PAIR;
385
386	return HIGH_CARD;
387}
388
389////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
390
391int SolveStraightFlushTie(HandStruct *hs1, HandStruct *hs2)
392{
393	CardStruct *c1 = &hs1->Cards[0];
394	CardStruct *c2 = &hs2->Cards[0];
395
396	CardValue HighestP1 = TWO;
397	CardValue HighestP2 = TWO;
398
399	for(int i = 0; i < NumCardsPerHand; i++){
400		if(c1[i].v > HighestP1)
401			HighestP1 = c1[i].v;
402		if(c2[i].v > HighestP2)
403			HighestP2 = c2[i].v;
404	}
405
406	if(HighestP1 > HighestP2)
407		return PLAYER1;
408
409	if(HighestP1 < HighestP2)
410		return PLAYER2;
411
412	return TIE;
413}
414
415////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
416
417int SolveFourOfaKindTie(HandStruct *hs1, HandStruct *hs2)
418{
419	CardStruct *c1 = &hs1->Cards[0];
420	CardStruct *c2 = &hs2->Cards[0];
421
422	CardValue HighestP1 = TWO;
423	CardValue HighestP2 = TWO;
424
425	CardValue QuadValP1 = c1[2].v;
426	CardValue QuadValP2 = c2[2].v;
427
428	if(c1[0].v != c1[1].v){
429		HighestP1 = c1[0].v;
430	} else {
431		HighestP1 = c1[4].v;
432	}
433
434	if(c2[0].v != c2[1].v){
435		HighestP2 = c2[0].v;
436	} else {
437		HighestP2 = c2[4].v;
438	}
439
440	if(QuadValP1 > QuadValP2)
441		return PLAYER1;
442
443	if(QuadValP1 < QuadValP2)
444		return PLAYER2;
445
446	if(HighestP1 > HighestP2)
447		return PLAYER1;
448
449	if(HighestP1 < HighestP2)
450		return PLAYER2;
451
452	return TIE;
453}
454
455////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
456
457int SolveFullHouseTie(HandStruct *hs1, HandStruct *hs2)
458{
459	CardStruct *c1 = &hs1->Cards[0];
460	CardStruct *c2 = &hs2->Cards[0];
461
462	CardValue PairValP1 = TWO;
463	CardValue PairValP2 = TWO;
464
465	CardValue TriValP1 = TWO;
466	CardValue TriValP2 = TWO;
467
468	if(c1[0].v == c1[1].v && c1[1].v == c1[2].v){
469		TriValP1  = c1[0].v;
470		PairValP1 = c1[4].v;
471	} else {
472		TriValP1  = c1[4].v;
473		PairValP1 = c1[0].v;
474	}
475
476	if(c2[0].v == c2[1].v && c2[1].v == c2[2].v){
477		TriValP2  = c2[0].v;
478		PairValP2 = c2[4].v;
479	} else {
480		TriValP2  = c2[4].v;
481		PairValP2 = c2[0].v;
482	}
483
484	if(TriValP1 > TriValP2)
485		return PLAYER1;
486
487	if(TriValP1 < TriValP2)
488		return PLAYER2;
489
490	if(PairValP1 > PairValP2)
491		return PLAYER1;
492
493	if(PairValP1 < PairValP2)
494		return PLAYER2;
495
496	return TIE;
497}
498
499////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
500
501int SolveFlushTie(HandStruct *hs1, HandStruct *hs2)
502{
503	CardStruct *c1 = &hs1->Cards[0];
504	CardStruct *c2 = &hs2->Cards[0];
505
506	for(int i = NumCardsPerHand-1; i >= 0; i--){
507		if(c1[i].v > c2[i].v){
508			return PLAYER1;
509		} else if(c1[i].v < c2[i].v){	
510			return PLAYER2;
511		}
512	}
513
514	return TIE;
515}
516
517////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
518
519int SolveStraightTie(HandStruct *hs1, HandStruct *hs2)
520{
521	CardStruct *c1 = &hs1->Cards[0];
522	CardStruct *c2 = &hs2->Cards[0];
523
524	if(c1[4].v > c2[4].v){
525		return PLAYER1;
526	} else if(c1[4].v < c2[4].v){	
527		return PLAYER2;
528	}
529
530	return TIE;
531}
532
533////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
534
535int SolveThreeOfaKindTie(HandStruct *hs1, HandStruct *hs2)
536{
537	CardStruct *c1 = &hs1->Cards[0];
538	CardStruct *c2 = &hs2->Cards[0];
539
540	CardValue TriValP1 = TWO;
541	CardValue TriValP2 = TWO;
542
543	CardValue HighValP1[2] = {TWO, TWO};
544	CardValue HighValP2[2] = {TWO, TWO};
545
546	if(c1[0].v == c1[1].v && c1[1].v == c1[2].v){
547		TriValP1 = c1[0].v;
548		HighValP1[0] = c1[4].v;
549		HighValP1[1] = c1[3].v;
550	} else if(c1[1].v == c1[2].v && c1[2].v == c1[3].v){
551		TriValP1 = c1[1].v;
552		HighValP1[0] = c1[4].v;
553		HighValP1[1] = c1[0].v;
554	} else if(c1[2].v == c1[3].v && c1[3].v == c1[4].v){
555		TriValP1 = c1[2].v;
556		HighValP1[0] = c1[1].v;
557		HighValP1[1] = c1[0].v;
558	}
559
560	if(c2[0].v == c2[1].v && c2[1].v == c2[2].v){
561		TriValP2 = c2[0].v;
562		HighValP2[0] = c2[4].v;
563		HighValP2[1] = c2[3].v;
564	} else if(c2[1].v == c2[2].v && c2[2].v == c2[3].v){
565		TriValP2 = c2[1].v;
566		HighValP2[0] = c2[4].v;
567		HighValP2[1] = c2[0].v;
568	} else if(c2[2].v == c2[3].v && c2[3].v == c2[4].v){
569		TriValP2 = c2[2].v;
570		HighValP2[0] = c2[1].v;
571		HighValP2[1] = c2[0].v;
572	}
573
574	if(TriValP1 > TriValP2)
575		return PLAYER1;
576
577	if(TriValP1 < TriValP2)
578		return PLAYER2;
579
580	if(HighValP1[0] > HighValP2[0])
581		return PLAYER1;
582
583	if(HighValP1[0] < HighValP2[0])
584		return PLAYER2;
585
586	if(HighValP1[1] > HighValP2[1])
587		return PLAYER1;
588
589	if(HighValP1[1] < HighValP2[1])
590		return PLAYER2;
591
592	return TIE;
593}
594
595////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
596
597int SolveTwoPairsTie(HandStruct *hs1, HandStruct *hs2)
598{
599	CardStruct *c1 = &hs1->Cards[0];
600	CardStruct *c2 = &hs2->Cards[0];
601
602	CardValue PairValP1[2] = {TWO, TWO};
603	CardValue PairValP2[2] = {TWO, TWO};
604
605	CardValue HighValP1 = TWO;
606	CardValue HighValP2 = TWO;
607
608	if(c1[0].v == c1[1].v && c1[2].v == c1[3].v && c1[4].v != c1[3].v){
609		PairValP1[0] = c1[2].v;
610		PairValP1[1] = c1[0].v;
611		HighValP1 = c1[4].v;
612	} else if(c1[1].v == c1[2].v && c1[3].v == c1[4].v && c1[0].v != c1[1].v){
613		PairValP1[0] = c1[3].v;
614		PairValP1[1] = c1[1].v;
615		HighValP1 = c1[0].v;
616	} else if(c1[0].v == c1[1].v && c1[3].v == c1[4].v && c1[2].v != c1[1].v && c1[2].v != c1[3].v){
617		PairValP1[0] = c1[3].v;
618		PairValP1[1] = c1[0].v;
619		HighValP1 = c1[2].v;
620	}	
621	
622	if(c2[0].v == c2[1].v && c2[2].v == c2[3].v && c2[4].v != c2[3].v){
623		PairValP2[0] = c2[2].v;
624		PairValP2[1] = c2[0].v;
625		HighValP2 = c2[4].v;
626	} else if(c2[1].v == c2[2].v && c2[3].v == c2[4].v && c2[0].v != c2[1].v){
627		PairValP2[0] = c2[3].v;
628		PairValP2[1] = c2[1].v;
629		HighValP2 = c2[0].v;
630	} else if(c2[0].v == c2[1].v && c2[3].v == c2[4].v && c2[2].v != c2[1].v && c2[2].v != c2[3].v){
631		PairValP2[0] = c2[3].v;
632		PairValP2[1] = c2[0].v;
633		HighValP2 = c2[2].v;
634	}	
635	
636	if(PairValP1[1] > PairValP2[1])
637		return PLAYER1;
638
639	if(PairValP1[1] < PairValP2[1])
640		return PLAYER2;
641
642	if(PairValP1[0] > PairValP2[0])
643		return PLAYER1;
644
645	if(PairValP1[0] < PairValP2[0])
646		return PLAYER2;
647
648	if(HighValP1 > HighValP2)
649		return PLAYER1;
650
651	if(HighValP1 < HighValP2)
652		return PLAYER2;
653
654	return TIE;
655}
656
657////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
658
659int SolveOnePairTie(HandStruct *hs1, HandStruct *hs2)
660{
661	CardStruct *c1 = &hs1->Cards[0];
662	CardStruct *c2 = &hs2->Cards[0];
663
664	CardValue PairValP1 = TWO;
665	CardValue PairValP2 = TWO;
666
667	CardValue HighValP1[3] = {TWO, TWO, TWO};
668	CardValue HighValP2[3] = {TWO, TWO, TWO};
669
670	if(c1[0].v == c1[1].v && c1[2].v != c1[1].v && c1[3].v != c1[1].v && c1[4].v != c1[1].v){
671		PairValP1 = c1[0].v;
672		HighValP1[0] = c1[4].v;
673		HighValP1[1] = c1[3].v;
674		HighValP1[2] = c1[2].v;
675	} else if(c1[1].v == c1[2].v && c1[3].v != c1[2].v && c1[4].v != c1[2].v && c1[0].v != c1[2].v){
676		PairValP1 = c1[1].v;
677		HighValP1[0] = c1[4].v;
678		HighValP1[1] = c1[3].v;
679		HighValP1[2] = c1[0].v;
680	} else if(c1[2].v == c1[3].v && c1[4].v != c1[3].v && c1[0].v != c1[3].v && c1[1].v != c1[3].v){
681		PairValP1 = c1[2].v;
682		HighValP1[0] = c1[4].v;
683		HighValP1[1] = c1[1].v;
684		HighValP1[2] = c1[0].v;
685	} else if(c1[3].v == c1[4].v && c1[0].v != c1[4].v && c1[1].v != c1[4].v && c1[2].v != c1[4].v){
686		PairValP1 = c1[3].v;
687		HighValP1[0] = c1[2].v;
688		HighValP1[1] = c1[1].v;
689		HighValP1[2] = c1[0].v;
690	}
691
692	if(c2[0].v == c2[1].v && c2[2].v != c2[1].v && c2[3].v != c2[1].v && c2[4].v != c2[1].v){
693		PairValP2 = c2[0].v;
694		HighValP2[0] = c2[4].v;
695		HighValP2[1] = c2[3].v;
696		HighValP2[2] = c2[2].v;
697	} else if(c2[1].v == c2[2].v && c2[3].v != c2[2].v && c2[4].v != c2[2].v && c2[0].v != c2[2].v){
698		PairValP2 = c2[1].v;
699		HighValP2[0] = c2[4].v;
700		HighValP2[1] = c2[3].v;
701		HighValP2[2] = c2[0].v;
702	} else if(c2[2].v == c2[3].v && c2[4].v != c2[3].v && c2[0].v != c2[3].v && c2[1].v != c2[3].v){
703		PairValP2 = c2[2].v;
704		HighValP2[0] = c2[4].v;
705		HighValP2[1] = c2[1].v;
706		HighValP2[2] = c2[0].v;
707	} else if(c2[3].v == c2[4].v && c2[0].v != c2[4].v && c2[1].v != c2[4].v && c2[2].v != c2[4].v){
708		PairValP2 = c2[3].v;
709		HighValP2[0] = c2[2].v;
710		HighValP2[1] = c2[1].v;
711		HighValP2[2] = c2[0].v;
712	}
713
714	if(PairValP1 > PairValP2)
715		return PLAYER1;
716
717	if(PairValP1 < PairValP2)
718		return PLAYER2;
719
720	if(HighValP1[0] > HighValP2[0])
721		return PLAYER1;
722
723	if(HighValP1[0] < HighValP2[0])
724		return PLAYER2;
725
726	if(HighValP1[1] > HighValP2[1])
727		return PLAYER1;
728
729	if(HighValP1[1] < HighValP2[1])
730		return PLAYER2;
731
732	if(HighValP1[2] > HighValP2[2])
733		return PLAYER1;
734
735	if(HighValP1[2] < HighValP2[2])
736		return PLAYER2;
737
738	return TIE;
739}
740
741////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
742
743int SolveHighCardTie(HandStruct *hs1, HandStruct *hs2)
744{
745	CardStruct *c1 = &hs1->Cards[0];
746	CardStruct *c2 = &hs2->Cards[0];
747
748	for(int i = NumCardsPerHand-1; i >= 0; i--){
749		if(c1[i].v > c2[i].v){
750			return PLAYER1;
751		} else if(c1[i].v < c2[i].v){	
752			return PLAYER2;
753		}
754	}
755
756	return TIE;
757}
758
759////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
760
761int AnalyseHands(int row)
762{
763	int r1 = AnalyseHand(PLAYER1, row);
764	int r2 = AnalyseHand(PLAYER2, row);
765
766	if(r1 > r2)
767		return PLAYER1;
768
769	if(r1 < r2)
770		return PLAYER2;
771
772	// Tie...
773	HandStruct hs1 = PlayersCards[PLAYER1][row];
774	HandStruct hs2 = PlayersCards[PLAYER2][row];
775	SortCards(&hs1);
776	SortCards(&hs2);
777
778	if(r1 == STRAIGHT_FLUSH)
779		return SolveStraightFlushTie(&hs1, &hs2);
780
781	if(r1 == FOUR_OF_A_KIND)
782		return SolveFourOfaKindTie(&hs1, &hs2);
783
784	if(r1 == FULL_HOUSE)
785		return SolveFullHouseTie(&hs1, &hs2);
786
787	if(r1 == FLUSH)
788		return SolveFlushTie(&hs1, &hs2);
789
790	if(r1 == STRAIGHT)
791		return SolveStraightTie(&hs1, &hs2);
792
793	if(r1 == THREE_OF_A_KIND)
794		return SolveThreeOfaKindTie(&hs1, &hs2);
795
796	if(r1 == TWO_PAIRS)
797		return SolveTwoPairsTie(&hs1, &hs2);
798
799	if(r1 == ONE_PAIR)
800		return SolveOnePairTie(&hs1, &hs2);
801
802	if(r1 == HIGH_CARD)
803		return SolveHighCardTie(&hs1, &hs2);
804
805	return TIE;
806}
807
808////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
809
810/*void PrintResult(int res)
811{
812	switch(res)
813	{
814	case ROYAL_FLUSH:	  printf("Royal Flush\n");     break;
815	case STRAIGHT_FLUSH:  printf("Straight Flush\n");  break;
816	case FOUR_OF_A_KIND:  printf("Four of a kind\n");  break;
817	case FULL_HOUSE:	  printf("Full House\n");      break;
818	case FLUSH:			  printf("Flush\n");           break;
819	case STRAIGHT:		  printf("Straight\n");        break;
820	case THREE_OF_A_KIND: printf("Three of a kind\n"); break;
821	case TWO_PAIRS:		  printf("Two Pairs\n");       break;
822	case ONE_PAIR:		  printf("One Pair\n");        break;
823	case HIGH_CARD:		  printf("High Card\n");       break;
824	}
825}*/
826
827////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
828////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
829////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
830
831int main()
832{
833	int NumRows = LoadFromFile("poker.txt");
834
835	int Tie = 0;
836	int P1Wins = 0;
837	int P2Wins = 0;
838
839	for(int row = 0; row < NumRows; row++){
840
841		int res = AnalyseHands(row);
842
843		if(res == PLAYER1){
844			printf("Player 1 Win\n");
845			P1Wins++;
846		} else if(res == PLAYER2){
847			printf("Player 2 Win\n");
848			P2Wins++;
849		} else {
850			printf("Tie\n");
851			Tie++;
852		}
853	}
854
855	printf("\nWin: %d, Lose: %d, Tie: %d\n\n", P1Wins, P2Wins, Tie);
856
857	return 0;
858}
859

I especially hate CharToCardValue LN49, where you let representation dictate the model rather than vice-versa, but I'd still agree reading JACK is better than 11.

0

Share this post


Link to post
Share on other sites

Yuck. So many named non-iterble things. So much code repetition. Heres an almost 20x shorter Python solution:

from collections import Counter

with open('poker.txt', 'r') as f:
	hands = [hand.strip() for hand in f]
	
values = [chr(ord('2')+k) for k in range(8)] + ['T', 'J', 'Q', 'K', 'A']
ace = values.index('A')
hands = [hand.split(' ') for hand in hands]

hand_values = [[values.index(card[0]) for card in hand] for hand in hands]
hand_suits = [[card[1] for card in hand] for hand in hands]

hand_values = [(hand[:5], hand[5:]) for hand in hand_values]
hand_suits = [(hand[:5], hand[5:]) for hand in hand_suits]

wins = [0, 0]
ties = 0
for vs, ss in zip(hand_values, hand_suits):
	rankings = []
	for v, s in zip(vs, ss):
		v = sorted(v, key=lambda x: [Counter(v)[x],x]) # bugfix
		straight = all(a + 1 == b for a, b in zip(v, v[1:]))
		flush = s.count(s[0]) == len(s)
		counts = Counter(Counter(v).values())
		if straight and flush and v[-1] == ace: rank = 9 # royal flush
		elif straight and flush: rank = 8 # straight flush
		elif counts[4] == 1: rank = 7 # four of a kind
		elif counts[3] == 1 and counts[2] == 1: rank = 6 # full house
		elif flush: rank = 5 # flush
		elif straight: rank = 4 # straight
		elif counts[3] == 1: rank = 3 # three of a kind
		elif counts[2] == 2: rank = 2 # two pairs
		elif counts[2] == 1: rank = 1 # one pair
		else: rank = 0 # high card
		rankings.append((rank, v[::-1]))
	winning_hand = max(rankings)
	if rankings.count(winning_hand) == 1:
		wins[rankings.index(winning_hand)] += 1
	else:
		ties += 1

print wins
print ties

EDIT: added one line of bugfixing (for card multiples, the multiple cards weren't properly used for decision before the high cards)...

Edited by l0calh05t
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0