Nostalgia: "Lizards" on a body shirt

    We all remember our first computer, and if you are a programmer, then the first program. My first computer was the ZX-Spectrum, and the first program written for it was the Lizards toy in BASIC. Well, to be honest, I just rewrote it from one book 1 , simultaneously understanding the code and correcting the mistakes made by the author :) 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 simple. And since I like to write batch files , I rewrote it on the last ones :) The biggest difficulty is the unsupported batch files of arrays, so I had to twist a little :) The self-learning game (primitive AI) consists of the game itself (pangolins.cmd) and a question base ( questions.txt). Description from the book:

    2.jpg - Picamatic - upload your images









    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 he will "ask" to ask him suggestive questions or "offer" him a new name.


    image

    I don’t know if it makes sense to describe how everything works, so if there are 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 = Hmm. It’s 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 describes 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 The number of lines is 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 = Once again? [d] / n:
    if not "% answer%" == "n" (
      goto: begin
    )
    


    Download (1.4 kB)

    1. O.S. Borovik, A.V. Parfenov, V.I. Syryamkin. Exciting games on the home computer, IHL "Rasko", Tomsk, 1992.

    Also popular now: