Command Prompt for Windows




Batch File 4ROW

The following batch file plays the game "Connect 4 in a Row", between you and the computer :-

@ECHO off
IF NOT "%winone%"=="" GOTO okay
ECHO This Batch file requires the WinOne(R) Command Prompt to run!
EXIT /b
:okay

SETLOCAL
 
DATA 1,100,500,1e20,1,800,4000,1e20
DATA 1,75,900,1e18,1,450,3000,1e18
 
FOR %%i IS 1 TO 16 DO {
    READ
    SET v.%%i=%read%
}
 
DLGBOX "Four In A Row" 1000 94 35 194 159 newgame
 
DLGMENU popup 1000 "&Game"
DLGMENU item 1000 "&New               " 300 newgame
DLGMENU separator 1000
DLGMENU item 1000 "E&xit" 301 exitgame
 
rem DLGMENU popup 1000 "&Help"
rem DLGMENU item 1000 "&Instructions      " 302
 
FOR %%i IS 6 TO 118 STEP 16 DO {
    FOR %%j IS 9 TO 163 STEP 22 DO {
        DLGITEM "" BLACKFRAME -1 1000 %%j %%i 20 15
    }
}
 
SET x=101
 
FOR %%i IS 121 TO 9  STEP -16 DO {
    FOR %%j IS 17 TO 171 STEP 22 DO {
        DLGITEM " " LTEXT %x% 1000 %%j %%i 6 9
        LET x=%x% + 1
    }
}
 
DLGITEM "1" BUTTON 200 1000 10 138 19 14 drop_1
DLGITEM "2" BUTTON 201 1000 32 138 19 14 drop_2
DLGITEM "3" BUTTON 202 1000 54 138 19 14 drop_3
DLGITEM "4" BUTTON 203 1000 76 138 19 14 drop_4
DLGITEM "5" BUTTON 204 1000 98 138 19 14 drop_5
DLGITEM "6" BUTTON 205 1000 120 138 19 14 drop_6
DLGITEM "7" BUTTON 206 1000 142 138 19 14 drop_7
DLGITEM "8" BUTTON 207 1000 164 138 19 14 drop_8
 
DLGSHOW 1000 0
 
ENDLOCAL
 
PROC drop_1
CALLPROC drop 1
ENDPROC
 
PROC drop_2
CALLPROC drop 2
ENDPROC
 
PROC drop_3
CALLPROC drop 3
ENDPROC
 
PROC drop_4
CALLPROC drop 4
ENDPROC
 
PROC drop_5
CALLPROC drop 5
ENDPROC
 
PROC drop_6
CALLPROC drop 6
ENDPROC
 
PROC drop_7
CALLPROC drop 7
ENDPROC
 
PROC drop_8
CALLPROC drop 8
ENDPROC
 
PROC exitgame
DLGEND 1000 0
ENDPROC
 
PROC newgame
SET over=0
GREY 1000 300 1
 
FOR %%i IS 1 TO 8 DO {
    FOR %%j IS 1 TO 8 DO {
        SET b.%%i.%%j=-&
    }
}
 
FOR %%i IS 1 TO 8 DO SET ll.%%i=0
 
FOR %%i IS 1 TO 4 DO {
    SET s.%%i=0
    SET f.%%i=0
    SET n.%%i=0
}
 
SET x=101
 
FOR %%x IS 101 TO 164 DO {
    SETTEXT 1000 %%x " "
}
 
FOR %%i IS 200 TO 208 DO ENABLE 1000 %%i 1
ENDPROC
 
PROC drop
FOR %%i IS 200 TO 208 DO ENABLE 1000 %%i 0
GREY 1000 300 1
GREY 1000 301 1
 
SET m=%1
ECHO You pick %m%
 
IF (%ll.m% > 7) {
    MSGBOX "Illegal Move" "That move is not allowed" 64
    GOTO done
}
 
LET id=100 + %m% + %ll.m% * 8
SETTEXT 1000 %id% "X"
 
LET ll.m=%ll.m% + 1
 
SET l=%ll.m%
SET b.l.m=X
 
CALLPROC check X
 
FOR %%z IS 1 TO 4 DO {
    IF (%s.%%z% >= 4) {
        MSGBOX "Game Over" "Congratulations. You Win!" 48
        GOTO endgame
    }
}
 
CALLPROC move
IF (%over% == 1) GOTO endgame
 
FOR %%i IS 1 TO 8 DO {
    IF (%ll.%%i% < 8) GOTO done
}
 
MSGBOX "Game Over" "It's a tie." 48
GOTO endgame
 
:done
FOR %%i IS 200 TO 208 DO ENABLE 1000 %%i 1
 
:endgame
GREY 1000 300 0
GREY 1000 301 0
ENDPROC
 
PROC move
SET m9=0
SET v1=0
SET n1=0
FOR %%h IS 1 TO 8 DO {
    LET l=%ll.%%h% + 1
    IF (%l% <= 8) {
        SET v=1
        SET m=%%h
 
        CALLPROC move2 O 0
        IF (%move2% == 1) GOTO out4
 
        CALLPROC move2 X 1
        IF (%move2% == 1) GOTO out4
 
        LET l=%l% + 1
        IF (%l% <= 8) {
            CALLPROC check X
 
            FOR %%z IS 1 TO 4 DO {
                IF (%s.%%z% > 3) SET v=2
            }
        }
 
        IF (%v% >= %v1%) {
            IF (%v% > %v1%) {
                SET n1=1
                SET v1=%v%
                SET m9=%%h
            } ELSE {
                LET n1=%n1% + 1
                IF (%random_number% <= 65534 / %n1%) {
                    SET v1=%v%
                    SET m9=%%h
                }
            }
        }
    }
}
 
IF (%m9% == 0) {
    MSGBOX "Game Over" "It's a tie." 48
    SET over=1
    GOTO done2
}
 
LET m=%m9%
 
:out4
ECHO I pick %m%
ECHO.
LET id=100 + %m% + %ll.m% * 8
SETTEXT 1000 %id% "O"
 
LET ll.m=%ll.m% + 1
 
SET l=%ll.m%
SET b.l.m=O
 
CALLPROC check O
 
FOR %%z IS 1 TO 4 DO {
    IF (%s.%%z% >= 4) {
        MSGBOX "Game Over" "I Win!" 48
        SET over=1
        GOTO done2
    }
}
 
:done2
ENDPROC
 
PROC move2
SET w=%2
SET move2=0
 
CALLPROC check %1
 
FOR %%k IS 1 TO 4 DO SET n.%%k=0
 
FOR %%z IS 1 TO 4 DO {
    SET s=%s.%%z%
    IF (%s% - %w% > 3) {
        SET move2=1
        GOTO out3
    }
 
    LET t=%s% + %f.%%z%
    IF (%t% >= 4) {
        LET v=%v% + 4
        LET n.s=%n.s% + 1
    }
}
 
FOR %%i IS 1 TO 4 DO {
    LET n=%n.%%i% - 1
    IF (%n% != -1) {
        LET i1=8 * %w% + 4 * sgn(%n%) + %%i
        LET tp=8 * %w% + %%i
        LET v=%v% + %v.i1% + %n% * %v.tp%
    }
}
 
:out3
ENDPROC
 
PROC check
SET p=%1
 
SET q=X
IF ("%p%" == "X") SET q=O
 
SET z=0
CALLPROC dir 0 1
CALLPROC dir 1 1
CALLPROC dir 1 0
CALLPROC dir 1 -1
ENDPROC
 
PROC dir
SET d1=%1
SET d2=%2
 
SET d=1
SET s=1
SET t=0
LET z=%z% + 1
 
:loop
SET c=0
FOR %%k IS 1 TO 3 DO {
    LET m5=%m% + %%k * %d1%
    LET l1=%l% + %%k * %d2%
 
    IF (%m5% >= 1 && %l1% >= 1 && %m5% <= 8 && %l1% <= 8) {
        IF (%c% == 0) {
            IF ("%b.l1.m5%" == "%p%") {
                LET s=%s% + 1
            } ELSE {
                SET c=1
                IF ("%b.l1.m5%" == "%q%") {
                    GOTO out2
                } ELSE {
                    LET t=%t% + 1
                }
            }
        } ELSE {
            IF ("%b.l1.m5%" == "%q%") {
                GOTO out2
            } ELSE {
                LET t=%t% + 1
            }
        }
    }
}
 
:out2
IF (%d% == 0) {
    SET s.z=%s%
    SET f.z=%t%
} ELSE {
    SET d=0
    LET d1=-%d1%
    LET d2=-%d2%
    GOTO loop
}
ENDPROC

When the game is first run, it display the following window :-

You play the X's and the computer plays the O's. A sample of a finished game is :-

All the moves made during the game are also displayed in the WinOne® Command Prompt window :-

You pick 3
I pick 4
 
You pick 3
I pick 3
 
You pick 5
I pick 4
 
You pick 5
I pick 4
 
You pick 5
I pick 4