📄 bluff pokerview.cpp
字号:
// Bluff PokerView.cpp : implementation of the CBluffPokerView class
//
#include "stdafx.h"
#include "Bluff Poker.h"
#include "cardsdll.h"
#include "Bluff PokerDoc.h"
#include "Bluff PokerView.h"
#include "mainfrm.h"
#include "playerdlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define random() ((rand () & 0x7fff) / ((float)0x7fff))
#define crandom() (2.0 * (random() - 0.5))
CEdit g_edMessages;
int g_deck[20];
int g_hand[5];
int g_hand2[5];
int g_hand3[5];
int g_hand4[5];
CString g_strMessage = "";
int g_pot = 0;
int g_bet;
int g_active = 0;
int g_maximum = 100000;
int g_allinamt = 0;
int g_ante = 5;
int g_allinperson = 0;
int g_previous = 0;
BOOL g_allin = FALSE;
BOOL g_showcards = FALSE;
BOOL g_showwinner = FALSE;
int g_people = 4;
int g_peoplein = 4;
int g_needinitialize = 0;
int g_cards[20] = { 0, 1, 2, 3, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
typedef struct tagWINNERS
{
int winners[4];
int numwinners;
}winners_t;
winners_t g_winners;
typedef struct tagPLAYER
{
BOOL in;
BOOL ingame;
BOOL allin;
BOOL called;
BOOL acted;
int money;
int inpot;
}client_t;
#define MAX_HANDS 50
typedef struct tagPATTERNS
{
int handsplayed;
hyper handranks[MAX_HANDS];
}patterns_t;
patterns_t g_patterns[4];
client_t g_clients[4];
int g_played = 0;
//int g_nTotal = 0;
//int g_nTotal2 = 0;
//int g_nTotal3 = 0;
//int g_times_shuffled = 0;
void SeedRandom()
{
DWORD dw = GetTickCount();
long tim = time(NULL);
srand(GetTickCount());
srand(dw * dw + rand() % 32000);
srand(dw + random()*1200);
for (int i = 0; i < random()*100; i++)
srand(dw * random() * 100 + 84 / random()*100);
srand((dw*dw*floor(random()*100)) - (dw*GetTickCount()));
}
void AddToEdit(CString strText)
{
CString str;
int cols;
g_edMessages.GetWindowText(str);
cols = str.GetLength();
str += "\r\n" + strText;
g_edMessages.SetWindowText(str);
int nLine = g_edMessages.GetLineCount() - 1;
int nIndex = g_edMessages.LineIndex(nLine);
g_edMessages.SetSel(nIndex, nIndex);
}
int GetSuit(int nCard)
{
return nCard % 4;
}
int GetCard(int nCard)
{
return (int)floor((int)nCard/4);
}
void ShuffleDeck()
{
//DWORD dwt1 = GetTickCount();
int done = 0;
int i = 0;
int nTmp;
int nFound = 0;
int tmp[20];
int tmp2[20];
for (int k = 0; k < 20; k++)
{
tmp2[k] = k;
g_deck[k] = -1;
tmp[k] = k;
}
SeedRandom();
while(done == 0)
{
if (i > 19)
{
done = 1;
continue;
}
/*if (i == 51)
{
g_deck[51] = tmp[0];
return;
}*/
nTmp = abs((int)floor(crandom() * 19));
for (int j = 0; j < 20; j++)
if (g_deck[j] == nTmp)
nFound = 1;
if (nFound == 1)
{
nFound = 0;
continue;
}
else
{
g_deck[i] = tmp[nTmp];
i++;
/*tmp[nTmp] = -1;
int j = nTmp+1;
int l = nTmp;
while (j < 52)
{
/*if (tmp2[j] == -1)
{
j++;
continue;
}
else
{*/
/* tmp2[l] = tmp[j];
l++;
j++;
//}
}
j = nTmp;
l = nTmp;
while (j < 52)
{
tmp[j] = tmp2[j];
j++;
}
DWORD dw2 = GetTickCount();
while(GetTickCount() < dw2 + 1)
;
*/}
// // tmp[51] = -1;
}
//g_times_shuffled++;
//g_nTotal += GetTickCount() - dwt1;
}
typedef struct TAGhand_t
{
// CString strType;
int kickers[5];
int hand;
}hand_t, *hand_pt;
typedef struct TAGplayer_t
{
hand_t hand;
int player;
}player_t;
typedef struct TAGplayers_t
{
player_t players[4];
}players_t;
hand_t EvaluateMultiples(int c1, int c2, int c3, int c4, int c5)
{
int hand[5] = { GetCard(c1), GetCard(c2), GetCard(c3), GetCard(c4), GetCard(c5) };
// int tmphand[5];
int tmp[14];
int pair = 0;
int twopair = 0;
int toak = 0;
int fullhouse = 0;
int foak = 0;
int higher = 0;
int lower = 0;
int kickers[5];
hand_t rethand;
for (int i = 0; i < 14; i++)
tmp[i] = 0;
for (i = 0; i < 5; i++)
{
kickers[i] = 0;
rethand.kickers[i] = 0;
}
for (i = 0; i < 5; i++)
if (hand[i] == 0)
hand[i] = 13;
int total = 0;
int score;
for (i = 0; i < 5; i++)
{
score = hand[i];
for (int j = 0; j < total; j++)
{
if (score > hand[j])
break;
}
for (int k = total; k > j; k--)
hand[k] = hand[k-1];
hand[j] = score;
total++;
}
for (i = 0; i < 5; i++)
tmp[hand[i]]++;
for (i = 1; i < 14; i++)
{
if (tmp[i] == 1 || tmp[i] == 0)
continue;
else if (tmp[i] == 2)
{
if (1 == pair)
{
twopair = 1;
pair = 0;
higher = i;
}
else if (1 == toak)
{
lower = i;
fullhouse = 1;
pair = 0;
toak = 0;
}
else
{
pair = 1;
lower = i;
}
}
else if (tmp[i] == 3)
{
if (1 == pair)
{
pair = 0;
toak = 0;
fullhouse = 1;
higher = i;
}
else
{
toak = 1;
higher = i;
}
}
else if (tmp[i] == 4)
{
foak = 1;
higher = i;
}
else
continue;
}
if (0 == pair && 0 == twopair && 0 == toak && 0 == fullhouse && 0 == foak)
{
for (i = 0; i < 5; i++)
rethand.kickers[i] = hand[i];
//rethand.strType = "Straight";
rethand.hand = 4;
int suits[5] = { GetSuit(c1), GetSuit(c2), GetSuit(c3), GetSuit(c4), GetSuit(c5) };
if ((suits[0] == suits[1]) && (suits[1] == suits[2])
&& (suits[2] == suits[3]) && (suits[3] == suits[4]))
{
// g_nTotal++;
//rethand.strType = "Straight Flush";
rethand.hand = 8;
}
//return rethand;
}
else if (1 == pair)
{
higher = lower;
int j = 0;
for (i = 0; i < 5; i++)
{
if (hand[i] == higher)
continue;
else
{
rethand.kickers[j+1] = hand[i];
j++;
}
}
//rethand.strType = "Pair";
rethand.kickers[0] = higher;
rethand.hand = 1;
//return rethand;
}
else if (1 == twopair)
{
for (i = 0; i < 5; i++)
if ((hand[i] != higher) && (hand[i] != lower))
{
rethand.kickers[2] = hand[i];
break;
}
rethand.kickers[0] = higher;
rethand.kickers[1] = lower;
//rethand.strType = "Two Pair";
rethand.hand = 2;
//return rethand;
}
else if (1 == toak)
{
//rethand.strType = "Three Of A Kind";
rethand.kickers[0] = higher;
rethand.hand = 3;
//return rethand;
}
else if (1 == fullhouse)
{
rethand.kickers[0] = higher;
rethand.kickers[1] = lower;
//rethand.strType = "Full House";
// g_nTotal3++;
rethand.hand = 6;
//return rethand;
}
else if (1 == foak)
{
//rethand.strType = "Four Of A Kind";
rethand.kickers[0] = higher;
// g_nTotal2++;
rethand.hand = 7;
//return rethand;
}
// g_times_shuffled++;
return rethand;
}
class CARDS_DLL
{
public:
CARDS_DLL();
~CARDS_DLL();
BOOL (WINAPI* InitCards)(int*, int*);
BOOL (WINAPI* DrawCard)(HDC, int, int, int, int, COLORREF);
BOOL (WINAPI* Term)(void);
HINSTANCE m_hLib;
// HINSTANCE m_hLib;
};
CARDS_DLL::CARDS_DLL()
{
if (m_hLib = LoadLibrary("CARDS.DLL"))
{
(FARPROC&) InitCards = GetProcAddress(m_hLib, "cdtInit");
(FARPROC&) DrawCard = GetProcAddress(m_hLib, "cdtDraw");
(FARPROC&) Term = GetProcAddress(m_hLib, "cdtTerm");
//InitCards((int*)0, (int*)0);
}
else
AfxMessageBox("CANNOT LOAD CARDS.DLL!");
}
CARDS_DLL::~CARDS_DLL()
{
if(m_hLib)
FreeLibrary(m_hLib);
}
CARDS_DLL _cards;
void SetDeck()
{
// This function is merely used for testing.
for (int i = 1; i < 5; i++)
{
g_hand[i - 1] = i * 4;
}
g_hand[4] = 0;
}
void DealHand()
{
int i = 0;
for (i; i < 5; i++)
{
if (g_clients[0].ingame == TRUE)
g_hand[i] = g_deck[i*4];
if (g_clients[1].ingame == TRUE)
g_hand2[i] = g_deck[(i*4)+1];
if (g_clients[2].ingame == TRUE)
g_hand3[i] = g_deck[(i*4)+2];
if (g_clients[3].ingame == TRUE)
g_hand4[i] = g_deck[(i*4)+3];
}
//setting the deck for testing
// SetDeck();
}
void DrawHand(CDC* pDC, POINT pt, int p)
{
int hand[5];
int i;
switch(p)
{
case 0:
for (i = 0; i < 5; i++)
hand[i] = g_hand[i];
break;
case 1:
for (i = 0; i < 5; i++)
hand[i] = g_hand2[i];
break;
case 2:
for (i = 0; i < 5; i++)
hand[i] = g_hand3[i];
break;
case 3:
for (i = 0; i < 5; i++)
hand[i] = g_hand4[i];
break;
default:
break;
}
for (i = 0; i < 5; i++)
{
if ((g_showcards == TRUE || p == 0) && (p != -1))
(*_cards.DrawCard)(pDC->GetSafeHdc(), (i*70) + pt.x, pt.y, g_cards[hand[i]], 0, RGB(255,255,255));
else if (p == -1)
(*_cards.DrawCard)(pDC->GetSafeHdc(), (i*70) + pt.x, pt.y, 0, 4, RGB(0, 150, 0));
else
(*_cards.DrawCard)(pDC->GetSafeHdc(), (i*70) + pt.x, pt.y, 53, 1, RGB(255,255,255));
}
}
hyper RankHand(hand_t hand)
{
hyper ret = 0;
ret += hand.hand * 10000000000;
ret += hand.kickers[0] * 100000000;
ret += hand.kickers[1] * 1000000;
ret += hand.kickers[2] * 10000;
// ret += hand.kickers[3] * 100;
// ret += hand.kickers[4] * 1;
return ret;
}
hyper GetHandRank (int hand[5])
{
hand_t eval = EvaluateMultiples (hand[0], hand[1], hand[2], hand[3], hand[4]);
return RankHand(eval);
}
char* ToText(int sing, int num)
{
char* strRet;
switch(num)
{
case 1:
if (sing == 1)
strRet = "a ten";
else
strRet = "tens";
break;
case 2:
if (sing == 1)
strRet = "a Jack";
else
strRet = "Jacks";
break;
case 3:
if (sing == 1)
strRet = "a Queen";
else
strRet = "Queens";
break;
case 4:
if (sing == 1)
strRet = "a King";
else
strRet = "Kings";
break;
default:
if (sing == 1)
strRet = "an Ace";
else
strRet = "Aces";
break;
}
return strRet;
}
void TextMessage (int split, hand_t hand)
{
CString strText;
switch(hand.hand)
{
case 1:
strText.Format("two %s.", ToText(0, hand.kickers[0]));
break;
case 2:
strText.Format("two %s and two %s, %s kicker.",
ToText(0, hand.kickers[0]), ToText(0, hand.kickers[1]),
ToText(1, hand.kickers[2]));
break;
case 3:
strText.Format("three %s.", ToText(0, hand.kickers[0]));
break;
case 4:
strText.Format("a straight.");
break;
case 6:
strText.Format("a fullhouse, %s over %s.", ToText(0, hand.kickers[0]),
ToText(0, hand.kickers[1]));
break;
case 7:
strText.Format("four %s.", ToText(0, hand.kickers[0]));
break;
case 8:
strText.Format("straight flush");
break;
default:
strText.Format("ERROR");
break;
}
if (split > 1)
{
CString strText2;
strText2.Format("The pot is split %i ways with %s", split, strText);
AddToEdit(strText2);
}
else
{
CString strText2;
strText2.Format("The pot is won with %s", strText);
AddToEdit(strText2);
}
}
winners_t ResultsHand()
{
int people = 0;
hand_t hand1, hand2, hand3, hand4;
hyper r1 = 0, r2 = 0, r3 = 0, r4 = 0;
if (g_clients[0].in == TRUE)
{
hand1 = EvaluateMultiples(g_hand[0], g_hand[1],
g_hand[2], g_hand[3], g_hand[4]);
r1 = RankHand(hand1);
if (g_patterns[0].handsplayed >= MAX_HANDS)
{
g_patterns[0].handsplayed = MAX_HANDS - 1;
for (int l = 1; l < MAX_HANDS; l++)
g_patterns[0].handranks[l-1] = g_patterns[0].handranks[l];
}
g_patterns[0].handranks[g_patterns[0].handsplayed] = r1;
g_patterns[0].handsplayed++;
people++;
}
if (g_clients[1].in == TRUE)
{
hand2 = EvaluateMultiples(g_hand2[0], g_hand2[1],
g_hand2[2], g_hand2[3], g_hand2[4]);
r2 = RankHand(hand2);
if (g_patterns[1].handsplayed >= MAX_HANDS)
{
g_patterns[1].handsplayed = MAX_HANDS - 1;
for (int l = 1; l < MAX_HANDS; l++)
g_patterns[1].handranks[l-1] = g_patterns[1].handranks[l];
}
g_patterns[1].handranks[g_patterns[1].handsplayed] = r2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -