commit e76d55ccae3112c148ce5f1f98710cc7919e2dc0
parent 34f30cd92bf011d6f31d9f1c028187a14f81fe8a
Author: Luxferre <lux@ferre>
Date: Wed, 17 Jan 2024 22:40:16 +0200
Implemented classic safe autohoming
Diffstat:
M | nnfc.awk | | | 34 | +++++++++++++++++++++------------- |
1 file changed, 21 insertions(+), 13 deletions(-)
diff --git a/nnfc.awk b/nnfc.awk
@@ -1,5 +1,5 @@
# no-nonsense FreeCell in POSIX AWK
-# run as: [n]awk -f nnfc.awk [-v ASCII=1] [-v MONOCHROME=1] [-v SEED=x] [-v NAH=1]
+# run as: [n]awk -f nnfc.awk [-v ASCII=1] [-v MONOCHROME=1] [-v SEED=x]
# commands: q - quit, u - undo, h - help, r - restart the run
# [number][number] - move from column/freecell to column/freecell/foundation:
# - 0 is foundation (can be omitted)
@@ -105,12 +105,13 @@ function undo(i) { # undo functionality
# main logic/move function (also handles the undo)
# statuses: 0 - invalid move, 1 - continue, 2 - victory
-function domove(cmd, valid, nums, from, to, amt, i, si, ec, efc) {
+function domove(cmd, isauto, valid, nums, from, to, amt, i, si, ec, efc) {
valid = 0 # invalid by default until all checks are done
# if cmd is not q, h, r or u, then it must be two "digits"
split(cmd, nums, "") # parse the command as a whole
from = nums[1] # location from which we're moving
to = nums[2] # location to which we're moving
+ if(to == "h") to = 0 # for compatibility with standard notation
amt = 1 # the amount of cards being moved (default 1)
# identify free cells
if(from == "a") from = 9
@@ -167,7 +168,17 @@ function domove(cmd, valid, nums, from, to, amt, i, si, ec, efc) {
if(cval > -1) { # we are moving a non-empty value
if(to == 0) { # moving to a foundation
si = getsuit(cval) # get suit index
- if((fnd[si]%13) == ((cval%13) - 1)) { # we can move there
+ efc = cval % 13 # reuse for card rank value
+ if((fnd[si]%13) == (efc - 1)) { # we can move there
+ # check the automove constraints, aces and twos are always played
+ if(isauto && efc > 1) {
+ # check if all cards with efc-1 of the opposite color are there
+ if(getcolor(cval) == 0) { # we're red, check black
+ if(!(fnd[0] >= (efc - 1) && fnd[3] >= (efc - 1))) return 0
+ } else { # we're black, check red
+ if(!(fnd[1] >= (efc - 1) && fnd[2] >= (efc - 1))) return 0
+ }
+ }
fnd[si] = cval
if(from < 9) { # moving from a tableau
delete table[rfrom,tablens[rfrom]-1] # delete the card
@@ -226,20 +237,20 @@ function autohome(chres, lres, i) {
do {
chres = 0
for(i=1;i<9;i++) {
- lres = domove(i "0")
+ lres = domove(i "0", 1)
if(lres == 2) return 2
else chres += lres
}
- lres = domove("a0")
+ lres = domove("a0", 1)
if(lres == 2) return 2
else chres += lres
- lres = domove("b0")
+ lres = domove("b0", 1)
if(lres == 2) return 2
else chres += lres
- lres = domove("c0")
+ lres = domove("c0", 1)
if(lres == 2) return 2
else chres += lres
- lres = domove("d0")
+ lres = domove("d0", 1)
if(lres == 2) return 2
else chres += lres
} while(chres > 0)
@@ -253,7 +264,6 @@ function help() {
print "-v ASCII=1\tuse ASCII instead of Unicode suits"
print "-v MONOCHROME=1\tdon't use colors"
print "-v SEED=xxxxx\tset game seed number (M$-compatible)"
- print "-v NAH=1\tdisable autohoming after every move (use ah for this)"
print "Commands: q - quit, u - undo, r - restart, h - this help"
print "Moves: [digit][digit], where \"digits\" are:"
print "- 0 is foundation (can be omitted)"
@@ -327,17 +337,15 @@ BEGIN { # main code part
cmd = tolower(cmd) # accept both uppercase and lowercase
if(cmd == "q") {print "Quitting..."; break}
else if(cmd == "h") res = help()
- else if(cmd == "ah") res = autohome()
else if(cmd == "u") undo()
else if(cmd == "r") { # restart logic
while(undoidx > 0) undo()
undomoves = 0
} else { # pass the command to the logic function
undomoves = 0 # set global undo move counter
- res = domove(cmd)
+ res = domove(cmd, 0) # manual move
if(res == 0) print "Invalid move!"
- else if(NAH != 1 && res < 2) res = autohome()
- print "Undomoves:", undomoves
+ else if(res < 2) res = autohome()
}
render()
if(res == 2) {print "Victory!"; break}