Skip to content

Commit

Permalink
Format C and C++ files with pre-commit hooks including clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel1rf committed May 3, 2024
1 parent 8de95ab commit 1b6d948
Show file tree
Hide file tree
Showing 59 changed files with 61,026 additions and 74,775 deletions.
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
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
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;
}
Loading

0 comments on commit 1b6d948

Please sign in to comment.