shuffle pieces and check for win
This commit is contained in:
parent
076969704d
commit
1032891929
|
@ -11,23 +11,28 @@ export var height: int = 512
|
||||||
|
|
||||||
var pieces: Array = []
|
var pieces: Array = []
|
||||||
var missing_piece: Vector2
|
var missing_piece: Vector2
|
||||||
|
var rng = RandomNumberGenerator.new()
|
||||||
|
|
||||||
func _draw() -> void:
|
func _draw() -> void:
|
||||||
draw_rect(Rect2(0, 0, width, height), Color.blue)
|
draw_rect(Rect2(0, 0, width, height), Color.blue)
|
||||||
|
|
||||||
|
func position_for_index(index: Vector2, size: int) -> Vector2:
|
||||||
|
var padding_w = (width - (size * columns)) / (columns + 1)
|
||||||
|
var padding_h = (height - (size * rows)) / (rows + 1)
|
||||||
|
return Vector2(padding_w + index.x * (size + padding_w), padding_h + index.y * (size + padding_h))
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
|
rng.randomize()
|
||||||
|
|
||||||
for c in range(columns):
|
for c in range(columns):
|
||||||
var pieces_row: Array = []
|
var pieces_row: Array = []
|
||||||
for r in range(rows):
|
for r in range(rows):
|
||||||
|
|
||||||
var piece = Piece.instance()
|
var piece = Piece.instance()
|
||||||
|
|
||||||
var padding_w = (width - (piece.size * columns)) / (columns + 1)
|
piece.position = position_for_index(Vector2(c, r), piece.size)
|
||||||
var padding_h = (height - (piece.size * rows)) / (rows + 1)
|
|
||||||
var initial_position = Vector2(padding_w + c * (piece.size + padding_w), padding_h + r * (piece.size + padding_h))
|
|
||||||
piece.set_number(1 + c + r * columns)
|
piece.set_number(1 + c + r * columns)
|
||||||
piece.translate(initial_position)
|
|
||||||
|
|
||||||
if r == rows - 1 && c == columns - 1:
|
if r == rows - 1 && c == columns - 1:
|
||||||
piece.visible = false
|
piece.visible = false
|
||||||
missing_piece.x = c
|
missing_piece.x = c
|
||||||
|
@ -38,15 +43,17 @@ func _ready() -> void:
|
||||||
|
|
||||||
pieces.append(pieces_row)
|
pieces.append(pieces_row)
|
||||||
|
|
||||||
|
shuffle(10)
|
||||||
|
|
||||||
func _input(event):
|
func _input(event):
|
||||||
if event.is_action_pressed("ui_up"):
|
if event.is_action_pressed("ui_up"):
|
||||||
move_piece(Direction.UP)
|
|
||||||
if event.is_action_pressed("ui_down"):
|
|
||||||
move_piece(Direction.DOWN)
|
move_piece(Direction.DOWN)
|
||||||
|
if event.is_action_pressed("ui_down"):
|
||||||
|
move_piece(Direction.UP)
|
||||||
if event.is_action_pressed("ui_left"):
|
if event.is_action_pressed("ui_left"):
|
||||||
move_piece(Direction.LEFT)
|
|
||||||
if event.is_action_pressed("ui_right"):
|
|
||||||
move_piece(Direction.RIGHT)
|
move_piece(Direction.RIGHT)
|
||||||
|
if event.is_action_pressed("ui_right"):
|
||||||
|
move_piece(Direction.LEFT)
|
||||||
|
|
||||||
enum Direction { UP, DOWN, LEFT, RIGHT }
|
enum Direction { UP, DOWN, LEFT, RIGHT }
|
||||||
|
|
||||||
|
@ -74,7 +81,11 @@ func move_piece(direction) -> bool:
|
||||||
|
|
||||||
swap(missing_piece, destination)
|
swap(missing_piece, destination)
|
||||||
missing_piece = destination
|
missing_piece = destination
|
||||||
|
|
||||||
update()
|
update()
|
||||||
|
if check_solved():
|
||||||
|
print("solved")
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
||||||
func swap(src: Vector2, dst: Vector2) -> void:
|
func swap(src: Vector2, dst: Vector2) -> void:
|
||||||
|
@ -82,4 +93,17 @@ func swap(src: Vector2, dst: Vector2) -> void:
|
||||||
pieces[src.x][src.y] = pieces[dst.x][dst.y]
|
pieces[src.x][src.y] = pieces[dst.x][dst.y]
|
||||||
pieces[dst.x][dst.y] = tmp
|
pieces[dst.x][dst.y] = tmp
|
||||||
pieces[src.x][src.y].position = position_for_index(src, tmp.size)
|
pieces[src.x][src.y].position = position_for_index(src, tmp.size)
|
||||||
pieces[dst.x][dst.y].position = position_for_index(dst, tmp.size)
|
pieces[dst.x][dst.y].position = position_for_index(dst, tmp.size)
|
||||||
|
|
||||||
|
func shuffle(count: int) -> void:
|
||||||
|
while count > 0:
|
||||||
|
count -= 1
|
||||||
|
var direction = rng.randi_range(Direction.UP, Direction.RIGHT)
|
||||||
|
move_piece(direction)
|
||||||
|
|
||||||
|
func check_solved() -> bool:
|
||||||
|
for c in range(columns):
|
||||||
|
for r in range(rows):
|
||||||
|
if pieces[c][r].number != 1 + c + r * columns:
|
||||||
|
return false
|
||||||
|
return true
|
Loading…
Reference in a new issue