简体   繁体   English

NodeJS Tic Tac Toe多人游戏

[英]NodeJS Tic Tac Toe multiplayer Game

im really downhearted, i cant solve this, im trying it since days, im developing a game (tic tac toe) it has multiplayer feature using nodeJS, also the problem is not the game, the problem is handling different rooms... 我真的很心灰意冷,我无法解决这个问题,即时通讯尝试它,即时开发游戏(tic tac toe)它有使用nodeJS的多人游戏功能,问题不在于游戏,问题在于处理不同的房间......

Im using socket.join etc, so user can join different room; 我使用socket.join等,所以用户可以加入不同的房间; to join different games also boards i pass through url game name, example: 加入不同的游戏也是我通过url游戏名称的板子,例如:

localhost?gameId=test 本地主机?游戏ID =测试

Then i parse this name and start sending board to these users. 然后我解析这个名字并开始向这些用户发送电路板。 Also the problem is, when i have more then one more, the game collapses, also the board info from room 1 collapse with room 2... i cant find the error, i paste the code here: 问题是,当我有一个以上,游戏崩溃,房间1的董事会信息崩溃与房间2 ...我无法找到错误,我在这里粘贴代码:

Create.html jsfiddle.net/svaae1vL/ Create.html jsfiddle.net/svaae1vL/

Enter.html jsfiddle.net/6qzbpbxx/ Enter.html jsfiddle.net/6qzbpbxx/

Server.js jsfiddle.net/1q0qo8xo/ Server.js jsfiddle.net/1q0qo8xo/

Like i sayd before, the problem is: 就像我之前说的那样,问题是:

room1: player1, player2 room2: player3, player4 room1:player1,player2 room2:player3,player4

room1:
[x,o,x]
[0, 0, 0]
[x,x,x]

room2:
[x,o,x]
[0, 0, 0]
[x,x,x]

Also when i click in room1, it affects room2, please help im stuck since days... 此外,当我点击room1,它影响room2,请帮助我卡住自天...

I believe your problem is that you are sharing variable emptyBoard in createRoom event: 我相信你的问题是你在createRoom事件中共享变量emptyBoard

boards[data.name] = emptyBoard;

Therefore, if you edit one board, it will edit all the others too. 因此,如果您编辑一块电路板,它也会编辑所有其他电路板。 Try changing the assignment to: 尝试将作业更改为:

boards[data.name] = emptyBoard.slice(0);

That should clone the array into another object. 那应该将数组克隆到另一个对象中。

Edit: 编辑:

Cloning the array with slice(0) won't clone objects in the array, so perhaps what you need is deep cloning, eg as described here . 克隆与阵列slice(0)阵列中不会克隆对象,因此,或许你需要的是深刻的克隆,如所描述这里

@Dygestor @Dygestor

Hey thank you so much, well, this line: 嘿,非常感谢你,好吧,这句话:

boards[data.name] = emptyBoard.slice(0); boards [data.name] = emptyBoard.slice(0);

Haven't fixed it, but the problem was what you sayd, so i tried this: 没有修复它,但问题是你说的,所以我试过这个:

    // Initalize board.
    boards[data.name] = [
                ['', '', ''],
                ['', '', ''],
                ['', '', '']
            ];

And it worked, thank you so much! 它工作,非常感谢你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM