I am quite new to c, and I'm trying to get better in it. I want to create a simple snake & ladder game.
I achieved the layout of the board that I wanted, but I'm not sure if this is really an efficient way of creating it.
Could you give me some input or review to that?
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
void initBoard(void) {
uint8_t board[100];
bool row_even = true;
for (uint8_t i = 1; i < 101; i++) {
board[i] = i;
}
for (uint8_t i = 100; i > 0; i -= 10){
if (row_even) { // Check if even or odd row
for (uint8_t k = 0; k < 11; k++) {
if (((k % 10) == 0) && (k != 0)) {
printf("\n");
row_even = false;
continue;
}
printf("%d ", board[i - k]);
}
} else {
for (int8_t k = 9; k > -1; k--) {
if (((k % 10) == 0) && (k != 10)) {
printf("%d ", board[i]);
printf("\n");
row_even = true;
continue;
}
printf("%d ", board[i - k]);
}
}
}
}
int main(void) {
initBoard();
return 0;
}
The output is as I wanted it to be:
100 99 98 97 96 95 94 93 92 91
81 82 83 84 85 86 87 88 89 90
80 79 78 77 76 75 74 73 72 71
61 62 63 64 65 66 67 68 69 70
60 59 58 57 56 55 54 53 52 51
41 42 43 44 45 46 47 48 49 50
40 39 38 37 36 35 34 33 32 31
21 22 23 24 25 26 27 28 29 30
20 19 18 17 16 15 14 13 12 11
01 02 03 04 05 06 07 08 09 10
99-0
instead of100-1
... It simply makes more sense (to me). \$\endgroup\$'0'
come from to print 1 as"01"
instead of"1"
as listed in the "output is as I wanted". \$\endgroup\$