commit c5705c3d694b4299a0730dc19d77862dcae128f2
parent 06d1ddbcf3506e4e24592d334573101c8a749b3c
Author: Luxferre <lux@ferre>
Date: Tue, 16 Jan 2024 13:33:15 +0200
Introduced autohome functionality
Diffstat:
M | nnfc.awk | | | 47 | ++++++++++++++++++++++++++++++++++++++++------- |
1 file changed, 40 insertions(+), 7 deletions(-)
diff --git a/nnfc.awk b/nnfc.awk
@@ -1,6 +1,6 @@
# no-nonsense FreeCell in POSIX AWK
# run as: [n]awk -f nnfc.awk [-v ASCII=1] [-v MONOCHROME=1] [-v SEED=x]
-# commands: q - quit, u - undo,
+# commands: q - quit, u - undo, ah - autohome, h - help,
# [number][number] - move from column/freecell to column/freecell/foundation:
# - 0 is foundation (can be omitted)
# - 1 to 8 are column numbers
@@ -84,7 +84,7 @@ function render(i, j, res) {
}
# main logic/move function (also handles the undo)
-# statuses: 0 - irrecoverable error, 1 - continue, 2 - victory
+# statuses: 0 - invalid move, 1 - continue, 2 - victory
function domove(cmd, valid, nums, from, to, si) {
if(cmd == "u") { # undo logic
if(undoidx > 0) {
@@ -97,7 +97,7 @@ function domove(cmd, valid, nums, from, to, si) {
return 1 # continue
}
valid = 0 # invalid by default until all checks are done
- # if cmd is not q or one of the above, then it must be two numbers
+ # if cmd is not q, h, ah or u, then it must be two numbers
split(cmd, nums, "") # numbers must be space-separated
from = nums[1] # location from which we're moving
to = nums[2] # location to which we're moving
@@ -161,10 +161,38 @@ function domove(cmd, valid, nums, from, to, si) {
undos["fcell",undoidx] = assocser(fcell, ",")
undos["tablens",undoidx] = assocser(tablens, ",")
}
- else print "Invalid move!"
# now, check for the win condition: all kings in fnd
if(fnd[0] == 12 && fnd[1] == 25 && fnd[2] == 38 && fnd[3] == 51) return 2
- else return 1 # continue
+ else return valid # we can use the external move status
+}
+
+# automatically move all eligible cards to the foundation
+function autohome(chres, lres, i) {
+ do {
+ chres = 0
+ for(i=1;i<9;i++) {
+ lres = domove(i "0")
+ if(lres == 2) return 2
+ else chres += lres
+ }
+ lres = domove("a0")
+ if(lres == 2) return 2
+ else chres += lres
+ lres = domove("b0")
+ if(lres == 2) return 2
+ else chres += lres
+ lres = domove("c0")
+ if(lres == 2) return 2
+ else chres += lres
+ lres = domove("d0")
+ if(lres == 2) return 2
+ else chres += lres
+ } while(chres > 0)
+ return 1 # always continue
+}
+
+function help() {
+ return 1 # continue
}
BEGIN { # main code part
@@ -219,13 +247,18 @@ BEGIN { # main code part
undos["tablens",0] = assocser(tablens, ",")
undoidx = 0 # the current undo index is 0
# first-time display
- print "nnfc by Luxferre"
+ print "nnfc by Luxferre\nenter h for command help"
render()
printf("\n> ")
while((getline cmd) > 0) { # main interactive loop
cmd = tolower(cmd) # accept both uppercase and lowercase
if(cmd == "q") {print "Quitting..."; break}
- res = domove(cmd) # pass the command to the logic function
+ else if(cmd == "h") res = help()
+ else if(cmd == "ah") res = autohome()
+ else { # pass the command to the logic function
+ res = domove(cmd)
+ if(res == 0) print "Invalid move!"
+ }
render()
if(res == 2) {print "Victory!"; break}
printf("\n> ")