ABAP Game Tic-Tac-Toe

ABAP Game Tic-Tac-Toe
Please find below a game written in ABAP, this is the famous Tic Tac Toe game.
This is GPL Code
REPORT ZEX_TICTACTOE1 .
*&---------------------------------------------------------------------*
*& Report ZTic-tac-toe
*
*&
*
*&---------------------------------------------------------------------*
* Game Tic-tac-toe in ABAP - GPL Licence.
*&---------------------------------------------------------------------*

*

*----------------------------------------------------------------------*
* INCLUDES
*
*----------------------------------------------------------------------*
INCLUDE CTSICONS."ICONS FOR THE GAME.
*----------------------------------------------------------------------*
* GLOBAL VARIABLES
*----------------------------------------------------------------------*
DATA: v_turn(1) VALUE 'X',"Select card.
v_win(1) VALUE ' '."Win a player(X) or tie(-)!

*

*----------------------------------------------------------------------*
* SELECTION-SCREEN
*
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON (4) icon1 USER-COMMAND uc_icon1."1x1
SELECTION-SCREEN PUSHBUTTON (4) icon2 USER-COMMAND uc_icon2."2x1
SELECTION-SCREEN PUSHBUTTON (4) icon3 USER-COMMAND uc_icon3."3x1
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON (4) icon4 USER-COMMAND uc_icon4."1x2
SELECTION-SCREEN PUSHBUTTON (4) icon5 USER-COMMAND uc_icon5."2x2
SELECTION-SCREEN PUSHBUTTON (4) icon6 USER-COMMAND uc_icon6."3x2
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON (4) icon7 USER-COMMAND uc_icon7."1x3
SELECTION-SCREEN PUSHBUTTON (4) icon8 USER-COMMAND uc_icon8."2x3
SELECTION-SCREEN PUSHBUTTON (4) icon9 USER-COMMAND uc_icon9."3x3
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON (12) restart USER-COMMAND uc_restart.
SELECTION-SCREEN END OF LINE.

PERFORM clear_buttons. WHEN 'UC_ICON8'. PERFORM push_button USING icon9. PERFORM restart_game. WHEN 'UC_ICON1'. WHEN 'UC_ICON9'. icon1 = '@5F@' . icon9 = '@5F@' . PERFORM push_button USING icon3. WHEN 'UC_ICON2'. *----------------------------------------------------------------------* * INITIALIZATION * *----------------------------------------------------------------------* INITIALIZATION. icon3 = '@5F@' . PERFORM push_button USING icon5. WHEN 'UC_ICON4'. icon8 = '@5F@' . WHEN 'UC_ICON6'. *----------------------------------------------------------------------* * AT SELECTION-SCREEN *----------------------------------------------------------------------* AT SELECTION-SCREEN. *----------------------------------------------------------------------* FORM clear_buttons .SELECTION-SCREEN END OF BLOCK b1. CASE sy-ucomm. WHEN 'UC_ICON3'. icon2 = '@5F@' . *&---------------------------------------------------------------------* *& Form clear_buttons *&---------------------------------------------------------------------* * Clear the buttons of Selection-Screen. PERFORM push_button USING icon1. icon7 = '@5F@' . restart = 'Restart'. icon5 = '@5F@' . WHEN 'UC_ICON5'. PERFORM push_button USING icon8. WHEN 'UC_RESTART'. WHEN 'UC_ICON7'. icon4 = '@5F@' ." clear_buttons *&---------------------------------------------------------------------* * . ENDCASE. PERFORM push_button USING icon2. ENDFORM. PERFORM push_button USING icon4. PERFORM push_button USING icon6. icon6 = '@5F@' . PERFORM push_button USING icon7.

"Col 3 | v_win = 'X'. * Winner or tie? CASE v_win. ENDIF. ENDIF. IF v_turn IS INITIAL. * Exit if button pushed. v_win = '-'. EXIT.*& Form push_button *&---------------------------------------------------------------------* * Checks when a user push a button. * Check tie IF v_win IS INITIAL AND icon1 <> '@5F@' AND icon2 <> '@5F@' AND icon3 <> '@5F@' AND icon4 <> '@5F@' AND icon5 <> '@5F@' AND icon6 <> '@5F@' AND icon7 <> '@5F@' AND icon8 <> '@5F@' AND icon9 <> '@5F@'. p_icon = '@C9@'. v_turn = 'X'. IF p_icon <> '@5F@'. CLEAR v_turn. ELSE. * Winner . * Check winner IF ( icon1 = icon5 AND icon5 = icon9 AND icon1 <> '@5F@' )"Diagonal \ OR ( icon3 = icon5 AND icon5 = icon7 AND icon3 <> '@5F@' )"Diagonal / OR ( icon1 = icon2 AND icon2 = icon3 AND icon1 <> '@5F@' )"Row 1 OR ( icon4 = icon5 AND icon5 = icon6 AND icon4 <> '@5F@' )"Row 2 OR ( icon7 = icon8 AND icon8 = icon9 AND icon7 <> '@5F@' )"Row 3 OR ( icon1 = icon4 AND icon4 = icon7 AND icon1 <> '@5F@' )"Col 1 | OR ( icon2 = icon5 AND icon5 = icon8 AND icon2 <> '@5F@' )"Col 2 | OR ( icon3 = icon6 AND icon6 = icon9 AND icon3 <> '@5F@' ). * Change icon. ENDIF. ENDIF. p_icon = '@7C@'. *----------------------------------------------------------------------* * --> p_icon Button pushed *----------------------------------------------------------------------* FORM push_button USING p_icon.

CLEAR v_win. MESSAGE i398(00) WITH 'Tie!'." push_button *&---------------------------------------------------------------------* *& Form restart_game *&---------------------------------------------------------------------* * Restart the game." restart_game .WHEN 'X'. PERFORM restart_game. IF v_turn IS INITIAL. ENDCASE. *----------------------------------------------------------------------* FORM restart_game . v_turn = 'X'. ENDFORM. MESSAGE i398(00) WITH 'Win player 2!'. ENDIF. MESSAGE i398(00) WITH 'Win player 1!'. ELSE. ENDFORM. PERFORM clear_buttons. * Tie WHEN '-'. PERFORM restart_game. PERFORM restart_game.

Sign up to vote on this title
UsefulNot useful