summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-06-01 12:11:22 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-06-01 12:11:22 +0800
commit8c2ba1b7f90ec1dce410d9e825a082df137a4a02 (patch)
tree58776831aa26a93bf10c8c37a573f2708071ae11
parent0c6ecb8ebd68910c556facf0eed5730687412c0a (diff)
Merge piece/position selection to single loop
-rw-r--r--sirtet.c72
1 files changed, 34 insertions, 38 deletions
diff --git a/sirtet.c b/sirtet.c
index e3c0590..1d53ac2 100644
--- a/sirtet.c
+++ b/sirtet.c
@@ -126,63 +126,38 @@ void sirtet() {
printrect(0, 0, H, W);
printmap(map, 1, 1, H, W);
- // select piece
- bool piece_confirmed = false;
+ // select piece and position
+ bool confirmed = false;
+
int pc_idx;
struct piece* pc;
for (int i = 0; i < P; i++) {
+ // find first non-null piece
if (hand[i] != NULL) {
pc_idx = i;
+ pc = hand[i];
break;
}
}
- while (!piece_confirmed) {
- printpieces(hand, H + 2, 0, P, pc_idx);
- refresh();
- // await user input
- int ch = getch();
- switch (ch) {
- case 10: // enter
- piece_confirmed = true;
- pc = hand[pc_idx];
- break;
- case KEY_LEFT:
- for (int i = 0; i < P; i++) {
- // find previous non-null piece
- pc_idx = (pc_idx + P - 1) % P;
- if (hand[pc_idx] != NULL)
- break;
- }
- break;
- case KEY_RIGHT:
- for (int i = 0; i < P; i++) {
- // find next non-null piece
- pc_idx = (pc_idx + 1) % P;
- if (hand[pc_idx] != NULL)
- break;
- }
- break;
- }
- }
-
-
- // select position
- bool pos_confirmed = false;
int row = 0;
int col = 0;
- while (!pos_confirmed) {
- printmap(map, 1, 1, H, W);
+ while (!confirmed) {
+ printmap(map, 1, 1, H, W);
+ printpieces(hand, H + 2, 0, P, pc_idx);
bool pos_placeable = placeable(map, pc, row, col, H, W);
printpiece(pc, row + 1, col * 2 + 1, (pos_placeable ? 1 : 2));
+ // await user input
int ch = getch();
switch (ch) {
- case 10: // enter
+ // enter: confirm choice
+ case 10:
if (pos_placeable)
- pos_confirmed = true;
+ confirmed = true;
break;
+ // arrow keys: move piece on map
case KEY_LEFT:
if (col > 0) col--;
break;
@@ -195,6 +170,27 @@ void sirtet() {
case KEY_DOWN:
if (row + pc->h < H) row++;
break;
+ // [ and ]: switch pieces
+ case '[':
+ for (int i = 0; i < P; i++) {
+ // find previous non-null piece
+ pc_idx = (pc_idx + P - 1) % P;
+ if (hand[pc_idx] != NULL) {
+ pc = hand[pc_idx];
+ break;
+ }
+ }
+ break;
+ case ']':
+ for (int i = 0; i < P; i++) {
+ // find next non-null piece
+ pc_idx = (pc_idx + 1) % P;
+ if (hand[pc_idx] != NULL) {
+ pc = hand[pc_idx];
+ break;
+ }
+ }
+ break;
}
}