Jump to content
Che

[C++] Algoritm carti de joc ?

Recommended Posts

Cum se manageriaza cartile de joc de poker in C++ ?

Avem urmatoarele carti:

A23456789TJQK

(T=10)

Practic sunt 13 carti si A tine loc si de 1 dar si de 14, astfel ca o chinta poate fi si A2345 dar si TJQKA.

Cele 13 carti exista, dupa cum stiti, in cate 4 exemplare fiecare (pica, cupa, caro, trefla) astfel ca in pachet sunt 13 x 4 = 52 de carti (nu punem Jokerii ca vorbim de poker tipul holdem).

Cum pot sa fac in asa fel incat sa creezi un algoritm care sa citeasca mainile, sa citeasca ce carti sunt pe masa si/sau sa faca diferite calcule probabilistice ?

Eu ma gandeam ca sa transformam toate cartile in numere intregi si sa le bagam intr-un vector int de la 1 la 13.

Bun, pentru 10, 11, 12 si 13 ne dam seama ca avem cartile T, J, Q, K.

Cum ne dam seama cand avem A ? Daca punem 1, cum il folosim cand ar trebui sa fie de fapt 14 ?

Doar sa facem cu un if si daca avem nevoie de 14 si nu de 1 in mana respectiva atunci sa facem if (este 1) then...

Altfel nu am idee. Nu ? Bun, sa zicem ca ar merge.

Cum facem cu culorile ? Adica le bagam in vector de tip de la 1 la 13, dar cum stim daca 9 e de trefla sau de pica ?

Si inca ceva, cum am putea citit boardul (cartile de pe masa) adica sa stim cand pe masa e o chinta, un full sau mai stiu eu ce ?

Multumesc mult !

Link to comment
Share on other sites

  • Active Members

Ceva de genu' asta ?


#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

int main()
{
srand(time(0));
int deck[52];
int i;
string suitnames[4]={"spades", "diamonds", "clubs", "hearts"};
string ranknames[13]={"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"};

// create a new deck, with cards in order, but unique
for(i=0; i<52; i++)
{
deck[i] = i;
}

// shuffle the deck:
for(i=0; i<52; i++)
{
// generate a random index to swap with the card at index i.
int j = rand() % 52;
int temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}


// print all the cards
for(i=0; i<52; i++)
{
int suitnumber = deck[i] / 13; // 0 - 3
int rank = deck[i] % 13;
cout << ranknames[rank] << " of " << suitnames[suitnumber]<< "\n";
}

// Check first 5 cards for an ace
cout << endl;
for(i=0; i<5; i++)
{
int acerank = 0;
int currentCardRank = deck[i]%13;
if(currentCardRank == acerank)
{
cout << "Got an ace!" << endl;
}
}

// Get the rank of the first 5 cards
int R[5]; // = {4, 7, 6, 3, 5}; // rank of the first 5 cards
int S[5];
for(i=0; i<5; i++)
{
R[i] = deck[i]%13;
S[i] = deck[i]/13;
}

// Sort the R array
bool swapped = false;
do
{
// 1 pass of the bubble sort
swapped = false;
for(int i=0; i<4; i++)
{
if(R[i] > R[i+1])
{
int temp = R[i];
R[i] = R[i+1];
R[i+1] = temp;
swapped = true;
}
}
}
while(swapped == true);

// print the sorted ranks and suits
for(i=0; i<5; i++)
{
cout << ranknames[R[i]] << " of " << suitnames[S[i]]<< "\n";
}

// Check for a straight:
if(R[1]==R[0]+1 && R[2]==R[1]+1 && R[3]==R[2]+1 && R[4]==R[3]+1)
{
cout << "You got a straight! (you must have cheated)" << endl;
}
else
{
cout << "No straight" << endl;
}

// Check first 5 cards for any pair
if(R[0] == R[1] || R[1]==R[2] || R[2]==R[3] || R[3]==R[4])
{
cout << "You got a pair!" << endl;
}
else
{
cout << "No pair" << endl;
}

// Check for a flush (all the same suit)
if(S[0] == S[1] && S[1]==S[2] && S[2]==S[3] && S[3]==S[4])
{
cout << "You got a flush!" << endl;
}
else
{
cout << "No flush" << endl;
}

// Check for straight flush

// Check for 4 of a kind

// Check for full house
system("pause");
return 0;
}

Nu e facut de mine dar iti poti face o idee destul de rapid despre algoritm.

//EDIT: asta e cel mai simplu exemplu pe care l-am gasit tinand cont de cunostintele tale actuale din diveresele posturi

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...