Slot Puzzle Game
[ close window ]

<script language="JavaScript1.1">
<!---- Hide script from old browsers
//	Slot Puzzle Game
//	written by Stephen Battey - August 1997
//	http://www.oocities.org/Heartland/Plains/5287





//------------------------------------------------
//		Global Game Variables
//------------------------------------------------


PlayMode=0

HoleCol=1
HoleRow=1
InPosition=0

Moves=0
Seconds=0
Minutes=0

ShufflesLeft=0
ShuffleDelay=0
NoNextMov=0


ImageTags = new Array()
for (i=1; i<6; i++) {
	ImageTags[i] = new Array()
}




//------------------------------------------------
//		Low level functions
//------------------------------------------------


function Round(number) {
//			function returns the extracted integer part
//			of a decimal number - Math.round is not used
//			since it is only availiable on a Unix platform

	v=number.toString()
	dp=v.indexOf(".")
	if ( dp==-1 )
		return number
	if ( ( dp==0 ) | ( v.substring(0,dp)=="-" ) )
		return 0 
	return eval(v.substring(0,dp))
}



function NumText(v,width,use) {
//			returns the number 'v' in a text string of minimum
//			length 'width', preceeding spaces being filled with
//			the character specified in 'use'

	post=v.toString()
	pre=""
	uselen=width-post.length
	for (i=1; i<=uselen; i++) {
		pre+=use
	}
	return pre+post
}



function mMODn(m,n) {
//			Function returns the least
//			residue of :   m (mod n)

	while ( m>=n )
		m=m-n
	return m
}



now4Random = new Date()
mRandom=1024
cRandom=55
aRandom=13
rRandom=((now4Random.getSeconds()-1)/60)*mRandom

function Random(minv,maxv) {
//			Function returns a random number in the range
//			minv - maxv

	rRandom=mMODn(((aRandom*rRandom)+cRandom),mRandom)
	return ( ((rRandom/1024)*(maxv-minv))+minv )
}






//------------------------------------------------
//		Sub-Level Functions
//------------------------------------------------





function RefreshSlot(r,c) {
//			Refresh row 'r' col 'c' slot in puzzle 

	ImNo=(r*5)+c+20
	document.images[ImNo].src=ImageTags[r][c]
}



function ResetSlate() {
//			set up a completed puzzle

	for (row=1; row<6; row++) {
		for (col=1; col<6; col++) {
			PicName="ChurTile"+row.toString()+col.toString()+".jpg"
			ImageTags[row][col]=PicName
			RefreshSlot(row,col)
		}
	}

	HoleRow=1
	HoleCol=1
	InPosition=25

}



function ShowTime() {
//			shows the time on the screen

	ToShow=NumText(Minutes,2," ")+":"+NumText(Seconds,2,"0")
	document.Disp.time.value=ToShow
}



function ShowMoves() {
//			show the number of moves taken on the screen

	document.Disp.moves.value=NumText(Moves,5," ")
}



function MovePiece(d) {
//			move the piece in the direction denoted by 'd'
//			into the current empty slot
//			d :  1-up , 2-left , 3-right , 4-down

	if ( ( d==1 ) & ( HoleRow==1 ) )
		return -1
	if ( ( d==4 ) & ( HoleRow==5 ) )
		return -1
	if ( ( d==2 ) & ( HoleCol==1 ) )
		return -1
	if ( ( d==3 ) & ( HoleCol==5 ) )
		return -1

	if ( ( d==1 ) | ( d==4 ) ) {
		FromCol=HoleCol
		if ( d<2 )
			FromRow=HoleRow-1
		else
			FromRow=HoleRow+1
	} else {
		FromRow=HoleRow
		if ( d>2 )
			FromCol=HoleCol+1
		else
			FromCol=HoleCol-1
	}

	InPic=ImageTags[FromRow][FromCol]
	IntoRow=eval(InPic.substring(8,9))
	IntoCol=eval(InPic.substring(9,10))

	if ( ( IntoRow==FromRow ) & ( IntoCol==FromCol ) )
		InPosition--

	if ( ( HoleRow==1 ) & ( HoleCol==1 ) )
		InPosition--

	ImageTags[FromRow][FromCol]="ChurTile11.jpg"
	ImageTags[HoleRow][HoleCol]=InPic
	RefreshSlot(HoleRow,HoleCol)
	RefreshSlot(FromRow,FromCol)

	if ( ( IntoRow==HoleRow ) & ( IntoCol==HoleCol ) )
		InPosition++

	if ( ( FromRow==1 ) & ( FromCol==1 ) )
		InPosition++

	HoleRow=FromRow
	HoleCol=FromCol

	return 0
}



function EndShuffle() {
//			end shuffling procedure

	PlayMode=2
	Moves=0
	ShowMoves()
	Minutes=0
	Seconds=0
	ShowTime()

	window.status="GO ! ! !"

	TimeTimer=window.setTimeout("UpdateTime()",1000)
}



function Shuffle() {
//			shuffle a piece in the puzzle

	donemove=false

	while ( !donemove ) {
		movedir=Round(Random(1,4.99))
		if ( movedir!=NoNextMov ) {
			if ( MovePiece(movedir)==0 )
				donemove=true
		}
	}

	NoNextMov=5-movedir

	ShufflesLeft--
	if ( ShufflesLeft==0 )
		ShuffleTimer=window.setTimeout("EndShuffle()",ShuffleDelay)
	else
		ShuffleTimer=window.setTimeout("Shuffle()",ShuffleDelay)

	if ( ShuffleDelay>10 ) {
		UnitStr="0123456789"
		ShufT=Round(ShufflesLeft/10)
		ShufU=ShufflesLeft-(10*ShufT)
		ToShow = "Shuffling : " + NumText(ShufT,2," ")
                                        + " - " + UnitStr.substring(0,ShufU)
		window.status=ToShow
	} else {
		if ( ShufflesLeft==40 )
			window.status="READY..."
		if ( ShufflesLeft<20 )
			window.status="STEADY.."
	}
}



function UpdateTime() {
//			move the clock counter on one second

	Seconds++
	if ( Seconds==60 ) {
		Seconds=0
		Minutes++
	}
	ShowTime()

	if ( ( Minutes==0 ) & ( Seconds==5 ) )
		window.status=""

	TimeTimer=window.setTimeout("UpdateTime()",1000)
}





//------------------------------------------------
//		Top-Level Functions
//------------------------------------------------




function StartShuffle() {
//			begin the shuffling of the puzzle

	if ( PlayMode==2 )
		clearTimeout(TimeTimer)

	window.scroll(0,300)
	ResetSlate()
	PlayMode=1

	menu=document.GameType.deepthval
	ShufflesLeft=eval(menu.options[menu.selectedIndex].value)
	ShuffleDelay=10
	if ( document.GameType.slowshuff.checked )
		ShuffleDelay=350

	NoNextMov=0

	ShuffleTimer=window.setTimeout("Shuffle()",ShuffleDelay)

}



function Move(ClickRow,ClickCol) {
//			handles move requests from the user

	if ( PlayMode<2 )
		return
	if ( ( ClickRow==HoleRow ) & ( ClickCol==HoleCol ) )
		return
	if ( ( ClickRow!=HoleRow ) & ( ClickCol!=HoleCol ) )
		return


	if ( ClickRow==HoleRow ) {
		if ( ClickCol<HoleCol ) {
			dir=2
			ToMove=HoleCol-ClickCol
		} else {
			dir=3
			ToMove=ClickCol-HoleCol
		}
	} else {
		if ( ClickRow<HoleRow ) {
			dir=1
			ToMove=HoleRow-ClickRow
		} else {
			dir=4
			ToMove=ClickRow-HoleRow
		}
	}

	for (m=1; m<=ToMove; m++) {
		MovePiece(dir)
	}


	Moves++
	ShowMoves()

	if ( InPosition==25) {
		clearTimeout(TimeTimer)
		PlayMode=0
		alert("\n**************************\n\n      !!   COMPLETE   !!  \n\n**************************\n"  )
	}
}





// end hiding -->
</script>




©1997 Stephen Battey
This page hosted by Get your own free home page