Created
December 22, 2011 01:12
-
-
Save furlan/1508463 to your computer and use it in GitHub Desktop.
Z_ABAP101_TTT_TOURNAMENTS - Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*&---------------------------------------------------------------------* | |
*& Report Z_ABAP101_TTT_TOURNAMENTS | |
*& | |
*&---------------------------------------------------------------------* | |
*& | |
*& | |
*&---------------------------------------------------------------------* | |
REPORT z_abap101_ttt_tournaments. | |
TYPE-POOLS: abap. | |
TYPES: BEGIN OF ty_leaderboard, | |
player TYPE c LENGTH 50, | |
win TYPE i, | |
deuce TYPE i, | |
lose TYPE i, | |
points TYPE i, | |
END OF ty_leaderboard. | |
PARAMETERS: p_file TYPE string DEFAULT 'C:\Documents and Settings\nspadm\Meus documentos\classes.txt'. | |
DATA: l_r_board TYPE REF TO zabap101ttt_board, | |
l_r_game TYPE REF TO zabap101ttt_game, | |
l_position_to_be_filled TYPE zabap101ttt_board_position. | |
DATA: l_r_player1 TYPE REF TO zifabap101_ttt_player_as, | |
l_r_player2 TYPE REF TO zifabap101_ttt_player_as, | |
l_r_player TYPE REF TO zifabap101_ttt_player_as. | |
DATA: l_player TYPE abap_bool, | |
l_board_table TYPE zabap101ttt_board_table, | |
l_winner TYPE zabap101ttt_board_value, | |
it_file1 TYPE ztt_fm_data, | |
it_file2 TYPE ztt_fm_data, | |
it_scores TYPE TABLE OF zttt_leaderboard, | |
l_s_scores TYPE zttt_leaderboard, | |
l_game TYPE i VALUE 1, | |
l_set TYPE i VALUE 1. | |
DATA: w_leaderboard TYPE ty_leaderboard, | |
t_leaderboard TYPE TABLE OF ty_leaderboard. | |
* Objects from FileMan | |
* Project site: https://cw.sdn.sap.com/cw/groups/abap-fileman | |
DATA: l_r_file TYPE REF TO zcl_fm_local. | |
FIELD-SYMBOLS: <l_fs_board_struc> TYPE zabap101ttt_board_struct, | |
<l_fs_file1_line> TYPE string, | |
<l_fs_file2_line> TYPE string, | |
<l_fs_leaderboard> TYPE ty_leaderboard. | |
START-OF-SELECTION. | |
* Open file | |
CREATE OBJECT l_r_file | |
EXPORTING | |
im_file_name = p_file. | |
TRY. | |
CALL METHOD l_r_file->zif_fm_file~open. | |
CATCH cx_sy_file_open . | |
CATCH cx_sy_codepage_converter_init . | |
CATCH cx_sy_conversion_codepage . | |
CATCH cx_sy_file_authority . | |
CATCH cx_sy_file_close . | |
ENDTRY. | |
* Return file contents | |
it_file1 = l_r_file->zif_fm_file~get_data( ). | |
it_file2 = l_r_file->zif_fm_file~get_data( ). | |
* Create objects. | |
CREATE OBJECT l_r_game. | |
* All classes will fight each other each four times. | |
* Victory = 3 points | |
* Deuce = 1 point | |
* Loose = 0 point | |
WRITE: / 'LIST OF ALL PARTICIPANTS'. | |
LOOP AT it_file1 ASSIGNING <l_fs_file1_line>. | |
WRITE: / <l_fs_file1_line>. | |
CLEAR w_leaderboard. | |
w_leaderboard-player = <l_fs_file1_line>. | |
APPEND w_leaderboard TO t_leaderboard. | |
ENDLOOP. | |
ULINE. | |
WRITE: / 'GAMES'. | |
LOOP AT it_file1 ASSIGNING <l_fs_file1_line>. | |
LOOP AT it_file2 ASSIGNING <l_fs_file2_line>. | |
IF <l_fs_file1_line> = <l_fs_file2_line>. | |
CONTINUE. | |
ENDIF. | |
* Each game, player plays 4 times. | |
DO 4 TIMES. | |
CREATE OBJECT l_r_player1 | |
TYPE | |
(<l_fs_file1_line>) | |
EXPORTING | |
im_as_player_value = 'X'. | |
CREATE OBJECT l_r_player2 | |
TYPE | |
(<l_fs_file2_line>) | |
EXPORTING | |
im_as_player_value = 'O'. | |
" Start new game. | |
" Each player have a chance to start the game. | |
IF l_set = 1 OR l_set = 3. | |
l_player = 'X'. | |
l_r_player = l_r_player1. | |
ELSE. | |
l_player = 'O'. | |
l_r_player = l_r_player2. | |
ENDIF. | |
CALL METHOD l_r_game->new_game | |
EXPORTING | |
im_first_player = l_player. | |
" Execute complete game. | |
DO 9 TIMES. | |
l_position_to_be_filled = l_r_player->decide_next_play( l_r_game->board ). | |
TRY. | |
CALL METHOD l_r_game->play | |
EXPORTING | |
im_position = l_position_to_be_filled. | |
CATCH zcx_ttt_play_same_position . | |
CATCH zcx_ttt_game_over . | |
EXIT. | |
ENDTRY. | |
IF l_player = 'X'. | |
l_player = 'O'. | |
l_r_player = l_r_player2. | |
ELSE. | |
l_player = 'X'. | |
l_r_player = l_r_player1. | |
ENDIF. | |
ENDDO. | |
CALL METHOD l_r_game->board->get_board | |
RECEIVING | |
re_board = l_board_table. | |
SKIP 1. | |
WRITE: / 'Game :', l_game, ' and set: ', l_set. | |
WRITE: / <l_fs_file1_line>, ' versus ', <l_fs_file2_line>. | |
l_winner = l_r_game->get_winner( ). | |
CASE l_winner. | |
WHEN 'X'. | |
" Sorry about perform.... | |
PERFORM f_update_leader_board USING <l_fs_file1_line> <l_fs_file2_line> abap_false. | |
WRITE: / 'Winner: ', <l_fs_file1_line>. | |
WHEN '0'. | |
" Sorry about perform.... | |
PERFORM f_update_leader_board USING <l_fs_file2_line> <l_fs_file1_line> abap_false. | |
WRITE: / 'Winner: ', <l_fs_file2_line>. | |
WHEN OTHERS. | |
" Sorry about perform.... | |
PERFORM f_update_leader_board USING <l_fs_file1_line> <l_fs_file2_line> abap_true. | |
WRITE: / 'Deuce'. | |
ENDCASE. | |
SKIP. | |
ADD 1 TO l_set. | |
ENDDO. | |
l_set = 1. | |
ADD 1 TO l_game. | |
ENDLOOP. | |
ENDLOOP. | |
ULINE. | |
WRITE: /25 'FINAL RESULTS'. | |
WRITE: /25 '============='. | |
SORT t_leaderboard BY points DESCENDING | |
win DESCENDING | |
lose DESCENDING | |
deuce DESCENDING. | |
WRITE: /2 'PLAYER', | |
38 'POINTS', | |
48 'WINS', | |
58 'LOSES', | |
68 'DEUCE'. | |
WRITE: /2 '======', | |
38 '======', | |
48 '====', | |
58 '=====', | |
68 '====='. | |
LOOP AT t_leaderboard ASSIGNING <l_fs_leaderboard>. | |
WRITE: /2 <l_fs_leaderboard>-player, | |
32 <l_fs_leaderboard>-points, | |
42 <l_fs_leaderboard>-win, | |
52 <l_fs_leaderboard>-lose, | |
62 <l_fs_leaderboard>-deuce. | |
ENDLOOP. | |
*&---------------------------------------------------------------------* | |
*& Form F_UPDATE_LEADER_BOARD | |
*&---------------------------------------------------------------------* | |
* text | |
*----------------------------------------------------------------------* | |
* -->P_WINNER text | |
*----------------------------------------------------------------------* | |
FORM f_update_leader_board USING p_winner | |
p_looser | |
p_deuce. | |
" YOU WIN!!! | |
READ TABLE t_leaderboard ASSIGNING <l_fs_leaderboard> | |
WITH KEY player = p_winner. | |
CHECK sy-subrc = 0. | |
IF p_deuce = abap_false. | |
ADD 1 TO <l_fs_leaderboard>-win. | |
ADD 3 TO <l_fs_leaderboard>-points. | |
" YOU LOSE!!! | |
READ TABLE t_leaderboard ASSIGNING <l_fs_leaderboard> | |
WITH KEY player = p_looser. | |
IF sy-subrc = 0. | |
ADD 1 TO <l_fs_leaderboard>-lose. | |
ENDIF. | |
ELSE. | |
ADD 1 TO <l_fs_leaderboard>-deuce. | |
ADD 1 TO <l_fs_leaderboard>-points. | |
READ TABLE t_leaderboard ASSIGNING <l_fs_leaderboard> | |
WITH KEY player = p_looser. | |
IF sy-subrc = 0. | |
ADD 1 TO <l_fs_leaderboard>-deuce. | |
ADD 1 TO <l_fs_leaderboard>-points. | |
ENDIF. | |
ENDIF. | |
ENDFORM. " F_UPDATE_LEADER_BOARD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment