Introduced autohome functionality
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# 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]
|
# 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:
|
# [number][number] - move from column/freecell to column/freecell/foundation:
|
||||||
# - 0 is foundation (can be omitted)
|
# - 0 is foundation (can be omitted)
|
||||||
# - 1 to 8 are column numbers
|
# - 1 to 8 are column numbers
|
||||||
@@ -84,7 +84,7 @@ function render(i, j, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# main logic/move function (also handles the undo)
|
# 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) {
|
function domove(cmd, valid, nums, from, to, si) {
|
||||||
if(cmd == "u") { # undo logic
|
if(cmd == "u") { # undo logic
|
||||||
if(undoidx > 0) {
|
if(undoidx > 0) {
|
||||||
@@ -97,7 +97,7 @@ function domove(cmd, valid, nums, from, to, si) {
|
|||||||
return 1 # continue
|
return 1 # continue
|
||||||
}
|
}
|
||||||
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 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
|
split(cmd, nums, "") # numbers must be space-separated
|
||||||
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
|
||||||
@@ -161,10 +161,38 @@ function domove(cmd, valid, nums, from, to, si) {
|
|||||||
undos["fcell",undoidx] = assocser(fcell, ",")
|
undos["fcell",undoidx] = assocser(fcell, ",")
|
||||||
undos["tablens",undoidx] = assocser(tablens, ",")
|
undos["tablens",undoidx] = assocser(tablens, ",")
|
||||||
}
|
}
|
||||||
else print "Invalid move!"
|
|
||||||
# now, check for the win condition: all kings in fnd
|
# 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
|
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
|
BEGIN { # main code part
|
||||||
@@ -219,13 +247,18 @@ BEGIN { # main code part
|
|||||||
undos["tablens",0] = assocser(tablens, ",")
|
undos["tablens",0] = assocser(tablens, ",")
|
||||||
undoidx = 0 # the current undo index is 0
|
undoidx = 0 # the current undo index is 0
|
||||||
# first-time display
|
# first-time display
|
||||||
print "nnfc by Luxferre"
|
print "nnfc by Luxferre\nenter h for command help"
|
||||||
render()
|
render()
|
||||||
printf("\n> ")
|
printf("\n> ")
|
||||||
while((getline cmd) > 0) { # main interactive loop
|
while((getline cmd) > 0) { # main interactive loop
|
||||||
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}
|
||||||
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()
|
render()
|
||||||
if(res == 2) {print "Victory!"; break}
|
if(res == 2) {print "Victory!"; break}
|
||||||
printf("\n> ")
|
printf("\n> ")
|
||||||
|
|||||||
Reference in New Issue
Block a user