 


Task

Implement the Fifteen Puzzle Game.



The   15-puzzle   is also known as:

   Fifteen Puzzle
   Gem Puzzle
   Boss Puzzle
   Game of Fifteen
   Mystic Square
   14-15 Puzzle
   and some others.


Related Tasks

   15 Puzzle Solver
   16 Puzzle Game


        ; Puzzle15 by grosged (march 2019)
        ; How to play ?.. Just press one of the arrow keys then [enter] to valid
        ; ( press [Ctrl+C] to escape )
 
        segment	.data
check:	db	"1   2   3   4",10,"  5   6   7   8",10,"  9  10  11  12",10," 13  14  1"
puzzle:	db	27,"c",10,"  1   2   3   4",10,"  5   6   7   8",10,"  9  10  11  12",10," 13  14  15    ",10,10
	db	" Direction ?",13             
	db	" Well done !  ",10,10
inKey:	dw	0,0,0,0
 
	segment	.text
	global	_start
 
_start:	mov	rax,100
	syscall
	mov	rcx,rax
	shr	rcx,3
	and	rcx,255
	and	rax,31
	lea	rsi,[_start+rax]
	mov	rbx,15
 
Mixing:	push	rcx
	mov	di,word[rsi+rcx*2]
	mov	rcx,8
quatre:	mov	ax,di
	shr	di,2
	and	ax,3
	add	ax,65
	call	SWAPPIN
	loop	quatre
	pop	rcx
	loop	Mixing
	cmp	cx,ax
 
MainLp:	mov	rdx,80	
Succes:	lea	rsi,[puzzle]
	mov	rdi,1
	mov	rax,1
End?:	syscall
	mov	rax,60
	je	End?
	mov	rdx,8
	lea	rsi,[inKey]
	mov	rdi,0
	mov	rax,0
	syscall
	mov	al,byte [rsi+rax-2]
	call	SWAPPIN
	lea 	rsi,[check]
	lea	rdi,[puzzle+5]
	mov	rcx,7
	repe	cmpsq
	jne	MainLp
	mov	rdx,95
	jmp	Succes
 
SWAPPIN:mov	rdx,rbx
	cmp	al,"A"
	jne	NotUp
	add	rbx,4
	cmp	rbx,16
	cmovae	rbx,rdx
NotUp:	cmp	al,"B"
	jne	NotDwn	
	sub	rbx,4
	cmovc	rbx,rdx	
NotDwn:	cmp	al,"C"
	jne	NotLft
	test	rbx,3
	jz	Endcll
	dec	rbx
NotLft:	cmp	al,"D"
	jne	Endcll
	inc	rbx
	test	rbx,3
	cmovz	rbx,rdx
Endcll:	mov	ax,"  "
	xchg	ax,word[puzzle+4+rbx*4]
	mov	word[puzzle+4+rdx*4],ax
	ret