' One Check is a program by David Ahl from his book: "Basic Computer Games Microcomputer Edition" 
' As found at https://archive.org/details/Basic_Computer_Games_Microcomputer_Edition_1978_Creative_Computing

' This BASIC Anywhere Machine port, significantly modded, by Charlie
' is based on another version of David Ahl's program found at https://hwiegman.home.xs4all.nl/gw-games/1CHECK.BAS.html
' MODS:
'       * Code significanly reorganized to use GOSUB subroutines instead of GOTO
'       * Modified to use mouse/touch to choose checker pieces and move destinations
'       * Modified to use character-based graphics and colour
' NOTE: Any remaining line numbers are breadcrumps to the original source code
' ' This program exported from BASIC Anywhere Machine (Version [5.2.3].[2024.07.14.16.44]) on 2024.09.03 at 00:36 (Coordinated Universal Time)

GOSUB 💦IntroScreen➔

' 🔶🔶🔶🔶 Declarations

SCREEN _NEWIMAGE(192,80,0)

LETCHR$ (1, _
    REPLACE$(" . . . . . . . . ", " ", "") + _
    REPLACE$(" . X X X X X X . ", " ", "") + _
    REPLACE$(" X X X . . X X X ", " ", "") + _
    REPLACE$(" X . X X X X . X ", " ", "") + _
    REPLACE$(" X . X X X X . X ", " ", "") + _
    REPLACE$(" X X X . . X X X ", " ", "") + _
    REPLACE$(" . X X X X X X . ", " ", "") + _
    REPLACE$(" . . . . . . . . ", " ", "") )
68 DIM AS STRING A(64)

' 🔶🔶🔶🔶 Main Program

GOSUB 🆕ResetTheGame➔ 

DO
    WHILE NOT _MOUSEBUTTON : WEND
    WHILE _MOUSEBUTTON : xpos = _MOUSEX : ypos = _MOUSEY : WEND
    selected_pos = ( xpos DIV 24 ) + 1 + (ypos DIV 8) * 8
    IF BETWEEN(selected_pos,1,64) THEN
        IF A(selected_pos)=CHR$(1) THEN F = selected_pos : GOSUB 🎨PrintTheBoard➔
        IF BETWEEN(F,1,64) AND A(selected_pos)=" " THEN T = selected_pos : GOSUB ✅CheckLegalityOfMove➔
    END IF
    IF BETWEEN(selected_pos,73,76) THEN GOSUB 📊EndGameSummary➔

LOOP 

' 🔶🔶🔶🔶 Subroutines

✅CheckLegalityOfMove➔: 

    118 REM *** CHECK LEGALITY OF MOVE
    120 F1=INT((F-1)/8)
    130 F2=F-8*F1
    140 T1=INT((T-1)/8)
    150 T2=T-8*T1
    160 IF F1>7 THEN 230
    170 IF T1>7 THEN 230
    180 IF F2>8 THEN 230
    190 IF T2>8 THEN 230
    200 IF ABS(F1-T1)<>2 THEN 230
    210 IF ABS(F2-T2)<>2 THEN 230
    212 IF A((T+F)/2)=" " THEN 230
    215 IF A(F)=" " THEN 230
    220 IF A(T)=CHR$(1) THEN 230
    225 GOSUB 🔃UpdateTheBoard➔  
    F = 0 : T = 0 : GOTO 240
    230 ALERT( "ILLEGAL MOVE.  TRY AGAIN..." )
    240

RETURN

🔃UpdateTheBoard➔: 

    245 REM *** UPDATE BOARD
    250 A(T)=CHR$(1)
    260 A(F)=" "
    270 A((T+F)/2)=" "
    290 M=M+1
    
' This subroutine flows into PrintTheBoard    

🎨PrintTheBoard➔:  

    310 REM *** PRINT BOARD
    CLS

    340 FOR J=1 TO 57 STEP 8
    IF ODD THEN ODD = FALSE ELSE ODD = TRUE
    IF ODD THEN pos_color = 0 ELSE pos_color = 6
    350 FOR I=J TO J+7
    IF A(I) = CHR$(1) AND I = F THEN forecolor = 14 ELSE forecolor = 15
    COLOR forecolor, pos_color
    360 PRINT " " + A(I) + " ";
    IF pos_color = 0 THEN pos_color = 6 ELSE pos_color = 0
    370 NEXT I
    380 ' PRINT
    390 NEXT J
    COLOR 7
    400 PRINT STRING$(24, CHR$(205)) + "DONE";

RETURN 

📊EndGameSummary➔:

    490 REM *** END GAME SUMMARY
    500 S=0
    510 FOR I=1 TO 64
    520 S=S+ABS(A(I) = CHR$(1))
    530 NEXT I

    ALERT("YOU MADE " + M + " JUMPS AND HAD " + S + " PIECES REMAINING ON THE BOARD. ")

    F = 0 : T = 0

    GOSUB 🆕ResetTheGame➔ 

RETURN

🆕ResetTheGame➔:

    80 FOR J=1 TO 64
    82 A(J)=CHR$(1)
    84 NEXT J
    86 FOR J=19 TO 43 STEP 8
    88 FOR I=J TO J+3
    90 A(I)=" "
    92 NEXT I
    94 NEXT J
    96 M=0
    GOSUB 🎨PrintTheBoard➔

RETURN

💦IntroScreen➔:

    SCREEN 12
    COLOR 14
    5 PRINT
    PRINT "SOLITAIRE CHECKER PUZZLE"
    PRINT 
    PRINT "ORIGINAL CODE LOGIC BY DAVID AHL"
    PRINT "ADAPTED BY CHARLIE VENIOT FOR BASIC ANYWHERE MACHINE"

    20 PRINT "48 CHECKERS ARE PLACED ON THE 2 OUTSIDE SPACES OF A"
    25 PRINT "STANDARD 64-SQUARE CHECKERBOARD.  THE OBJECT IS TO"
    30 PRINT "REMOVE AS MANY CHECKERS AS POSSIBLE BY DIAGONAL JUMPS"
    35 PRINT "(AS IN STANDARD CHECKERS)."
    PRINT "SELECT A CHECKER TO MOVE BY CLICKING/TOUCHING IT."
    PRINT "THEN SELECT THE DESTINATION TILE THE CHECKER WILL LAND ON"
    PRINT "AFTER JUMPING OVER ANOTHER CHECKER."
    50 PRINT "WHEN YOU HAVE NO POSSIBLE JUMPS REMAINING,"
    60 PRINT "CLICK/TOUCH 'DONE'"
    62 PRINT
    PRINT "PRESS ANY KEY TO CONTINUE"
    SLEEP

RETURN