You are browsing as a guest. Sign up (or log in) to start making projects!

3h 7m 33s logged

Mostly cleaning up my messy code 🧹

Updates:

  • Made bishop not able to pass through pieces
  • Made the code creating rook destination squares much cleaner
  • Made the code creating bishop destination squares much cleaner
  • Fixed rook and bishop not creating destination sqaures at the edges of the board
  • Made code for creating knight destination squares cleaner and fix if statement condition of rook in ChessboardContext.jsx
  • Made code for creating pawn destination squares cleaner and make pawn unable to pass through pieces
  • Fixed bug while clicking on a piece when another piece is already selected

In last devlog I made the rook unable to pass through pieces so it was very logical to do the same to bishop. Making the bishop unable to pass through pieces was pretty simple as the code wasn’t much different from the rook one.


Idk what clicked in me after doing that I started to thinking about making the rook destionation squares creation code cleaner. The code was very messy and repitetive and I believed it can made cleaner and smaller. I used 4 while loops before which had almost the same code but different values and slightly different calculations. After thinking and trying I turned the 4 while loops in to a single while loop. Now the code looks much better.


Seeing the success in rook destination squares creation code I then moved on to the bishop’s code. Made similar changes to the code as the rook’s code. Also i believe the queen works perfectly as it’s mixture of rook and bishop code but I haven’t really tested it on board.


After cleaning up both rook and bishop I found a small bug. The bug appeared because of my slight mistake while trying to make the code better. Basically the rooke and bishop weren’t creating any destination square at the edges of the board. The fix was simple so this didn’t take long to fix.


I already didn’t like the knight’s destination squares creation code. Me cleaning up the rook and bishop code gave me the motivation to make the knights code better. It did take some time for me for cleaning up that messy and tangled code. I was using break keyword for rook and bishop so I also used break for stopping the loop but I didn’t want the loop to stop completely I just needed to skip the loop if there way a piece or if the knight was on the edge of the board. After a quick search I found that continue keyword does the job.


The pawn code wasn’t that messy but it could definetely do better. I mainly changed the pawn’s code cuz I wanted the pawn to also be unable to pass through pieces. Before there were two loops now I one loop does the job. Then I also made the pawn unable to pass through pieces by breaking the loop.


Fixed the annoying bug. If a piece is clicked when another piece is already selected then it looks like both pieces are selected. Desitnation of both pieces are shown and both pieces are hightlighted. So, as clicking on a square reset the selected piece, I reset the previous piece if user selected another piece with a small change.


Now I’m trying to making all these things work from the black side as well. Okay this much for this devlog. See you on the next one. Biee…👋

0
118

Comments 1

@PAPER_01

\0=0/