Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply code formatting to C and C++ files #97

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/benchmark/benchmark.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "benchmark/benchmark.h"

#include "phevaluator/card_sampler.h"
#include "phevaluator/phevaluator.h"

Expand Down
79 changes: 39 additions & 40 deletions cpp/examples/c_example.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <phevaluator/phevaluator.h>
#include <phevaluator/rank.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
* This C code is a demonstration of how to calculate the card id, which will
* be used as the parameter in the evaluator. It also shows how to use the
* return value to determine which hand is the stronger one.
*/
int main()
{
/*
* In this example we use a scenario in the game Texas Holdem:
* Community cards: 9c 4c 4s 9d 4h (both players share these cards)
* Player 1: Qc 6c
* Player 2: 2c 9h
*
* Both players have full houses, but player 1 has only a four full house
* while player 2 has a nine full house.
int main() {
/*
* In this example we use a scenario in the game Texas Holdem:
* Community cards: 9c 4c 4s 9d 4h (both players share these cards)
* Player 1: Qc 6c
* Player 2: 2c 9h
*
* The result is player 2 has a stronger hand than player 1.
*/
* Both players have full houses, but player 1 has only a four full house
* while player 2 has a nine full house.
*
* The result is player 2 has a stronger hand than player 1.
*/

/*
* To calculate the value of each card, we can either use the Card Id
Expand All @@ -36,36 +35,36 @@ int main()
* And the suits are:
* club = 0, diamond = 1, heart = 2, spade = 3
*/
// Community cards
int a = 7 * 4 + 0; // 9c
int b = 2 * 4 + 0; // 4c
int c = 2 * 4 + 3; // 4s
int d = 7 * 4 + 1; // 9d
int e = 2 * 4 + 2; // 4h
// Community cards
int a = 7 * 4 + 0; // 9c
int b = 2 * 4 + 0; // 4c
int c = 2 * 4 + 3; // 4s
int d = 7 * 4 + 1; // 9d
int e = 2 * 4 + 2; // 4h

// Player 1
int f = 10 * 4 + 0; // Qc
int g = 4 * 4 + 0; // 6c
// Player 1
int f = 10 * 4 + 0; // Qc
int g = 4 * 4 + 0; // 6c

// Player 2
int h = 0 * 4 + 0; // 2c
int i = 7 * 4 + 2; // 9h
// Player 2
int h = 0 * 4 + 0; // 2c
int i = 7 * 4 + 2; // 9h

// Evaluating the hand of player 1
int rank1 = evaluate_7cards(a, b, c, d, e, f, g);
// Evaluating the hand of player 2
int rank2 = evaluate_7cards(a, b, c, d, e, h, i);
// Evaluating the hand of player 1
int rank1 = evaluate_7cards(a, b, c, d, e, f, g);
// Evaluating the hand of player 2
int rank2 = evaluate_7cards(a, b, c, d, e, h, i);

assert(rank1 == 292);
assert(rank2 == 236);

printf("The rank of the hand in player 1 is %d\n", rank1); // expected 292
printf("The rank of the hand in player 2 is %d\n", rank2); // expected 236
printf("Player 2 has a stronger hand\n");
printf("The rank of the hand in player 1 is %d\n", rank1); // expected 292
printf("The rank of the hand in player 2 is %d\n", rank2); // expected 236
printf("Player 2 has a stronger hand\n");

// Since the return value of the hand in player 2 is less than player 1,
// it's considered to be a higher rank and stronger hand.
// So player 2 beats player 1.
// Since the return value of the hand in player 2 is less than player 1,
// it's considered to be a higher rank and stronger hand.
// So player 2 beats player 1.

enum rank_category category = get_rank_category(rank2);
assert(category == FULL_HOUSE);
Expand All @@ -78,10 +77,10 @@ int main()
assert(strcmp(rank_description, "Nines Full over Fours") == 0);

const char* rank_sample_hand = describe_sample_hand(rank2);
printf("The best hand from player 2 is %s %s\n",
rank_sample_hand, is_flush(rank2) ? "flush": "");
printf("The best hand from player 2 is %s %s\n", rank_sample_hand,
is_flush(rank2) ? "flush" : "");
assert(strcmp(rank_sample_hand, "99944") == 0);
assert(!is_flush(rank2));

return 0;
return 0;
}
41 changes: 23 additions & 18 deletions cpp/examples/cpp_example.cc
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#include <phevaluator/phevaluator.h>
#include <iostream>

#include <cassert>
#include <iostream>

int main()
{
/*
* This demonstrated scenario is the same as the one shown in example 1.
* Community cards: 9c 4c 4s 9d 4h (both players share these cards)
* Player 1: Qc 6c
* Player 2: 2c 9h
*/
phevaluator::Rank rank1 = phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "Qc", "6c");
phevaluator::Rank rank2 = phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "2c", "9h");
int main() {
/*
* This demonstrated scenario is the same as the one shown in example 1.
* Community cards: 9c 4c 4s 9d 4h (both players share these cards)
* Player 1: Qc 6c
* Player 2: 2c 9h
*/
phevaluator::Rank rank1 =
phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "Qc", "6c");
phevaluator::Rank rank2 =
phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "2c", "9h");

// expected 292
// expected 292
assert(rank1.value() == 292);
std::cout << "The rank of the hand in player 1 is " << rank1.value() << std::endl;
// expected 236
std::cout << "The rank of the hand in player 1 is " << rank1.value()
<< std::endl;
// expected 236
assert(rank2.value() == 236);
std::cout << "The rank of the hand in player 2 is " << rank2.value() << std::endl;
std::cout << "The rank of the hand in player 2 is " << rank2.value()
<< std::endl;

assert(rank1 < rank2);
std::cout << "Player 2 has a stronger hand" << std::endl;
Expand All @@ -28,10 +32,11 @@ int main()
std::cout << "Player 2 has a " << rank2.describeCategory() << std::endl;

assert(rank2.describeRank() == "Nines Full over Fours");
std::cout << "More specifically, player 2 has a " << rank2.describeRank() << std::endl;
std::cout << "More specifically, player 2 has a " << rank2.describeRank()
<< std::endl;

assert(rank2.describeSampleHand() == "99944");
assert(!rank2.isFlush());
std::cout << "The best hand from player 2 is " << rank2.describeSampleHand() <<
(rank2.isFlush() ? " in flush" : "") << std::endl;
std::cout << "The best hand from player 2 is " << rank2.describeSampleHand()
<< (rank2.isFlush() ? " in flush" : "") << std::endl;
}
22 changes: 14 additions & 8 deletions cpp/examples/evaluator5_standalone_example.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <phevaluator/phevaluator.h>
#include <iostream>

#include <cassert>
#include <iostream>

/*
* This example uses library pheval5.
Expand All @@ -11,17 +12,22 @@
* and follow `examples/cpp_example.cc`.
*/

int main()
{
phevaluator::Rank rank1 = phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h");
phevaluator::Rank rank2 = phevaluator::EvaluateCards("8c", "7c", "6s", "5d", "4s");
int main() {
phevaluator::Rank rank1 =
phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h");
phevaluator::Rank rank2 =
phevaluator::EvaluateCards("8c", "7c", "6s", "5d", "4s");

assert(rank1.value() == 292);
std::cout << "The rank of the hand in player 1 is " << rank1.value() << std::endl;
std::cout << "The rank of the hand in player 1 is " << rank1.value()
<< std::endl;

assert(rank2.value() == 1606);
std::cout << "The rank of the hand in player 2 is " << rank2.value() << std::endl;
std::cout << "The rank of the hand in player 2 is " << rank2.value()
<< std::endl;

assert(rank1.value() < rank2.value());
std::cout << "Due to rank1.value() < rank2.value(), player 1 has a stronger hand" << std::endl;
std::cout
<< "Due to rank1.value() < rank2.value(), player 1 has a stronger hand"
<< std::endl;
}
22 changes: 14 additions & 8 deletions cpp/examples/evaluator6_standalone_example.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <phevaluator/phevaluator.h>
#include <iostream>

#include <cassert>
#include <iostream>

/*
* This example uses library pheval6.
Expand All @@ -11,17 +12,22 @@
* and follow `examples/cpp_example.cc`.
*/

int main()
{
phevaluator::Rank rank1 = phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "7d");
phevaluator::Rank rank2 = phevaluator::EvaluateCards("8c", "7c", "6s", "5d", "4s", "2s");
int main() {
phevaluator::Rank rank1 =
phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "7d");
phevaluator::Rank rank2 =
phevaluator::EvaluateCards("8c", "7c", "6s", "5d", "4s", "2s");

assert(rank1.value() == 292);
std::cout << "The rank of the hand in player 1 is " << rank1.value() << std::endl;
std::cout << "The rank of the hand in player 1 is " << rank1.value()
<< std::endl;

assert(rank2.value() == 1606);
std::cout << "The rank of the hand in player 2 is " << rank2.value() << std::endl;
std::cout << "The rank of the hand in player 2 is " << rank2.value()
<< std::endl;

assert(rank1.value() < rank2.value());
std::cout << "Due to rank1.value() < rank2.value(), player 1 has a stronger hand" << std::endl;
std::cout
<< "Due to rank1.value() < rank2.value(), player 1 has a stronger hand"
<< std::endl;
}
26 changes: 16 additions & 10 deletions cpp/examples/evaluator7_standalone_example.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <phevaluator/phevaluator.h>
#include <iostream>

#include <cassert>
#include <iostream>

/*
* This example uses library pheval7.
Expand All @@ -11,18 +12,23 @@
* and follow `examples/cpp_example.cc`.
*/

int main()
{
phevaluator::Rank rank1 = phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "Qc", "6c");
phevaluator::Rank rank2 = phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "2c", "9h");
int main() {
phevaluator::Rank rank1 =
phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "Qc", "6c");
phevaluator::Rank rank2 =
phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "2c", "9h");

// expected 292
// expected 292
assert(rank1.value() == 292);
std::cout << "The rank of the hand in player 1 is " << rank1.value() << std::endl;
// expected 236
std::cout << "The rank of the hand in player 1 is " << rank1.value()
<< std::endl;
// expected 236
assert(rank2.value() == 236);
std::cout << "The rank of the hand in player 2 is " << rank2.value() << std::endl;
std::cout << "The rank of the hand in player 2 is " << rank2.value()
<< std::endl;

assert(rank2.value() < rank1.value());
std::cout << "Due to rank2.value() < rank1.value(), player 2 has a better hand" << std::endl;
std::cout
<< "Due to rank2.value() < rank1.value(), player 2 has a better hand"
<< std::endl;
}
34 changes: 18 additions & 16 deletions cpp/examples/omaha_example.cc
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
#include <iostream>
#include <phevaluator/phevaluator.h>
#include <phevaluator/rank.h>

#include <cassert>
#include <iostream>

/*
* This example is mostly the same as plo4_example.cc
* In this example, we call EvaluateOmahaCards, which is exactly the same as EvaluatePlo4Cards
* In this example, we call EvaluateOmahaCards, which is exactly the same as
* EvaluatePlo4Cards
*/
int main()
{
int main() {
/*
* Community cards: 4c 5c 6c 7s 8s
* Player 1: 2c 9c As Kd
* Player 2: 6s 9s Ts Js
*/
phevaluator::Rank rank1 =
phevaluator::EvaluateOmahaCards("4c", "5c", "6c", "7s", "8s", // community cards
"2c", "9c", "As", "Kd"); // player hole cards
phevaluator::Rank rank2 =
phevaluator::EvaluateOmahaCards("4c", "5c", "6c", "7s", "8s", // community cards
"6s", "9s", "Ts", "Js"); // player hole cards
phevaluator::Rank rank1 = phevaluator::EvaluateOmahaCards(
"4c", "5c", "6c", "7s", "8s", // community cards
"2c", "9c", "As", "Kd"); // player hole cards
phevaluator::Rank rank2 = phevaluator::EvaluateOmahaCards(
"4c", "5c", "6c", "7s", "8s", // community cards
"6s", "9s", "Ts", "Js"); // player hole cards

/*
* It seems that Player 2 can make a straight-flush, but that's not true. Because each
* player can only select 3 cards from the community cards and 2 cards from his own hole
* cards, so Player 2 cannot get a straight-flush in this example.
* It seems that Player 2 can make a straight-flush, but that's not true.
* Because each player can only select 3 cards from the community cards and 2
* cards from his own hole cards, so Player 2 cannot get a straight-flush in
* this example.
*
* Therefore the result is, Player 1 can make a 9-high flush in clubs, and Player 2 can
* only make a 10-high straight.
* Therefore the result is, Player 1 can make a 9-high flush in clubs, and
* Player 2 can only make a 10-high straight.
*/
assert(rank1.value() == 1578);
std::cout << "Player 1 has:" << std::endl;
Expand All @@ -43,5 +45,5 @@ int main()
std::cout << rank2.describeRank() << std::endl;
std::cout << rank2.describeSampleHand() << std::endl;

return 0;
return 0;
}
Loading
Loading