? Grid help (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password
This website has been moved, please see this post

Grid help

  • #1 ✎ 3 BlasticusSaturn Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Pokemon Is Awesome! I love Pokemon! Express Yourself Drawing I like to draw! Hobbies I'm making a chess game, and the problem I ran into was how I would organize the "grid" of the board. I need a way for it to match up the cursor location with a grid square and from there determine whether or not a piece is there, and if one is, which type. The answer is probably Arrays, but how would I use them to achieve what I want? Posted
  • #2 ✎ 1882 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself You should use an 8x8 array: DIM BOARD[8,8]Access each square with BOARD[Y,X] (yes, Y and X are reversed) You could use numbers to store which piece is on each spot, like 0=empty, 1=white pawn, 2=white rook, etc. (or use a string array and use the names of the pieces) And to fill the array with pieces at the start, you should use COPY: COPY BOARD,@RESET @RESET DATA 2,3,4,5,6,4,3,2 DATA 1,1,1,1,1,1,1,1 DATA 0,0,0,0,0,0,0,0 DATA ... Posted
  • #3 ✎ 3 BlasticusSaturn Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Pokemon Is Awesome! I love Pokemon! Express Yourself Drawing I like to draw! Hobbies thanks, that makes sense. if anyone else sees this and think they have another good method, don't hesitate to share it. it'll only widen my understanding. Posted
  • #4 ✎ 94 Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies This is what I recommend doing: These are just things that I have done: Create the board array as a one-dimensional array from 0 to 63 Define the black and white pieces in alternating order, so
    1. 0=Empty Space
    2. 1=Black Pawn
    3. 2=White Pawn
    4. 3=Black Knight
    5. 4=White Knight
    6. 5=Black Bishop
    7. 6=White Bishop
    8. 7=Black Rook
    9. 8=White Rook
    10. 9=Black Queen
    11. 10=White Queen
    12. 11=Black King
    13. 12=White King
    Then the code would work as so: DIM BOARD[64] RESETBOARD DEF RESETBOARD RESTORE @NEWBOARD FOR I=0 TO 63 READ BOARD[I] NEXT @NEWBOARD DATA 7,3,5,11,9,5,3,7 DATA 1,1,1,1,1,1,1,1 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 2,2,2,2,2,2,2,2 DATA 8,4,6,12,10,6,4,8 END 'Here are a few extra functions that may help you 'FAST FUNCTION TO CHECK IF AN X,Y POSITION IS WITHIN BOUNDS OF THE BOARD DEF INBOUNDS(X,Y) RETURN X>=0&&Y>=0&&X<8&&Y<8 END 'EXTRACT THE X VALUE OUT OF AN INDEX DEF GETXINDEX(N) IF N>=0&&N<64 THEN RETURN N MOD 8 ELSE RETURN -1 END 'EXTRACT THE Y VALUE OUT OF AN INDEX DEF GETYINDEX(N) IF N>=0&&N<64 THEN RETURN N DIV 8 ELSE RETURN -1 END 'CONVERT FROM X AND Y TO A SINGLE INDEX FROM 0-63 DEF GETINDEX(X,Y) IF !INBOUNDS(X,Y) THEN RETURN -1 RETURN Y*8+X END '-1=ERROR '0=PAWN '1=KNIGHT '2=BISHOP '3=ROOK '4=QUEEN '5=KING DEF GETPIECE(X,Y) IF !INBOUNDS(X,Y) THEN RETURN -1 RETURN (BOARD[Y*8+X]-1) DIV 2 END '-1=ERROR '0=BLACK '1=WHITE DEF GETCOLOR(X,Y) IF !INBOUNDS(X,Y) THEN RETURN -1 RETURN BOARD[Y*8+X] MOD 2 END If you are trying to build a chess AI
    1. Create a function that can list all the possible positions a piece can be placed with the current board situation
    2. Create a scoring system to determine which of those moves will give you the most points
    3. Find a minimax algorithm, preferably alpha-beta-pruning, which will use logic to search a certain depth (like 5 moves ahead) in a tree-like structure to rank each path and find the path that has the highest score.
    Sorry I got a little carried away, but I hope this helps you at least a little bit.
    Posted Edited by Simeon
  • #5 ✎ 1130 snail_ Power User QSP Contest 1 Contest Participant I participated in the first SmileBASIC Source QSP Contest! Helper Received for being very helpful around SmileBASIC Source Achievements Amazing Contributor Someone thinks I'm an awesome person who has done so much for the community! Achievements Why use specifically a 1D array for your approach instead of a 2D [Y,X] one like 12Me did? Here they should be identical aside from how you're indexing them, unless I'm missing something. Also instead of doing this here: RESTORE @NEWBOARD FOR I=0 TO 63 READ BOARD[I] NEXT You can do this: COPY BOARD, @NEWBOARD EDIT: 12Me already mentioned COPY but oh well. Posted Edited by snail_
  • #6 ✎ 94 Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies I chose one dimensional so that positions on the board can be a single number instead of two It's not a big deal, but I find it easier to manage when dealing with more complex functions Like making a function GETBESTMOVE, you can simply make it return one number and avoid the OUT X,Y at the end I just thought it'd be simpler on the user Also that array initialization... Wow! Efficiency That's a neat trick Posted
  • #7 ✎ 1882 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself I thought it might be better to number the pieces like 0=empty, 1=white pawn, -1=black pawn, 2=white rook, -2=black rook, etc. That way you can check the owner of a piece with SGN, and the type of piece with ABS. DIM BOARD[8,8] COPY BOARD,@RESET ABS(BOARD[Y,X]) 'get piece SGN(BOARD[Y,X]) 'get color When moving a piece, SWAP is probably the best way: 'check color of piece at new position VAR COL=SGN(BOARD[NEWY,NEWX]) 'if the space doesn't have one of your own pieces IF COL!=PLAYER THEN 'if space has one of the other player's pieces, capture it IF COL==-PLAYER THEN BOARD[NEWY,NEWX]=0 'move your piece SWAP BOARD[OLDY,OLDX],BOARD[NEWY,NEWX] ENDIF Posted Edited by 12Me21
  • #8 ✎ 212 seggiepants I didn't know you could put Data in a function or the copy trick. Very nice. My only addition to the thread would be to make some named pseudo constants so your code is more readable. Something like if piece == KNIGHT_BLACK then is much easier to read when debugging months later than if piece == -24 then. I like the sign idea too, very nice. Posted
  • #9 ✎ 1882 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself
    I didn't know you could put Data in a function or the copy trick. Very nice. My only addition to the thread would be to make some named pseudo constants so your code is more readable. Something like if piece == KNIGHT_BLACK then is much easier to read when debugging months later than if piece == -24 then. I like the sign idea too, very nice.
    Yeah, that would be a good idea. Maybe just KNIGHT for white and -KNIGHT for black would be enough. oh, and if you're going to use the default chess pieces, you should number them in the same order, which is pawn, rook, knight, bishop, queen, king
    Posted Edited by 12Me21
  • #10 ✎ 3 BlasticusSaturn Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Pokemon Is Awesome! I love Pokemon! Express Yourself Drawing I like to draw! Hobbies thanks again for all the help. I didn't expect so many people to pitch in. Posted
  • #11 ✎ 94 Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies So many excellent ideas here! Posted