📜 ⬆️ ⬇️

Nostalgia: “Lizards” on a batch file

We all remember our first computer, and if you are a programmer, then the first program. The first computer I had was the ZX-Spectrum, and the first program written under it was the toy “Lizards” in BASIC. Well, to be honest, I just rewrote it from one book 1 , simultaneously sorting out the code and correcting the mistakes made by the author :)

2.jpg - Picamatic - upload your images

I decided to give a second life to the above toy. But to rewrite it in any of the languages ​​I know would be too easy. And since I like to write batch file , I rewrote it in the last :)
')


The biggest difficulty in not supporting batch file arrays, so I had to pervert a little :) The game is self-learning (primitive AI), consists of the game itself (pangolins.cmd) and the question base (questions.txt).

Description from the book:

The second program (program number 2) is the game “Lizards”, in which the computer guesses the name of the animal conceived by the player. In this case, the computer asks questions, and the player answers only "yes" or "no." If the computer was not previously “familiar” with such an animal, then it “asks” to ask him leading questions or “offers” a new name to it.


image

I don’t know if it makes sense to describe how everything works, so if you have specific questions about the code, ask in the comments;)

 @echo off
 : begin

 set qfile = questions.txt
 set tfile = questions_tmp.txt
 set cursor = 1
 set prew_cursor = 1
 set count =
 rem ------------------------------------------------- ----------------------

 echo Think of an animal and press any key to continue ...
 pause> nul


 call: get_current

 : get_answer

 set answer =
 set / p answer =% question% [d] / n:

 set prew_cursor =% cursor%
 set prew_answer =% answer%
 if not "% answer%" == "n" (
   set cursor =% yes_id%
 ) else (
   set cursor =% no_id%
 )

 call: get_current

 if not "% yes_id%" == "0" (
   goto: get_answer
 )

 set answer =
 set / p answer = This is% question%.  I guessed?  [d] / n:

 if not "% answer%" == "n" (
   echo I thought so.
   goto: end
 )

 rem ------------------------------------------------- ----------------------

 call: get_count
 set / a new_animal_id =% count% + 1
 set / a new_question_id =% count% + 2

 call: update_current

 : get_new_animal
 set new_animal =
 set / p new_animal = Hm.  Strange.  Well, what is it ?:
 if "% new_animal%" == "" (
   goto: get_new_animal
 )

 echo% new_animal%: 0: 0 >>% qfile%

 : get_new_question
 set new_question =
 set / p new_question = Please write a question that characterizes the differences between animals% new_animal% and% question%:
 if "% new_question%" == "" (
   goto: get_new_question
 )

 set right_answer =
 set / p right_answer = And what is the correct answer for the animal% new_animal% [d] / n:
 if not "% right_answer%" == "n" (
   echo% new_question%:% new_animal_id%:% cursor% >>% qfile%
 ) else (
   echo% new_question%:% cursor%:% new_animal_id% >>% qfile%
 )




 goto: end
 rem ------------------------------------------------- ----------------------

 : get_count
 for / f "delims =:" %% i in ('findstr / r / n. *% qfile%') do set count = %% i
 rem Number of lines one less (empty line at the end)
 rem set / a count =% count% -1
 exit / b



 : get_current
 for / f "tokens = 1-4 delims =:" %% i in ('findstr / r / n. *% qfile%') do call: get_current_callback %% i "% j" %% k %% l
 exit / b

 : get_current_callback
 if "% ~ 1" == "% cursor%" (
   set question =% ~ 2
   set yes_id =% ~ 3
   set no_id =% ~ 4
 )
 exit / b



 : update_current
 if exist% tfile% (
   del% tfile%
 )

 for / f "tokens = 1-4 delims =:" %% i in ('findstr / r / n. *% qfile%') do call: update_current_callback %% i "% j" %% k %% l
 copy / y% tfile%% qfile%> nul
 exit / b

 : update_current_callback
 if "% ~ 1" == "% prew_cursor%" (
   if not "% prew_answer%" == "n" (
     echo% ~ 2:% new_question_id%:% ~ 4 >>% tfile%
   ) else (
     echo% ~ 2:% ~ 3:% new_question_id% >>% tfile%
   )
 ) else (
   echo% ~ 2:% ~ 3:% ~ 4 >>% tfile%
 )
 exit / b



 rem ------------------------------------------------- ----------------------
 : end
 set answer =
 set / p answer = One more time?  [d] / n:

 if not "% answer%" == "n" (
   goto: begin
 )


Download (1.4 KB)

  1. O.S. Borovik, A.V. Parfenov, V.I.Syryamkin. Fascinating games on a domestic computer, IHL "Rasko", Tomsk, 1992.

Source: https://habr.com/ru/post/72832/


All Articles