Virtual Slide Puzzle

by ezman in Circuits > Microsoft

1046 Views, 2 Favorites, 0 Comments

Virtual Slide Puzzle

Cover_1.jpg

Introduction
A few years ago, I saw this simple batch game slide puzzle [ 'Original instructable https://www.instructables.com/id/Batch-Game-Slide... ], it was cool. I made it, played it a few times but I lost interest because of its simplicity. With that in mind I thought I could modify it and add some additional features. Here is the result of that thought.

This game is written in Windows batch scripting language, it has very basic commands and operations, but is super simple and quite effective in application. Batch files are used frequently to automate software installation and updates, copy and execute, directory search and find, etc. With any coding language, game creation is used to help teach and reinforce learning. Even though it is a basic language there is room for your modifications and for your entertainment enjoyment.

Modifications:
• User instructions.
• Screen size and color.
• Close window command.
• Random game library.
• Move counter.

Build Skill Level:
Easy

Time to Build:
5 minutes

Game Skill Level:
Easy to Difficult

Time to Play:
3 – 8 minutes

Let’s go make a Virtual Slide Puzzle.

Next Screen Size and Color

Screen Size and Color

Color_Width.jpg

The 1st Modification, this part of the code was added just for looks, without setting the Width and Height, you have a large area of nothing, using unnecessary real estate. You do not have to set it but it adds to the overall presentation. Furthermore, if you are taking a break at work a small game board is less noticeable. It is set at the beginning of the code.

' set the game screen size [Width,Height]
Mode 58,28

As for Color you have different choices. Color attributes are specified by 2 hex digits — the 1st is the Background; the 2nd is the Text. You do not need to set the background and text but color is nice. You could call it anytime and change text color to call attention to something important. You will have to read up on how to do that. In this case it is simply called by adding the below line.

' Color Blue Background with Yellow Text
color 1e

Background
0= Black
1= Navy
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Blue

Text
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White

Next User Instructions

User Instructions

1st.jpg

The 2nd Modification, this part of the code was added so I could remember what "ASDW" meant. Furthermore, user instructions adds to the overall presentation.

"Echo" is used to display exactly what follows.

Note: There are spaces or tabs after echo to align the gameboard away from the left side of the border. If those spaces or tabs do not copy over correctly just add spaces or tabs to align the gameboard where you want it to be. There is no centering option within Windows batch scripting. The easiest way to align the gameboard and text is to use spaces and tabs.

echo Using the keyboard keys "ASDW" to move
echo a number to the adjacent empty square.

echo.
echo A = Slide Left
echo S = Slide Down R = Reset Gameboard
echo D = Slide Right C = Close Game
echo W = Slide Up

echo Type the direction you want to slide the number.
echo ( "A"- Left "S"- Down "D"- Right "W"- Up )

Next Close Window Command

Close Window Command

3.jpg

The 3rd Modification, this part of the code was added for the convenience of closing the game without using the mouse or shortcut keys. It is a nice feature. It can be called different ways but the basic code is as follows.

'Close Command
exit /B

In this game I added the choice to the existing options. Within the choice options I selected "C" for the obvious reason then assigned what to do if "C" was selected.

choice /c WASDRC /n
if %errorlevel% == 1 goto moveW
if %errorlevel% == 2 goto moveA
if %errorlevel% == 3 goto moveS
if %errorlevel% == 4 goto moveD
if %errorlevel% == 5 goto reset
if %errorlevel% == 6 exit /B

Next Random Game Library

Random Game Library

Cover.jpg
R1.jpg

The 4th Modification, this part of the code was added to give the gamer other games boards. Random in Windows batch scripting isn't as random as its name leads on to be nor is it as coding friendly as other languages. With that in mind, I ended up creating a library of 13 boards. To select a random game you select "R" and a number between 0 and 12 is created and the code goes to that library option and the game is displayed.

:reset
' Randomizer
set /a rand=%random% %%12
goto %rand%

If the Random number generator select "0" the code will goto :0

Library
: 0
set slide1=1
set slide2=7

etc.

Next Move Counter

Move Counter

MoveC.jpg

The 5th Modification, this part of the code was added to give the gamer an added challenge. I like to be challenged and so a "Move Counter" gave me a way to track the number of moves I make.

' Counter
set count=0

Then add the counter code so 1 will be added to the total after the move has been made.

set /a count=%count%+1

Code

code.jpg

Here is the whole code. Follow the directions on how to create the file.

Note: there is spaces or tabs after echo to align the gameboard away from the left side of the border. If those spaces or tabs do not copy over correctly just add spaces or tabs to align the gameboard where you want it to be. There is no centering option within Windows batch scripting. The easiest way to align the gameboard and text is to use spaces and tabs.

Because of the above html tab and space issue in coping I have attached the file. You can still copy the below code but you will have to adjust the spacing.

1) Open the Notepad application:
Click The Window key => type Notepad then select Notepad App from the list of Best match
Or
Click The Window Icon => type Notepad then select Notepad App from the list of Best match
Or
Click The Window key + R => Click Run => Type Notepad in the Run input box then Click OK.

2) Copy the code below the Apostrophe and Asterisks line then Paste it into Notepad.

' *********************************************

@echo off
title Slide Puzzle
setlocal enabledelayedexpansion

' set the game screen size [Width,Height]
Mode 58,28

set default= %
set pos=9
set loop=1

' Counter
set count=0

' Color [Field Text]
color 1e

:reset
' Randomizer
set /a rand=%random% %%12
goto %rand%

:display
cls
echo.
echo Using the keyboard keys "ASDW" to move
echo a number to the adjacent empty square.

echo.
echo A = Slide Left
echo S = Slide Down R = Reset Gameboard
echo D = Slide Right C = Close Game
echo W = Slide Up
echo.

echo ____ ____ ____
echo ^| ^| ^| ^|
echo ^| %slide1% ^| %slide2% ^| %slide3% ^|
echo ^|____^|____^|____^|
echo ^| ^| ^| ^|
echo ^| %slide4% ^| %slide5% ^| %slide6% ^|
echo ^|____^|____^|____^|
echo ^| ^| ^| ^|
echo ^| %slide7% ^| %slide8% ^| %slide9% ^|
echo ^|____^|____^|____^|
echo.
echo Type the direction you want to slide the number.
echo ( "A"- Left "S"- Down "D"- Right "W"- Up )
echo.
echo Number of moves you've made %count%

choice /c wasdrc /n
if %errorlevel% == 1 goto movew
if %errorlevel% == 2 goto movea
if %errorlevel% == 3 goto moves
if %errorlevel% == 4 goto moved
if %errorlevel% == 5 goto reset
if %errorlevel% == 6 exit /B

:movew
if %pos% GEQ 7 goto display
set /a helper=%pos% + 3
set /a slide%pos%=!slide%helper%!
set slide%helper%=%default%
set /a pos=%pos% + 3

set /a count=%count%+1
goto display

:movea
if %pos% == 3 goto display
if %pos% == 6 goto display
if %pos% == 9 goto display
set /a helper=%pos% + 1
set /a slide%pos%=!slide%helper%!
set slide%helper%=%default%
set /a pos=%pos% + 1

set /a count=%count%+1
goto display

:moves
if %pos% LEQ 3 goto display
set /a helper=%pos% - 3
set /a slide%pos%=!slide%helper%!
set slide%helper%=%default%
set /a pos=%pos% - 3

set /a count=%count%+1
goto display

:moved
if %pos% == 1 goto display
if %pos% == 4 goto display
if %pos% == 7 goto display
set /a helper=%pos% - 1
set /a slide%pos%=!slide%helper%!
set slide%helper%=%default%
set /a pos=%pos% - 1

set /a count=%count%+1
goto display

Library
: 0
set slide1=1
set slide2=7
set slide3=3
set slide4=5
set slide5=8
set slide6=4
set slide7=2
set slide8=6
set slide9=%default%
set pos=9
set count=0
goto display

: 1
set slide1=7
set slide2=1
set slide3=6
set slide4=2
set slide5=5
set slide6=4
set slide7=3
set slide8=%default%
set slide9=8
set pos=8
set count=0
goto display

:2
set slide1=8
set slide2=%default%
set slide3=2
set slide4=5
set slide5=7
set slide6=3
set slide7=6
set slide8=4
set slide9=1
set pos=2
set count=0
goto display

:3
set slide1=2
set slide2=8
set slide3=%default%
set slide4=5
set slide5=6
set slide6=1
set slide7=4
set slide8=7
set slide9=3
set pos=3
set count=0
goto display

:4
set slide1=4
set slide2=8
set slide3=2
set slide4=%default%
set slide5=5
set slide6=1
set slide7=7
set slide8=3
set slide9=6
set pos=4
set count=0
goto display

:5
set slide1=6
set slide2=8
set slide3=5
set slide4=3
set slide5=%default%
set slide6=1
set slide7=7
set slide8=2
set slide9=4
set pos=5
set count=0
goto display

:6
set slide1=3
set slide2=8
set slide3=5
set slide4=7
set slide5=1
set slide6=%default%
set slide7=2
set slide8=6
set slide9=4
set pos=5
set count=0
goto display

:7
set slide1=1
set slide2=8
set slide3=3
set slide4=7
set slide5=5
set slide6=%default%
set slide7=6
set slide8=2
set slide9=4
set pos=6
set count=0
goto display

:8
set slide1=8
set slide2=%default%
set slide3=6
set slide4=5
set slide5=4
set slide6=7
set slide7=3
set slide8=2
set slide9=1
set pos=2
set count=0
goto display

:9
set slide1=1
set slide2=8
set slide3=%default%
set slide4=4
set slide5=3
set slide6=2
set slide7=5
set slide8=7
set slide9=6
set pos=3
set count=0
goto display

:10
set slide1=3
set slide2=6
set slide3=8
set slide4=5
set slide5=1
set slide6=7
set slide7=2
set slide8=4
set slide9=%default%
set pos=9
set count=0
goto display

:11
set slide1=2
set slide2=7
set slide3=%default%
set slide4=5
set slide5=1
set slide6=4
set slide7=3
set slide8=8
set slide9=6
set pos=3
set count=0
goto display

: 12
set slide1=1
set slide2=8
set slide3=2
set slide4=%default%
set slide5=4
set slide6=3
set slide7=7
set slide8=6
set slide9=5
set pos=4
set count=0
goto display

Downloads

Save the File

Save.jpg

1) Click File,
2) Click Save,
3) Choose the location where to Save this file,
4) Change Save as type: from Text Documents (*.txt) to "All Files",
5) Give the file a name i.e. Slide Puzzle.bat,
6) Click Save.

Congratulations you are done!

To play go to the folder where you placed the file and open or double click the file. Then play.

Observations & Summary

O.jpg
O1.jpg

Warning: If you make the wrong change to the aforementioned code. You could, at worst, ruin your day. It is no fun chasing errors. What I have learned is that it is usually a simple mistake. Sometimes copying code from html may add unusual characters or spaces, which could interfere with the operation.
Disclaimer: Modify at your own risk.

Observations
1) Fun and challenging,
2) Easy to make and modify.
3) If it does not work. Then recopy the aforementioned code and paste it into the Notepad application then save the file with the extension .bat.

Summary
This is a fun little app. I am satisfied with the results of this Slide Puzzle.bat

Hear advice, and receive instruction, so that you may be wise in your latter end.