Need help with Chess Program I am writing

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
qbguy
Coder
Posts: 14
Joined: Tue Dec 26, 2006 7:27 pm

Post by qbguy » Sat Dec 30, 2006 5:18 pm

OK, I found two mistakes in the code I wrote and fixed them, but when I run it, it still doesn't work. HELP!!!

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Mon Jan 01, 2007 7:17 pm

qbguy wrote:OK, I found two mistakes in the code I wrote and fixed them, but when I run it, it still doesn't work. HELP!!!
i'll try to wade through it this weekend(if i have time)

qbguy
Coder
Posts: 14
Joined: Tue Dec 26, 2006 7:27 pm

Post by qbguy » Thu Jan 04, 2007 5:18 pm

sid6.7 wrote:
qbguy wrote:OK, I found two mistakes in the code I wrote and fixed them, but when I run it, it still doesn't work. HELP!!!
i'll try to wade through it this weekend(if i have time)
Good because I have no clue how to fix it myself.

User avatar
seaBiscuit$
Coder
Posts: 45
Joined: Sun Nov 26, 2006 2:00 pm
Location: Champaign, Illinois

Post by seaBiscuit$ » Thu Jan 04, 2007 7:02 pm

qbguy wrote:
sid6.7 wrote:
qbguy wrote:OK, I found two mistakes in the code I wrote and fixed them, but when I run it, it still doesn't work. HELP!!!
i'll try to wade through it this weekend(if i have time)
Good because I have no clue how to fix it myself.
If you wrote it then you can fix it!

Just use PRINT to display the values of critical variables as it's executing, so rather than weeding through the code, you can (hopefully) catch it in the act and narrow it down to a certain SUB or line of code.

If the problem occurs when the player tries to make a move, then start with the subs it runs that handle input, check for legal moves, print the new board, etc.

qbguy
Coder
Posts: 14
Joined: Tue Dec 26, 2006 7:27 pm

Post by qbguy » Fri Jan 05, 2007 9:10 pm

Found two more mistakes, but still have the same problem with the computer saying B8-A8. [ignore everything after this; I have fixed it.]Also, the board is not updated after E2-E4. I did not notice this earlier because the colors were messed up.
Last edited by qbguy on Sat Jan 06, 2007 10:56 pm, edited 1 time in total.

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Sat Jan 06, 2007 7:21 pm

qbguy wrote:Found two more mistakes, but still have the same problem with the computer saying B8-A8. Also, the board is not updated after E2-E4. I did not notice this earlier because the colors were messed up.
could you print the error fixes so i dont have to redo everything you've done?

qbguy
Coder
Posts: 14
Joined: Tue Dec 26, 2006 7:27 pm

Post by qbguy » Sat Jan 06, 2007 10:55 pm

sid6.7 wrote:
qbguy wrote:Found two more mistakes, but still have the same problem with the computer saying B8-A8. Also, the board is not updated after E2-E4. I did not notice this earlier because the colors were messed up.
could you print the error fixes so i dont have to redo everything you've done?
here is the fully updates source code:
{deleted because found another mistake}
I also fixed the thing where White's move does not show up.
Last edited by qbguy on Sun Jan 07, 2007 1:17 pm, edited 1 time in total.

Antoni
Veteran
Posts: 132
Joined: Wed Jun 15, 2005 3:01 pm
Contact:

Post by Antoni » Sun Jan 07, 2007 7:35 am

You could have edited your first post and updated the code there...

qbguy
Coder
Posts: 14
Joined: Tue Dec 26, 2006 7:27 pm

Found another mistake

Post by qbguy » Sun Jan 07, 2007 1:16 pm

I found another mistake in the movelist SUB and fixed the KNIGHT subroutine so that it works.

qbguy
Coder
Posts: 14
Joined: Tue Dec 26, 2006 7:27 pm

Post by qbguy » Sun Jan 07, 2007 9:53 pm

there is still a bug with the AI. Also, i noticed that the queen cannot move like a rook because of a problem in the rook sub.

qbguy
Coder
Posts: 14
Joined: Tue Dec 26, 2006 7:27 pm

Post by qbguy » Sat Jan 13, 2007 11:50 am

Fixed the rook sub
Last edited by qbguy on Sat Jan 20, 2007 2:55 pm, edited 1 time in total.

nkk_kan
Veteran
Posts: 57
Joined: Thu Jun 01, 2006 10:45 am
Location: Gujrat,India,Asia
Contact:

Post by nkk_kan » Sat Jan 13, 2007 12:28 pm

(* PSst Qbguy! Do you see that Edit button beside your post? you can edit your post from there! *)

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Sat Jan 13, 2007 10:24 pm

yeah qbguy just re-edit your one post of code with the fixes
so the thread dont get so long....

i removed the other two that were older and left the last one

...

qbguy
Coder
Posts: 14
Joined: Tue Dec 26, 2006 7:27 pm

Post by qbguy » Sat Jan 20, 2007 2:55 pm

Found another mistake but now it makes multiple moves:

Code: Select all

DECLARE SUB SQUARE (A, B, C)
DECLARE SUB SHOWMAN (A, B, FLAG)
DECLARE SUB SHOW ()
DECLARE SUB IO (A, B, X, Y, RESULT)
DECLARE FUNCTION INCHECK (X)
DECLARE SUB MAKEMOVE (A, B, X, Y)
DECLARE SUB KNIGHT (A, B, XX(), YY(), NDX)
DECLARE SUB KING (A, B, XX(), YY(), NDX)
DECLARE SUB QUEEN (A, B, XX(), YY(), NDX)
DECLARE SUB ROOK (A, B, XX(), YY(), NDX)
DECLARE SUB BISHOP (A, B, XX(), YY(), NDX)
DECLARE SUB MOVELIST (A, B, XX(), YY(), NDX)
DECLARE SUB PAWN (A, B, XX(), YY(), NDX)
DECLARE FUNCTION EVALUATE (ID, PRUNE)
DIM SHARED BOARD(0 TO 7, 0 TO 7)
DIM SHARED BESTA(0 TO 7), BESTB(0 TO 7), BESTX(0 TO 7), BESTY(0 TO 7)
DIM SHARED LEVEL, MAXLEVEL, SCORE, CFLAG
CFLAG = 0
LEVEL = 0: MAXLEVEL = 5
DATA -500,-270,-300,-900,-7500,-300,-270,-500
DATA -100,-100,-100,-100, -100,-100,-100,-100
DATA 0, 0, 0, 0, 0, 0, 0, 0
DATA 0, 0, 0, 0, 0, 0, 0, 0
DATA 0, 0, 0, 0, 0, 0, 0, 0
DATA 0, 0, 0, 0, 0, 0, 0, 0
DATA 100, 100, 100, 100, 100, 100, 100, 100
DATA 500, 270, 300, 900, 5000, 300, 270, 500
FOR X = 0 TO 7
FOR Y = 0 TO 7
READ Z
BOARD(X, Y) = Z
NEXT Y
NEXT X
A = -1: RESULT = 0
DO
SCORE = 0
CALL IO(A, B, X, Y, RESULT)
CLS : CALL SHOW
RESULT = EVALUATE(-1, 10000)
A = BESTA(1): B = BESTB(1): X = BESTX(1): Y = BESTY(1)
LOOP

SUB BISHOP (A, B, XX(), YY(), NDX)
ID = SGN(BOARD(B, A))
FOR DXY = 1 TO 7
X = A - DXY: Y = B + DXY
IF X <0> 7 OR Y <0> 7 THEN EXIT FOR
GOSUB 3
IF BOARD(Y, X) <> 0 THEN EXIT FOR
NEXT
FOR DXY = 1 TO 7
X = A + DXY: Y = B + DXY
IF X <0> 7 OR Y <0> 7 THEN EXIT FOR
GOSUB 3
IF BOARD(Y, X) <> 0 THEN EXIT FOR
NEXT
FOR DXY = 1 TO 7
X = A - DXY: Y = B - DXY
IF X <0> 7 OR Y <0> 7 THEN EXIT FOR
GOSUB 3
IF BOARD(Y, X) <> 0 THEN EXIT FOR
NEXT
FOR DXY = 1 TO 7
X = A + DXY: Y = B - DXY
IF X <0> 7 OR Y <0> 7 THEN EXIT FOR
GOSUB 3
IF BOARD(Y, X) <> 0 THEN EXIT FOR
NEXT
EXIT SUB
3 REM
IF ID <> SGN(BOARD(Y, X)) THEN
NDX = NDX + 1: XX(NDX) = X: YY(NDX) = Y
END IF
RETURN
END SUB

FUNCTION EVALUATE (ID, PRUNE)
DIM XX(27), YY(27)
LEVEL = LEVEL + 1
BESTSCORE = INT(1000 * ID)
FOR B = 7 TO 0 STEP -1
FOR A = 7 TO 0 STEP -1
IF SGN(BOARD(B, A)) <> ID THEN GOTO 1
IF LEVEL = 1 THEN CALL SHOWMAN(A, B, 8)
CALL MOVELIST(A, B, XX(), YY(), NDX)
FOR I = 0 TO NDX
X = XX(I): Y = YY(I)
IF LEVEL = 1 THEN
LOCATE 1, 1
PRINT "TRYING:"; CHR$(65 + A); 8 - B; "-"; CHR$(65 + X); 8 - Y
CALL SHOWMAN(X, Y, 8)
END IF
OLDSCORE = SCORE: MOVER = BOARD(B, A): TARGET = BOARD(X, Y)
CALL MAKEMOVE(A, B, X, Y)
IF (LEVEL < MAXLEVEL) THEN SCORE = SCORE + EVALUATE(INT(-1 * ID), BESTSCORE - TARGET + INT(ID * (8 - INT(ABS(4 - X)) - INT(ABS(4 - Y)))))
IF (ID <0> BESTSCORE) OR (ID > 0 AND SCORE < BESTSCORE) THEN
BESTA(LEVEL) = A: BESTB(LEVEL) = B
BESTX(LEVEL) = X: BESTY(LEVEL) = Y
BESTSCORE = SCORE
IF (ID <0>= PRUNE) OR (ID > 0 AND BESTSCORE <PRUNE>= 0 THEN GOTO 6
CALL MOVELIST(A, B, XX(), YY(), NDX)
FOR I = 0 TO NDX STEP 1
X = XX(I): Y = YY(I)
IF BOARD(Y, X) = 5000 THEN
PRINT "YOU ARE IN CHECK!"
PRINT " "
PRINT " "
INCHECK = 1
EXIT FUNCTION
END IF
NEXT
6 NEXT
NEXT
INCHECK = 0
END FUNCTION

SUB IO (A, B, X, Y, RESULT)
DIM XX(0 TO 26), YY(0 TO 26)
CLS
IF A >= 0 THEN
IF RESULT < -2500 THEN
PRINT "I RESIGN"
SLEEP
SYSTEM
END IF
PIECE = BOARD(Y, X)
CALL MAKEMOVE(A, B, X, Y)
PRINT "MY MOVE: "; CHR$(65 + A); 8 - B; "-"; CHR$(65 + X); 8 - Y
IF PIECE <> 0 THEN
PRINT "I TOOK YOUR ";
IF PIECE = 100 THEN PRINT "PAWN"
IF PIECE = 270 THEN PRINT "KNIGHT"
IF PIECE = 300 THEN PRINT "BISHOP"
IF PIECE = 500 THEN PRINT "ROOK"
IF PIECE = 900 THEN PRINT "QUEEN"
IF PIECE = 5000 THEN PRINT "KING"
END IF
NULL = INCHECK(0)
END IF
DO
CALL SHOW
LOCATE 24, 1: INPUT "YOUR MOVE: ", IN$
IF UCASE$(IN$) = "QUIT" THEN CLS : END
IF UCASE$(IN$) = "O-O" OR IN$ = "0-0" THEN
IF CFLAG <> 0 THEN GOTO 16
IF BOARD(7, 7) <> 500 THEN GOTO 16
IF BOARD(7, 6) <> 0 OR BOARD(7, 5) <> 0 THEN GOTO 16
BOARD(7, 6) = 5000
BOARD(7, 4) = 0
BOARD(7, 5) = 500
BOARD(7, 7) = 0
CFLAG = 1: EXIT SUB
END IF
IF UCASE$(IN$) = "O-O-O" OR IN$ = "0-0-0" THEN
IF CFLAG <> 0 THEN GOTO 16
IF BOARD(7, 0) <> 500 THEN GOTO 16
IF BOARD(7, 1) <> 0 OR BOARD(7, 2) <> 0 OR BOARD(7, 3) <> 0 THEN GOTO 16
BOARD(7, 2) = 5000
BOARD(7, 4) = 0
BOARD(7, 3) = 500
BOARD(7, 0) = 0
CFLAG = 1: EXIT SUB
END IF
IF LEN(IN$) <5> 7 OR B <0> 7 OR A <0> 7 OR X <0> 7 OR Y < 0 THEN GOTO 16
IF BOARD(B, A) <= 0 THEN GOTO 16
CALL MOVELIST(A, B, XX(), YY(), NDX)
FOR K = 0 TO NDX STEP 1
IF X = XX(K) AND Y = YY(K) THEN
MOVER = BOARD(B, A): TARGET = BOARD(Y, X)
CALL MAKEMOVE(A, B, X, Y)
LOCATE 1, 1: IF INCHECK(0) = 0 THEN EXIT SUB
BOARD(B, A) = MOVER: BOARD(Y, X) = TARGET
GOTO 16
END IF
NEXT
16 CLS
LOOP
END SUB

SUB KING (A, B, XX(), YY(), NDX)
ID = SGN(BOARD(B, A))
FOR DY = -1 TO 1
IF B + DY <0> 7 THEN GOTO 12
FOR DX = -1 TO 1
IF A + DX <0> 7 THEN GOTO 11
IF ID <> SGN(BOARD(B + DY, A + DX)) THEN
NDX = NDX + 1: XX(NDX) = A + DX
YY(NDX) = B + DY
END IF
11 NEXT
12 NEXT
END SUB

SUB KNIGHT (A, B, XX(), YY(), NDX)
ID = SGN(BOARD(B, A))
X = A - 1: Y = B - 2: GOSUB 5
X = A - 2: Y = B - 1: GOSUB 5
X = A + 1: Y = B - 2: GOSUB 5
X = A + 2: Y = B - 1: GOSUB 5
X = A - 1: Y = B + 2: GOSUB 5
X = A - 2: Y = B + 1: GOSUB 5
X = A + 1: Y = B + 2: GOSUB 5
X = A + 2: Y = B + 1: GOSUB 5
EXIT SUB
5 REM
IF X <0> 7 OR Y <0> 7 THEN RETURN
IF ID <SGN>= 0 AND (A - 1) <7>= 0 AND (B - ID) <7>= 0 AND (A + 1) <7>= 0 AND (B - ID) <7>= 0 AND A <7>= 0 AND (B - ID) <= 7 THEN
IF BOARD((B - ID), A) = 0 THEN
NDX = NDX + 1: XX(NDX) = A: YY(NDX) = B - ID
IF (ID <0> 0 AND B = 6) THEN
IF BOARD((B - ID - ID), A) = 0 THEN
NDX = NDX + 1: XX(NDX) = A: YY(NDX) = B - ID - ID
END IF
END IF
END IF
END IF

END SUB

SUB QUEEN (A, B, XX(), YY(), NDX)
CALL BISHOP(A, B, XX(), YY(), NDX)
CALL ROOK(A, B, XX(), YY(), NDX)
END SUB

SUB ROOK (A, B, XX(), YY(), NDX)
ID = SGN(BOARD(B, A))
FOR X = A - 1 TO 0 STEP -1
  IF ID <> SGN(BOARD(B, X)) THEN
    NDX = NDX + 1: XX(NDX) = X: YY(NDX) = B
  END IF
  IF BOARD(B, X) <> 0 THEN EXIT FOR
NEXT
FOR X = A + 1 TO 7 STEP 1
  IF ID <> SGN(BOARD(B, X)) THEN
    NDX = NDX + 1: XX(NDX) = X: YY(NDX) = B
  END IF
  IF BOARD(B, X) <> 0 THEN EXIT FOR
NEXT
FOR Y = B - 1 TO 0 STEP -1
  IF ID <> SGN(BOARD(Y, A)) THEN
    NDX = NDX + 1: XX(NDX) = A: YY(NDX) = Y
  END IF
  IF BOARD(Y, A) <> 0 THEN EXIT FOR
NEXT
FOR Y = B + 1 TO 7 STEP 1
  IF ID <> SGN(BOARD(B, A)) THEN
    NDX = NDX + 1: XX(NDX) = A: YY(NDX) = Y
  END IF
  IF BOARD(Y, A) <> 0 THEN EXIT FOR
NEXT
END SUB

SUB SHOW
LOCATE 3, 30
COLOR 7, 0
PRINT "A  B  C  D  E  F  G  H"
FOR K = 0 TO 25
LOCATE 4, 28 + K
COLOR 6, 0
PRINT CHR$(220)
NEXT
FOR B = 0 TO 7
LOCATE 2 * B + 5, 26
COLOR 7, 0
PRINT CHR$(56 - B)
LOCATE 2 * B + 5, 28
COLOR 6, 0
PRINT CHR$(219)
LOCATE 2 * B + 6, 28
COLOR 6, 0
PRINT CHR$(219)
FOR A = 0 TO 7
IF ((A + B) MOD 2) <> 0 THEN
COLOUR = 8
ELSE COLOUR = 12
END IF
CALL SQUARE(3 * A + 31, 2 * B + 5, COLOUR)
NEXT
LOCATE 2 * B + 5, 53
COLOR 6, 0
PRINT CHR$(219)
LOCATE 2 * B + 6, 53
COLOR 6, 0
PRINT CHR$(219)
LOCATE 2 * B + 6, 55
COLOR 7, 0
PRINT CHR$(56 - B)
NEXT
FOR K = 0 TO 25
LOCATE 21, 28 + K
COLOR 6, 0
PRINT CHR$(223)
NEXT
LOCATE 22, 30
COLOR 7, 0
PRINT "A  B  C  D  E  F  G  H"
FOR B = 0 TO 7
FOR A = 0 TO 7
CALL SHOWMAN(A, B, 0)
NEXT
NEXT
COLOR 7, 0
END SUB

SUB SHOWMAN (A, B, FLAG)
IF BOARD(B, A) <0> 0 THEN BACK = 7
FORE = 7 - BACK + FLAG
IF BOARD(B, A) = 0 THEN
IF ((A + B) MOD 2) <0> 0)
END IF
N$ = " "
PIECE = INT(ABS(BOARD(B, A)))
IF PIECE = 0 THEN N$ = CHR$(219)
IF PIECE = 100 THEN N$ = "P"
IF PIECE = 270 THEN N$ = "N"
IF PIECE = 300 THEN N$ = "B"
IF PIECE = 500 THEN N$ = "R"
IF PIECE = 900 THEN N$ = "Q"
IF PIECE = 5000 OR PIECE = 7500 THEN N$ = "K"
LOCATE 2 * B + 5 - (BOARD(B, A) > 0), 3 * A + 30
COLOR FORE, BACK
PRINT N$
LOCATE 1, 1
COLOR 7, 0
END SUB

SUB SQUARE (A, B, C)
MT$ = CHR$(219): MT$ = MT$ + MT$ + MT$
LOCATE B, A - 2
COLOR C, C
PRINT MT$
LOCATE B + 1, A - 2
COLOR C, C
PRINT MT$
COLOR 7, 0
END SUB


Post Reply