Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with an Othello (AKA Reversi) Game
#4
The problem with the AI isn't in the checking if the move is legal, otherwise it would actually make the move, not try to (if the Legal Move funtion gave back the wrong value, then it wouldn't continue looping). As for the illegal moves it tries, I think they are always in a place that already has a piece in it. The problem occurs in the AI deciding where to go and in what it treats as a leagal move, not with the leagal move checker. The Out of Memory error appears even if I stop the execution before I begin playing, while it shows the menus (which use the mouse):
The following is the code for the main module and the first sub it calls (other than the ones to show the mouse):
Code:
OPTION BASE 1
DEFINT A-Z
'$INCLUDE: 'general.bi'
'$INCLUDE: 'mouse.bi'
DECLARE SUB PxlCng (x%, y%)
DECLARE SUB AIMove ()
DECLARE SUB AskAI ()
DECLARE SUB CBoard ()
DECLARE SUB CTurn (CurTurn AS INTEGER)
DECLARE SUB Flip (CRow AS INTEGER, CColumn AS INTEGER, PColor AS INTEGER)
DECLARE SUB Piece (PRow AS INTEGER, PColumn AS INTEGER, BYVAL PieceColor AS INTEGER)
DECLARE SUB Save ()
DECLARE SUB WaitForMouseClick ()
DECLARE FUNCTION CheckLeag! (ORow AS INTEGER, OColumn AS INTEGER, PColor AS INTEGER, RCng AS INTEGER, CCng AS INTEGER)
DECLARE FUNCTION CkMoveDoIt$ ()
DECLARE FUNCTION ExitAsk$ ()
DECLARE FUNCTION LeagFileName! (Name$)
DECLARE FUNCTION LeagMove! (PRow AS INTEGER, PColumn AS INTEGER, PieceColor AS INTEGER)
DECLARE FUNCTION Max! (number1!, number2!)
DECLARE FUNCTION CSTR$ (numeric.expression%)
DECLARE FUNCTION TRIM$ (str AS STRING)
DIM SHARED Board(8, 8) AS INTEGER
DIM SHARED FlipperL(9) AS INTEGER
DIM SHARED FlipperM(3, 3) AS INTEGER
DIM SHARED Pieces(2)  AS INTEGER
DIM SHARED row AS INTEGER
DIM SHARED Column AS INTEGER
DIM SHARED Turn AS INTEGER
DIM SHARED Finished AS INTEGER
DIM SHARED AIPlaying AS INTEGER
DIM SHARED AIColor AS INTEGER
DIM SHARED MRow AS INTEGER
DIM SHARED MCol AS INTEGER
DIM SHARED MlButton AS INTEGER
DIM SHARED MrButton AS INTEGER
DIM SHARED PGRow AS INTEGER
DIM SHARED PGColumn AS INTEGER
CLEAR , , STACK
STACK STACK
begin:
RESET
MouseInit
MouseShow
CALL AskAI
CALL CBoard
waitkey:
        CALL WaitForMouseClick
        SELECT CASE CkMoveDoIt$
                CASE "y"
                        GOTO begin
                CASE "n"
                        GOTO leave
        END SELECT
GOTO waitkey
leave:
SCREEN 0
MouseHide
CLS
MouseShow
PRINT "Thank you for using this program."
PRINT
PRINT
PRINT "This program was made by Jason Gross."
END
exiter:
        SELECT CASE ExitAsk$
                CASE "e"
                        RETURN leave
                CASE "b"
                        RETURN begin
                CASE "s"
                        CALL Save
                        CALL CBoard
                CASE "r"
                        CALL CBoard
                        KEY(15) ON
                        KEY(16) OFF
                        RETURN
        END SELECT
GOTO exiter
leaver: GOTO leave



SUB AskAI
        KEY 15, CHR$(0) + CHR$(1)
        KEY 16, CHR$(0) + CHR$(1)
        ON KEY(15) GOSUB exiter
        ON KEY(16) GOSUB leaver
        KEY(15) OFF
        KEY(16) ON
        MouseHide
        CLS
        MouseShow
        SCREEN 0
        PRINT TAB(30); "*******************"
        PRINT TAB(30); "*                 *"
        PRINT TAB(30); "*     OTHELLO     *"
        PRINT TAB(30); "*                 *"
        PRINT TAB(30); "*       BY        *"
        PRINT TAB(30); "*   Jason Gross   *"
        PRINT TAB(30); "*                 *"
        PRINT TAB(30); "*******************"
        PRINT
        PRINT
        PRINT
        PRINT TAB(26); "************   *************"
        PRINT TAB(26); "* New Game *   * Load Game *"
        PRINT TAB(26); "************   *************"
        PRINT
        CALL MousePoll(MRow, MCol, MlButton, MrButton)
        DIM lastR AS INTEGER
        DIM lastC AS INTEGER
        ik$ = ""
        WHILE ik$ = ""
                CALL MousePoll(MRow, MCol, MlButton, MrButton)
                ik$ = ""
                IF MRow > 11 AND MRow < 15 AND MlButton THEN
                        IF MCol > 27 AND MCol < 39 THEN ik$ = "n"
                        IF MCol > 41 AND MCol < 53 THEN ik$ = "l"
                END IF
        WEND
        IF ik$ = "n" THEN
                PRINT "Would you like to play against:"
                PRINT
                PRINT TAB(26); "***********   ****************"
                PRINT TAB(26); "* A Human *   * The Computer *"
                PRINT TAB(26); "***********   ****************"
                ik$ = ""
                WHILE ik$ = ""
                        CALL MousePoll(MRow, MCol, MlButton, MrButton)
                        ik$ = ""
                        IF MRow > 17 AND MRow < 21 AND MlButton THEN
                                IF MCol > 27 AND MCol < 38 THEN ik$ = "h"
                                IF MCol > 40 AND MCol < 55 THEN ik$ = "c"
                        END IF
                WEND
                IF ik$ = "c" THEN
                        AIPlaying = -1
                        MouseHide
                        CLS
                        MouseShow
                        PRINT "Which would you like to be (black goes first)?"
                        PRINT
                        PRINT TAB(30); "*********   *********"
                        PRINT TAB(30); "* Black *   * White *"
                        PRINT TAB(30); "*********   *********"
                        PRINT
                        CALL MousePoll(MRow, MCol, MlButton, MrButton)
                        ik$ = ""
                        WHILE ik$ = ""
                                CALL MousePoll(MRow, MCol, MlButton, MrButton)
                                ik$ = ""
                                IF MRow > 2 AND MRow < 6 AND MlButton THEN
                                        IF MCol > 31 AND MCol < 40 THEN ik$ = "b"
                                        IF MCol > 42 AND MCol < 50 THEN ik$ = "w"
                                END IF
                        WEND
                        IF ik$ = "b" THEN AIColor = 1 ELSE AIColor = 2
                END IF
                CLS
                Board(4, 4) = 1
                Board(5, 5) = 1
                Board(4, 5) = 2
                Board(5, 4) = 2
                Turn = 1
                Pieces(1) = 2
                Pieces(2) = 2
        ELSE
                MouseHide
                CLS
                MouseShow
                Turn = -3
                Pieces(1) = 0
                Pieces(2) = 0
askagain:
                INPUT "What is the name of the game that you would like to load"; path$
                WHILE NOT (LeagFileName(path$))
                        MouseHide
                        CLS
                        MouseShow
                        PRINT "Invalid file path or name."
                        PRINT
                        INPUT "What is the name of the game that you would like to load"; path$
                WEND
                OPEN path$ FOR APPEND AS #1
                IF LOF(1) = 0 THEN
                        MouseHide
                        CLS
                        MouseShow
                        
                        PRINT "The file does not exist.  Choose a valid file."
                        PRINT
                        CLOSE #1
                        GOTO askagain
                END IF
                CLOSE #1
                OPEN path$ FOR INPUT AS #1
                counter = 0
                FOR MRow = 1 TO 8
                        FOR MColumn = 1 TO 8
                                counter = counter + 1
                                LINE INPUT #1, current$
                                Board(MRow, MColumn) = VAL(current$)
                                IF VAL(current$) = 1 THEN
                                        Pieces(1) = Pieces(1) + 1
                                ELSEIF VAL(current$) = 2 THEN
                                        Pieces(2) = Pieces(2) + 1
                                END IF
                        NEXT MColumn
                NEXT MRow
                LINE INPUT #1, current$
                Turn = VAL(current$)
                LINE INPUT #1, current$
                AIPlaying = VAL(current$)
                LINE INPUT #1, current$
                AIColor = VAL(current$)
                CLOSE #1
        END IF
END SUB

If I stop the execution (Ctrl+Scroll) after just clicking New Game, it gives me the out of memory error when I try to edit something. As for the stack, I'm not 100% sure exactly where in the AIMove sub it occurs, and it dosen't occur every time; I just disabled the loop in the AIMove sub (whenever it didn't go, I went for it), and the Out of Stack error didn't occur once. It might have only occured before I added mouse capabilities into the game (when it used on key gosub for the arrow keys; I didn't know what the "real arrow keys" gave to inkey$, so I used on key to trap the num pad arrow keys). If it occurs again (I'll play a few more games), I'll post where it happens in the sub. Thanks again.
Reply


Messages In This Thread
Help with an Othello (AKA Reversi) Game - by Z!re - 08-03-2005, 03:39 AM
Help with an Othello (AKA Reversi) Game - by JasonSG - 08-03-2005, 06:45 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)