1 *&---------------------------------------------------------------------* 2 *& Report ZCHENH087 3 *& 4 *&---------------------------------------------------------------------* 5 *& 6 *& 7 *&---------------------------------------------------------------------* 8 * 来自汪子熙的Github :https://github.com/i042416/KnowlegeRepository/blob/master/ABAP/SmallApp/072_game.abap 9 REPORT zchenh087. 10 INCLUDE <icon>. 11 12 CONSTANTS: 13 " >> board cell values 14 blank_hidden TYPE c VALUE '0', 15 blank_marked TYPE c VALUE 'm', 16 blank_opened TYPE c VALUE '.', 17 18 bomb_hidden TYPE c VALUE '*', 19 bomb_marked TYPE c VALUE 'M', 20 bomb_opened TYPE c VALUE '&', 21 22 endgame_bomb_boom TYPE c VALUE 'X', 23 endgame_bomb_missmark TYPE c VALUE '@', 24 border TYPE c VALUE '#', 25 26 " >> game state 27 game_in VALUE '1', 28 game_over VALUE '2', 29 game_win VALUE '3'. 30 31 DATA: 32 board(9999) TYPE c, " 2D board, x_size * y_size + borders 33 ofs TYPE i, " board[ofs] = cell unique ID 34 min TYPE i, " board[min] .. board[max] 35 max TYPE i, 36 rdx TYPE i, " = 2 + width of board 37 rdy TYPE i, " = 2 + height of board 38 square TYPE i, " = x_size * y_size = visible area 39 square2 TYPE i, " = rdx * rdy = visible area + border 40 range TYPE i, " = max - min + 1 41 rest TYPE i, " = square - bomb_cnt = empty cells to invent 42 game TYPE c, " gamestate = 1,2,3 43 game_size TYPE c, " B=Beginner, I=Interm, E=Expert, C=Custom 44 game_time(5) TYPE c, " seconds 45 b_left(4) TYPE c. " unmarked bombs left 46 47 " >> eight directions: North, South, East, West, NE, NW, SE, SW 48 DATA: eight_directions TYPE TABLE OF i INITIAL SIZE 8 WITH HEADER LINE . 49 50 " >> cells2update list, to track board[] changes 51 TYPES: 52 BEGIN OF celltype, 53 offset(4) TYPE c, 54 color TYPE c, 55 END OF celltype. 56 DATA: cells2update TYPE TABLE OF celltype INITIAL SIZE 1000 WITH HEADER LINE. 57 58 " >> misc 59 CONSTANTS: 60 x_ofs TYPE i VALUE 1, 61 y_ofs TYPE i VALUE 5. 62 DATA: 63 game_time1 TYPE timestamp, " game begin 64 game_time2 TYPE timestamp. 65 66 " >> high_scores 67 CONSTANTS: 68 database_id_prefix(21) TYPE c VALUE 'ABAPMINESWEEPERSCORES'. 69 TYPES: 70 BEGIN OF score_line, 71 user LIKE sy-uname, 72 time(5) TYPE c, 73 END OF score_line. 74 75 DATA: 76 high_scores TYPE SORTED TABLE OF score_line 77 WITH UNIQUE KEY time WITH HEADER LINE, 78 database_id LIKE indx-srtfd. " export/import to database ID 79 80 " >> game difficulty 81 SELECTION-SCREEN BEGIN OF BLOCK bl_game_difficulty. 82 83 SELECTION-SCREEN: BEGIN OF LINE. 84 SELECTION-SCREEN: COMMENT (23) hstitle1. 85 SELECTION-SCREEN: END OF LINE. 86 87 SELECTION-SCREEN: BEGIN OF LINE. 88 PARAMETERS g1 RADIOBUTTON GROUP one 89 USER-COMMAND radiogroup01. 90 SELECTION-SCREEN: COMMENT (10) text1. 91 SELECTION-SCREEN: END OF LINE. 92 93 SELECTION-SCREEN: BEGIN OF LINE. 94 PARAMETERS g2 RADIOBUTTON GROUP one. 95 SELECTION-SCREEN: COMMENT (12) text2. 96 SELECTION-SCREEN: END OF LINE. 97 98 SELECTION-SCREEN: BEGIN OF LINE. 99 PARAMETERS g3 RADIOBUTTON GROUP one. 100 SELECTION-SCREEN: COMMENT (10) text3. 101 SELECTION-SCREEN: END OF LINE. 102 103 SELECTION-SCREEN: BEGIN OF LINE. 104 PARAMETERS g4 RADIOBUTTON GROUP one. 105 SELECTION-SCREEN: COMMENT (10) text4. 106 SELECTION-SCREEN: END OF LINE. 107 108 SELECTION-SCREEN: END OF BLOCK bl_game_difficulty. 109 110 " >> High Scores table 111 SELECTION-SCREEN BEGIN OF BLOCK bl_high_scores. 112 113 SELECTION-SCREEN: BEGIN OF LINE. 114 SELECTION-SCREEN: COMMENT (1) dummy1. 115 SELECTION-SCREEN: END OF LINE. 116 117 SELECTION-SCREEN: BEGIN OF LINE. 118 SELECTION-SCREEN: COMMENT (23) hstitle2. 119 SELECTION-SCREEN: END OF LINE. 120 121 DEFINE displ_hsline. 122 selection-screen: begin of line. 123 selection-screen: comment (1) hs1_&1. 124 selection-screen: comment (12) hs2_&1. 125 selection-screen: comment (5) hs3_&1. 126 selection-screen: end of line. 127 END-OF-DEFINITION. 128 129 SELECTION-SCREEN ULINE /1(20). 130 displ_hsline 0. 131 SELECTION-SCREEN ULINE /1(20). 132 displ_hsline 1. 133 displ_hsline 2. 134 displ_hsline 3. 135 displ_hsline 4. 136 displ_hsline 5. 137 displ_hsline 6. 138 displ_hsline 7. 139 displ_hsline 8. 140 displ_hsline 9. 141 142 SELECTION-SCREEN: END OF BLOCK bl_high_scores. 143 144 " >> Window: Custom Game Dimensions 145 146 SELECTION-SCREEN BEGIN OF SCREEN 1001. 147 148 SELECTION-SCREEN: BEGIN OF LINE. 149 SELECTION-SCREEN: COMMENT (12) txtcustw. 150 PARAMETERS: x_size TYPE i DEFAULT '09'. 151 152 SELECTION-SCREEN: END OF LINE. 153 154 SELECTION-SCREEN: BEGIN OF LINE. 155 SELECTION-SCREEN: COMMENT (12) txtcusth. 156 PARAMETERS: y_size TYPE i DEFAULT '09'. 157 SELECTION-SCREEN: END OF LINE. 158 159 SELECTION-SCREEN: BEGIN OF LINE. 160 SELECTION-SCREEN: COMMENT (12) txtcustb. 161 PARAMETERS: bomb_cnt TYPE i DEFAULT '10'. 162 SELECTION-SCREEN: END OF LINE. 163 164 SELECTION-SCREEN END OF SCREEN 1001. 165 166 " >> modify board[ofs] and track changes 167 DEFINE setcell. 168 board+ofs(1) = &1. 169 170 cells2update-offset = ofs. 171 cells2update-color = &1. 172 append cells2update. 173 174 if game_time1 is initial. 175 get time stamp field game_time1. 176 endif. 177 END-OF-DEFINITION. 178 179 *----------------------------------------------------------------------* 180 * CLASS cl_my_gui_html_viewer DEFINITION 181 *----------------------------------------------------------------------* 182 * Custom HTML contol 183 *----------------------------------------------------------------------* 184 CLASS cl_my_gui_html_viewer DEFINITION INHERITING FROM 185 cl_gui_html_viewer. 186 PUBLIC SECTION. 187 METHODS: constructor IMPORTING parent TYPE REF TO cl_gui_container. 188 METHODS: html_board_update. 189 PRIVATE SECTION. 190 METHODS: on_sapevent FOR EVENT sapevent OF cl_gui_html_viewer 191 IMPORTING action query_table. 192 DATA: js TYPE STANDARD TABLE OF char255 INITIAL SIZE 1000. 193 194 ENDCLASS. "cl_my_gui_html_viewer DEFINITION 195 196 DATA: 197 lo_dock TYPE REF TO cl_gui_docking_container, 198 lo_cont TYPE REF TO cl_gui_container, 199 html_control TYPE REF TO cl_my_gui_html_viewer, 200 wnd_style TYPE i. 201 202 INITIALIZATION. 203 hstitle1 = ' :: GAME DIFFICULTY ::'. 204 205 text1 = 'Beginner'. 206 text2 = 'Intermediate'. 207 text3 = 'Expert'. 208 text4 = 'Custom'. 209 210 txtcustw = 'Width'. 211 txtcusth = 'Height'. 212 txtcustb = 'Bombs'. 213 214 hstitle2 = ' :: HIGH SCORES ::'. 215 hs1_0 = '#'. 216 hs2_0 = 'user'. 217 hs3_0 = 'time'. 218 219 hs1_1 = '1'. 220 hs1_2 = '2'. 221 hs1_3 = '3'. 222 hs1_4 = '4'. 223 hs1_5 = '5'. 224 hs1_6 = '6'. 225 hs1_7 = '7'. 226 hs1_8 = '8'. 227 hs1_9 = '9'. 228 229 " >> create controls 230 wnd_style = cl_gui_control=>ws_thickframe + cl_gui_control=>ws_child. 231 232 CREATE OBJECT lo_dock 233 EXPORTING 234 repid = sy-cprog 235 dynnr = sy-dynnr 236 ratio = 80 237 side = cl_gui_docking_container=>dock_at_right 238 name = 'DOCK_CONT' 239 style = wnd_style. 240 241 lo_cont = lo_dock. 242 243 CREATE OBJECT html_control 244 EXPORTING 245 parent = lo_cont. 246 247 " >> 248 PERFORM game_create USING 'B' x_size y_size bomb_cnt. 249 250 PERFORM html_load_gifs. 251 252 PERFORM html_show. 253 254 AT LINE-SELECTION. " abap-mode, cmd 'PICK' 255 ADD -1 TO sy-lsind. 256 257 IF sy-lisel CS 'switch'. 258 LEAVE LIST-PROCESSING. 259 ELSE. 260 CHECK game = game_in. 261 262 PERFORM cell_get_clicked CHANGING ofs. 263 CHECK ofs > 0. 264 265 PERFORM cell_open USING ofs. 266 IF rest <= 0 AND game = game_in. 267 268 GET TIME STAMP FIELD game_time2. 269 game_time = game_time2 - game_time1. 270 271 PERFORM game_ok. 272 ENDIF. 273 PERFORM game_print_abap. 274 275 ENDIF. 276 277 AT PF09. " abap-mode, cmd 'MARK' 278 ADD -1 TO sy-lsind. 279 IF game = game_in. 280 PERFORM cell_get_clicked CHANGING ofs. 281 CHECK ofs > 0. 282 PERFORM cell_mark USING ofs. 283 PERFORM game_print_abap. 284 ENDIF. 285 286 AT SELECTION-SCREEN OUTPUT. 287 IF lines( cells2update ) > 0. 288 " here: switch back from abap to html 289 CALL METHOD html_control->html_board_update( ). 290 ENDIF. 291 292 AT SELECTION-SCREEN ON RADIOBUTTON GROUP one. 293 CHECK sy-ucomm = 'RADIOGROUP01'. 294 IF g1 = 'X'. 295 PERFORM game_create USING 'B' 09 09 10. 296 ELSEIF g2 = 'X'. 297 PERFORM game_create USING 'I' 16 16 40. 298 ELSEIF g3 = 'X'. 299 PERFORM game_create USING 'E' 30 16 99. 300 ELSE. 301 CALL SELECTION-SCREEN 1001 302 STARTING AT 20 4. 303 PERFORM game_create USING 'C' x_size y_size bomb_cnt. 304 ENDIF. 305 306 PERFORM html_show. 307 308 *&---------------------------------------------------------------------* 309 *& Form game_print_abap. 310 *&---------------------------------------------------------------------* 311 FORM game_print_abap. 312 DATA: ofs TYPE i, ch TYPE c. 313 314 SKIP TO LINE 3. 315 316 WRITE: / 'Bombs left: ', b_left NO-ZERO. 317 318 ofs = rdx. 319 SKIP TO LINE y_ofs. 320 321 DO y_size TIMES. 322 WRITE AT x_ofs '|' NO-GAP. 323 324 DO x_size TIMES. 325 ADD 1 TO ofs. 326 ch = board+ofs(1). 327 CASE ch. 328 WHEN blank_opened. 329 WRITE: ' '. 330 331 WHEN '1' OR '2' OR '3' OR '4' OR '5' OR '6' OR '7' OR '8'. 332 WRITE: ' ' NO-GAP, ch NO-GAP. 333 334 WHEN bomb_marked. 335 WRITE icon_breakpoint AS ICON NO-GAP. 336 337 WHEN blank_marked 338 OR 'a' OR 'b' OR 'c' OR 'd' OR 'e' OR 'f' OR 'g' OR 'h'. 339 WRITE icon_breakpoint AS ICON NO-GAP. 340 341 WHEN endgame_bomb_missmark. 342 WRITE icon_breakpoint_disable AS ICON NO-GAP. 343 344 WHEN endgame_bomb_boom. 345 WRITE icon_system_cancel AS ICON NO-GAP. 346 347 WHEN bomb_opened. "endgame only 348 WRITE icon_dummy AS ICON NO-GAP. 349 350 WHEN OTHERS. 351 WRITE: icon_wd_transparent_container AS ICON NO-GAP. 352 ENDCASE. 353 ENDDO. 354 WRITE '|'. 355 ADD 2 TO ofs. 356 NEW-LINE. 357 ENDDO. 358 WRITE: AT x_ofs '' NO-GAP, ' switch back ' COLOR 2 HOTSPOT ON. 359 360 IF game = game_over. 361 WRITE: /, /4 'Game over', /, /. 362 ELSEIF game = game_win. 363 WRITE: /, /4 'You win', /, /. 364 ELSE. 365 SKIP 3. 366 ENDIF. 367 WRITE: / ' open: double-click'. 368 WRITE: / ' mark: click and press F9'. 369 ENDFORM. "game_print_abap 370 371 *&---------------------------------------------------------------------* 372 *& Form CELL_MARK 373 *&---------------------------------------------------------------------* 374 * mark a cell with 'bomb sign' 375 *----------------------------------------------------------------------* 376 FORM cell_mark USING value(ofs) TYPE i. 377 DATA: ch TYPE c. 378 ch = board+ofs(1). 379 CASE ch. 380 WHEN blank_hidden. setcell blank_marked. ADD -1 TO b_left. 381 WHEN blank_marked. setcell blank_hidden. ADD +1 TO b_left. 382 WHEN bomb_hidden. setcell bomb_marked. ADD -1 TO b_left. 383 WHEN bomb_marked. setcell bomb_hidden. ADD +1 TO b_left. 384 385 WHEN 'A' OR 'B' OR 'C' OR 'D' OR 'E' OR 'F' OR 'G' OR 'H'. 386 TRANSLATE ch TO LOWER CASE. 387 setcell ch. ADD -1 TO b_left. 388 389 WHEN 'a' OR 'b' OR 'c' OR 'd' OR 'e' OR 'f' OR 'g' OR 'h'. 390 TRANSLATE ch TO UPPER CASE. 391 setcell ch. ADD +1 TO b_left. 392 393 ENDCASE. 394 ENDFORM. "CELL_MARK 395 396 *&---------------------------------------------------------------------* 397 *& Form cell_open 398 *&---------------------------------------------------------------------* 399 * open a cell, at one's own risk 400 *----------------------------------------------------------------------* 401 FORM cell_open USING value(ofs) TYPE i. 402 CASE board+ofs(1). 403 WHEN blank_hidden. 404 PERFORM cell_floodfill USING ofs. 405 WHEN 'A'. setcell '1'. ADD -1 TO rest. 406 WHEN 'B'. setcell '2'. ADD -1 TO rest. 407 WHEN 'C'. setcell '3'. ADD -1 TO rest. 408 WHEN 'D'. setcell '4'. ADD -1 TO rest. 409 WHEN 'E'. setcell '5'. ADD -1 TO rest. 410 WHEN 'F'. setcell '6'. ADD -1 TO rest. 411 WHEN 'G'. setcell '7'. ADD -1 TO rest. 412 WHEN 'H'. setcell '8'. ADD -1 TO rest. 413 WHEN '1' OR '2' OR '3' OR '4' OR '5' OR '6' OR '7' OR '8'. 414 PERFORM cell_open_around USING ofs. 415 WHEN bomb_hidden. 416 setcell endgame_bomb_boom. 417 PERFORM game_lose. 418 ENDCASE. 419 ENDFORM. "cell_open 420 421 *&---------------------------------------------------------------------* 422 *& Form cell_get_clicked 423 *&---------------------------------------------------------------------* 424 FORM cell_get_clicked CHANGING ofs TYPE i. 425 DATA: row TYPE i, col TYPE i. 426 row = sy-curow - y_ofs. 427 col = ( sy-cucol - x_ofs - 2 ) DIV 2. 428 ofs = ( 1 + row ) * rdx + col + 1. 429 IF row < 0 OR row > y_size OR 430 col < 0 OR col > x_size. 431 ofs = 0. 432 ENDIF. 433 ENDFORM. "cell_get_clicked 434 435 DATA: floodfill TYPE TABLE OF i INITIAL SIZE 1000. 436 437 *&---------------------------------------------------------------------* 438 *& Form cell_flood_fill 439 *&---------------------------------------------------------------------* 440 * open all adjacent empty cells 441 *----------------------------------------------------------------------* 442 FORM cell_floodfill USING value(x) TYPE i. 443 DATA: ofs TYPE i. 444 445 ofs = x + 00. " cell itself 446 setcell blank_opened. ADD -1 TO rest. 447 448 APPEND x TO floodfill. 449 LOOP AT floodfill INTO x. 450 LOOP AT eight_directions. 451 ofs = x + eight_directions. 452 453 CASE board+ofs(1). 454 WHEN blank_hidden. 455 setcell blank_opened. ADD -1 TO rest. 456 APPEND ofs TO floodfill. 457 WHEN 'A'. setcell '1'. ADD -1 TO rest. 458 WHEN 'B'. setcell '2'. ADD -1 TO rest. 459 WHEN 'C'. setcell '3'. ADD -1 TO rest. 460 WHEN 'D'. setcell '4'. ADD -1 TO rest. 461 WHEN 'E'. setcell '5'. ADD -1 TO rest. 462 WHEN 'F'. setcell '6'. ADD -1 TO rest. 463 WHEN 'G'. setcell '7'. ADD -1 TO rest. 464 WHEN 'H'. setcell '8'. ADD -1 TO rest. 465 ENDCASE. 466 ENDLOOP. 467 ENDLOOP. 468 REFRESH floodfill. 469 ENDFORM. "cell_flood_fill 470 471 *&---------------------------------------------------------------------* 472 *& Form cell_open_eight_directions 473 *&---------------------------------------------------------------------* 474 * Open up to 8 cells around current one 475 *----------------------------------------------------------------------* 476 FORM cell_open_around USING value(x) TYPE i. 477 DATA: ofs TYPE i. 478 DATA: value TYPE i. 479 480 " 1 >> get cell's VALUE (1 to 481 value = board+x(1). 482 483 " 2 >> look around it, get count of marked 484 LOOP AT eight_directions. 485 ofs = x + eight_directions. 486 CASE board+ofs(1). 487 WHEN bomb_marked 488 OR blank_marked 489 OR 'a' " digit_maked 490 OR 'b' " .. 491 OR 'c' 492 OR 'd' 493 OR 'e' 494 OR 'f' 495 OR 'g' 496 OR 'h'. 497 value = value - 1. 498 ENDCASE. 499 ENDLOOP. 500 501 " 3 >> check its EQness 502 CHECK value = 0. 503 504 " 4 >> here: opening is possible 505 506 " 5 >> do open 507 LOOP AT eight_directions. 508 ofs = x + eight_directions. 509 CASE board+ofs(1). 510 WHEN blank_hidden. 511 PERFORM cell_floodfill USING ofs. 512 513 WHEN 'A'. setcell '1'. ADD -1 TO rest. 514 WHEN 'B'. setcell '2'. ADD -1 TO rest. 515 WHEN 'C'. setcell '3'. ADD -1 TO rest. 516 WHEN 'D'. setcell '4'. ADD -1 TO rest. 517 WHEN 'E'. setcell '5'. ADD -1 TO rest. 518 WHEN 'F'. setcell '6'. ADD -1 TO rest. 519 WHEN 'G'. setcell '7'. ADD -1 TO rest. 520 WHEN 'H'. setcell '8'. ADD -1 TO rest. 521 522 WHEN blank_marked 523 OR 'a' 524 OR 'b' 525 OR 'c' 526 OR 'd' 527 OR 'e' 528 OR 'f' 529 OR 'g' 530 OR 'h'. 531 setcell endgame_bomb_missmark. 532 533 WHEN bomb_hidden. 534 setcell endgame_bomb_boom. 535 PERFORM game_lose. 536 ENDCASE. 537 538 ENDLOOP. 539 ENDFORM. "cell_open_eight_directions 540 541 *&---------------------------------------------------------------------* 542 *& Form game_lose 543 *&--------------------------------------------------------------------- 544 FORM game_lose. 545 546 game = game_over. 547 ADD -1 TO b_left. 548 " >> show actual bombs 549 WHILE board(square2) CA '*abcdefghm'. " digit_marked + blank_marked 550 ofs = sy-fdpos. 551 IF board+ofs(1) = bomb_hidden. " if = '*' 552 setcell bomb_opened. 553 ELSE. 554 setcell endgame_bomb_missmark. 555 ENDIF. 556 ENDWHILE. 557 558 MESSAGE s000(su) WITH 'Game over'. 559 ENDFORM. "game_lose 560 561 *&---------------------------------------------------------------------* 562 *& Form game_ok 563 *&---------------------------------------------------------------------* 564 FORM game_ok. 565 566 game = game_win. 567 b_left = 0. 568 " >> show bombs 569 WHILE board(square2) CS bomb_hidden. 570 ofs = sy-fdpos. 571 setcell bomb_marked. 572 ENDWHILE. 573 574 MESSAGE s000(su) WITH 'You win!' . 575 576 CHECK game_size <> 'C'. 577 578 PERFORM high_scores_read. 579 PERFORM high_scores_update. 580 PERFORM high_scores_show. 581 582 ENDFORM. "game_ok 583 584 *&---------------------------------------------------------------------* 585 *& Form game_create 586 *&---------------------------------------------------------------------* 587 FORM game_create USING 588 value(sz) TYPE c 589 value(x) TYPE i 590 value(y) TYPE i 591 value(b) TYPE i. 592 593 DATA: 594 ofs TYPE i, 595 j TYPE i, 596 drop_cnt TYPE i, 597 drop_bombs TYPE c. 598 DATA: 599 prng TYPE REF TO cl_abap_random_int, 600 backgr TYPE c, 601 foregr TYPE c, 602 half TYPE i. 603 604 game_size = sz. 605 x_size = x. 606 y_size = y. 607 bomb_cnt = b. 608 609 CONCATENATE database_id_prefix game_size INTO database_id. 610 611 CLEAR: 612 game_time, 613 game_time1, 614 game_time2. 615 616 game = game_in. 617 618 PERFORM high_scores_read. 619 PERFORM high_scores_show. 620 621 CLEAR: board. 622 623 square = x_size * y_size. 624 IF bomb_cnt > square. 625 bomb_cnt = square. " limit bombs to 100% 626 ENDIF. 627 b_left = bomb_cnt. 628 629 rest = square - bomb_cnt. " empty places to invent 630 631 rdx = 2 + x_size. " width + left and right border 632 rdy = 2 + y_size. " height + top and button border 633 634 square2 = rdx * rdy. 635 636 IF square2 > 9999. 637 MESSAGE e000(su) WITH 'board too large (9999)'. 638 ENDIF. 639 640 " >> board[9999]'s used space 641 min = 1 + 1 * rdx. " topleft 642 max = square2 - rdx - 2. " bottomright 643 range = max - min + 1. 644 645 " >> directions 646 REFRESH eight_directions. 647 eight_directions = -1 + rdx * -1. APPEND eight_directions. " NW 648 eight_directions = 00 + rdx * -1. APPEND eight_directions. " North 649 eight_directions = +1 + rdx * -1. APPEND eight_directions. " NE 650 eight_directions = -1 + rdx * 00. APPEND eight_directions. " W 651 eight_directions = +1 + rdx * 00. APPEND eight_directions. " E 652 eight_directions = -1 + rdx * +1. APPEND eight_directions. " SW 653 eight_directions = 00 + rdx * +1. APPEND eight_directions. " S 654 eight_directions = 01 + rdx * +1. APPEND eight_directions. " SE 655 656 " >> bomb placement 657 658 " >> speed optimization: 659 " if bombs < 50%, place bombs randomly 660 " if bombs > 50%, place spaces randomly 661 662 half = x_size * y_size DIV 2. 663 IF bomb_cnt < half. 664 drop_bombs = 'X'. " straight order 665 drop_cnt = bomb_cnt. 666 backgr = blank_hidden. 667 foregr = bomb_hidden. 668 ELSE. 669 drop_bombs = ' '. " reversed order 670 drop_cnt = rest. 671 backgr = bomb_hidden. 672 foregr = blank_hidden. 673 ENDIF. 674 675 " >> fill background 676 ofs = min. 677 DO range TIMES. 678 board+ofs(1) = backgr. 679 ADD 1 TO ofs. 680 ENDDO. 681 682 " >> horizontal border 683 DO rdx TIMES. 684 685 ofs = sy-index - 1. 686 board+ofs(1) = border. 687 688 ofs = square2 - sy-index . 689 board+ofs(1) = border. 690 ENDDO. 691 " >> vertical border 692 DO y_size TIMES. 693 ofs = rdx * sy-index. 694 board+ofs(1) = border. 695 ofs = sy-index * rdx + rdx - 1. 696 board+ofs(1) = border. 697 ENDDO. 698 699 " >> actual placement 700 DATA: seed TYPE i. 701 seed = cl_abap_random=>seed( ). 702 703 prng = cl_abap_random_int=>create( 704 seed = seed min = min max = max ). 705 706 DO drop_cnt TIMES. 707 DO. 708 ofs = prng->get_next( ). 709 710 CASE board+ofs(1). 711 WHEN foregr OR border. 712 " skip used cells 713 WHEN OTHERS. 714 EXIT. " found unused (BG 1 2 3 4 5 6 7 715 ENDCASE. 716 ENDDO. 717 718 board+ofs(1) = foregr. 719 720 IF drop_bombs = 'X'. 721 " add 1 point to cells around 722 LOOP AT eight_directions. 723 j = ofs + eight_directions. 724 CASE board+j(1). 725 WHEN bomb_hidden OR border. 726 727 WHEN OTHERS. 728 board+j(1) = board+j(1) + 1. 729 ENDCASE. 730 ENDLOOP. 731 ELSE. 732 " get 1 point from every bomb around 733 LOOP AT eight_directions. 734 j = ofs + eight_directions. 735 CASE board+j(1). 736 WHEN bomb_hidden. 737 board+ofs(1) = board+ofs(1) + 1. 738 ENDCASE. 739 ENDLOOP. 740 ENDIF. 741 ENDDO. 742 743 " >> hide digits 744 TRANSLATE board(square2) USING '1A2B3C4D5E6F7G8H'. 745 746 ENDFORM. "game_create 747 748 *----------------------------------------------------------------------* 749 * CLASS my_cl_gui_html_viewer IMPLEMENTATION 750 *----------------------------------------------------------------------* 751 * 752 *----------------------------------------------------------------------* 753 CLASS cl_my_gui_html_viewer IMPLEMENTATION. 754 755 METHOD constructor. 756 DATA: myevent_tab TYPE cntl_simple_events, 757 myevent TYPE cntl_simple_event. 758 759 CALL METHOD super->constructor( 760 parent = parent 761 uiflag = html_control->uiflag_noiemenu 762 ). 763 764 myevent-appl_event = 'X'. 765 myevent-eventid = html_control->m_id_sapevent. 766 APPEND myevent TO myevent_tab. 767 myevent-eventid = html_control->m_id_navigate_complete. 768 APPEND myevent TO myevent_tab. 769 770 CALL METHOD html_control->set_registered_events( 771 events = myevent_tab 772 ). 773 774 SET HANDLER me->on_sapevent FOR html_control. 775 776 ENDMETHOD. "constructor 777 778 " >> HTML callback event 779 METHOD on_sapevent. " arguments: action, query_table[] 780 DATA: param LIKE LINE OF query_table. 781 DATA: cell_ofs TYPE i. 782 783 READ TABLE query_table WITH KEY name = 'ID' INTO param. 784 IF sy-subrc EQ 0. cell_ofs = param-value. ENDIF. 785 786 READ TABLE query_table WITH KEY name = 'TIME' INTO param. 787 IF sy-subrc EQ 0. game_time = param-value. ENDIF. 788 789 CASE action. 790 791 WHEN 'click'. " left-click 792 CHECK game = game_in. 793 PERFORM cell_open USING cell_ofs. 794 IF rest <= 0 AND game = game_in. 795 PERFORM game_ok. 796 ENDIF. 797 CALL METHOD me->html_board_update( ). 798 799 WHEN 'mouseup'. " right-click 800 CHECK game = game_in. 801 PERFORM cell_mark USING cell_ofs. 802 CALL METHOD me->html_board_update( ). 803 804 WHEN 'newgame'. 805 PERFORM game_create USING game_size x_size y_size bomb_cnt. 806 PERFORM html_show. 807 808 WHEN 'switch'. 809 LEAVE TO LIST-PROCESSING. 810 SET PF-STATUS space. 811 PERFORM game_print_abap. 812 ENDCASE. 813 814 ENDMETHOD. "on_sapevent 815 816 " >> transport board[]'s changes (saved in cells2update[]), to HTML 817 METHOD html_board_update. 818 DATA: ofs TYPE i, new TYPE i, len TYPE i, end TYPE i. 819 DATA: buf TYPE string. 820 DATA: jsline LIKE LINE OF js. 821 822 " >> convert cells2update[] to plain string 823 CONCATENATE LINES OF cells2update INTO buf SEPARATED BY '|'. 824 CONDENSE buf NO-GAPS. 825 826 " >> convert plain string to JavaScript code 827 " and pack it to HTML_VIEWER's compatible table 828 ofs = 0. 829 end = strlen( buf ). 830 831 WHILE ofs < end. 832 new = ofs + 249. " 255 - strlen ( b+=""; ) is 249 833 834 IF new > end. " 835 len = end - ofs. 836 ELSE. 837 len = 249. 838 ENDIF. 839 840 CONCATENATE 'b+="' buf+ofs(len) '";' INTO jsline. 841 APPEND jsline TO js. 842 843 ofs = ofs + len. 844 ENDWHILE. 845 846 CONCATENATE 'updateCells(' game ',"' b_left+1 '")' INTO jsline. 847 APPEND jsline TO js. 848 849 " >> actual transfer 850 CALL METHOD me->set_script( script = js[] ). 851 CALL METHOD me->execute_script( ). 852 853 " >> 854 REFRESH js. 855 REFRESH cells2update. 856 857 ENDMETHOD. "html_board_update 858 859 ENDCLASS. "cl_my_gui_html_viewer IMPLEMENTATION 860 861 *&---------------------------------------------------------------------* 862 *& Form html_create 863 *&---------------------------------------------------------------------* 864 * text 865 *----------------------------------------------------------------------* 866 * -->S text 867 *----------------------------------------------------------------------* 868 FORM html_create TABLES html. 869 DATA: html_str TYPE string, s TYPE string, 870 table1 TYPE TABLE OF char255 WITH HEADER LINE INITIAL SIZE 150. 871 872 IF 1 = 2. 873 874 " CALL FUNCTION 'WS_UPLOAD' 875 " EXPORTING 876 " filename = 'C:.html' 877 " filetype = 'ASC' 878 " TABLES 879 " data_tab = html[] 880 " EXCEPTIONS 881 " OTHERS = 1. 882 " CHECK sy-subrc EQ 0. 883 " 884 " CONCATENATE LINES OF html INTO html_str SEPARATED BY '~'. 885 886 ELSE. 887 888 " >> create html 889 CONCATENATE 890 891 '<html><head>' 892 '<meta http-equiv="content-type" content="text/html">' 893 '<style type="text/css">' 894 '* { font: bold 12px sans-serif}' 895 'span { position: absolute; width: 16px; height: 16px;' 896 ' font-size: 6pt; background: url("blank.gif")}' 897 'div { background: silver; margin: 8px 0;' 898 ' text-align: center; border: 2px inset white}' 899 'td { text-align: center; padding: 0}' 900 'td div{height: 24px; width: 30px; margin: 0;' 901 ' padding: 3px; border: 1px gray solid}' 902 '#wMain{position: absolute; padding: 0 8px;' 903 ' border: 2px outset white}' 904 '#wTool{height: 34px}' 905 '</style>' 906 907 '<script>' 908 'var DX=10, DY=10, BB=98,' 909 ' game=1, tID, tm=0, mouse_left=0, mouse_right=2,' 910 ' images=[], SZ=16, x, y, b="",' 911 ' imagesInfo = [' 912 ' "blank *0ABCDEFGH",' 913 914 ' "bombdeath X", "bombrevealed &",' 915 ' "bombmisflagged @", "bombflagged mMabcdefgh",' 916 917 ' "open1 1", "open2 2", "open3 3",' 918 ' "open4 4", "open5 5", "open6 6",' 919 ' "open7 7", "open8 8", "open0 ."' 920 '];' 921 922 '/*VARIABLES_INITIALIZATION_HERE_DONT_DELETE*/' 923 924 'function init(){' 925 ' c1.innerText = BB;' 926 927 ' c2.onclick = abapHandler("IMG" , mouse_left, "newgame");' 928 ' document.onclick = abapHandler("SPAN", mouse_left, "click");' 929 ' document.onmouseup= abapHandler("SPAN", mouse_right,"mouseup");' 930 931 ' var p, w = DX*SZ+4; var h = DY*SZ+4;' 932 ' setSize(wBrd, w, h);' 933 ' setSize(wMain, w+20);' 934 ' map(imagesInfo, function(x){' 935 ' map((p = x.split(" "))[1].split(""),' 936 ' function(ch){images[ch]=p[0]+".gif"})}' 937 ' )' 938 '}' 939 940 'function updateCells(game_state, bomb_left){' 941 ' game = game_state; c1.innerText = bomb_left;' 942 943 ' var x, i, seq = b.split("|"); b = "";' 944 ' for(i=0;i<seq.length;i++)' 945 ' if (x=seq[i]) with(document.getElementById("x"+x.slice(0,-1)))' 946 ' style.backgroundImage = "url("+images[x.slice(-1)]+")";' 947 ' if (game==2) {clearTimeout(tID); c2.src = "facedead.gif"}' 948 ' if (game==3) {clearTimeout(tID); c2.src = "facewin.gif" }' 949 '}' 950 951 'function abapHandler(tag, mouse_button, action){' 952 ' return(' 953 ' function(){' 954 ' var ev = window.event, obj = ev.srcElement;' 955 ' if (obj.nodeName == tag && ev.button==mouse_button) {' 956 ' tID = tID ? tID : setTimeout("onTimer()", 1000);' 957 ' callback.href =' 958 ' "SAPEVENT:"+action+"?ID="+obj.id.slice(1)+"&TIME="+tm;' 959 ' callback.click();' 960 ' }' 961 ' }' 962 ' )' 963 '}' 964 965 'function onTimer(){' 966 ' c3.innerText = ++tm;' 967 ' tID = (game==1) ? setTimeout("onTimer()", 1000) : tID;' 968 '}' 969 970 'function setSize(obj, w, h){' 971 ' obj.style.width = w;' 972 ' if (h) obj.style.height = h;' 973 '}' 974 975 'function map(list, func){' 976 ' var a = new Array(list.length);' 977 ' for (var i=0;i<list.length;i++) a[i]=func(list[i],i);' 978 ' return a;' 979 '}' 980 '</script></head>' 981 982 '<body onload="init()">' 983 '<div id=wMain>' 984 '<div id=wTool><table border=0 id=wHdr>' 985 '<tr>' 986 ' <td width=34px><div id=c1>0</div></td>' 987 ' <td width=100%><img id=c2 src="facesmile.gif"></td>' 988 ' <td width=34px><div id=c3>0</div></td>' 989 '</tr></table></div>' 990 '<div id=wBrd>' 991 '<script>' 992 ' for(y=0;y<DY;y++) for(x=0;x<DX;x++) document.write(' 993 ' "<span id=x"+((y+1)*(DX+2)+x+1)+" style=left:"' 994 ' +(10+SZ*x)+"px;top:"+(52+SZ*y)+"px;></span>")' 995 '</script>' 996 '</div>' 997 '<div><a id=btnSwitch href="SAPEVENT:switch?0">' 998 'switch to ABAP</a>' 999 '</div></div>' 1000 '<a id=callback style="display:none"' 1001 ' href="SAPEVENT:click?dummy">SAPEVENT:click</a>' 1002 '</body>' 1003 '</html>' 1004 1005 INTO html_str SEPARATED BY '~'. 1006 1007 ENDIF. 1008 1009 CONDENSE html_str. 1010 1011 " >> patch html with game parameters 1012 DATA: xstr(4) TYPE c, ystr(4) TYPE c. 1013 WRITE x_size TO xstr NO-GROUPING. 1014 WRITE y_size TO ystr NO-GROUPING. 1015 1016 CONCATENATE 'DX=' xstr ',DY=' ystr ',BB="' b_left '";' INTO s. 1017 1018 REPLACE FIRST OCCURRENCE OF 1019 '/*VARIABLES_INITIALIZATION_HERE_DONT_DELETE*/' 1020 IN html_str WITH s. 1021 1022 IF sy-subrc <> 0. 1023 MESSAGE e000(su) WITH 'html template is broken'. 1024 ENDIF. 1025 1026 " >> performance improvement: condense html to width 255, 1027 " to reduce whitespaces sent to client 1028 1029 SPLIT html_str AT '~' INTO TABLE table1. 1030 1031 DATA: ofs TYPE i, len TYPE i, nex TYPE i. 1032 1033 REFRESH html. 1034 ofs = 0. 1035 LOOP AT table1. 1036 len = strlen( table1 ). 1037 CHECK len > 0. 1038 nex = ofs + len. 1039 1040 IF nex < 255. 1041 html+ofs(len) = table1. 1042 ofs = nex. 1043 ELSE. 1044 APPEND html. 1045 html = table1. 1046 ofs = len. 1047 ENDIF. 1048 ENDLOOP. 1049 APPEND html. 1050 1051 ENDFORM. "html_create 1052 1053 *&---------------------------------------------------------------------* 1054 *& Form load_graphics 1055 *&---------------------------------------------------------------------* 1056 * create GIF-images from scratch 1057 *----------------------------------------------------------------------* 1058 FORM html_load_gifs. 1059 1060 PERFORM html_load_gif USING 'open0.gif' 1061 'R0lGODlhEAAQAIAAAHt7e729vSH5BAAAAAAALAAAAAAQABAAA' 1062 'AIdhI9pwe0PnnRxzmphlniz7oGbmJGWeXmU2qAcyxQAOw==' 1063 '' '' '' ''. 1064 1065 PERFORM html_load_gif USING 'open1.gif' 1066 'R0lGODlhEAAQAJEAAAAA/3t7e729vQAAACH5BAAAAAAALAAAA' 1067 'AAQABAAAAIpjI9pwu0fnnRRAiCruxg+3lEbmFlX+Z1eGjZaw6' 1068 'EMGLsjLU7Tq9v9UwAAOw==' '' '' ''. 1069 1070 PERFORM html_load_gif USING 'open2.gif' 1071 'R0lGODlhEAAQAJEAAAB7AHt7e729vQAAACH5BAAAAAAALAAAA' 1072 'AAQABAAAAItjI9pwu0fnnSRgYsvtbm3ijkhU02jUIon+ngbt3' 1073 '4wMJFzR9sCnsm1lPrphI0CADs=' '' '' ''. 1074 1075 PERFORM html_load_gif USING 'open3.gif' 1076 'R0lGODlhEAAQAJEAAHt7e729vf8AAAAAACH5BAAAAAAALAAAA' 1077 'AAQABAAAAIshI9pwe0PnnQxiIsxZbmLVk0aE0pjUFrd5niseI' 1078 'ETF5O0V6O3K89S6tMFHQUAOw==' '' '' ''. 1079 1080 PERFORM html_load_gif USING 'open4.gif' 1081 'R0lGODlhEAAQAJEAAAAAe3t7e729vQAAACH5BAAAAAAALAAAA' 1082 'AAQABAAAAIujI9pwu0fnnSxgSsuoE5n3FTfNnKMV4anxoJC1b' 1083 'bqhDLiU79Tau/5vZsFTcNGAQA7' '' '' ''. 1084 1085 PERFORM html_load_gif USING 'open5.gif' 1086 'R0lGODlhEAAQAJEAAHsAAHt7e729vQAAACH5BAAAAAAALAAAA' 1087 'AAQABAAAAIpjI9pwu0fnnRRgItzq7mDzWATaF0jw3kmqYro6Q' 1088 'rVFKcte5MntUO9UwAAOw==' '' '' ''. 1089 1090 PERFORM html_load_gif USING 'open6.gif' 1091 'R0lGODlhEAAQAJEAAAB7e3t7e729vQAAACH5BAAAAAAALAAAA' 1092 'AAQABAAAAIsjI9pwu0fnnSRgYsvtTlvgU1NFYoC2ZUMmmorBz' 1093 'gqK7sn3I5NG+sm9AMGGwUAOw==' '' '' ''. 1094 1095 PERFORM html_load_gif USING 'open7.gif' 1096 'R0lGODlhEAAQAJEAAAAAAHt7e729vQAAACH5BAAAAAAALAAAA' 1097 'AAQABAAAAIqjI9pwu0fnnRRgItzq7mDPTEYGI5MJZlneVGTuq' 1098 'at+8CxYwtoSPO9zygAADs=' '' '' ''. 1099 1100 PERFORM html_load_gif USING 'open8.gif' 1101 'R0lGODlhEAAQAIAAAHt7e729vSH5BAAAAAAALAAAAAAQABAAA' 1102 'AIphI9pwe0PnnSRqdXqPdliXwXaJ37hRmVXWoZt96onuFpywk' 1103 '6Sq8O9UwAAOw==' '' '' ''. 1104 1105 PERFORM html_load_gif USING 'blank.gif' 1106 'R0lGODlhEAAQAJEAAHt7e729vf///wAAACH5BAAAAAAALAAAA' 1107 'AAQABAAAAIqlI8ZyRdggpxUAiiqfnjXG3kTmIlWZ3KhSaZqya' 1108 'LxKrYpyF36ruf8DygAADs=' '' '' ''. 1109 1110 PERFORM html_load_gif USING 'bombdeath.gif' 1111 'R0lGODlhEAAQAJEAAAAAAHt7e/8AAP///yH5BAAAAAAALAAAA' 1112 'AAQABAAAAI0jI9pwu0fHgNSREnlFRR4zzUb933O5g1DmVlNup' 1113 'YiydbuVIMMmrdjfnrRQL5KK4ipjCqOAgA7' '' '' ''. 1114 1115 PERFORM html_load_gif USING 'bombflagged.gif' 1116 'R0lGODlhEAAQAKIAAAAAAHt7e729vf8AAP///wAAAAAAAAAAA' 1117 'CH5BAAAAAAALAAAAAAQABAAAAM8SLrc0nCJoIS92AZK8hjZVl' 1118 'nfF3JkCWJil5osisFXmwnAOWJ5vOOAoM8VLBY1MqMR+bsxJ5u' 1119 'oVAqdWjcJADs=' '' ''. 1120 1121 PERFORM html_load_gif USING 'bombmisflagged.gif' 1122 'R0lGODlhEAAQAKIAAAAAAHt7e729vf8AAP///wAAAAAAAAAAA' 1123 'CH5BAAAAAAALAAAAAAQABAAAANEGLrcKjDKGSYEVtQxJI6cBo' 1124 'VAWQqhOA5mmaoCwBEuG1WXzHInVLU6Vgtm4gg/RJ0SiVsVOzf' 1125 'QsRZFQWNSn9UjCTUzkwQAOw==' '' ''. 1126 1127 PERFORM html_load_gif USING 'bombrevealed.gif' 1128 'R0lGODlhEAAQAJEAAAAAAHt7e729vf///yH5BAAAAAAALAAAA' 1129 'AAQABAAAAI0jI9pwu0fHgNSREnlFRR4zzUb933O5g1DmVlNup' 1130 'YiydbuVIMMmrdjfnrRQL5KK4ipjCqOAgA7' '' '' ''. 1131 1132 PERFORM html_load_gif USING 'facedead.gif' 1133 'R0lGODlhGgAaAKIAAAAAAHt7e729vf//AP///wAAAAAAAAAAA' 1134 'CH5BAAAAAAALAAAAAAaABoAAAOAGLrcziKQSau9M0rMr95CKI' 1135 '4kyWRlWp6ECrxvypbvYNvxOI/A7eO6BSrU+/0Aop2g2CsOmsA' 1136 'PiHhz4qqhnRN63UpRVuPx21qKz0jtWZwWlsOwmq+tGG6PWLKI' 1137 'yY4qzWtPSW4kYXNBdWU8cEiIUzRxMoQqlFlCUg+ZDZianZydm' 1138 'gkAOw=='. 1139 1140 PERFORM html_load_gif USING 'facesmile.gif' 1141 'R0lGODlhGgAaAKIAAAAAAHt7e729vf//AP///wAAAAAAAAAAA' 1142 'CH5BAAAAAAALAAAAAAaABoAAAN/GLrcziKQSau9M0rMr95CKI' 1143 '4kyWRlWp6ECrxvypbvYNvxOI/A7eO6BSrU+/0Aop2gaDyGlMw' 1144 'aLvoUtpY+6UC7/YCwzSZyxwxnvaiyGUi+qXFu9BW8PcblojKM' 1145 'i8Tn111VCkNEZn2CXzxqOUlWLnsyjiqTiC0Pl5hemJsBmpyYC' 1146 'QA7'. 1147 1148 PERFORM html_load_gif USING 'facesmile.gif' 1149 'R0lGODlhGgAaAKIAAAAAAHt7e729vf//AP///wAAAAAAAAAAA' 1150 'CH5BAAAAAAALAAAAAAaABoAAAN/GLrcziKQSau9M0rMr95CKI' 1151 '4kyWRlWp6ECrxvypbvYNvxOI/A7eO6BSrU+/0Aop2gaDyGlMw' 1152 'aLvoUtpY+6UC7/YCwzSZyxwxnvaiyGUi+qXFu9BW8PcblojKM' 1153 'i8Tn111VCkNEZn2CXzxqOUlWLnsyjiqTiC0Pl5hemJsBmpyYC' 1154 'QA7'. 1155 1156 PERFORM html_load_gif USING 'facewin.gif' 1157 'R0lGODlhGgAaAKIAAAAAAHt7AHt7e729vf//AP///wAAAAAAA' 1158 'CH5BAAAAAAALAAAAAAaABoAAAOEKLrczkOUSau9M0rMr95DKI' 1159 '4kyWRlWp6FCrxvypYvYdvxOI/A7eO6BSrU+/0Aot2gaDyGlEW' 1160 'YtEZAfkBLnLS6rV5RvTCMWwt/W8tigMoNaM/Ephy5Y8p9dCE6' 1161 'izverFB4XUBwInZNVoWGd4mKhoc5SXouUjKTKphPQlcPnQ2cn' 1162 'qGgoZ4JADs='. 1163 ENDFORM. "html_load_gifs 1164 1165 *&---------------------------------------------------------------------* 1166 *& Form create_gif 1167 *&---------------------------------------------------------------------* 1168 FORM html_load_gif USING 1169 image_name TYPE c 1170 s1 TYPE c 1171 s2 TYPE c 1172 s3 TYPE c 1173 s4 TYPE c 1174 s5 TYPE c 1175 s6 TYPE c. 1176 1177 DATA: gif_size TYPE i, 1178 gif_base64 TYPE string, 1179 gif_binary TYPE xstring, 1180 gif_binary_table TYPE TABLE OF w3mime. 1181 1182 CONCATENATE s1 s2 s3 s4 s5 s6 INTO gif_base64. 1183 1184 CALL FUNCTION 'SSFC_BASE64_DECODE' 1185 EXPORTING 1186 b64data = gif_base64 1187 IMPORTING 1188 bindata = gif_binary 1189 EXCEPTIONS 1190 OTHERS = 1. 1191 CHECK sy-subrc EQ 0. 1192 1193 PERFORM xstring_to_table 1194 TABLES gif_binary_table 1195 USING gif_binary 1196 CHANGING gif_size. 1197 1198 CALL METHOD html_control->load_data 1199 EXPORTING 1200 url = image_name 1201 type = 'image' 1202 subtype = 'gif' 1203 size = gif_size 1204 CHANGING 1205 data_table = gif_binary_table 1206 EXCEPTIONS 1207 dp_invalid_parameter = 1 1208 dp_error_general = 2 1209 cntl_error = 3 1210 OTHERS = 4. 1211 ENDFORM. "html_load_gif 1212 1213 *---------------------------------------------------------------------* 1214 * FORM XSTRING_TO_TABLE * 1215 *---------------------------------------------------------------------* 1216 * convert xstring to xtable[255] 1217 *---------------------------------------------------------------------* 1218 FORM xstring_to_table 1219 TABLES table1 1220 USING buffer TYPE xstring 1221 CHANGING binary_size TYPE i. 1222 1223 DATA: rows TYPE i, pos TYPE i. 1224 FIELD-SYMBOLS: <fs> TYPE x. 1225 1226 ASSIGN COMPONENT 0 OF STRUCTURE table1 TO <fs> TYPE 'X'. 1227 binary_size = xstrlen( buffer ). 1228 rows = ( binary_size + 255 - 1 ) DIV 255. 1229 DO rows TIMES. 1230 <fs> = buffer+pos. 1231 pos = pos + 255. 1232 APPEND table1. 1233 ENDDO. 1234 ENDFORM. "xstring_to_table 1235 1236 *&---------------------------------------------------------------------* 1237 *& Form html_show 1238 *&---------------------------------------------------------------------* 1239 FORM html_show. 1240 DATA: doc_url(80), 1241 html TYPE TABLE OF w3html INITIAL SIZE 150. 1242 1243 PERFORM html_create TABLES html. 1244 1245 CALL METHOD html_control->load_data( 1246 IMPORTING 1247 assigned_url = doc_url 1248 CHANGING 1249 data_table = html 1250 EXCEPTIONS 1251 dp_invalid_parameter = 1 1252 dp_error_general = 2 1253 cntl_error = 3 1254 OTHERS = 4 1255 ). 1256 1257 CALL METHOD html_control->show_url( url = doc_url ). 1258 1259 ENDFORM. "html_show 1260 1261 *---------------------------------------------------------------------* 1262 * FORM high_scores_read * 1263 *---------------------------------------------------------------------* 1264 FORM high_scores_read. 1265 REFRESH high_scores. 1266 IMPORT lines = high_scores[] FROM DATABASE indx(st) ID database_id. 1267 ENDFORM. "high_scores_read 1268 1269 *---------------------------------------------------------------------* 1270 * FORM high_scores_show * 1271 *---------------------------------------------------------------------* 1272 FORM high_scores_show. 1273 DATA: s(6) TYPE c, line TYPE c. 1274 FIELD-SYMBOLS: <fs1> TYPE c, <fs2> TYPE c. 1275 1276 DO 9 TIMES. 1277 line = sy-index. 1278 CONCATENATE 'hs2_' line INTO s. 1279 ASSIGN (s) TO <fs1>. 1280 CONCATENATE 'hs3_' line INTO s. 1281 ASSIGN (s) TO <fs2>. 1282 READ TABLE high_scores INDEX sy-index. 1283 IF sy-subrc EQ 0. 1284 <fs1> = high_scores-user. 1285 <fs2> = high_scores-time. 1286 ELSE. 1287 CLEAR: <fs1>, <fs2>. 1288 ENDIF. 1289 ENDDO. 1290 ENDFORM. "high_scores_show 1291 1292 *---------------------------------------------------------------------* 1293 * FORM high_scores_update * 1294 *---------------------------------------------------------------------* 1295 FORM high_scores_update. 1296 1297 high_scores-user = sy-uname. 1298 WRITE game_time TO high_scores-time RIGHT-JUSTIFIED. 1299 1300 INSERT high_scores INTO TABLE high_scores[]. 1301 1302 LOOP AT high_scores FROM 10. 1303 DELETE high_scores INDEX sy-tabix. 1304 ENDLOOP. 1305 1306 EXPORT lines = high_scores[] TO DATABASE indx(st) ID database_id. 1307 ENDFORM. "high_scores_update
View Code
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18020.html