From 8c2ba1b7f90ec1dce410d9e825a082df137a4a02 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Wed, 1 Jun 2022 12:11:22 +0800 Subject: Merge piece/position selection to single loop --- sirtet.c | 72 ++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 34 insertions(+), 38 deletions(-) (limited to 'sirtet.c') 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; } } -- cgit v1.2.3