2048: Difference between revisions

43 bytes added ,  10 years ago
bugfix: moving just a 2 onto a 2* not a valid move
(moved comment to avoid potential confusion)
(bugfix: moving just a 2 onto a 2* not a valid move)
Line 117:
}
 
# Put a "2*" into an empty cell on the board. The star is to indicate it's new
# for the player's convenience.
proc spawn-new {} {
set emptyCell [pick [empty [cell-indexes]]]
if {[llength $emptyCell] > 0} {
forcells [list $emptyCell] i j cell {
set cell 2*
}
}
return $emptyCell
}
 
Line 145:
set newIndex [vector-add "$i $j" $directionVect]
set removedStar 0
 
if {$cell eq {2*}} {
set cell 2
set removedStar 1
}
 
# For every nonempty source cell and valid destination cell...
if {$cell != 0 && [valid-cell? $newIndex]} {
if {[cell-get $newIndex] == 0} {
# Destination is empty.
if {$checkOnly} {
# -level 2 is to return from both forcells and move-all.
Line 166 ⟶ 161:
} elseif {([cell-get $newIndex] eq $cell) &&
[string first + $cell] == -1} {
# Destination is the same number as source.
if {$checkOnly} {
return -level 2 true
Line 178 ⟶ 173:
}
}
}
 
if {$checkOnly && $removedStar} {
set cell {2*}
}
}
Line 223 ⟶ 214:
}
 
# Pretty-print the board. Specify an index in highlight to highlight a cell.
proc print-board {{highlight {-1 -1}}} {
forcells [cell-indexes] i j cell {
if {$j == 0} {
Line 231 ⟶ 222:
puts -nonewline [
if {$cell != 0} {
formatif "\{[%4s\]::struct::list equal "$i $cellj" $highlight]} {
format "\[%4s\]" $cell*
if {$cell eq {2*} } else {
format "\[%4s\]" $cell
set cell 2 }
 
} else {
lindex "......"
Line 264 ⟶ 260:
set possibleMoves {}
 
# Add new tile to the board and print the board highlighting this tile.
spawn-new
print-board [spawn-new]
 
check-win
 
Anonymous user