在记忆游戏应用中保持状态:翻转卡片时单元格消失

huangapple go评论112阅读模式
英文:

Maintaining State in a Memory Game Application: Cells Disappear when Flipping Cards

问题

我正在开发一个记忆游戏应用程序,其中我有一个名为Board的类负责表示游戏棋盘。但是,我遇到了一个问题,当我尝试根据卡片的ID翻转卡片时,游戏棋盘似乎会失去所有的单元格。

我的Board类的简化版本如下:

public class Board
{
    // ... (现有的代码)

    public bool FlipCard(int cardId)
    {
        var cellToFlip = cells.First(x => x.Id == cardId);
        if (cellToFlip != null) { cellToFlip.IsFlipped = true; return true; }
        return false;
    }
}

问题在于,当我调用FlipCard方法时,_gameBoard实例会失去所有的单元格。我怀疑这可能与请求之间的状态不持久有关。

我尝试使用会话状态来存储游戏棋盘,但它没有按预期工作。游戏棋盘的单元格在请求之间没有保持,导致它们消失。

是否有推荐的方法来在请求之间保持游戏棋盘的状态?如何确保在根据ID翻转卡片时游戏棋盘保留其单元格?

英文:

I'm developing a memory game application in which I have a Board class responsible for representing the game board. However, I'm encountering an issue where the game board seems to lose all its cells when I attempt to flip a card by its ID.

simplified version of my Board class:

public class Board
{
    // ... (existing code)

    public bool FlipCard(int cardId)
    {
        var cellToFlip = cells.First(x => x.Id == cardId);
        if (cellToFlip != null) { cellToFlip.IsFlipped = true; return true; }
        return false;
    }
}

The problem is that when I call the FlipCard method, the _gameBoard instance loses all its cells. I suspect this might be related to state not persisting between requests.

I've attempted to use session state to store the game board, but it didn't work as expected. The game board's cells were not maintained across requests, resulting in their disappearance.

Is there a recommended approach for persisting the game board's state between requests? How can I ensure that the game board retains its cells when flipping a card by ID?

答案1

得分: 1

你需要决定如何存储游戏进度。可以采用多种方式:

  • 在数据库中存储会话。
  • 在客户端存储游戏进度。
  • 其他方式...

其中一种方法:

  1. 当玩家请求“洗牌”时,您需要在某处存储带有新游戏ID的棋盘信息。
  2. 当玩家请求翻牌时,他需要提供游戏ID和卡片ID。您的后端将根据游戏ID找到游戏会话,并根据您的逻辑执行请求的更改。

您的MVC并不会一直保持活动状态,也不像桌面应用程序一样运行。想象一下,每次您的HTTP调用都会从头开始启动MVC应用程序。MVC应用程序生命周期的一些示例:
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/lifecycle-of-an-aspnet-mvc-5-application

https://dotnettutorials.net/lesson/asp-net-mvc-life-cycle/

英文:

You need to decide how do you want to store the gaming progress. It can be done in multiple ways:

  • Store session in a DB.
  • Store gaming progress on client side
  • else...

One of the approaches:

  1. When a player request to "shuffle", you need to store board information with new game id somewhere.
  2. When the player request to flip a card, he needs to provide game id and card id. Your back-end will find game session based on game id and execute requested changes based on your logic.

Your MVC does not stay alive all the time and does not behavies as a desktop application. Imagine that every your http call starts the MVC application from scratch.
Few examples of MVC application life cycle:
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/lifecycle-of-an-aspnet-mvc-5-application

https://dotnettutorials.net/lesson/asp-net-mvc-life-cycle/

huangapple
  • 本文由 发表于 2023年7月6日 22:37:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定