Как стать автором
Обновить

Игра 2048 батником на Windows

Время на прочтение 10 мин
Количество просмотров 26K


Слово muffe — как оказалось с немецкого это муфта. Но придумывалось как исковерканное слово move и coffee.

В игре присутствуют отличия от оригинальной игры 2048.
Так как алгоритм разрабатывал вслепую. Тыкал и смотрел что выходит. Пытался повторить это в командной строке cmd batch-файлом.

1. не меняется цвет ячеек. Понял как это сделать. Но еще не реализовал эту возможность.
Можно с помощью FINDSTR или сторонней DOS утилиты.
2. отсутствует анимация.

Поправил графическое отображение. Оказывается в FAR можно добавлять символы псевдографики. А то я до этого набирал текст, в Блокноте

Поправил синтаксис. Использовал кое, что из батника созданного AAbrosov
Теперь управляющие батники стали в 4 раза короче.

Перемещение и сложение выделил в отдельные файлы, которые вызываю при помощи CALL.
На каждое направление свой отдельный файл.
WASD — выбираем направление, стрелочки так и не получилось заставить.
X — выход из игры в любой момент, если только не зависла. Тогда можно просто закрыть окно cmd.
R — начать заново, в любой момент.

Сам не программист, и наверно допустил много глупостей. Но главное что работает. На все это потратил уже 4 недели. Вдумчиво рассматривал исходный код странички coffee2048.com, мало чего понял. За то упорно изучал командную строку и создание батников. Мне еще на первом компе 386 нравилось писать autoexec.bat и config.sys для запуска игр. Иногда этот процесс был интересней самой игры. Еще вдохновился шуткой от Microsoft на 1 апреля. MS-DOS для Windows Phone там понравилась камера и танцы с бубном, что бы запустить игру «камень-ножницы-бумага». Cortana прикольно помогает.

Были проблемы с dynamic variable. сначала нужно было понять, что это такое и с чем это едят, а потом найти как это можно использовать в cmd. Еще были проблемы с IF. Как оказалось туда можно вставлять только дно действие. Пришлось использовать GOTO. Не получилось использовать цикл FOR. Вышел из ситуации с помощью IF и GOTO.

Для управление в игре использовал команду CHOICE. Пока искал как отдавать команды игре, наткнулся на игру змейку тоже в батнике. Там управление передавалось через SET /P но я так и не понял как вводить буквы и не нажимать после этого ВВОД. Остановился на CHOICE. Хотя мне не нравится то что эта команда пищит, иногда очень громко, если нажата клавиша которая не обозначена в «choice /C wasdxr /N >nul».

содержимое батника muffe9.bat
@echo off
title muffe (Game 2048 for Command prompt)
if not exist muffe_BestSc.txt echo 0>muffe_BestSc.txt
(set /p BestSc=)<muffe_BestSc.txt
set BestSc=%BestSc: =%
:Begin1
cls

SETLOCAL ENABLEDELAYEDEXPANSION

set "echo2=echo ЪДДДДїЪДДДДїЪДДДДїЪДДДДї"
set "echo3=echo АДДДДЩАДДДДЩАДДДДЩАДДДДЩ"
set contor=0


:::::::::::::::::::::::::::::::::::::::::::::::::

set contentR11=0
set contentR12=0
set contentR13=0
set contentR14=0

set contentR21=0
set contentR22=0
set contentR23=0
set contentR24=0

set contentR31=0
set contentR32=0
set contentR33=0
set contentR34=0

set contentR41=0
set contentR42=0
set contentR43=0
set contentR44=0

:::::::::::::::::::::::::::::::::::::::::::::::::

set /a "X=%RANDOM% * 100 / 32768 + 1"
if [%X%] LEQ [90] set cell=2
if [%X%] GEQ [91] set cell=4

:::::::::::::::::::::::::::::::::::::::::::::::::

set /a "X=%RANDOM% * 4 / 32768 + 1"
set /a "Y=%RANDOM% * 4 / 32768 + 1"
set contentR%Y%%X%=%cell%
set "content%Y%%X%=   %cell%"

set exitC=0

:::::::::::::::::::::::::::::::::::::::::::::::::

if [%contentR11%] == [0] (set "content11=    ")
if [%contentR12%] == [0] (set "content12=    ")
if [%contentR13%] == [0] (set "content13=    ")
if [%contentR14%] == [0] (set "content14=    ")

if [%contentR21%] == [0] (set "content21=    ")
if [%contentR22%] == [0] (set "content22=    ")
if [%contentR23%] == [0] (set "content23=    ")
if [%contentR24%] == [0] (set "content24=    ")

if [%contentR31%] == [0] (set "content31=    ")
if [%contentR32%] == [0] (set "content32=    ")
if [%contentR33%] == [0] (set "content33=    ")
if [%contentR34%] == [0] (set "content34=    ")

if [%contentR41%] == [0] (set "content41=    ")
if [%contentR42%] == [0] (set "content42=    ")
if [%contentR43%] == [0] (set "content43=    ")
if [%contentR44%] == [0] (set "content44=    ")

set version=1

:Direction


::::::::::::::::::::::::::::::::::::::::

set exitC=0

:Rand
set /a "exitC+=1"
if [%exitC%] == [60] (goto Exit1)

set /a "X=%RANDOM% * 100 / 32768 + 1"
if [%X%] LEQ [90] set cell=2
if [%X%] GEQ [91] set cell=4

set /a "X=%RANDOM% * 4 / 32768 + 1"
set /a "Y=%RANDOM% * 4 / 32768 + 1"
set name=contentR%Y%%X%

if [!%name%!] == [0] (set %name%=%cell%
) else (goto Rand)
set "content%Y%%X%=   %cell%"
if [%version%] == [0] goto Anulare


::::::::::::::::::::::::::::::::::::::::

if %contor% GTR %BestSc% set BestSc=%contor%

:Cls1
cls

%echo2%
echo і%content11%іі%content12%іі%content13%іі%content14%і    Score = %contor%
%echo3%
%echo2%
echo і%content21%іі%content22%іі%content23%іі%content24%і
%echo3%
%echo2%
echo і%content31%іі%content32%іі%content33%іі%content34%і
%echo3%
%echo2%
echo і%content41%іі%content42%іі%content43%іі%content44%і    BestS = %BestSc%
%echo3%
echo(
echo WASD or X or R

choice /C wasdxr /N >nul

if errorlevel 6 (goto Begin1)
if errorlevel 5 (goto Exit1)
if errorlevel 4 (goto D4)
if errorlevel 3 (goto S3)
if errorlevel 2 (goto Aa2)
if errorlevel 1 (goto W1)
if errorlevel 0 (goto Direction)

:::::::::::::::::::::::::::::::::::::::::

:D4
call muffe_D4.bat
goto Direction

:S3
call muffe_S3.bat
goto Direction

:Aa2
call muffe_Aa2.bat
goto Direction

:W1
call muffe_W1.bat
goto Direction

:::::::::::::::::::::::::::::::::::::::::

:Anulare
set contentR%Y%%X%=0
set "content%Y%%X%=    "
goto Cls1


:Exit1
echo %BestSc% >muffe_BestSc.txt





содержимое управляющих батников:

muffe_Aa2.bat
:Aa2
set version=0

call :ToZero

if [%contentR11%] == [%contentR12%] (call :Sum1000 contentR11 contentR12 content11 content12)
if [%contentR12%] == [%contentR13%] (call :Sum1000 contentR12 contentR13 content12 content13)
if [%contentR13%] == [%contentR14%] (call :Sum1000 contentR13 contentR14 content13 content14)

if [%contentR21%] == [%contentR22%] (call :Sum1000 contentR21 contentR22 content21 content22)
if [%contentR22%] == [%contentR23%] (call :Sum1000 contentR22 contentR23 content22 content23)
if [%contentR23%] == [%contentR24%] (call :Sum1000 contentR23 contentR24 content23 content24)

if [%contentR31%] == [%contentR32%] (call :Sum1000 contentR31 contentR32 content31 content32)
if [%contentR32%] == [%contentR33%] (call :Sum1000 contentR32 contentR33 content32 content33)
if [%contentR33%] == [%contentR34%] (call :Sum1000 contentR33 contentR34 content33 content34)

if [%contentR41%] == [%contentR42%] (call :Sum1000 contentR41 contentR42 content41 content42)
if [%contentR42%] == [%contentR43%] (call :Sum1000 contentR42 contentR43 content42 content43)
if [%contentR43%] == [%contentR44%] (call :Sum1000 contentR43 contentR44 content43 content44)

call :ToZero

exit /B

:ToZero

set rrnn=1
:PPNN
if [%contentR11%] == [0] (call :Zero1000 contentR11 contentR12 content11 content12)
if [%contentR12%] == [0] (call :Zero1000 contentR12 contentR13 content12 content13)
if [%contentR13%] == [0] (call :Zero1000 contentR13 contentR14 content13 content14)

if [%contentR21%] == [0] (call :Zero1000 contentR21 contentR22 content21 content22)
if [%contentR22%] == [0] (call :Zero1000 contentR22 contentR23 content22 content23)
if [%contentR23%] == [0] (call :Zero1000 contentR23 contentR24 content23 content24)

if [%contentR31%] == [0] (call :Zero1000 contentR31 contentR32 content31 content32)
if [%contentR32%] == [0] (call :Zero1000 contentR32 contentR33 content32 content33)
if [%contentR33%] == [0] (call :Zero1000 contentR33 contentR34 content33 content34)

if [%contentR41%] == [0] (call :Zero1000 contentR41 contentR42 content41 content42)
if [%contentR42%] == [0] (call :Zero1000 contentR42 contentR43 content42 content43)
if [%contentR43%] == [0] (call :Zero1000 contentR43 contentR44 content43 content44)
set /a "rrnn+=1"
if [%rrnn%] LEQ [3] (goto PPNN)
exit /B

:Sum1000

set /a "PP=!%1!+!%2!"
if [%PP%] == [0] (exit /B)
set %1=%PP%
set %3=%PP%
set /a contor+=%PP%
IF %PP% LSS 1000 set "%3= %PP%"
IF %PP% LSS 100 set "%3=  %PP%"
IF %PP% LSS 10 set "%3=   %PP%"
set "%4=    "
set %2=0
set version=1
exit /B

:Zero1000

set /a "PP=!%1!+!%2!"
if [%PP%] == [0] (exit /B)
set %3=!%4!
set %1=!%2!
set "%4=    "
set %2=0
set version=1
exit /B



muffe_D4.bat
:D4
set version=0

call :ToZero

if [%contentR14%] == [%contentR13%] (call :Sum1000 contentR14 contentR13 content14 content13)
if [%contentR13%] == [%contentR12%] (call :Sum1000 contentR13 contentR12 content13 content12)
if [%contentR12%] == [%contentR11%] (call :Sum1000 contentR12 contentR11 content12 content11)

if [%contentR24%] == [%contentR23%] (call :Sum1000 contentR24 contentR23 content24 content23)
if [%contentR23%] == [%contentR22%] (call :Sum1000 contentR23 contentR22 content23 content22)
if [%contentR22%] == [%contentR21%] (call :Sum1000 contentR22 contentR21 content22 content21)

if [%contentR34%] == [%contentR33%] (call :Sum1000 contentR34 contentR33 content34 content33)
if [%contentR33%] == [%contentR32%] (call :Sum1000 contentR33 contentR32 content33 content32)
if [%contentR32%] == [%contentR31%] (call :Sum1000 contentR32 contentR31 content32 content31)

if [%contentR44%] == [%contentR43%] (call :Sum1000 contentR44 contentR43 content44 content43)
if [%contentR43%] == [%contentR42%] (call :Sum1000 contentR43 contentR42 content43 content42)
if [%contentR42%] == [%contentR41%] (call :Sum1000 contentR42 contentR41 content42 content41)

call :ToZero

exit /B

:ToZero

set rrnn=1
:PPNN
if [%contentR14%] == [0] (call :Zero1000 contentR14 contentR13 content14 content13)
if [%contentR13%] == [0] (call :Zero1000 contentR13 contentR12 content13 content12)
if [%contentR12%] == [0] (call :Zero1000 contentR12 contentR11 content12 content11)

if [%contentR24%] == [0] (call :Zero1000 contentR24 contentR23 content24 content23)
if [%contentR23%] == [0] (call :Zero1000 contentR23 contentR22 content23 content22)
if [%contentR22%] == [0] (call :Zero1000 contentR22 contentR21 content22 content21)

if [%contentR34%] == [0] (call :Zero1000 contentR34 contentR33 content34 content33)
if [%contentR33%] == [0] (call :Zero1000 contentR33 contentR32 content33 content32)
if [%contentR32%] == [0] (call :Zero1000 contentR32 contentR31 content32 content31)

if [%contentR44%] == [0] (call :Zero1000 contentR44 contentR43 content44 content43)
if [%contentR43%] == [0] (call :Zero1000 contentR43 contentR42 content43 content42)
if [%contentR42%] == [0] (call :Zero1000 contentR42 contentR41 content42 content41)
set /a "rrnn+=1"
if [%rrnn%] LEQ [3] (goto PPNN)
exit /B

:Sum1000

set /a "PP=!%1!+!%2!"
if [%PP%] == [0] (exit /B)
set %1=%PP%
set %3=%PP%
set /a contor+=%PP%
IF %PP% LSS 1000 set "%3= %PP%"
IF %PP% LSS 100 set "%3=  %PP%"
IF %PP% LSS 10 set "%3=   %PP%"
set "%4=    "
set %2=0
set version=1
exit /B

:Zero1000

set /a "PP=!%1!+!%2!"
if [%PP%] == [0] (exit /B)
set %3=!%4!
set %1=!%2!
set "%4=    "
set %2=0
set version=1
exit /B



muffe_S3.bat
:S3
set version=0

call :ToZero

if [%contentR41%] == [%contentR31%] (call :Sum1000 contentR41 contentR31 content41 content31)
if [%contentR31%] == [%contentR21%] (call :Sum1000 contentR31 contentR21 content31 content21)
if [%contentR21%] == [%contentR11%] (call :Sum1000 contentR21 contentR11 content21 content11)

if [%contentR42%] == [%contentR32%] (call :Sum1000 contentR42 contentR32 content42 content32)
if [%contentR32%] == [%contentR22%] (call :Sum1000 contentR32 contentR22 content32 content22)
if [%contentR22%] == [%contentR12%] (call :Sum1000 contentR22 contentR12 content22 content12)

if [%contentR43%] == [%contentR33%] (call :Sum1000 contentR43 contentR33 content43 content33)
if [%contentR33%] == [%contentR23%] (call :Sum1000 contentR33 contentR23 content33 content23)
if [%contentR23%] == [%contentR13%] (call :Sum1000 contentR23 contentR13 content23 content13)

if [%contentR44%] == [%contentR34%] (call :Sum1000 contentR44 contentR34 content44 content34)
if [%contentR34%] == [%contentR24%] (call :Sum1000 contentR34 contentR24 content34 content24)
if [%contentR24%] == [%contentR14%] (call :Sum1000 contentR24 contentR14 content24 content14)

call :ToZero

exit /B

:ToZero

set rrnn=1
:PPNN
if [%contentR41%] == [0] (call :Zero1000 contentR41 contentR31 content41 content31)
if [%contentR31%] == [0] (call :Zero1000 contentR31 contentR21 content31 content21)
if [%contentR21%] == [0] (call :Zero1000 contentR21 contentR11 content21 content11)

if [%contentR42%] == [0] (call :Zero1000 contentR42 contentR32 content42 content32)
if [%contentR32%] == [0] (call :Zero1000 contentR32 contentR22 content32 content22)
if [%contentR22%] == [0] (call :Zero1000 contentR22 contentR12 content22 content12)

if [%contentR43%] == [0] (call :Zero1000 contentR43 contentR33 content43 content33)
if [%contentR33%] == [0] (call :Zero1000 contentR33 contentR23 content33 content23)
if [%contentR23%] == [0] (call :Zero1000 contentR23 contentR13 content23 content13)

if [%contentR44%] == [0] (call :Zero1000 contentR44 contentR34 content44 content34)
if [%contentR34%] == [0] (call :Zero1000 contentR34 contentR24 content34 content24)
if [%contentR24%] == [0] (call :Zero1000 contentR24 contentR14 content24 content14)
set /a "rrnn+=1"
if [%rrnn%] LEQ [3] (goto PPNN)
exit /B

:Sum1000

set /a "PP=!%1!+!%2!"
if [%PP%] == [0] (exit /B)
set %1=%PP%
set %3=%PP%
set /a contor+=%PP%
IF %PP% LSS 1000 set "%3= %PP%"
IF %PP% LSS 100 set "%3=  %PP%"
IF %PP% LSS 10 set "%3=   %PP%"
set "%4=    "
set %2=0
set version=1
exit /B

:Zero1000

set /a "PP=!%1!+!%2!"
if [%PP%] == [0] (exit /B)
set %3=!%4!
set %1=!%2!
set "%4=    "
set %2=0
set version=1
exit /B



muffe_W1.bat
:W1
set version=0

call :ToZero

if [%contentR11%] == [%contentR21%] (call :Sum1000 contentR11 contentR21 content11 content21)
if [%contentR21%] == [%contentR31%] (call :Sum1000 contentR21 contentR31 content21 content31)
if [%contentR31%] == [%contentR41%] (call :Sum1000 contentR31 contentR41 content31 content41)

if [%contentR12%] == [%contentR22%] (call :Sum1000 contentR12 contentR22 content12 content22)
if [%contentR22%] == [%contentR32%] (call :Sum1000 contentR22 contentR32 content22 content32)
if [%contentR32%] == [%contentR42%] (call :Sum1000 contentR32 contentR42 content32 content42)

if [%contentR13%] == [%contentR23%] (call :Sum1000 contentR13 contentR23 content13 content23)
if [%contentR23%] == [%contentR33%] (call :Sum1000 contentR23 contentR33 content23 content33)
if [%contentR33%] == [%contentR43%] (call :Sum1000 contentR33 contentR43 content33 content43)

if [%contentR14%] == [%contentR24%] (call :Sum1000 contentR14 contentR24 content14 content24)
if [%contentR24%] == [%contentR34%] (call :Sum1000 contentR24 contentR34 content24 content34)
if [%contentR34%] == [%contentR44%] (call :Sum1000 contentR34 contentR44 content34 content44)

call :ToZero

exit /B

:ToZero

set rrnn=1
:PPNN
if [%contentR11%] == [0] (call :Zero1000 contentR11 contentR21 content11 content21)
if [%contentR21%] == [0] (call :Zero1000 contentR21 contentR31 content21 content31)
if [%contentR31%] == [0] (call :Zero1000 contentR31 contentR41 content31 content41)

if [%contentR12%] == [0] (call :Zero1000 contentR12 contentR22 content12 content22)
if [%contentR22%] == [0] (call :Zero1000 contentR22 contentR32 content22 content32)
if [%contentR32%] == [0] (call :Zero1000 contentR32 contentR42 content32 content42)

if [%contentR13%] == [0] (call :Zero1000 contentR13 contentR23 content13 content23)
if [%contentR23%] == [0] (call :Zero1000 contentR23 contentR33 content23 content33)
if [%contentR33%] == [0] (call :Zero1000 contentR33 contentR43 content33 content43)

if [%contentR14%] == [0] (call :Zero1000 contentR14 contentR24 content14 content24)
if [%contentR24%] == [0] (call :Zero1000 contentR24 contentR34 content24 content34)
if [%contentR34%] == [0] (call :Zero1000 contentR34 contentR44 content34 content44)
set /a "rrnn+=1"
if [%rrnn%] LEQ [3] (goto PPNN)
exit /B

:Sum1000

set /a "PP=!%1!+!%2!"
if [%PP%] == [0] (exit /B)
set %1=%PP%
set %3=%PP%
set /a contor+=%PP%
IF %PP% LSS 1000 set "%3= %PP%"
IF %PP% LSS 100 set "%3=  %PP%"
IF %PP% LSS 10 set "%3=   %PP%"
set "%4=    "
set %2=0
set version=1
exit /B

:Zero1000

set /a "PP=!%1!+!%2!"
if [%PP%] == [0] (exit /B)
set %3=!%4!
set %1=!%2!
set "%4=    "
set %2=0
set version=1
exit /B



лучший счет
muffe_BestSc.txt
0



скачать архив yadi.sk/d/Glgq4y1igcvoa
кое что позаимствовано у AAbrosov

старые версии
скачать архив yadi.sk/d/s7cmQlLXgKbng

добавил проверку изменений, теперь 2 не появляются если не было смещения и/или сложения
не очень элегантно вышло, но работает. нужно будет доработать и использовать переключатель
скачать архив 2 yadi.sk/d/fZXAQi9ngLGDE

скачать архив 3 yadi.sk/d/-YxSIUXpgQf2n

Теги:
Хабы:
+14
Комментарии 23
Комментарии Комментарии 23

Публикации

Истории

Ближайшие события

Московский туристический хакатон
Дата 23 марта – 7 апреля
Место
Москва Онлайн
Геймтон «DatsEdenSpace» от DatsTeam
Дата 5 – 6 апреля
Время 17:00 – 20:00
Место
Онлайн