Skip to content

Commit

Permalink
Format C files with clang-format, delete trailing whitespace, fix lin…
Browse files Browse the repository at this point in the history
…e endings, and add final newline
  • Loading branch information
azriel1rf committed May 3, 2024
1 parent 9157517 commit b65344b
Show file tree
Hide file tree
Showing 58 changed files with 61,058 additions and 74,790 deletions.
13 changes: 7 additions & 6 deletions cpp/benchmark/benchmark.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "benchmark/benchmark.h"

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

using namespace phevaluator;

static void EvaluateAllFiveCards(benchmark::State& state) {
static void EvaluateAllFiveCards(benchmark::State &state) {
for (auto _ : state) {
for (int a = 0; a < 48; a++) {
for (int b = a + 1; b < 49; b++) {
Expand All @@ -21,7 +22,7 @@ static void EvaluateAllFiveCards(benchmark::State& state) {
}
BENCHMARK(EvaluateAllFiveCards);

static void EvaluateAllSixCards(benchmark::State& state) {
static void EvaluateAllSixCards(benchmark::State &state) {
for (auto _ : state) {
for (int a = 0; a < 47; a++) {
for (int b = a + 1; b < 48; b++) {
Expand All @@ -40,7 +41,7 @@ static void EvaluateAllSixCards(benchmark::State& state) {
}
BENCHMARK(EvaluateAllSixCards);

static void EvaluateAllSevenCards(benchmark::State& state) {
static void EvaluateAllSevenCards(benchmark::State &state) {
for (auto _ : state) {
for (int a = 0; a < 46; a++) {
for (int b = a + 1; b < 47; b++) {
Expand All @@ -63,7 +64,7 @@ BENCHMARK(EvaluateAllSevenCards);

const int SIZE = 100;

static void EvaluateRandomFiveCards(benchmark::State& state) {
static void EvaluateRandomFiveCards(benchmark::State &state) {
std::vector<std::vector<int>> hands;
card_sampler::CardSampler cs{};
for (int i = 0; i < SIZE; i++) {
Expand All @@ -78,7 +79,7 @@ static void EvaluateRandomFiveCards(benchmark::State& state) {
}
BENCHMARK(EvaluateRandomFiveCards);

static void EvaluateRandomSixCards(benchmark::State& state) {
static void EvaluateRandomSixCards(benchmark::State &state) {
std::vector<std::vector<int>> hands;
card_sampler::CardSampler cs{};

Expand All @@ -95,7 +96,7 @@ static void EvaluateRandomSixCards(benchmark::State& state) {
}
BENCHMARK(EvaluateRandomSixCards);

static void EvaluateRandomSevenCards(benchmark::State& state) {
static void EvaluateRandomSevenCards(benchmark::State &state) {
std::vector<std::vector<int>> hands;
card_sampler::CardSampler cs{};

Expand Down
2 changes: 1 addition & 1 deletion cpp/benchmark/benchmark_plo4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace phevaluator;

const int SIZE = 100;

static void EvaluateRandomPlo4Cards(benchmark::State& state) {
static void EvaluateRandomPlo4Cards(benchmark::State &state) {
std::vector<std::vector<int>> hands;
card_sampler::CardSampler cs{};

Expand Down
2 changes: 1 addition & 1 deletion cpp/benchmark/benchmark_plo5.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace phevaluator;

const int SIZE = 100;

static void EvaluateRandomPlo5Cards(benchmark::State& state) {
static void EvaluateRandomPlo5Cards(benchmark::State &state) {
std::vector<std::vector<int>> hands;
card_sampler::CardSampler cs{};

Expand Down
2 changes: 1 addition & 1 deletion cpp/benchmark/benchmark_plo6.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace phevaluator;

const int SIZE = 100;

static void EvaluateRandomPlo6Cards(benchmark::State& state) {
static void EvaluateRandomPlo6Cards(benchmark::State &state) {
std::vector<std::vector<int>> hands;
card_sampler::CardSampler cs{};

Expand Down
7 changes: 1 addition & 6 deletions cpp/examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
c_example
cpp_example
plo4_example
plo5_example
plo6_example
omaha_example
c_example cpp_example plo4_example plo5_example plo6_example omaha_example
86 changes: 53 additions & 33 deletions cpp/examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,47 +1,67 @@
CC=gcc
CXX=g++
TOPDIR=..
INCLUDE=${TOPDIR}/include
CFLAGS+=-std=c99
CXXFLAG+=-std=c++17
CC = gcc CXX = g++ TOPDIR =..INCLUDE = ${TOPDIR} / include CFLAGS += -std =
c99 CXXFLAG += -std =
c++ 17

all: c_example cpp_example plo4_example omaha_example plo5_example plo6_example
all
: c_example cpp_example plo4_example omaha_example plo5_example plo6_example

c_example: c_example.c ../libpheval.a
${CC} ${CFLAGS} -I${INCLUDE} $^ -o $@
c_example
: c_example.c../ libpheval.a ${CC} ${CFLAGS} - I${INCLUDE} $ ^
-o $ @

cpp_example: cpp_example.cc ../libpheval.a
${CXX} ${CFLAGS} ${CXXFLAG} -I${INCLUDE} $^ -o $@
cpp_example
: cpp_example.cc../ libpheval.a ${CXX} ${CFLAGS} ${CXXFLAG} -
I${INCLUDE} $ ^
-o $ @

plo4_example: plo4_example.cc ../libphevalplo4.a
${CXX} ${CFLAGS} ${CXXFLAG} -I${INCLUDE} $^ -o $@
plo4_example
: plo4_example.cc../ libphevalplo4.a ${CXX} ${CFLAGS} ${CXXFLAG} -
I${INCLUDE} $ ^
-o $ @

omaha_example: omaha_example.cc ../libphevalplo4.a
${CXX} ${CFLAGS} ${CXXFLAG} -I${INCLUDE} $^ -o $@
omaha_example
: omaha_example.cc../ libphevalplo4.a ${CXX} ${CFLAGS} ${CXXFLAG} -
I${INCLUDE} $ ^
-o $ @

plo5_example: plo5_example.cc ../libphevalplo5.a
${CXX} ${CFLAGS} ${CXXFLAG} -I${INCLUDE} $^ -o $@
plo5_example
: plo5_example.cc../ libphevalplo5.a ${CXX} ${CFLAGS} ${CXXFLAG} -
I${INCLUDE} $ ^
-o $ @

plo6_example: plo6_example.cc ../libphevalplo6.a
${CXX} ${CFLAGS} ${CXXFLAG} -I${INCLUDE} $^ -o $@
plo6_example
: plo6_example.cc../ libphevalplo6.a ${CXX} ${CFLAGS} ${CXXFLAG} -
I${INCLUDE} $ ^
-o $ @

../libpheval.a:
$(MAKE) -C ../;
../
libpheval.a
: $(MAKE) - C../
;

../libphevalplo4.a:
$(MAKE) -C ../ libphevalplo4.a
../ libphevalplo4.a : $(MAKE) -
C../
libphevalplo4
.a

../libphevalplo5.a:
$(MAKE) -C ../ libphevalplo5.a
../
libphevalplo5.a : $(MAKE) -
C../
libphevalplo5
.a

../libphevalplo6.a:
$(MAKE) -C ../ libphevalplo6.a
../
libphevalplo6.a : $(MAKE) -
C../ libphevalplo6.a

%.o: %.cc
${CXX} -c ${CXXFLAG} -I${INCLUDE} $< -o $@
%.o : %.cc ${CXX} -
c ${CXXFLAG} - I${INCLUDE} $ <
-o $ @

%.o: %.c
${CC} -c ${CFLAGS} -I${INCLUDE} $< -o $@
%.o : %.c ${CC} -
c ${CFLAGS} - I${INCLUDE} $ <
-o $ @

clean:
rm -f *.o c_example cpp_example plo4_example omaha_example plo5_example plo6_example
clean : rm
- f *.o c_example cpp_example plo4_example omaha_example plo5_example
plo6_example
85 changes: 42 additions & 43 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,52 +35,52 @@ 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);
const char* rank_category_description = describe_rank_category(category);
const char *rank_category_description = describe_rank_category(category);
assert(strcmp(rank_category_description, "Full House") == 0);
printf("Player 2 has a %s\n", rank_category_description);

const char* rank_description = describe_rank(rank2);
const char *rank_description = describe_rank(rank2);
printf("More specifically, player 2 has a %s\n", rank_description);
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": "");
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" : "");
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;
}
Loading

0 comments on commit b65344b

Please sign in to comment.