Tuesday, April 29, 2008

Master Mind - Juego en ABAP


En la tarde, publiqué este blog en el SDN ABAP's Mind Game, así que me toca publicarlo aquí también -;)

Hice un juego al estilo de Master Mind, que es donde la PC piensa en el orden de 5 números y nosotros tenemos que adivinar cual es...Claro que el sistema nos indica si las posiciones que ingresamos son correctas o incorrectas.

Primero comencé con un simple Dynpro...


Luego, escribí el código fuente...

REPORT ZABAP_MIND_GAME.

*&---------------------------------------------------------*
* CONSTANTS *
*&---------------------------------------------------------*
CONSTANTS: line_length TYPE i VALUE 254.

*&---------------------------------------------------------*
* TYPES *
*&---------------------------------------------------------*
TYPES: BEGIN OF ty_rand_table,
value(1) TYPE c,
END OF ty_rand_table.

*&---------------------------------------------------------*
* INTERNAL TABLES *
*&---------------------------------------------------------*
DATA: t_rand_table TYPE STANDARD TABLE OF ty_rand_table,
t_user_table TYPE STANDARD TABLE OF ty_rand_table,
t_game_lines TYPE TABLE OF tline-tdline,
data_rand TYPE REF TO data,
data_lines TYPE REF TO data.

*&---------------------------------------------------------*
* FIELD-SYMBOLS *
*&---------------------------------------------------------*
FIELD-SYMBOLS: <fs_rand_table> LIKE LINE OF t_rand_table,
<fs_user_table> LIKE LINE OF t_user_table,
<fs_rand_value> LIKE LINE OF t_rand_table.

*&---------------------------------------------------------*
* VARIABLES *
*&---------------------------------------------------------*
DATA: custom_container TYPE REF TO cl_gui_custom_container,
text_editor TYPE REF TO cl_gui_textedit,
w_ucomm TYPE sy-ucomm,
ran_int TYPE qf00-ran_int,
flag TYPE c,
counter TYPE c,
gv_line TYPE string,
button_count TYPE c,
xtext TYPE tline-tdline,
game_win TYPE string,
game_counter TYPE string.

DATA: message_one TYPE string,
message_two TYPE string,
message_three TYPE string,
message_four TYPE string,
message_five TYPE string.

DATA: gv_one TYPE c,
gv_two TYPE c,
gv_three TYPE c,
gv_four TYPE c,
gv_five TYPE c.

DATA: gv_flag_one TYPE c,
gv_flag_two TYPE c,
gv_flag_three TYPE c,
gv_flag_four TYPE c,
gv_flag_five TYPE c.

DATA: gv_user_one TYPE c,
gv_user_two TYPE c,
gv_user_three TYPE c,
gv_user_four TYPE c,
gv_user_five TYPE c.

*----------------------------------------------------------*
* START-OF-SELECTION *
*----------------------------------------------------------*
START-OF-SELECTION.
CALL SCREEN 0100.

*&---------------------------------------------------------*
*& Form call_editor *
*&---------------------------------------------------------*
FORM call_editor.

IF text_editor IS INITIAL.
CREATE OBJECT custom_container
EXPORTING
container_name = 'CUSTOM_CONTROL'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.

CREATE OBJECT text_editor
EXPORTING
wordwrap_mode =
cl_gui_textedit=>wordwrap_at_fixed_position
wordwrap_position = line_length
wordwrap_to_linebreak_mode = cl_gui_textedit=>true
parent = custom_container
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
gui_type_not_supported = 5
others = 6.

CALL METHOD text_editor->set_readonly_mode
EXPORTING readonly_mode = 1.
ENDIF.

PERFORM get_randoms.

ENDFORM.

*&---------------------------------------------------------*
*& Form get_randoms *
*&---------------------------------------------------------*
FORM get_randoms.

CREATE DATA data_rand TYPE ty_rand_table.
ASSIGN data_rand->* TO <fs_rand_table>.

WHILE flag EQ space.
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
ran_int_max = 5
ran_int_min = 1
IMPORTING
ran_int = ran_int
EXCEPTIONS
invalid_input = 1
OTHERS = 2.

READ TABLE t_rand_table ASSIGNING <fs_rand_value>
WITH KEY value = ran_int.
IF sy-subrc NE 0.
<fs_rand_table>-value = ran_int.
APPEND <fs_rand_table> TO t_rand_table.
counter = counter + 1.
IF counter EQ 5.
flag = 'X'.
ENDIF.
ENDIF.
ENDWHILE.

game_counter = 1.

ENDFORM. " get_randoms

*&---------------------------------------------------------*
*& Form validate_game *
*&---------------------------------------------------------*
FORM validate_game.

LOOP AT t_rand_table ASSIGNING <fs_rand_value>.
IF gv_one IS INITIAL.
gv_one = <fs_rand_value>-value.
ELSEIF gv_two IS INITIAL.
gv_two = <fs_rand_value>-value.
ELSEIF gv_three IS INITIAL.
gv_three = <fs_rand_value>-value.
ELSEIF gv_four IS INITIAL.
gv_four = <fs_rand_value>-value.
ELSEIF gv_five IS INITIAL.
gv_five = <fs_rand_value>-value.
ENDIF.
ENDLOOP.

CLEAR: gv_user_one,gv_user_two,gv_user_three,
gv_user_four,gv_user_five.

LOOP AT t_user_table ASSIGNING <fs_user_table>.
IF gv_user_one IS INITIAL.
gv_user_one = <fs_user_table>-value.
ELSEIF gv_user_two IS INITIAL.
gv_user_two = <fs_user_table>-value.
ELSEIF gv_user_three IS INITIAL.
gv_user_three = <fs_user_table>-value.
ELSEIF gv_user_four IS INITIAL.
gv_user_four = <fs_user_table>-value.
ELSEIF gv_user_five IS INITIAL.
gv_user_five = <fs_user_table>-value.
ENDIF.
ENDLOOP.

CLEAR: game_win.

IF gv_one EQ gv_user_one.
message_one = 'First is Ok!'.
game_win = game_win + 1.
ELSE.
message_one = 'First is Wrong!'.
game_win = game_win - 1.
ENDIF.
IF gv_two EQ gv_user_two.
message_two = 'Second is Ok!'.
game_win = game_win + 1.
ELSE.
message_two = 'Second is Wrong!'.
game_win = game_win - 1.
ENDIF.
IF gv_three EQ gv_user_three.
message_three = 'Third is Ok!'.
game_win = game_win + 1.
ELSE.
message_three = 'Third is Wrong!'.
game_win = game_win - 1.
ENDIF.
IF gv_four EQ gv_user_four.
message_four = 'Forth is Ok!'.
game_win = game_win + 1.
ELSE.
message_four = 'Forth is Wrong!'.
game_win = game_win - 1.
ENDIF.
IF gv_five EQ gv_user_five.
message_five = 'Fifth is Ok!'.
game_win = game_win + 1.
ELSE.
message_five = 'Fifth is Wrong!'.
game_win = game_win - 1.
ENDIF.

IF game_win EQ 5.
CLEAR: message_one,message_two,message_three,
message_four,message_five.
message_one = 'You win!'.
CONCATENATE 'In' game_counter 'tries'
INTO message_two SEPARATED BY space.
ELSE.
game_counter = game_counter + 1.
CLEAR: gv_flag_one,gv_flag_two,gv_flag_three,
gv_flag_four,gv_flag_five,sy-ucomm.
ENDIF.

ENDFORM. " validate_game

*&---------------------------------------------------------*
*& Module STATUS_0100 OUTPUT *
*&---------------------------------------------------------*
MODULE status_0100 OUTPUT.

SET PF-STATUS 'MAIN'.
SET TITLEBAR 'TITLE'.

IF flag EQ space.
PERFORM call_editor.
ELSE.

LOOP AT SCREEN.
IF screen-name EQ 'ONE' AND gv_flag_one EQ 'X'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
IF screen-name EQ 'TWO' AND gv_flag_two EQ 'X'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
IF screen-name EQ 'THREE' AND gv_flag_three EQ 'X'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
IF screen-name EQ 'FOUR' AND gv_flag_four EQ 'X'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
IF screen-name EQ 'FIVE' AND gv_flag_five EQ 'X'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
ENDLOOP.

IF button_count EQ 5.

CLEAR button_count.

SHIFT gv_line LEFT DELETING LEADING space.

SPLIT gv_line AT space INTO TABLE t_user_table.

PERFORM validate_game.

xtext = gv_line.
APPEND xtext TO t_game_lines.

CLEAR gv_line.

CALL METHOD text_editor->set_text_as_r3table
EXPORTING table = t_game_lines.

ENDIF.
ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT

*&---------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT *
*&---------------------------------------------------------*
MODULE user_command_0100 INPUT.

w_ucomm = sy-ucomm.

CASE w_ucomm.
WHEN 'BACK' OR 'CANCEL' OR 'EXIT'.
SET SCREEN 0.
EXIT.
WHEN 'ONE'.
CONCATENATE gv_line '1' INTO
gv_line SEPARATED BY space.
button_count = button_count + 1.
gv_flag_one = 'X'.
WHEN 'TWO'.
CONCATENATE gv_line '2' INTO
gv_line SEPARATED BY space.
button_count = button_count + 1.
gv_flag_two = 'X'.
WHEN 'THREE'.
CONCATENATE gv_line '3' INTO
gv_line SEPARATED BY space.
button_count = button_count + 1.
gv_flag_three = 'X'.
WHEN 'FOUR'.
CONCATENATE gv_line '4' INTO
gv_line SEPARATED BY space.
button_count = button_count + 1.
gv_flag_four = 'X'.
WHEN 'FIVE'.
CONCATENATE gv_line '5' INTO
gv_line SEPARATED BY space.
button_count = button_count + 1.
gv_flag_five = 'X'.
ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

Finalmente, tomé algunos pantallazos -;)




Espero que les guste -:)

Saludos,

Blag.

No comments: