aboutsummaryrefslogtreecommitdiff
path: root/src/ctaklm.c
blob: ad8ab6eb062f72c688573502152a2c77c5e1acf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <3rd-party/linenoise.h>
#include <tak.h>

char *rev = "\033[7m";

char *blk = "\033[41m", *wht = "\033[44m";

char *und = "\033[4m";
char *rst = "\033[0m";

#define SQUARE_W 5
#define SQUARE_H 3

#define CHAR_FLT '#'
#define CHAR_STN '/'
#define CHAR_CAP '*'

static void
put_stone(const enum STONE_VARIANT stone, const enum COLOUR colour,
          const uint8_t top, const uint8_t beyond_carry_limit) {
  if (beyond_carry_limit) {
    fputs(und,stdout);
  } else {
    if (colour == C_BLACK) fputs(blk, stdout);
    else fputs(wht,stdout);
  }
  if (top) {
    switch (stone) {
      case STONE_FLAT:     putchar(CHAR_FLT); break;
      case STONE_STANDING: putchar(CHAR_STN); break;
      case STONE_CAPSTONE: putchar(CHAR_CAP); break;
    }
  } else {
    putchar( (colour == C_BLACK) ? 'B' : 'W' );
  }
  fputs(rst,stdout);
}

static void
print_cell_line(const uint8_t line,
                const uint8_t col, const uint8_t row) {

  const uint8_t location = THE_COORDS(col, row),
    stack_size = COUNT_AT(location);

  uint8_t idx;
  for (uint8_t k = 0; k < SQUARE_W; k++) {
    idx = line + k*SQUARE_H;
    // Are we in the last column to be displayed?
    if ((k+1)*SQUARE_H > stack_size) {
      // If so, then if we can't fill it offset the starting line so
      // that it fills from the bottom up instead of the top down
      if (line < (k+1)*SQUARE_H - stack_size ) {
        // skip these lines
        idx = 0xFF;
      } else {
        // offset back
        idx -= (k+1)*SQUARE_H - stack_size;
      }
    }
    if (idx < stack_size) {
      put_stone(STONE_AT(location), colours[location] & (1 << idx),
                idx == 0, idx >= board_size);
    } else {
      putchar(' ');
    }
  }
}

static void
print_board_line(const uint8_t line) {
  const uint8_t mod = line % (SQUARE_H + 1),
    row = board_size - line/(SQUARE_H + 1) - 1;

  if (mod == (SQUARE_H + 1)/2 ) printf("%d. ",row + 1);
  else fputs("   ",stdout);

  if (mod == 0) {
      for (uint8_t x = 0; x < board_size; x++) {
        putchar('+');
        for (uint8_t k = 0; k < SQUARE_W; k++) putchar('-');
      }
      puts("+");
  } else {
    if (line < board_size*(SQUARE_H+1)) {
      for (uint8_t x = 0; x < board_size; x++) {
        putchar('|');
        print_cell_line(mod - 1 , x, row);
      }
      puts("|");
    } else {
      for (uint8_t x = 0; x < board_size; x++) {
        for (uint8_t k = 0; k <= SQUARE_W/2; k++) putchar(' ');
        printf("%c.",x+'a');
        for (uint8_t k = 0; k < SQUARE_W-SQUARE_W/2-2; k++) putchar(' ');
      }
      putchar('\n');
    }
  }
}

static void
print_board(void) {
    for (uint8_t k = 0; k<=board_size*(SQUARE_H+1)+1; k++) {
      print_board_line(k);
  }
}

static void
print_square(const uint8_t col, const uint8_t row) {
  if (row < board_size && col < board_size) {
    printf("%c%c: ",'a'+col,'1'+row);
    const uint8_t stack_size = COUNT_AT(THE_COORDS(col, row));
    if (stack_size > 0) {
      uint8_t mask = 1 << (stack_size - 1);
      for (uint8_t k = 0; k < stack_size; k++, mask >>= 1) {
        put_stone(STONE_AT(THE_COORDS(col, row)),
                  colours[THE_COORDS(col, row)] & mask,
                  k+1 == stack_size,
                  stack_size-k >= board_size);
      }
      printf("%s <-- top\n",rst);
    } else {
      puts("(empty)");
    }
  } else {
    printf("Requested square not on board (%d x %d).\n",board_size,board_size);
  }
}

int
main(int argc, char **argv) {
  reset_state(5);

  for (uint8_t k = 0; k<board_size; k++) {
    try_place(THE_COORDS(1, k), C_WHITE, STONE_FLAT);
    if (k+1<board_size)
      try_place(THE_COORDS(0, k), C_BLACK, STONE_FLAT);
  }

  check_win();

  enum E_RESULT r;
  char *line;
  while((line = linenoise("ctaklm> ")) != NULL) {
    if (!strncmp(line,"board",6)) {
      print_board();
    } else if (!strncmp(line,"square",6)) {
      if (strnlen(line, 9) >= 9
          && line[7] >= 'a' && line[7] <= '`'+board_size
          && line[8] >= '1' && line[8] <= '0'+board_size) {
        print_square(line[7]-'a', line[8]-'1');
      } else {
        printf("Usage: square [a-%c][1-%c]\n",'`'+board_size,'0'+board_size);
      }
    } else {
      r = do_ptn(line);
      printf("%d\n",r);
      print_board();
    }

    /* /\* Do something with the string. *\/ */
    /* if (line[0] != '\0' && line[0] != '/') { */
    /*   printf("echo: '%s'\n", line); */
    /*   linenoiseHistoryAdd(line); /\* Add to the history. *\/ */
    /*   /\* linenoiseHistorySave("history.txt"); /\\* Save the history on disk. *\\/ *\/ */
    /* } else if (!strncmp(line,"/historylen",11)) { */
    /*   /\* The "/historylen" command will change the history len. *\/ */
    /*   int len = atoi(line+11); */
    /*   linenoiseHistorySetMaxLen(len); */
    /* } else if (!strncmp(line, "/mask", 5)) { */
    /*   linenoiseMaskModeEnable(); */
    /* } else if (!strncmp(line, "/unmask", 7)) { */
    /*   linenoiseMaskModeDisable(); */
    /* } else if (line[0] == '/') { */
    /*   printf("Unreconized command: %s\n", line); */
    /* } */

    free(line);
  }
  return 0;

}