report ztictactoe no standard page heading.
************************************************************************
* TYPES
************************************************************************
* build up table of possible, valid options
types:
begin of t_options,
key(1),
one(1),
two(1),
three(1),
four(1),
five(1),
six(1),
seven(1),
eight(1),
nine(1),
end of t_options.
* Build list of all possible numbers
types:
begin of t_possible,
number(2),
end of t_possible.
types:
begin of t_my_pick,
number(2),
end of t_my_pick.
types:
begin of t_your_pick,
number(2),
end of t_your_pick.
types:
begin of t_win_combo,
one(2),
two(2),
three(2),
end of t_win_combo.
types:
begin of t_best_for_me,
count type i,
importance type i,
best_number(2),
one(2),
two(2),
three(2),
end of t_best_for_me.
types:
begin of t_weighting,
number(2),
rank type i,
end of t_weighting.
************************************************************************
* DATA DECLARATIONS FOR INTERNAL TABLES
************************************************************************
data:
i_options type standard table of t_options,
wa_options type t_options,
i_possible type standard table of t_possible,
wa_possible type t_possible.
data:
i_my_pick type standard table of t_my_pick,
i_your_pick type standard table of t_your_pick,
i_win_combo type standard table of t_win_combo,
i_best_for_me type standard table of t_best_for_me,
i_best_for_you type standard table of t_best_for_me,
i_weighting type standard table of t_weighting,
wa_my_pick type t_my_pick,
wa_your_pick type t_your_pick,
wa_win_combo type t_win_combo,
wa_best_for_me type t_best_for_me,
wa_best_for_you type t_best_for_me,
wa_weighting type t_weighting.
************************************************************************
* DATA
************************************************************************
data:
best_save type i,
l_random type i,
once_only(1),
importance_save_me type i,
importance_save_you type i,
block(1),
my_lines like sy-dbcnt,
your_lines like sy-dbcnt,
total_lines like sy-dbcnt,
finish(1).
data:
p1(5),
p2(5),
p3(5),
p4(5),
p5(5),
p6(5),
p7(5),
p8(5),
p9(5).
data:
user_first(1) value ‘ ‘,
index(2) value ‘P5’,
cursor(5),
my_count type i,
your_count type i,
best(2),
best_number(2),
win_flag(1).
constants:
noughts(1) value ‘O’,
crosses(1) value ‘X’,
c_rank1 type i value ‘3’,
c_rank2 type i value ‘2’,
c_rank3 type i value ‘1’.
************************************************************************
* SETUP POSSIBLE VALUE MATRIX
************************************************************************
selection-screen begin of block main.
selection-screen pushbutton 15(25) p_head user-command us01.
selection-screen skip 1.
selection-screen pushbutton 15(25) p_tail user-command us02.
selection-screen end of block main.
************************************************************************
* INITIALIZATION
************************************************************************
initialization.
move ‘Heads’ to p_head.
move ‘Tails’ to p_tail.
************************************************************************
* PROCESSING
************************************************************************
************************************************************************
* AT SELECTION-SCREEN IS RUN FIRST
************************************************************************
at selection-screen.
check finish ne ‘X’.
check once_only = ‘ ‘.
once_only = ‘X’.
perform initialization.
* randomly pick heads or tails based on system time
check sy-ucomm eq ‘US01’ or sy-ucomm eq ‘US02’.
l_random = sy-uzeit mod 2.
if sy-ucomm eq ‘US01’.
if l_random eq 0.
p_head = ‘You Win’.
p_tail = ‘ ‘.
user_first = ‘X’.
else.
p_head = ‘You Lose’.
p_tail = ‘ ‘.
endif.
else.
if l_random eq 1.
p_tail = ‘You Win’.
p_head = ‘ ‘.
user_first = ‘X’.
else.
p_tail = ‘You Lose’.
p_head = ‘ ‘.
endif.
endif.
* Here work out, statistically what is the best number to have
perform get_best_number.
************************************************************************
* AT START-OF-SELECTION RUN SECOND
************************************************************************
start-of-selection.
skip 1.
check once_only = ‘X’.
if user_first eq ‘X’.
write:20 p1, sy-vline, p2, sy-vline, p3.
write:/20 sy-uline(21).
write:/20 p4, sy-vline, p5, sy-vline, p6.
write:/20 sy-uline(21).
write:/20 p7, sy-vline, p8, sy-vline, p9.
else.
perform write_screen using index
noughts
‘ ‘.
endif.
************************************************************************
* AT LINE SELECTION RUN FROM NOW ON
************************************************************************
at line-selection.
clear i_best_for_me[].
clear i_best_for_you[].
clear wa_best_for_me.
clear wa_best_for_you.
* Find where the user wishes to place their cross, this has to be the
* User
get cursor field cursor.
read table i_my_pick
into wa_my_pick
with key number = cursor.
if sy-subrc eq 0.
exit.
endif.
read table i_your_pick
into wa_your_pick
with key number = cursor.
if sy-subrc eq 0.
exit.
endif.
if cursor is initial.
exit.
endif.
perform write_screen using cursor
crosses
‘X’.
* Here we look at who has what against the winning combos
* What runs could I make, what runs could the user make
* rank the available numbers, then decide which is the best number
* for me or the user
loop at i_win_combo into wa_win_combo.
perform find_best_number using wa_win_combo-one.
perform find_best_number using wa_win_combo-two.
perform find_best_number using wa_win_combo-three.
case my_count.
when 0.
wa_best_for_me-importance = wa_best_for_me-importance * c_rank3.
when 1.
wa_best_for_me-importance = wa_best_for_me-importance * c_rank2.
when 2.
wa_best_for_me-importance = wa_best_for_me-importance * c_rank1.
endcase.
if my_count ge your_count.
move my_count to wa_best_for_me-count.
move wa_win_combo-one to wa_best_for_me-one.
move wa_win_combo-two to wa_best_for_me-two.
move wa_win_combo-three to wa_best_for_me-three.
append wa_best_for_me to i_best_for_me.
else.
move your_count to wa_best_for_you-count.
move wa_win_combo-one to wa_best_for_you-one.
move wa_win_combo-two to wa_best_for_you-two.
move wa_win_combo-three to wa_best_for_you-three.
append wa_best_for_you to i_best_for_you.
endif.
clear my_count.
clear your_count.
clear importance_save_me.
clear importance_save_you.
clear wa_best_for_me.
clear wa_best_for_you.
endloop.
sort i_best_for_me by count descending
importance descending.
sort i_best_for_you by count descending
importance descending.
delete i_best_for_me where importance is initial.
delete i_best_for_you where importance is initial.
read table i_best_for_me into wa_best_for_me index 1.
read table i_best_for_you into wa_best_for_you index 1.
* if both wa are initial then the end has come
* if we have two numbers and the user does not have the last
* put it in
if wa_best_for_me-count eq 2.
block = ‘X’.
win_flag = ‘X’.
endif.
if wa_best_for_you-count eq 2.
block = ‘X’.
endif.
if not block = ‘X’.
if wa_best_for_me-importance ge wa_best_for_you-importance.
* find which number they have put in
wait up to 2 seconds.
perform write_screen using wa_best_for_me-best_number
noughts
‘ ‘.
else.
wait up to 2 seconds.
perform write_screen using wa_best_for_you-best_number
noughts
‘ ‘.
endif.
else.
if win_flag = ‘X’.
* Computer has third of winning row and wins
wait up to 2 seconds.
perform write_screen using wa_best_for_me-best_number
noughts
‘ ‘.
write:/ ‘I win’.
finish = ‘X’.
else.
wait up to 2 seconds.
if not wa_best_for_you-best_number is initial.
perform write_screen using wa_best_for_you-best_number
noughts
‘ ‘.
else.
perform write_screen using wa_best_for_me-best_number
noughts
‘ ‘.
endif.
endif.
clear i_best_for_me[].
clear i_best_for_you[].
endif.
sort i_my_pick.
sort i_your_pick.
delete adjacent duplicates from i_my_pick.
delete adjacent duplicates from i_your_pick.
describe table i_my_pick lines my_lines.
describe table i_your_pick lines your_lines.
total_lines = my_lines + your_lines.
if total_lines ge 10.
write:/ ‘A Draw’.
finish = ‘X’.
endif.
*&———————————————————————*
*& Form write_screen
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM write_screen using cursor
marker
process.
case cursor.
when ‘P1’.
p1 = marker.
when ‘P2’.
p2 = marker.
when ‘P3’.
p3 = marker.
when ‘P4’.
p4 = marker.
when ‘P5’.
p5 = marker.
when ‘P6’.
p6 = marker.
when ‘P7’.
p7 = marker.
when ‘P8’.
p8 = marker.
when ‘P9’.
p9 = marker.
endcase.
if marker eq crosses.
move cursor to wa_your_pick.
append wa_your_pick to i_your_pick.
clear wa_your_pick.
else.
move cursor to wa_my_pick.
append wa_my_pick to i_my_pick.
clear wa_my_pick.
endif.
check process is initial.
skip 1.
write:20 p1, sy-vline, p2, sy-vline, p3.
write:/20 sy-uline(21).
write:/20 p4, sy-vline, p5, sy-vline, p6.
write:/20 sy-uline(21).
write:/20 p7, sy-vline, p8, sy-vline, p9.
ENDFORM. ” write_screen
*eject
*&———————————————————————*
*& Form initialization
*&———————————————————————*
FORM initialization.
* build vertical index agains horizontal columns
* fill win combo.
move ‘P1’ to wa_win_combo-one.
move ‘P2’ to wa_win_combo-two.
move ‘P3’ to wa_win_combo-three.
append wa_win_combo to i_win_combo.
clear wa_win_combo.
move ‘P4’ to wa_win_combo-one.
move ‘P5’ to wa_win_combo-two.
move ‘P6’ to wa_win_combo-three.
append wa_win_combo to i_win_combo.
clear wa_win_combo.
move ‘P7’ to wa_win_combo-one.
move ‘P8’ to wa_win_combo-two.
move ‘P9’ to wa_win_combo-three.
append wa_win_combo to i_win_combo.
clear wa_win_combo.
move ‘P1’ to wa_win_combo-one.
move ‘P4’ to wa_win_combo-two.
move ‘P7’ to wa_win_combo-three.
append wa_win_combo to i_win_combo.
clear wa_win_combo.
move ‘P2’ to wa_win_combo-one.
move ‘P5’ to wa_win_combo-two.
move ‘P8’ to wa_win_combo-three.
append wa_win_combo to i_win_combo.
clear wa_win_combo.
move ‘P3’ to wa_win_combo-one.
move ‘P6’ to wa_win_combo-two.
move ‘P9’ to wa_win_combo-three.
append wa_win_combo to i_win_combo.
clear wa_win_combo.
move ‘P1’ to wa_win_combo-one.
move ‘P5’ to wa_win_combo-two.
move ‘P9’ to wa_win_combo-three.
append wa_win_combo to i_win_combo.
clear wa_win_combo.
move ‘P7’ to wa_win_combo-one.
move ‘P5’ to wa_win_combo-two.
move ‘P3’ to wa_win_combo-three.
append wa_win_combo to i_win_combo.
clear wa_win_combo.
* fill weighting table
move ‘P5’ to wa_weighting-number.
move ‘9’ to wa_weighting-rank.
append wa_weighting to i_weighting.
clear wa_weighting.
move ‘P3’ to wa_weighting-number.
move ‘8’ to wa_weighting-rank.
append wa_weighting to i_weighting.
clear wa_weighting.
move ‘P7’ to wa_weighting-number.
move ‘7’ to wa_weighting-rank.
append wa_weighting to i_weighting.
clear wa_weighting.
move ‘P9’ to wa_weighting-number.
move ‘6’ to wa_weighting-rank.
append wa_weighting to i_weighting.
clear wa_weighting.
move ‘P1’ to wa_weighting-number.
move ‘5’ to wa_weighting-rank.
append wa_weighting to i_weighting.
clear wa_weighting.
move ‘P2’ to wa_weighting-number.
move ‘4’ to wa_weighting-rank.
append wa_weighting to i_weighting.
clear wa_weighting.
move ‘P8’ to wa_weighting-number.
move ‘3’ to wa_weighting-rank.
append wa_weighting to i_weighting.
clear wa_weighting.
move ‘P6’ to wa_weighting-number.
move ‘2’ to wa_weighting-rank.
append wa_weighting to i_weighting.
clear wa_weighting.
move ‘P4’ to wa_weighting-number.
move ‘1’ to wa_weighting-rank.
append wa_weighting to i_weighting.
clear wa_weighting.
* 1
write ‘1’ to wa_options-key.
write ‘ ‘ to wa_options-one.
write ‘1’ to wa_options-two.
write ‘1’ to wa_options-three.
write ‘1’ to wa_options-four.
write ‘1’ to wa_options-five.
write ‘ ‘ to wa_options-six.
write ‘1’ to wa_options-seven.
write ‘ ‘ to wa_options-eight.
write ‘1’ to wa_options-nine.
append wa_options to i_options.
clear wa_options.
* 2
write ‘2’ to wa_options-key.
write ‘1’ to wa_options-one.
write ‘ ‘ to wa_options-two.
write ‘1’ to wa_options-three.
write ‘ ‘ to wa_options-four.
write ‘1’ to wa_options-five.
write ‘ ‘ to wa_options-six.
write ‘ ‘ to wa_options-seven.
write ‘1’ to wa_options-eight.
write ‘ ‘ to wa_options-nine.
append wa_options to i_options.
clear wa_options.
* 3
write ‘3’ to wa_options-key.
write ‘1’ to wa_options-one.
write ‘1’ to wa_options-two.
write ‘ ‘ to wa_options-three.
write ‘ ‘ to wa_options-four.
write ‘1’ to wa_options-five.
write ‘1’ to wa_options-six.
write ‘1’ to wa_options-seven.
write ‘ ‘ to wa_options-eight.
write ‘1’ to wa_options-nine.
append wa_options to i_options.
clear wa_options.
* 4
write ‘4’ to wa_options-key.
write ‘1’ to wa_options-one.
write ‘ ‘ to wa_options-two.
write ‘ ‘ to wa_options-three.
write ‘ ‘ to wa_options-four.
write ‘1’ to wa_options-five.
write ‘1’ to wa_options-six.
write ‘1’ to wa_options-seven.
write ‘ ‘ to wa_options-eight.
write ‘ ‘ to wa_options-nine.
append wa_options to i_options.
clear wa_options.
* 5
write ‘5’ to wa_options-key.
write ‘1’ to wa_options-one.
write ‘1’ to wa_options-two.
write ‘1’ to wa_options-three.
write ‘1’ to wa_options-four.
write ‘ ‘ to wa_options-five.
write ‘1’ to wa_options-six.
write ‘1’ to wa_options-seven.
write ‘1’ to wa_options-eight.
write ‘1’ to wa_options-nine.
append wa_options to i_options.
clear wa_options.
* 6
write ‘6’ to wa_options-key.
write ‘ ‘ to wa_options-one.
write ‘ ‘ to wa_options-two.
write ‘1’ to wa_options-three.
write ‘1’ to wa_options-four.
write ‘1’ to wa_options-five.
write ‘ ‘ to wa_options-six.
write ‘ ‘ to wa_options-seven.
write ‘ ‘ to wa_options-eight.
write ‘1’ to wa_options-nine.
append wa_options to i_options.
clear wa_options.
* 7
write ‘7’ to wa_options-key.
write ‘1’ to wa_options-one.
write ‘ ‘ to wa_options-two.
write ‘1’ to wa_options-three.
write ‘1’ to wa_options-four.
write ‘1’ to wa_options-five.
write ‘ ‘ to wa_options-six.
write ‘ ‘ to wa_options-seven.
write ‘1’ to wa_options-eight.
write ‘1’ to wa_options-nine.
append wa_options to i_options.
clear wa_options.
* 8
write ‘8’ to wa_options-key.
write ‘ ‘ to wa_options-one.
write ‘1’ to wa_options-two.
write ‘ ‘ to wa_options-three.
write ‘ ‘ to wa_options-four.
write ‘1’ to wa_options-five.
write ‘ ‘ to wa_options-six.
write ‘1’ to wa_options-seven.
write ‘ ‘ to wa_options-eight.
write ‘1’ to wa_options-nine.
append wa_options to i_options.
clear wa_options.
* 9
write ‘9’ to wa_options-key.
write ‘1’ to wa_options-one.
write ‘ ‘ to wa_options-two.
write ‘1’ to wa_options-three.
write ‘ ‘ to wa_options-four.
write ‘1’ to wa_options-five.
write ‘1’ to wa_options-six.
write ‘1’ to wa_options-seven.
write ‘1’ to wa_options-eight.
write ‘ ‘ to wa_options-nine.
append wa_options to i_options.
clear wa_options.
* Initialization all possible numbers
write ‘P1’ to wa_possible-number.
append wa_possible to i_possible.
write ‘P2’ to wa_possible-number.
append wa_possible to i_possible.
write ‘P3’ to wa_possible-number.
append wa_possible to i_possible.
write ‘P4’ to wa_possible-number.
append wa_possible to i_possible.
write ‘P5’ to wa_possible-number.
append wa_possible to i_possible.
write ‘P6’ to wa_possible-number.
append wa_possible to i_possible.
write ‘P7’ to wa_possible-number.
append wa_possible to i_possible.
write ‘P8’ to wa_possible-number.
append wa_possible to i_possible.
write ‘P9’ to wa_possible-number.
append wa_possible to i_possible.
ENDFORM. ” initialization
*eject
*&———————————————————————*
*& Form get_best_number
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM get_best_number.
do 9 times.
loop at i_possible into wa_possible.
read table i_options
into wa_options
with key key = wa_possible-number.
best = wa_options-one +
wa_options-two +
wa_options-three +
wa_options-four +
wa_options-five +
wa_options-six +
wa_options-seven +
wa_options-eight +
wa_options-nine .
if best gt best_save.
best_save = best.
index = wa_possible-number.
endif.
clear wa_possible.
clear best.
endloop.
enddo.
* write index if it is my go first
if user_first = ‘X’.
perform write_screen using ‘ ‘
‘ ‘
‘ ‘.
else.
perform write_screen using index
noughts
‘ ‘.
endif.
ENDFORM. ” get_best_number
*eject
*&———————————————————————*
*& Form find_best_number
*&———————————————————————*
* text
*———————————————————————-*
* –>P_WA_WIN_COMBO_ONE text
*———————————————————————-*
FORM find_best_number USING WA_WIN_COMBO.
* Look at number 1 from win combo possible
read table i_my_pick
into wa_my_pick
with key number = wa_win_combo.
* Do I already have it
if sy-subrc eq 0.
my_count = my_count + 1.
else.
* If not check if the user has it
read table i_your_pick
into wa_your_pick
with key number = wa_win_combo.
if sy-subrc ne 0.
read table i_weighting
into wa_weighting
with key number = wa_win_combo.
wa_best_for_me-importance = wa_weighting-rank +
wa_best_for_me-importance.
if wa_weighting-rank ge importance_save_me.
importance_save_me = wa_weighting-rank.
wa_best_for_me-best_number = wa_win_combo.
endif.
wa_best_for_you-importance = wa_weighting-rank +
wa_best_for_you-importance.
if wa_weighting-rank ge importance_save_you.
importance_save_you = wa_weighting-rank.
wa_best_for_you-best_number = wa_win_combo.
endif.
else.
your_count = your_count + 1.
endif.
endif.
ENDFORM. ” find_best_number