SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

A simple Hangman Implementation (Игра Виселица)



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Mon Feb 25, 2008 1:24 pm    Post subject: A simple Hangman Implementation (Игра Виселица) Reply with quote

Code:
report zexp_hangman no standard page heading.title

***********************************************************
* A simple game: Hangman
*
*
*history
*  1.00 29th February 2000    rnr   downloaded from internet
*  1.10  1th March    2000    rnr   minor changes, layout, error corr
*
***********************************************************


* Put correct UNIX Words file on the next line.
  data unix_dict_file(80)
    " value '/usr/share/lib/dict/words'.
      value 'd:\dict\web2.txt'.

  include <icon>.
  data:
    dict_tab(20)    occurs 10000 with header line,
    giveup(20),
    line(80),
    guesses         occurs 10    with header line,
    correct_guesses occurs 10    with header line,
    mesg(60),
    len       type i,
    num_words type i,
    num_trys  type i,
    num_fails type i,
    num_bads  type i,
    num_goods type i,
    letter,
    runt      type i,
    sec       type i,
    begin of fig occurs 10,
      x(2),
      y(2),
      l,
    end of fig.

* P B O ( )
  initialization.
    append '6006|' to fig.
    append '60070' to fig.
    append '6008|' to fig.
    append '5908/' to fig.
    append '5909/' to fig.
    append '6109\' to fig.
    append '6108\' to fig.
    open dataset unix_dict_file for input in text mode.
    if sy-subrc is initial.
      do.
        read dataset unix_dict_file into line length len.
        if sy-subrc = 0.
          if len < 20.
            append line to dict_tab.
          endif.
        else.
          exit.
        endif.
      enddo.
      close dataset unix_dict_file.
    else.
      message e208(00) with 'No Dictionary found. Inform Administrator'.
    endif.
    describe table dict_tab lines num_words.
*   write: 'Number of words in dict:',num_words.

    perform print_out.
    data abcde like sy-abcde.


  form print_out.
    data:
      word_mask like dict_tab,
      index type i.

    abcde = sy-abcde.
    sy-lsind = 0.
    uline: at /(16), at 18(16), at 35(15), at 51(13).
    format color col_key.
    write:/ '|',icon_activate as icon no-gap, ' New Word ' hotspot,
     '|' no-gap ,space no-gap color col_background,
          '|', icon_failure as icon no-gap, ' Give Up! ' hotspot,
     '|' no-gap ,space no-gap color col_background,
          '|',icon_system_help as icon no-gap,' Help Me ' hotspot,
     '|' no-gap ,space no-gap color col_background,
          '|',icon_breakpoint as icon no-gap,' Exit! ' hotspot,'|'.
    format reset.
    uline: at /(16), at 18(16), at 35(15), at 51(13).
    write /.

    if not dict_tab is initial.

*     print the word mask
      len = strlen( dict_tab ).
      do.
        index =  sy-index - 1.
        letter = dict_tab+index(1).
        search  correct_guesses for letter.
        if sy-subrc = 0.
          word_mask+index = letter.
        else.
          word_mask+index = '_'.
        endif.
        if len = sy-index .
          exit.
        endif.
      enddo.

*     found the word
      if word_mask = dict_tab.
        add 1 to num_goods.
        write:/ 'Got it!. Correct word is',dict_tab,
            /  'It took ' ,num_trys,'tries.'.
        clear: num_trys, num_fails, dict_tab.

      else.
        write /  'Guess the Word:'.
        do len times.
          index = sy-index - 1.
          letter = word_mask+index.
          write letter.
        enddo.

*       print guesses
        write /  'Guesses'.
        sort guesses.
        loop at guesses.
          write  guesses no-gap.
        endloop.
        new-line.

*       check max failures
        if num_fails < 7.
*       print keyboard
          uline at /(55).
          do 26 times.
            letter = abcde.
            if sy-index = 14 or sy-index = 1. write / space. endif.
            write: (3)letter centered hotspot color col_key .
            shift abcde.
          enddo.
          write / mesg.
        else.
          write:/ 'Correct Word was',dict_tab.
          write / 'Pick a new word'.
          add 1 to num_bads.
          clear dict_tab.
        endif.

        perform draw_fig.
      endif.
    elseif not giveup is initial.
      write:/ 'Correct Word was',giveup.
      clear giveup.
    endif.
    skip to line 11.
    uline at /(64).
    write:/'|',
            (20) 'Words fully guessed', (7)num_goods,'| ',
            (20) 'Words not guessed',(7)num_bads,'|'.
*   write:/'Letter Guesses', num_trys, 'Failed Guesses',num_fails.
    uline at /(64).

  endform.


  form random.
    get run time field runt.
    get time.
    sec = sy-uzeit mod 60.
    runt = runt mod 100000.
    multiply runt by sec.
    get time.
    runt = runt mod 100000.
    add sec to runt.
  endform.


  at line-selection.
    data: field(30),value(50).
    get cursor field field value value.
    if field = 'ICON_ACTIVATE' or value = ' New Word'.
      perform new_word.
    elseif field = 'ICON_FAILURE' or value = ' Give Up!'.
      perform give_up.
    elseif field = 'ICON_SYSTEM_HELP' or value = ' Help Me'.
      perform print_help.
    elseif field = 'ICON_BREAKPOINT' or value = ' Exit!'.
      leave program.
    elseif field = 'LETTER' and not value is initial.
      perform try_word using value+1(1).
    endif.


  form new_word.
    if not dict_tab is initial.
      add 1 to num_bads.
    endif.
    do.
      perform random.
      runt = runt mod num_words.
      read table dict_tab index runt.
      if not dict_tab(1) co sy-abcde and dict_tab na '0123456789'.
        exit.
      endif.
    enddo.
    translate dict_tab to upper case.
    clear: num_trys, num_fails.
    refresh: guesses, correct_guesses.
    perform print_out.
  endform.


  form try_word using p_value type c.
    sy-lsind = 0.
    search guesses for p_value.
    if sy-subrc = 0.
      concatenate 'Letter' p_value 'already guessed'
        into mesg separated by space.
    else.
      append p_value to guesses.
      if dict_tab cs p_value.
        concatenate 'Letter' p_value 'matches'
          into mesg separated by space.
        append p_value to correct_guesses .
      else.
        concatenate 'Letter' p_value 'does not match'
          into mesg separated by space.
        add 1 to num_fails.
      endif.
      add 1 to num_trys.
    endif.
    perform print_out.
  endform.


  at pf8.
    perform new_word.


  form give_up.
    add 1 to num_bads.
    giveup = dict_tab.
    clear dict_tab.
    perform print_out.
  endform.

  form draw_fig.
*   |
*   0
*  /|\
*  / \
*
    back.
    skip 4.
    write: /55 '  -------'.
    write: /55 '  |'.
    write: /55 '  |'.
    write: /55 '  |'.
    write: /55 '  |'.
    write: /55 '  |'.
    write: /55 '---------'.
    loop at fig to num_fails.
      back.
      skip to line fig-y.
      write at fig-x fig-l.
    endloop.
    back.
  endform.

  form print_help.
    uline at /(55).
    write:
      /'What Is "Hangman?"',
      /,
      /'"Hangman" is an old schoolkid game where the',
      /'computer picks a word or a phrase, and you',
      /'try to reconstruct it by picking letters.',
      /'You have a certain number of chances to win.',
      /'Each time you pick an incorrect letter, you ',
      /'lose a chance.',/,
      /'Loss of a chance is traditionally depicted by adding',
      /'more body parts to a picture of a hanging man.',
      /'Once the man is complete, the game is over and you lose.'.
    uline at /(55).
    write:/ 'Click Green Back Arrow or press F3 to continue...'.
    uline at /(55).
  endform.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.