-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoreboard.c
83 lines (75 loc) · 2.02 KB
/
scoreboard.c
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
#include <curses.h>
#include <stdlib.h>
#include "scoreboard.h"
void prettyPrint(int value, char* buffer);
ScoreBoard* scoreboard_init() {
ScoreBoard* board = malloc(sizeof(ScoreBoard));
board->linesCleared = 0;
board->score = 0;
board->difficulty = 0;;
return board;
}
void scoreboard_free(ScoreBoard* board) {
if (board != NULL) {
free(board);
}
}
void scoreboard_clearLines(ScoreBoard* board, int n) {
if (board != NULL) {
board->linesCleared += n;
int score;
switch (n) {
case 1: score = 40; break;
case 2: score = 100; break;
case 3: score = 300; break;
case 4: score = 1200; break;
default: score = 0;
}
board->score += score;
// check for difficulty
board->difficulty = board->linesCleared/10;
}
}
void scoreboard_draw(ScoreBoard* board, WINDOW* win) {
if (board == NULL || win == NULL) {
return;
}
box(win, 0, 0);
char buffer[64];
prettyPrint(board->score, buffer);
mvwprintw(win, 1, 1, "Score: %5s", buffer);
mvwprintw(win, 2, 1, "Cleared: %5d", board->linesCleared);
mvwprintw(win, 3, 1, "Difficuly: %5d", board->difficulty);
touchwin(win);
wrefresh(win);
}
void prettyPrint(int value, char* buffer) {
// figure out how many digits there are
int digits = 0;
int valCopy = value;
while (valCopy > 0) {
digits++;
valCopy /= 10;
}
if (digits == 0) {
buffer[0] = '0';
buffer[1] = '\0';
} else {
int mask = 1;
int i, bi;
for (i = 0; i < digits; i++) {
mask *= 10;
}
for (i = 0, bi = 0; i < digits; i++) {
int digit = (value % mask) / (mask / 10);
buffer[bi] = (char) ('0' + digit);
bi++;
mask /= 10;
if ((digits - i - 1) % 3 == 0 && (digits - i - 1) > 0) {
buffer[bi] = ',';
bi++;
}
}
buffer[bi] = '\0';
}
}