0

I'm creating a simple chess board and have created objects for squares and chess pieces, like so...

let a1 = {occupiedBy: "whiteRook1", north1: "a2", east1: "b1" ... } and so on

and

let whiteRook1 = {square: a1, value 5, alive: true ... } and so on 

I need both objects in the game, but if I need to look for example which piece is in a1 square and if it is alive, I would need to do:

console.log(eval(a1.occupiedBy).alive) // true

and if I would like to see if the square in front of a1 is free:

console.log(eval(a1.north1).occupiedBy != none) // false

Also, I can't make a1 as a child of whiteRook1 and whiteRook1 as a child of a1 simultaneously. At least I haven't figured out a workaround how to do that. With the square being the child of the piece, at least I can find the adjacent squares withtout eval() like this:

console.log(whiteRook1.square.north1) // "a2"

Main problem is the speed of the calculation, especially if I would like to have depth in calculating every move into the future. Eval() is slow and bad (takes 20s to calculate every 400 moves in depth 2), and also it would be slow to filter all 64 squares to see if whiteRook1 is their children per every calculation.

I know this is not the best way to create a chessboard, but do you have any solutions for a problem like this?

panup
  • 11
  • 3
  • 1
    Don't use numeric-indexed variable names like that - use an array instead (or object), and then lookup is trivial with bracket notation – CertainPerformance Nov 03 '22 at 22:49
  • For me, a chessboard would be a simple 2D array with [8][8] sizes, of objects such as you posted, with 'alive', and some piece-specific name such as WRQ (white rook queen). Then A1 would be [0,0] and F8 [7,7]. – Aid Hadzic Nov 03 '22 at 22:57

0 Answers0