Skip to main content

Strange and inconsistent behavior during modifying an object in JavaScript in Chrome environment [duplicate]

I could not find anything remotely close to this problem anywhere else on the internet. This is simply a bizarre issue. I am making a Chess game using Typescript. I have a Validator class that holds all the states of the game including an object representation for an Chess board, which is modified after each valid move. And that's where the bug occurs. Here is the board object ->

export interface BoardType {
    [file: string]: {
        [rank: string]: string;
    };
}

export const BoardMap: BoardType = {
    a: {
        '1': '',
        '2': '',
        '3': '',
        '4': '',
        '5': '',
        '6': '',
        '7': '',
        '8': '',
    },

then 7 more files. I have a function that takes this board object and adds the pieces according to the given FEN string. That function works correctly and is not relevant here. After running that function with the default FEN the board[a]['1'] would be 'R' (white Rook), board[g]['8'] would be 'n' (black Knight) and so on. Here is the code that updates the board and where the issue is most likely occurring ->

export const updateBoardMap = (
  piece: string,
  origin: string,
  dest: string,
  boardMap: BoardType
): BoardType => {
  // getOriginAndDestInfo works correctly
  const [originFile, originRank, destFile, destRank] = getOriginAndDestInfo(
    origin,
    dest
  );

  let boardMapToUpdate: BoardType = { ...boardMap };

  // Empty the original square and place the piece on the destination square
  boardMapToUpdate[originFile][originRank] = '';
  boardMapToUpdate[destFile][destRank] = piece;

  return boardMapToUpdate;
};

This updateBoardMap function is called here ->

private NewMove(): void {
    // Update the board map
    const updatedBoardMap = helpers.updateBoardMap(
      this.movingPiece,
      this.movingPiecesOrigin,
      this.movingPiecesDest,
      this.boardMap
    );
    
    this.boardMap = { ...updatedBoardMap };
    
    // Toggle the color's turn
    this.whitesTurn = !this.whitesTurn;
}

This is only called only if the move is valid. Alright, so here is the actual issue. The function updates the board most of the time correctly, about 50% of the time, but for the rest of the cases, it adds random pieces on random squares on the board with no pattern whatsoever. For example, if I play the move 1. e4, it sometimes adds a black pawn on e6 (the original e7 pawn is still there though) or a King on d5 etc. If I reload the page and play 1. e4 again, it might put pieces on totally random squares or will not do anything odd at all and work perfectly. I will post two scrreenshots of what it looks like.

Picture of bug 1

Picture of bug 1

You will see that after the first move, it adds random pieces on random squares. This is incredibly frustrating. This happens ONLY in the browser environment (Chrome) and never on Node. And since the validity of a move depends on boardMap I might not be able to play moves that should be valid. Like,if it puts a pawn on e6, I won't be able to play 1. ..e5 even though that move would have been valid without the bug.

I have 0 idea why this occurs. This is not a very complicated operation. I know that objects are passed by references so I am using the spread operator {...} both times. I have wrote extensive tests for this function with many moves and all of them pass. This issue never occurs in Node as I mentioned earlier. I have tested it in different ways many, many times and it always works. But as soon as I am on the browser, it shows up. There is no consistency or pattern and honestly I am out of ideas to solve this. I would highly appreciate someone solving this mystery for me. Thank you.

Via Active questions tagged javascript - Stack Overflow https://ift.tt/MqyfUEt

Comments

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data