SCREEN 0
CLS
'FOR i = 1 TO 31
'COLOR i: PRINT i; CHR$(219)
'NEXT
'FOR i = 33 TO 255
'PRINT i; "-"; CHR$(i);
'NEXT
'END

COLOR 12, 0: LOCATE 1, 60: PRINT "LINES:"; : COLOR 11, 0: PRINT "CGA REMAKE"
COLOR 10, 0: LOCATE 2, 63: PRINT "MARKICHEV D."

COLOR 7, 0

RANDOMIZE (VAL(MID$(TIME$, 7, 2)))
KEY 15, CHR$(0) + CHR$(&H51)'PGDN
KEY 16, CHR$(0) + CHR$(&H49)'PGUP
KEY 17, CHR$(0) + CHR$(&H4F)'END
KEY 18, CHR$(0) + CHR$(&H47)'HOME
KEY 19, CHR$(0) + CHR$(&H4C)'NUMPAD 5

ON KEY(15) GOSUB 15 'PGDN
ON KEY(16) GOSUB 16 'PGUP
ON KEY(17) GOSUB 17 'END
ON KEY(18) GOSUB 18 'HOME
ON KEY(11) GOSUB 11 'UP
ON KEY(12) GOSUB 12 'LEFT
ON KEY(13) GOSUB 13 'RIGHT
ON KEY(14) GOSUB 14 'DOWN
ON KEY(19) GOSUB 19'NUMPAD 5

y = 9
x = 9
c = 7
yr = 1
col = 3
li = 5
place = 21
calc = y * x
DIM array(y + 1, x + 1), m(y + 1, x + 1), c(col), bills(21), owners$(21)

REM LOAD RESULTS
OPEN "lines.res" FOR INPUT AS #1
FOR i = 1 TO 20
INPUT #1, owners$(i)
INPUT #1, bills(i)
COLOR 0, 0:  LOCATE 2 + i, 1: PRINT SPACE$(18)
COLOR 14, 0: LOCATE 2 + i, 1: PRINT owners$(i)
COLOR 15, 0: LOCATE 2 + i, 12: PRINT bills(i)
NEXT
CLOSE
owners$(place) = "NEWPLAYER"
COLOR 0, 0:  LOCATE 2 + place, 1: PRINT SPACE$(18)
COLOR 13, 0: LOCATE 2 + place, 1: PRINT owners$(place)
COLOR 13, 0: LOCATE 2 + place, 12: PRINT bills(place)

FOR yi = 0 TO y + 1
        m(yi, 0) = 233
        m(yi, x + 1) = 233
NEXT
FOR xi = 0 TO x + 1
        m(0, xi) = 233
        m(y + 1, xi) = 233
NEXT

xc = 1
yc = 1
insy = 0
insx = 0

COLOR 7, 0'COLOR OF FIELD

FOR yi = 1 TO y + 1
FOR xi = 1 TO x + 1
        IF xi <> x + 1 THEN LOCATE yr + 1 + yi * 2, INT((80 - (x * 4 + 1)) / 2) - 2 + xi * 4: PRINT STRING$(3, 196)
        IF xi > 1 AND xi < x + 1 AND yi > 1 AND yi < y + 1 THEN LOCATE yr + 1 + yi * 2, INT((80 - (x * 4 + 1)) / 2) - 3 + xi * 4: PRINT CHR$(197)
        IF yi <> y + 1 THEN LOCATE yr + 2 + yi * 2, INT((80 - (x * 4 + 1)) / 2) - 3 + xi * 4: PRINT CHR$(179)
        IF xi > 1 AND xi < x + 1 AND yi = 1 THEN LOCATE yr + 1 + yi * 2, INT((80 - (x * 4 + 1)) / 2) - 3 + xi * 4: PRINT CHR$(194)
        IF xi > 1 AND xi < x + 1 AND yi = y + 1 THEN LOCATE yr + 1 + yi * 2, INT((80 - (x * 4 + 1)) / 2) - 3 + xi * 4: PRINT CHR$(193)
        IF xi = 1 AND yi > 1 AND yi < y + 1 THEN LOCATE yr + 1 + yi * 2, INT((80 - (x * 4 + 1)) / 2) - 3 + xi * 4: PRINT CHR$(195)
        IF xi = x + 1 AND yi > 1 AND yi < y + 1 THEN LOCATE yr + 1 + yi * 2, INT((80 - (x * 4 + 1)) / 2) - 3 + xi * 4: PRINT CHR$(180)
NEXT
NEXT
LOCATE yr + 3, INT((80 - (x * 4 + 1)) / 2) + 1: PRINT CHR$(218)
LOCATE yr + 21, INT((80 - (x * 4 + 1)) / 2) + 1: PRINT CHR$(192)
LOCATE yr + 3, INT((80 - (x * 4 + 1)) / 2) + 37: PRINT CHR$(191)
LOCATE yr + 21, INT((80 - (x * 4 + 1)) / 2) + 37: PRINT CHR$(217)

FOR xi = 1 TO x
        LOCATE yr + 2, INT((80 - (x * 4 + 1)) / 2) + xi * 4 - 2: PRINT xi
        LOCATE yr + 22, INT((80 - (x * 4 + 1)) / 2) + xi * 4 - 2: PRINT xi
NEXT

FOR yi = 1 TO y
LOCATE yr + 2 + yi * 2, INT((80 - (x * 4 + 1)) / 2) - 3: PRINT yi
LOCATE yr + 2 + yi * 2, INT((80 - (x * 4 + 1)) / 2) + 39: PRINT yi
NEXT

4 : GOSUB 7'LABEL FOR RESTART
GOTO 6
7 : 'CAST A BONES

'calc = 0

        'FOR yi = 1 TO y
        'FOR xi = 1 TO x
                'IF array(yi, xi) = 0 THEN calc = calc + 1
        'NEXT
        'NEXT

IF start = 0 THEN 9'FIRST LOAD COLORS TO (UNLOAD IN ARRAY AND AGAIN LOAD)

8 :     'IF calc < col THEN cast = calc ELSE cast = col
        ii = 0
        WHILE ii < col AND calc > 0
        ii = ii + 1
        calc = calc - 1
10 :    yrandom = INT(RND(1) * y) + 1
        xrandom = INT(RND(1) * x) + 1
        IF array(yrandom, xrandom) <> 0 THEN 10
        array(yrandom, xrandom) = c(ii)
        LOCATE yr + 2 + yrandom * 2, INT((80 - (x * 4 + 1)) / 2) + xrandom * 4 - 2
        COLOR c(ii)
        PRINT STRING$(3, 219)
        y0 = yrandom
        x0 = xrandom
        GOSUB 52
        IF xp > 0 THEN calc = calc + xp: xp = 0
        IF calc = 0 THEN 20
WEND
       
9 : FOR i = 1 TO col
        c(i) = INT(RND(1) * c) + 1
        IF c(i) = 7 THEN c(i) = 14
        IF c(i) = 6 THEN c(i) = 12
        LOCATE 12, 61 + i * 3: COLOR c(i): PRINT STRING$(3, 219)
NEXT

IF start = 0 THEN start = 1: GOTO 8

RETURN

6 : GOSUB 38

WHILE break = 0
        GOSUB 30
        GOSUB 39
        i$ = INKEY$
        IF i$ = CHR$(32) THEN GOSUB 19
        IF i$ = CHR$(27) THEN break = 1
        IF i$ = CHR$(8) THEN
                IF LEN(owners$(place)) > 0 THEN
                        COLOR 0, 0: LOCATE 2 + place, LEN(owners$(place)): PRINT " "
                        owners$(place) = MID$(owners$(place), 1, LEN(owners$(place)) - 1)
                END IF
        END IF
        IF i$ <> "" THEN
                IF ASC(i$) > 32 AND LEN(owners$(place)) < 11 THEN
                        COLOR 13, 0: LOCATE 2 + place, LEN(owners$(place)) + 1: PRINT i$
                        owners$(place) = owners$(place) + i$
                END IF
        END IF

WEND

OPEN "lines.res" FOR OUTPUT AS #1
FOR i = 1 TO 20
        IF owners$(i) <> "" THEN PRINT #1, owners$(i) ELSE PRINT #1, SPACE$(10)
        PRINT #1, bills(i)
NEXT
CLOSE

END

11 : 'UP
        GOSUB 39
        GOSUB 31
        yc = yc - 1 + y * INT(1 / yc)
        GOSUB 38
RETURN

14 : 'DOWN
        GOSUB 39
        GOSUB 31
        yc = yc + 1 - y * INT(yc / y)
        GOSUB 38
RETURN

12 : 'LEFT
        GOSUB 39
        GOSUB 31
        xc = xc - 1 + x * INT(1 / xc)
        GOSUB 38
RETURN

13 : 'RIGHT
        GOSUB 39
        GOSUB 31
        xc = xc + 1 - x * INT(xc / x)
        GOSUB 38
RETURN

15 : 'PGDN
        GOSUB 39
        GOSUB 31
        yc = yc + 1
        xc = xc + 1
        IF yc > y AND xc < x + 2 THEN SWAP xc, yc: yc = y + 2 - yc: xc = x + 2 - xc
        IF yc < y + 2 AND xc > x THEN SWAP xc, yc: yc = y + 2 - yc: xc = x + 2 - xc
        GOSUB 38
RETURN

16 : 'PGUP
        GOSUB 39
        GOSUB 31
        yc = yc - 1
        xc = xc + 1
        IF yc = 0 THEN SWAP xc, yc: yc = yc - 1: xc = 1
        IF yc > 0 AND xc = 10 THEN SWAP xc, yc: yc = 9: xc = xc + 1
        GOSUB 38
RETURN

17 : 'END
        GOSUB 39
        GOSUB 31
        yc = yc + 1
        xc = xc - 1
        IF yc = 10 THEN yc = xc + 1: xc = 9
        IF yc < 10 AND xc = 0 THEN xc = yc - 1: yc = 1
        GOSUB 38
RETURN

18 : 'HOME
        GOSUB 39
        GOSUB 31
        yc = yc - 1
        xc = xc - 1
        IF yc = 0 THEN yc = 9 - xc: xc = 9
        IF xc = 0 AND yc > 0 THEN xc = 9 - yc: yc = 9
        GOSUB 38
RETURN

19 : 'NUMPAD 5
        GOSUB 39
        IF array(yc, xc) = 0 THEN
                IF insy <> 0 THEN
                        LOCATE yr + 1 + 1 * 2, INT((80 - (x * 4 + 1)) / 2) + x * 4 + 1: COLOR 15, 4: PRINT CHR$(191)
                        FOR yi = 1 TO y
                        FOR xi = 1 TO x
                                m(yi, xi) = array(yi, xi)
                        NEXT
                        NEXT
                DIM a(1000, 2), b(1000, 2)
                mv = 0
                ka = 1
                kb = 1
                a(1, 1) = insy: a(1, 2) = insx
                WHILE mv = 0 AND kb > 0 AND ka > 0
                'CYCLE A
                kb = 0
                FOR i = 1 TO ka
                IF m(a(i, 1) + 1, a(i, 2)) = 0 THEN
                        IF a(i, 1) + 1 = yc AND a(i, 2) = xc THEN mv = 1
                        kb = kb + 1
                        b(kb, 1) = a(i, 1) + 1: b(kb, 2) = a(i, 2)
                        m(b(kb, 1), b(kb, 2)) = 100
                END IF
                IF m(a(i, 1) - 1, a(i, 2)) = 0 THEN
                        IF a(i, 1) - 1 = yc AND a(i, 2) = xc THEN mv = 1
                        kb = kb + 1
                        b(kb, 1) = a(i, 1) - 1: b(kb, 2) = a(i, 2)
                        m(b(kb, 1), b(kb, 2)) = 100
                END IF
                IF m(a(i, 1), a(i, 2) + 1) = 0 THEN
                        IF a(i, 1) = yc AND a(i, 2) + 1 = xc THEN mv = 1
                        kb = kb + 1
                        b(kb, 1) = a(i, 1): b(kb, 2) = a(i, 2) + 1
                        m(b(kb, 1), b(kb, 2)) = 100
                END IF
                IF m(a(i, 1), a(i, 2) - 1) = 0 THEN
                        IF a(i, 1) = yc AND a(i, 2) - 1 = xc THEN mv = 1
                        kb = kb + 1
                        b(kb, 1) = a(i, 1): b(kb, 2) = a(i, 2) - 1
                        m(b(kb, 1), b(kb, 2)) = 100
                END IF
                m(a(i, 1), a(i, 2)) = 100
                NEXT
                ERASE a
                DIM a (1000, 2)
                'CYCLE B
                ka = 0
                FOR i = 1 TO kb
                IF m(b(i, 1) + 1, b(i, 2)) = 0 THEN
                        IF b(i, 1) + 1 = yc AND b(i, 2) = xc THEN mv = 1
                        ka = ka + 1
                        a(ka, 1) = b(i, 1) + 1: a(ka, 2) = b(i, 2)
                        m(a(ka, 1), a(ka, 2)) = 100
                END IF
                IF m(b(i, 1) - 1, b(i, 2)) = 0 THEN
                        IF b(i, 1) - 1 = yc AND b(i, 2) = xc THEN mv = 1
                        ka = ka + 1
                        a(ka, 1) = b(i, 1) - 1: a(ka, 2) = b(i, 2)
                        m(a(ka, 1), a(ka, 2)) = 100
                END IF
                IF m(b(i, 1), b(i, 2) + 1) = 0 THEN
                        IF b(i, 1) = yc AND b(i, 2) + 1 = xc THEN mv = 1
                        ka = ka + 1
                        a(ka, 1) = b(i, 1): a(ka, 2) = b(i, 2) + 1
                        m(a(ka, 1), a(ka, 2)) = 100
                END IF
                IF m(b(i, 1), b(i, 2) - 1) = 0 THEN
                        IF b(i, 1) = yc AND b(i, 2) - 1 = xc THEN mv = 1
                        ka = ka + 1
                        a(ka, 1) = b(i, 1): a(ka, 2) = b(i, 2) - 1
                        m(a(ka, 1), a(ka, 2)) = 100
                END IF
                m(b(i, 1), b(i, 2)) = 100
                NEXT
                ERASE b
                DIM b (1000, 2)
                WEND
                IF mv = 1 THEN
                        SWAP array(yc, xc), array(insy, insx)
                        LOCATE yr + 2 + insy * 2, INT((80 - (x * 4 + 1)) / 2) + insx * 4 - 2
                        COLOR 0, 0
                        PRINT "   "
                        SWAP insx, xc: SWAP insy, yc: GOSUB 31: yc = insy: xc = insx
                        LOCATE yr + 2 + yc * 2, INT((80 - (x * 4 + 1)) / 2) + xc * 4 - 2
                        COLOR array(yc, xc)
                        PRINT STRING$(3, 219)
                        x0 = xc: y0 = yc
                        GOSUB 52
                        IF xp > 0 THEN calc = calc + xp: xp = 0 ELSE GOSUB 7
                        GOSUB 38
                        insy = 0
                        insx = 0
                ELSE
                        BEEP
                END IF
                ERASE a, b
                END IF
        ELSE
                IF insy <> 0 THEN IF insy <> yc OR insx <> xc THEN SWAP insx, xc: SWAP insy, yc: GOSUB 31: yc = insy: xc = insx
                insy = yc: insx = xc
                'GOSUB 31
                'GOSUB 38
        END IF
RETURN

31 : 'UNSELECT
        IF insx = xc AND insy = yc THEN
                r = array(yc, xc) + 16
                COLOR r, 0
                LOCATE yr + 2 + yc * 2, INT((80 - (x * 4 + 1)) / 2) + xc * 4 - 2
                PRINT STRING$(3, 219)
        ELSE
                COLOR array(yc, xc)
                LOCATE yr + 2 + yc * 2, INT((80 - (x * 4 + 1)) / 2) + xc * 4 - 2
                PRINT STRING$(3, 219)
        END IF
        'COLOR 15, 0
        'LOCATE yr + 1 + yc * 2, INT((80 - (x * 4 + 1)) / 2) + xc * 4 - 2
        'PRINT STRING$(3, 196)
        'LOCATE yr + 3 + yc * 2, INT((80 - (x * 4 + 1)) / 2) + xc * 4 - 2
        'PRINT STRING$(3, 196)
RETURN

38 : 'SELECT
        'COLOR 15, 2
        'LOCATE yr + 1 + yc * 2, INT((80 - (x * 4 + 1)) / 2) + xc * 4 - 2
        'PRINT STRING$(3, 196)
        'LOCATE yr + 3 + yc * 2, INT((80 - (x * 4 + 1)) / 2) + xc * 4 - 2
        'PRINT STRING$(3, 196)
        COLOR array(yc, xc), 7
        LOCATE yr + 2 + yc * 2, INT((80 - (x * 4 + 1)) / 2) + xc * 4 - 2
        PRINT STRING$(3, 223)
        COLOR 15, 2
        LOCATE yr + 1 + 1 * 2, INT((80 - (x * 4 + 1)) / 2) + x * 4 + 1
        PRINT CHR$(191)
RETURN

20 : 'RESTART
COLOR 0, 0:  LOCATE 2 + place, 1: PRINT SPACE$(18)
COLOR 14, 0: LOCATE 2 + place, 1: PRINT owners$(place)
COLOR 15, 0: LOCATE 2 + place, 12: PRINT bills(place)
owners$(21) = owners$(place)
place = 21
bills(place) = 0
COLOR 0, 0:  LOCATE 2 + place, 1: PRINT SPACE$(18)
COLOR 13, 0: LOCATE 2 + place, 1: PRINT owners$(place)
COLOR 13, 0: LOCATE 2 + place, 12: PRINT bills(place)

OPEN "lines.res" FOR OUTPUT AS #1
FOR i = 1 TO 20
        IF owners$(i) <> "" THEN PRINT #1, owners$(i) ELSE PRINT #1, SPACE$(10)
        PRINT #1, bills(i)
NEXT
CLOSE

FOR yi = 1 TO y
FOR xi = 1 TO x
        array(yi, xi) = 0
        LOCATE yr + 2 + yi * 2, INT((80 - (x * 4 + 1)) / 2) + xi * 4 - 2
        COLOR 0, array(yi, xi)
        PRINT "   "
NEXT
NEXT
start = 0
insy = 0
insx = 0
calc = y * x
ERASE a, b
GOTO 4'AGAIN

30 : 'INITIATE KEYS
KEY(11) ON: KEY(12) ON: KEY(13) ON: KEY(14) ON
KEY(15) ON: KEY(16) ON: KEY(17) ON: KEY(18) ON: KEY(19) ON
RETURN

39 : 'STOP KEYS
KEY(11) STOP: KEY(12) STOP: KEY(13) STOP: KEY(14) STOP
KEY(15) STOP: KEY(16) STOP: KEY(17) STOP: KEY(18) STOP: KEY(19) STOP
RETURN

77 : 'EXPA

bills(place) = bills(place) + xp * 10

WHILE bills(place) > bills(place - 1) AND place > 1
        T = 1
        SWAP bills(place), bills(place - 1)
        SWAP owners$(place), owners$(place - 1)
        COLOR 0, 0:  LOCATE 1 + place, 1: PRINT SPACE$(18)
        COLOR 0, 0:  LOCATE 2 + place, 1: PRINT SPACE$(18)
        COLOR 14, 0: LOCATE 2 + place, 1: PRINT owners$(place)
        COLOR 13, 0: LOCATE 1 + place, 1: PRINT owners$(place - 1)
        COLOR 15, 0: LOCATE 2 + place, 12: PRINT bills(place)
        COLOR 13, 0: LOCATE 1 + place, 12: PRINT bills(place - 1)
        place = place - 1
WEND

IF T = 0 THEN
        COLOR 13, 0
        LOCATE 2 + place, 12
        PRINT bills(place)
ELSE
        T = 0
END IF

RETURN

52 : 'VERIFY

keep = 0
i1 = 0
i2 = 0

FOR yi = 1 TO y
        IF array(yi, x0) = array(yi + 1, x0) AND array(yi + 1, x0) <> 0 THEN
                keep = keep + 1
        ELSE
                IF keep > li - 2 THEN
                        i1 = yi: i2 = keep
                        EXIT FOR
                END IF
                keep = 0
        END IF
NEXT

keep = 0
i3 = 0
i4 = 0

FOR xi = 1 TO x
        IF array(y0, xi) = array(y0, xi + 1) AND array(y0, xi + 1) <> 0 THEN
                keep = keep + 1
        ELSE
                IF keep > li - 2 THEN
                        i3 = xi: i4 = keep
                        EXIT FOR
                END IF
                keep = 0
        END IF
NEXT

keep = 0
i5 = 0
i6 = 0
i7 = 0

IF x0 > y0 THEN xi = x0 - y0 + 1: yi = 1 ELSE yi = y0 - x0 + 1: xi = 1
WHILE xi < 10 AND yi < 10
        IF array(yi, xi) = array(yi + 1, xi + 1) AND array(yi + 1, xi + 1) <> 0 THEN
                keep = keep + 1
        ELSE
                IF keep > li - 2 THEN
                        i5 = keep: i6 = yi: i7 = xi
                END IF
                keep = 0
        END IF
        xi = xi + 1
        yi = yi + 1
WEND

keep = 0
i8 = 0

IF (x + 1) - x0 > y0 THEN xi = x + 1 - (((x + 1 - x0) - y0) + 1): yi = 1 ELSE yi = y0 - (x + 1 - x0) + 1: xi = x
WHILE xi > 0 AND yi < 10
        IF array(yi, xi) = array(yi + 1, xi - 1) AND array(yi + 1, xi - 1) <> 0 THEN
                keep = keep + 1
        ELSE
                IF keep > li - 2 THEN
                        i8 = i8 + 1
                        FOR i = 0 TO keep
                                array(yi - i, xi + i) = 0
                                COLOR 0, 0
                                LOCATE yr + 2 + (yi - i) * 2, INT((80 - (x * 4 + 1)) / 2) + (xi + i) * 4 - 2
                                PRINT "   "
                        NEXT
                        xp = xp + keep + 1
                END IF
                keep = 0
        END IF
        xi = xi - 1
        yi = yi + 1
WEND


'1
IF i1 <> 0 THEN
        i8 = i8 + 1
        FOR i = i1 - i2 TO i1
                array(i, x0) = 0
                COLOR 0, 0
                LOCATE yr + 2 + i * 2, INT((80 - (x * 4 + 1)) / 2) + x0 * 4 - 2
                PRINT "   "
        NEXT
        xp = xp + i2 + 1
END IF

'2
IF i3 <> 0 THEN
        i8 = i8 + 1
        FOR i = i3 - i4 TO i3
                array(y0, i) = 0
                COLOR 0, 0
                LOCATE yr + 2 + y0 * 2, INT((80 - (x * 4 + 1)) / 2) + i * 4 - 2
                PRINT "   "
        NEXT
        xp = xp + i4 + 1
END IF

'3
IF i5 <> 0 THEN
        i8 = i8 + 1
        FOR i = 0 TO i5
                array(i6 - i, i7 - i) = 0
                COLOR 0, 0
                LOCATE yr + 2 + (i6 - i) * 2, INT((80 - (x * 4 + 1)) / 2) + (i7 - i) * 4 - 2
                PRINT "   "
        NEXT
        xp = xp + i5 + 1
END IF

IF i8 > 1 THEN xp = xp - i8 + 1
IF xp > 0 THEN BEEP: GOSUB 77'EXPA

RETURN