Browse Source

deck basics

master
monaxys 2 years ago
parent
commit
f99f5cdea2
  1. 6
      Makefile
  2. 3
      config.mk
  3. 34
      src/card.h
  4. 59
      src/dealer.c
  5. 10
      src/dealer.h
  6. 12
      src/main.c
  7. 10
      src/player.h

6
Makefile

@ -1,10 +1,10 @@ @@ -1,10 +1,10 @@
include config.mk
c-jack: src/main.c
$(CC) $^ -o $@
c-jack: src/main.c src/dealer.c
$(CC) $^ -o $@ $(LDFLAGS)
%.o: src/%.c
$(CC) $^ -c
$(CC) $(CFLAGS) $^ -c
clean:
rm -f *.o c-jack

3
config.mk

@ -1,2 +1,3 @@ @@ -1,2 +1,3 @@
CC=cc
CFLAGS=-ansi -pedantic -Wall -g
CFLAGS=-std=c99 -pedantic -Wall -g
LDFLAGS =

34
src/card.h

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
#ifndef CARD_H_ENTRY
#define CARD_H_ENTRY
//Описание структуры карты
enum suits
{
hearts = 0,
spades,
clubs,
diamonds
};
enum rangs
{
two = 0,
three,
four,
five,
six,
seven,
eight,
nine,
ten,
jack,
queen,
king,
ace
};
struct card
{
enum rangs rang;
enum suits suit;
};
#endif /* CARD_H_ENTRY */

59
src/dealer.c

@ -0,0 +1,59 @@ @@ -0,0 +1,59 @@
#include "dealer.h"
#include <stdint.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define CARD_AMOUNT_SUIT 13
#define SUIT_AMOUNT 4
#define CARD_AMOUNT_DECK CARD_AMOUNT_SUIT * SUIT_AMOUNT
int fill_deck(struct card *deck)
{
puts("заполняем колоду...");
for(int i = 0; i < SUIT_AMOUNT; i++)
{
for (int k = 0; k < CARD_AMOUNT_SUIT; k++)
{
deck[k + i * CARD_AMOUNT_SUIT].rang = k;
deck[k + i * CARD_AMOUNT_SUIT].suit = i;
}
}
return 0;
}
static uint8_t get_num()
{
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
return -1;
}
uint8_t c;
do{
read(fd, &c, 1);
if(c == EOF){
close(fd);
return -2;
}
}while(c >= (UCHAR_MAX + 1) / CARD_AMOUNT_DECK * CARD_AMOUNT_DECK);
close(fd);
return abs(c % CARD_AMOUNT_DECK);
}
int shuffle_deck(struct card *deck)
{
for(int i = 0, k = 0; i < 1000; i++, k++){
if(k >= CARD_AMOUNT_DECK){
k = 0;
}
uint8_t chg_card = get_num();
if (chg_card >= CARD_AMOUNT_DECK || chg_card < 0){
return 1;
}
struct card tmp = deck[k];
deck[k] = deck[chg_card];
deck[chg_card] = tmp;
}
return 0;
}

10
src/dealer.h

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
#ifndef DEALER_H_ENTRY
#define DEALER_H_ENTRY
#include <stdio.h>
#include "card.h"
struct card *pick_card();
int fill_deck(struct card *deck);
int shuffle_deck(struct card *deck);
#endif /* DEALER_H_ENTRY */

12
src/main.c

@ -1,4 +1,16 @@ @@ -1,4 +1,16 @@
#include <stdio.h>
#include "dealer.h"
#define PLAERSCNT 4
int money, bet;
char players[PLAERSCNT] = {1, 2, 3, 4};
int main(int argc, char **argv)
{
struct card deck[52];
fill_deck(deck);
if(shuffle_deck(deck)){
return 2;
}
return 0;
}

10
src/player.h

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
#ifndef PLAYER_H_ENTRY
#define PLAYER_H_ENTRY
struct player
{
int money;
}
#endif /* PLAYER_H_ENTRY */
Loading…
Cancel
Save