Skip to content

Instantly share code, notes, and snippets.

@G36maid
Created December 29, 2024 08:59
Show Gist options
  • Select an option

  • Save G36maid/9ef943e1edfabe6da976f5848cbc72b8 to your computer and use it in GitHub Desktop.

Select an option

Save G36maid/9ef943e1edfabe6da976f5848cbc72b8 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include "poker.h"
// 2 > A > K > Q > J > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3
int compareCards(const void *a, const void *b);
int32_t QBT_sort(int8_t cards[]);
int random_test();
int err_test();
int main() {
int points = 0;
for(int i=0 ; i<10 ; i++){
if(random_test() == 0){
points++;
}
}
points += err_test()*2;
printf("Points: %d\n", points);
return 0;
}
int random_test(){
int8_t cardsA[13];
int8_t cardsB[13];
srand(time(NULL));
for (int i = 0; i < 13; i++) {
bool unique;
do {
unique = true;
cardsA[i] = rand() % 52 + 1;
for (int j = 0; j < i; j++) {
if (cardsA[i] == cardsA[j]) {
unique = false;
break;
}
}
} while (!unique);
}
for (int i = 0; i < 13; i++) {
cardsB[i] = cardsA[i];
}
if(QBT_sort(cardsA)!=big_two_sort(cardsB)){
return -1;
}
for (int i = 0; i < 13; i++) {
if (cardsA[i] != cardsB[i]) {
return -1;
}
}
return 0;
}
int err_test(){
int points = 0;
//1 same card
int8_t cardsA[13] = {1, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int8_t cardsB[13] = {1, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
points += (big_two_sort(cardsA) == QBT_sort(cardsB));
//2 out of range
int8_t cardsC[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 53, 13};
int8_t cardsD[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 53, 13};
points += (big_two_sort(cardsC) == QBT_sort(cardsD));
//3 negative number
int8_t cardsE[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, -10, 11, 12, 13};
int8_t cardsF[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, -10, 11, 12, 13};
points += (big_two_sort(cardsE) == QBT_sort(cardsF));
//4 zero
int8_t cardsG[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11, 12, 13};
int8_t cardsH[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11, 12, 13};
points += (big_two_sort(cardsG) == QBT_sort(cardsH));
//5 0-based
int8_t cardsI[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int8_t cardsJ[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
points += (big_two_sort(cardsI) == QBT_sort(cardsJ));
printf("Points: %d\n", points);
return points;
}
int32_t QBT_sort(int8_t cards[]){
if(cards == NULL){
return -1;
}
for(int i = 0; i < 13; i++){
if(cards[i] < 1 || cards[i] > 52){
return -1;
}
}
qsort(cards, 13, sizeof(int8_t), compareCards);
//same card nearby
for(int i = 0; i < 12; i++){
if(cards[i] == cards[i+1]){
return -1;
}
}
return 0;
}
int compareCards(const void *a, const void *b){
static int rank_map[13] = {12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int cardA = *(const int8_t *)a;
int cardB = *(const int8_t *)b;
int rankA = (cardA - 1) % 13 ;
int rankB = (cardB - 1) % 13 ;
if (rankA != rankB) {
return rank_map[rankA] - rank_map[rankB];
}
return cardB - cardA;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment