Move and rename binpad and add a Makefile

parent c3271473
all:
g++ main.cpp decimal_to_binary.c -o royal-game-of-ur
/** returns pointer to binary representation of 'n' zero padded to 'sz'.
* returns pointer to string contianing binary representation of
* unsigned 64-bit (or less ) value zero padded to 'sz' digits.
* https://stackoverflow.com/questions/32488011/printing-the-binary-representation-of-a-number#
*/
#include <stdlib.h>
#include "decimal_to_binary.h"
char *decimal_to_binary (unsigned long n, unsigned long sz)
{
static char s[32 + 1] = {0};
char *p = s + 32;
register size_t i;
for (i = 0; i < sz; i++)
*--p = (n>>i & 1) ? '1' : '0';
return p;
}
#ifndef DECIMAL_TO_BINARY
#define DECIMAL_TO_BINARY
char* decimal_to_binary (unsigned long n, unsigned long sz);
#endif
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include "decimal_to_binary.h"
#define DEBUG 1
/** returns pointer to binary representation of 'n' zero padded to 'sz'.
* returns pointer to string contianing binary representation of
* unsigned 64-bit (or less ) value zero padded to 'sz' digits.
* https://stackoverflow.com/questions/32488011/printing-the-binary-representation-of-a-number#
*/
char *binpad (unsigned long n, size_t sz);
char *binpad (unsigned long n, size_t sz)
{
static char s[32 + 1] = {0};
char *p = s + 32;
register size_t i;
for (i = 0; i < sz; i++)
*--p = (n>>i & 1) ? '1' : '0';
return p;
}
struct Player {
short score; // defaults o 0
short saturation; // their colour
......@@ -71,7 +53,7 @@ void throwDice( int dice) {
dice = (dice << 1) | (rand() % 2);
}
#ifdef DEBUG
printf("Thrown dice: %s: %i\n", binpad(dice, 4), diceValue(dice));
printf("Thrown dice: %s: %i\n", decimal_to_binary(dice, 4), diceValue(dice));
#endif
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment