Implemented classic safe autohoming

This commit is contained in:
Luxferre
2024-01-17 22:40:16 +02:00
parent 34f30cd92b
commit e76d55ccae
+21 -13
View File
@@ -1,5 +1,5 @@
# no-nonsense FreeCell in POSIX AWK # 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 # commands: q - quit, u - undo, h - help, r - restart the run
# [number][number] - move from column/freecell to column/freecell/foundation: # [number][number] - move from column/freecell to column/freecell/foundation:
# - 0 is foundation (can be omitted) # - 0 is foundation (can be omitted)
@@ -105,12 +105,13 @@ function undo(i) { # undo functionality
# main logic/move function (also handles the undo) # main logic/move function (also handles the undo)
# statuses: 0 - invalid move, 1 - continue, 2 - victory # 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 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" # if cmd is not q, h, r or u, then it must be two "digits"
split(cmd, nums, "") # parse the command as a whole split(cmd, nums, "") # parse the command as a whole
from = nums[1] # location from which we're moving from = nums[1] # location from which we're moving
to = nums[2] # location to 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) amt = 1 # the amount of cards being moved (default 1)
# identify free cells # identify free cells
if(from == "a") from = 9 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(cval > -1) { # we are moving a non-empty value
if(to == 0) { # moving to a foundation if(to == 0) { # moving to a foundation
si = getsuit(cval) # get suit index 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 fnd[si] = cval
if(from < 9) { # moving from a tableau if(from < 9) { # moving from a tableau
delete table[rfrom,tablens[rfrom]-1] # delete the card delete table[rfrom,tablens[rfrom]-1] # delete the card
@@ -226,20 +237,20 @@ function autohome(chres, lres, i) {
do { do {
chres = 0 chres = 0
for(i=1;i<9;i++) { for(i=1;i<9;i++) {
lres = domove(i "0") lres = domove(i "0", 1)
if(lres == 2) return 2 if(lres == 2) return 2
else chres += lres else chres += lres
} }
lres = domove("a0") lres = domove("a0", 1)
if(lres == 2) return 2 if(lres == 2) return 2
else chres += lres else chres += lres
lres = domove("b0") lres = domove("b0", 1)
if(lres == 2) return 2 if(lres == 2) return 2
else chres += lres else chres += lres
lres = domove("c0") lres = domove("c0", 1)
if(lres == 2) return 2 if(lres == 2) return 2
else chres += lres else chres += lres
lres = domove("d0") lres = domove("d0", 1)
if(lres == 2) return 2 if(lres == 2) return 2
else chres += lres else chres += lres
} while(chres > 0) } while(chres > 0)
@@ -253,7 +264,6 @@ function help() {
print "-v ASCII=1\tuse ASCII instead of Unicode suits" print "-v ASCII=1\tuse ASCII instead of Unicode suits"
print "-v MONOCHROME=1\tdon't use colors" print "-v MONOCHROME=1\tdon't use colors"
print "-v SEED=xxxxx\tset game seed number (M$-compatible)" 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 "Commands: q - quit, u - undo, r - restart, h - this help"
print "Moves: [digit][digit], where \"digits\" are:" print "Moves: [digit][digit], where \"digits\" are:"
print "- 0 is foundation (can be omitted)" print "- 0 is foundation (can be omitted)"
@@ -327,17 +337,15 @@ BEGIN { # main code part
cmd = tolower(cmd) # accept both uppercase and lowercase cmd = tolower(cmd) # accept both uppercase and lowercase
if(cmd == "q") {print "Quitting..."; break} if(cmd == "q") {print "Quitting..."; break}
else if(cmd == "h") res = help() else if(cmd == "h") res = help()
else if(cmd == "ah") res = autohome()
else if(cmd == "u") undo() else if(cmd == "u") undo()
else if(cmd == "r") { # restart logic else if(cmd == "r") { # restart logic
while(undoidx > 0) undo() while(undoidx > 0) undo()
undomoves = 0 undomoves = 0
} else { # pass the command to the logic function } else { # pass the command to the logic function
undomoves = 0 # set global undo move counter undomoves = 0 # set global undo move counter
res = domove(cmd) res = domove(cmd, 0) # manual move
if(res == 0) print "Invalid move!" if(res == 0) print "Invalid move!"
else if(NAH != 1 && res < 2) res = autohome() else if(res < 2) res = autohome()
print "Undomoves:", undomoves
} }
render() render()
if(res == 2) {print "Victory!"; break} if(res == 2) {print "Victory!"; break}