Tic Tac Toe - 1.2 Game in ABAP

Title  : Tic Tac Toe - 1.2 Game in ABAP

Author : Jaideep Srinivasan  
Submitted : 11/14/2007

Description

Here the main objective of this program is to develop a game in abap.The game is called as "Tic Tac Toe" which can be played against a computer.I have used simple procedures to write this program.This is the first version of code and may not be a efficient one.I have used selection screen concepts for this game. In my next version i will soon add advanced AI functionality.Let us start developing more games in SAP!!!!

Demo Screen :
  
The Code Sample :
REPORT ZGAME.

TABLES SSCRFIELDS.

DATA: FLAG, count(10) type c ,

f1,f2,f3,f4,f5,f6,f7,f8,f9,

p1(2) type c,

p2(2) type c,

player(2) type c,

ch type c.

SELECTION-SCREEN BEGIN OF BLOCK rad1

WITH FRAME TITLE text-002 no intervals.

PARAMETERS Message(10) TYPE c .

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN:

BEGIN OF LINE,

PUSHBUTTON 2(5) BUT1 USER-COMMAND CLI1 MODIF ID sc1,

PUSHBUTTON 7(5) BUT2 USER-COMMAND CLI2 MODIF ID sc2,

PUSHBUTTON 12(5) BUT3 USER-COMMAND CLI3 MODIF ID sc3,

END OF LINE,

BEGIN OF LINE,

PUSHBUTTON 2(5) BUT4 USER-COMMAND CLI4 MODIF ID sc4,

PUSHBUTTON 7(5) BUT5 USER-COMMAND CLI5 MODIF ID sc5,

PUSHBUTTON 12(5) BUT6 USER-COMMAND CLI6 MODIF ID sc6,

END OF LINE,

BEGIN OF LINE,

PUSHBUTTON 2(5) BUT7 USER-COMMAND CLI7 MODIF ID sc7,

PUSHBUTTON 7(5) BUT8 USER-COMMAND CLI8 MODIF ID sc8,

PUSHBUTTON 12(5) BUT9 USER-COMMAND CLI9 MODIF ID sc9,

END OF LINE.

SELECTION-SCREEN END OF BLOCK rad1.

\* DISPLAYING THE SCORE FILED AS A LABEL

\*disanling process*******************************\*

form box_disable using ch.

LOOP AT SCREEN.

IF screen-group1 = ch.

screen-input = '0'.

MODIFY SCREEN.

ENDIF.

ENDLOOP.

endform. "box_disable

AT SELECTION-SCREEN OUTPUT.

if f1 = 1.

perform box_disable using 'SC1'.

endif.

if f2 = 1.

perform box_disable using 'SC2'.

endif.

if f3 = 1.

perform box_disable using 'SC3'.

endif.

if f4 = 1.

perform box_disable using 'SC4'.

endif.

if f5 = 1.

perform box_disable using 'SC5'.

endif.

if f6 = 1.

perform box_disable using 'SC6'.

endif.

if f7 = 1.

perform box_disable using 'SC7'.

endif.

if f8 = 1.

perform box_disable using 'SC8'.

endif.

if f9 = 1.

perform box_disable using 'SC9'.

endif.

AT SELECTION-SCREEN .

CASE SSCRFIELDS.

WHEN 'CLI1'.

count = count + 1.

perform player_turn.

BUT1 = p1.

f1 = '1'.

perform computer_move.

WHEN 'CLI2'.

count = count + 1.

perform player_turn.

BUT2 = p1.

f2 = '1'.

perform computer_move.

WHEN 'CLI3'.

count = count + 1.

perform player_turn.

BUT3 = p1.

f3 = '1'.

perform computer_move.

WHEN 'CLI4'.

count = count + 1.

perform player_turn.

BUT4 = p1.

f4 = '1'.

perform computer_move.

WHEN 'CLI5'.

count = count + 1.

perform player_turn.

BUT5 = p1.

f5 = '1'.

perform computer_move.

WHEN 'CLI6'.

count = count + 1.

perform player_turn.

BUT6 = p1.

f6 = '1'.

perform computer_move.

WHEN 'CLI7'.

count = count + 1.

perform player_turn.

BUT7 = p1.

f7 = '1'.

perform computer_move.

WHEN 'CLI8'.

count = count + 1.

perform player_turn.

BUT8 = p1.

f8 = '1'.

perform computer_move.

WHEN 'CLI9'.

count = count + 1.

perform player_turn.

BUT9 = p1.

f9 = '1'.

perform computer_move.

ENDCASE.

\*this is for toggle operation p1 = X ; p2 = O ;

form player_turn.

player = count MOD 2.

\*BUT2 = player.

if player = 1.

\*BUT4 = 'player1'.

p1 = 'X'.

else.

\*BUT4 = 'player2'.

p1 = 'O'.

endif.

endform. "player_turn

\*&---------------------------------------------------------------------\*

\*& Form computer_move

\*&---------------------------------------------------------------------\*

\* text

\*----------------------------------------------------------------------\*

form computer_move.

if count = 9.

Message = 'DRAW'.

perform reset.

endif.

if count = 7.

perform fourth_move.

endif.

if count = 5.

perform third_move.

endif.

if count = 3.

perform second_move.

endif.

if count = 1.

perform first_move.

endif.

perform who_wins.

endform. "computer_move

\*computer - O

form first_move.

if but5 <> 'X'.

but5 = 'O'.

else.

BUT9 = 'O'.

endif.

count = count + 1.

endform. "first_move

\*&---------------------------------------------------------------------\*

\*& Form second_move

\*&---------------------------------------------------------------------\*

\* text

\*----------------------------------------------------------------------\*

form second_move.

data str1 type c.

str1 = 'X'.

perform check_possible using 'X'.

if count = 3.

perform misc_move.

endif.

if count = 3.

perform empty_fill.

endif.

endform. "second_move

\*&---------------------------------------------------------------------\*

\*& Form check_possible

\*&---------------------------------------------------------------------\*

\* text

\*----------------------------------------------------------------------\*

\* \-->STR text

\*----------------------------------------------------------------------\*

form check_possible using str type c.

\*checking for rows

\*1st row \--3rd col empty

if but1 = str and but2 = str and but3 = ' '.

but3 = 'O'.

count = count + 1.

endif.

\*2ndrow

if but4 = str and but5 = str and but6 = ' '.

but6 = 'O'.

count = count + 1.

endif.
{color:#0000ff}\*3rd row{color}
if but7 = str and but8 = str and but9 = ' '.

but9 = 'O'.

count = count + 1.

endif.
{color:#0000ff}\*1st row \--2nd col empty{color}
if but1 = str and but2 = ' ' and but3 = str.

but2 = 'O'.

count = count + 1.

endif.
{color:#0000ff}\*2ndrow{color}
if but4 = str and but5 = ' ' and but6 = str.

but5 = 'O'.

count = count + 1.

endif.
{color:#0000ff}\*3rd row{color}
if but7 = str and but8 = ' ' and but9 = str.

but8 = 'O'.

count = count + 1.

endif.
{color:#0000ff}\*1st row \--1st col empty{color}
if but1 = ' ' and but2 = str and but3 = str.

but1 = 'O'.

count = count + 1.

endif.

\*2ndrow

if but4 = ' ' and but5 = str and but6 = str.

but4 = 'O'.

count = count + 1.

endif.

\*3rd row

if but7 = ' ' and but8 = str and but9 = str.

but7 = 'O'.

count = count + 1.

endif.

\*check for columns

\*1st col \--3rd col empty

if but1 = str and but4 = str and but7 = ' '.

but7 = 'O'.

count = count + 1.

endif.

\*2nd col

if but2 = str and but5 = str and but8 = ' '.

but8 = 'O'.

count = count + 1.

endif.

\*3rd col

if but3 = str and but6 = str and but9 = ' '.

but9 = 'O'.

count = count + 1.

endif.

\*1st col \--2nd col empty

if but1 = str and but4 = ' ' and but7 = str.

but4 = 'O'.

count = count + 1.

endif.

\*2nd col

if but2 = str and but5 = ' ' and but8 = str.

but5 = 'O'.

count = count + 1.

endif.

\*3rd col

if but3 = str and but6 = ' ' and but9 = str.

but6 = 'O'.

count = count + 1.

endif.

\*1st col \--1st col empty

if but1 = ' ' and but4 = str and but7 = str.

but1 = 'O'.

count = count + 1.

endif.

\*2nd col

if but2 = ' ' and but5 = str and but8 = str.

but2 = 'O'.

count = count + 1.

endif.

\*3rd col

if but3 = ' ' and but6 = str and but9 = str.

but3 = 'O'.

count = count + 1.

endif.

\*diagonal

\*left-top

if but1 = ' ' and but5 = str and but9 = str.

but1 = 'O'.

count = count + 1.

endif.

if but1 = str and but5 = str and but9 = ' '.

but9 = 'O'.

count = count + 1.

endif.

\*right-top

if but3 = ' ' and but5 = str and but7 = str.

but3 = 'O'.

count = count + 1.

endif.

if but3 = str and but5 = str and but7 = ' '.

but7 = 'O'.

count = count + 1.

endif.

endform. "check_possible
{color:#0000ff}\*&---------------------------------------------------------------------\*{color}

{color:#0000ff}\*& Form misc_move{color}

{color:#0000ff}\*&---------------------------------------------------------------------\*{color}

{color:#0000ff}\* text{color}

{color:#0000ff}\*----------------------------------------------------------------------\*{color}
form misc_move.

if but6 = 'X' and but8 = 'X'.

but9 = 'O'.

count = count + 1.

endif.

if but1 = 'X' and but9 = 'X'.

but2 = 'O'.

count = count + 1.

endif.

if but3 = 'X' and but7 = 'X'.

but2 = 'O'.

count = count + 1.

endif.

if but1 = 'X' and but5 = 'X'.

but7 = 'O'.

count = count + 1.

endif.

if but1 = 'X' and but8 = 'X'.

but7 = 'O'.

count = count + 1.

endif.

if but3 = 'X' and but8 = 'X'.

but9 = 'O'.

count = count + 1.

endif.

if but6 = 'X' and but7 = 'X'.

but9 = 'O'.

count = count + 1.

endif.

if but4 = 'X' and but6 = 'X'.

but1 = 'O'.

count = count + 1.

endif.
{color:#0000ff}\*----------------------------misc--last updated trial \-error{color}
if but2 = 'X' and but8 = 'X'.

but1 = 'O'.

count = count + 1.

exit .

endif.

if but2 = 'X' and but7 = 'X'.

but9 = 'O'.

count = count + 1.

endif.

if but2 = 'X' and but9 = 'X'.

but7 = 'O'.

count = count + 1.

endif.

if but4 = 'X' and but8 = 'X'.

but1 = 'O'.

count = count + 1.

endif.

if but1 = 'X' and but6 = 'X'.

but3 = 'O'.

count = count + 1.

endif.

if but2 = 'X' and but6 = 'X'.

but9 = 'O'.

count = count + 1.

endif.

if but2 = 'X' and but4 = 'X'.

but3 = 'O'.

count = count + 1.

endif.

if but4 = 'X' and but9 = 'X'.

but7 = 'O'.

count = count + 1.

endif.

if but4 = 'X' and but3 = 'X'.

but1 = 'O'.

count = count + 1.

endif.

endform. "misc_move

\*&---------------------------------------------------------------------\*

\*& Form empty_fill

\*&---------------------------------------------------------------------\*

\* text

\*----------------------------------------------------------------------\*

form empty_fill.

if but1 <> 'X' and but1 <> 'O'.

but1 = 'O'.

count = count + 1.

elseif but2 <> 'X' and but2 <> 'O'.

but2 = 'O'.

count = count + 1.

elseif but3 <> 'X' and but3 <> 'O'.

but3 = 'O'.

count = count + 1.

elseif but4 <> 'X' and but4 <> 'O'.

but4 = 'O'.

count = count + 1.

elseif but5 <> 'X' and but5 <> 'O'.

but5 = 'O'.

count = count + 1.

elseif but6 <> 'X' and but6 <> 'O'.

but6 = 'O'.

count = count + 1.

elseif but7 <> 'X' and but7 <> 'O'.

but7 = 'O'.

count = count + 1.

elseif but8 <> 'X' and but8 <> 'O'.

but8 = 'O'.

count = count + 1.

elseif but9 <> 'X' and but9 <> 'O'.

but9 = 'O'.

count = count + 1.

endif.

endform. "empty_fill

\*&---------------------------------------------------------------------\*

\*& Form third_move

\*&---------------------------------------------------------------------\*

\* text

\*----------------------------------------------------------------------\*

form third_move.

perform check_possible using 'O'.

if count = 5.

perform check_possible using 'X'.

endif.

if count = 5.

perform empty_fill.

endif.

endform. "third_move

\*&---------------------------------------------------------------------\*

\*& Form fourth_move

\*&---------------------------------------------------------------------\*

\* text

\*----------------------------------------------------------------------\*

form fourth_move.

perform check_possible using 'O'.

if count = 7.

perform check_possible using 'X'.

endif.

if count = 7.

perform empty_fill.

endif.

endform. "fourth_move

\*check for win ,reset-------------------------------------------------------------------------------\-

\*&---------------------------------------------------------------------\*

\*& Form check_x_win

\*&---------------------------------------------------------------------\*

\* text

\*----------------------------------------------------------------------\*

form check_x_win.
{color:#0000ff}\* checking of the rows{color}
data a type i.

if but1 = 'X' AND but2 = 'X' AND but3 = 'X'.

Message = ' X wins '.

perform reset.

endif.

if but4 = 'X' AND but5 = 'X' AND but6 = 'X'.

Message = ' X wins '.

perform reset.

endif.

if but7 = 'X' AND but8 = 'X' AND but9 = 'X'.

Message = ' X wins '.

perform reset.

endif.
{color:#0000ff}\* checking of the cols{color}
if but1 = 'X' AND but4 = 'X' AND but7 = 'X'.

Message = ' X wins '.

perform reset.

endif.

if but2 = 'X' AND but5 = 'X' AND but8 = 'X'.

Message = ' X wins '.

perform reset.

endif.

if but3 = 'X' AND but6 = 'X' AND but9 = 'X'.

Message = ' X wins '.

perform reset.

endif.

\* checking for diagonal

if but1 = 'X' AND but5 = 'X' AND but9 = 'X'.

Message = ' X wins '.

perform reset.

endif.

if but3 = 'X' AND but5 = 'X' AND but7 = 'X'.

Message = ' X wins '.

perform reset.

endif.

endform. "check_x_win

\*&---------------------------------------------------------------------\*

\*& Form check_o_win

\*&---------------------------------------------------------------------\*

\* text

\*----------------------------------------------------------------------\*

form check_o_win.

\* checking of the rows

if but1 = 'O' AND but2 = 'O' AND but3 = 'O'.

Message = ' O wins '.

perform reset.

endif.

if but4 = 'O' AND but5 = 'O' AND but6 = 'O'.

Message = ' O wins '.

perform reset.

endif.

if but7 = 'O' AND but8 = 'O' AND but9 = 'O'.

Message = ' O wins '.

perform reset.

endif.

\* checking of the cols

if but1 = 'O' AND but4 = 'O' AND but7 = 'O'.

Message = 'O wins '.

perform reset.

endif.

if but2 = 'O' AND but5 = 'O' AND but8 = 'O'.

Message = ' O wins '.

perform reset.

endif.

if but3 = 'O' AND but6 = 'O' AND but9 = 'O'.

Message = ' O wins '.

perform reset.

endif.

\* checking for diagonal

if but1 = 'O' AND but5 = 'O' AND but9 = 'O'.

Message = ' O wins '.

perform reset.

endif.

if but3 = 'O' AND but5 = 'O' AND but7 = 'O'.

Message = ' O wins '.

perform reset.

endif.

endform. "check_o_win
{color:#0000ff}\*&---------------------------------------------------------------------\*{color}

{color:#0000ff}\*& Form who_wins{color}

{color:#0000ff}\*&---------------------------------------------------------------------\*{color}

{color:#0000ff}\* text{color}

{color:#0000ff}\*----------------------------------------------------------------------\*{color}
form who_wins.

perform check_x_win.

perform check_o_win.

endform. "who_wins
{color:#0000ff}\*&---------------------------------------------------------------------\*{color}

{color:#0000ff}\*& Form reset{color}

{color:#0000ff}\*&---------------------------------------------------------------------\*{color}

{color:#0000ff}\* text{color}

{color:#0000ff}\*----------------------------------------------------------------------\*{color}
form reset.

BUT1 = ' '.

BUT2 = ' '.

BUT3 = ' '.

BUT4 = ' '.

BUT5 = ' '.

BUT6 = ' '.

BUT7 = ' '.

BUT8 = ' '.

BUT9 = ' '.

f1 = ' '.

f2 =' '.

f3 = ' '.

f4 =' '.

f5 = ' '.

f6 =' '.

f7 = ' '.

f8 =' '.

f9 = ' '.
{color:#0000ff}\* Message = ' '.{color}

{color:#0000ff}\* Score = ' '.{color}
count = 0.

endform. "reset

initialization.

count = 0.

BUT1 = p1.

BUT2 = p1.

BUT3 = p1.

BUT4 = p1.

BUT5 = p1.

BUT6 = p1.

BUT7 = p1.

BUT8 = p1.

BUT9 = p1.

Labels

games games Delete
abap abap Delete
tic-tac-toe tic-tac-toe Delete
tutorial tutorial Delete
game-examples game-examples Delete
snippet snippet Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.