main.cpp 2.39 KB
Newer Older
Thomas R. Koll's avatar
Thomas R. Koll committed
1
2
3
4
5
6
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

#define DEBUG 1

Thomas R. Koll's avatar
Thomas R. Koll committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

/** 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;
}


Thomas R. Koll's avatar
Thomas R. Koll committed
27
28
29
30
31
struct Player {
  short score; // defaults o 0
  short saturation; // their colour
  int stones[6];
  unsigned int field;
Thomas R. Koll's avatar
Thomas R. Koll committed
32
  char* name;
Thomas R. Koll's avatar
Thomas R. Koll committed
33
  Player();
Thomas R. Koll's avatar
Thomas R. Koll committed
34
  Player(unsigned int field, char* name) : field(field), score(0), name(name) {};
Thomas R. Koll's avatar
Thomas R. Koll committed
35
36
37
38
39
40
};

struct Stone {
  int position; // where a stone is at right now
};

41
42
43
44
45
46
struct Game {
  int numberOfTurns;
  int playingField;
  Game() : numberOfTurns(0), playingField(0) {};
};

Thomas R. Koll's avatar
Thomas R. Koll committed
47
48
49
50
51
52
53
54
55
56
57
58
// two bit masks to with the board layout for each player
// starts with the each player's four outer fields, followed
// by the eight shared field and again each player's two outer finish fields
//
// 1  2  3  4       18 17
// 9 10 11 12 13 14 15 16
// 5  6  7  8       20 19

const unsigned int playerOneField = 0b11110000111111111100;
const unsigned int playerTwoField = 0b00001111111111110011;
const unsigned int protectedFields = 0b1111111110001; // it's just one field in the middle

59
60
Game game;

Thomas R. Koll's avatar
Thomas R. Koll committed
61
62
void throwDice( int dice) {
  dice = 0;
63
  for (int i = 0; i < 4; i++ ) {
Thomas R. Koll's avatar
Thomas R. Koll committed
64
65
    dice = (dice << 1) | (rand() % 2);
  }
Thomas R. Koll's avatar
Thomas R. Koll committed
66
67
  #ifdef DEBUG
    printf("Thrown dice: %s\n", binpad(dice, 4));
Thomas R. Koll's avatar
Thomas R. Koll committed
68
69
70
  #endif
}

71
72
int turn(Player player) {
    #ifdef DEBUG
Thomas R. Koll's avatar
Thomas R. Koll committed
73
      printf("New turn #%i for %s\n", game.numberOfTurns, player.name);
74
75
76
77
78
79
    #endif
  static int dice;
  throwDice(dice);
  // wait for user input
}

Thomas R. Koll's avatar
Thomas R. Koll committed
80
81
82
83
int moveStone(Player player, Stone stone, int value) {
}

int main() {
84
  game = Game();
Thomas R. Koll's avatar
Thomas R. Koll committed
85
  srand (time(NULL));
Thomas R. Koll's avatar
Thomas R. Koll committed
86
87
  Player playerOne(playerOneField, (char*) "One");
  Player playerTwo(playerTwoField, (char*) "Two");
88
89
90
91
92
93
94
  #ifdef DEBUG
    printf("Starting game...\n");
  #endif
  while(playerOne.score < 6 && playerTwo.score < 6 && game.numberOfTurns < 100) {
    game.numberOfTurns++;
    turn(game.numberOfTurns % 2 ? playerOne : playerTwo);
  }
Thomas R. Koll's avatar
Thomas R. Koll committed
95
}