简体   繁体   English

C#如何“保存”随机生成的Class对象的特定副本?

[英]C# How can I “save” a specific copy of a randomly generated Class object?

I am making a battleship game in console and I would like to implement a replay-system, where I could rewatch games. 我正在用控制台制作战舰游戏,我想实现一个重播系统,在这里我可以重新观看比赛。 For this I use StreamWriter to record the player's moves, and in a different loop, a StreamReader uses these to 'replay' the game on screen. 为此,我使用StreamWriter记录玩家的动作,并在另一个循环中,StreamReader使用这些动作在屏幕上“重播”游戏。 However, the position of the ships is not the same, because it is generated when a new game is started. 但是,船只的位置不同,因为它是在开始新游戏时生成的。 The NEWGAME loop is the same as the REPLAY, but in the replay the comp automatically makes the moves via the streamreader, however, the map is different because it is always randomly generated. NEWGAME循环与REPLAY相同,但是在重播中,复合音色会通过流读取器自动进行移动,但是,地图始终是随机生成的,因此有所不同。 My question is that how can I save an exact copy of a 'map' that is in this situation a class? 我的问题是,在这种情况下,如何保存“地图”的精确副本? Thanks in advance! 提前致谢! If you have any questions I can answer. 如果您有任何疑问,我可以回答。

switch (x)
        {
            case "newgame":
        Mezo Játékos2Hajói= new Mezo();
        Mezo Játékos1AmitLát = new Mezo();
        Mezo Játékos1Hajói = new Mezo();
        Mezo Játékos2AmitLát = new Mezo();
....

This generates the field for the new game, then come the rest, player attacks etc. 这样就为新游戏生成了场,然后剩下的就是玩家攻击了。

The replay case works the same as a new game, but the attacks are inserted by the computer from a streamreader. 重播案例与新游戏的工作原理相同,但是攻击是由计算机从流读取器插入的。

        case "replay":
        Mezo Játékos2VHajói= new Mezo();
        Mezo Játékos1VAmitLát = new Mezo();
        Mezo Játékos1VHajói = new Mezo();
        Mezo Játékos2VAmitLát = new Mezo();

The problem is here. 问题在这里。 This case generates a new for itself, because it wouldn't work, but here I would like to use the one used in newgame". 这种情况会自行产生一个新的情况,因为它不起作用,但是在这里我要使用newgame中使用的一个。”

This is exactly the kind of thing that the Command Pattern is great for. 这正是“ 命令模式”最适合的一种方式。 Here's a simplified version of something you could do: 这是您可以做的事情的简化版:

interface ICommand { void Execute(); }

class PlaceShip : ICommand
{
    int x;
    int y;
    Ship ship;

    public PlaceShip(int x, int y, Ship ship)
    {
        // Initialize fields
    }

    public void Execute()
    {
        // Place the ship
    }
}

class Fire : ICommand
{
    int x;
    int y;
    Player player;

    public Fire(int x, int y, Player player)
    {
        // Initialize fields
    }

    public void Execute()
    {
        // Try to hit enemy
    }
}

Then you can keep a history of ICommand objects which you can replay easily by just iterating through the list again. 然后,您可以保留ICommand对象的历史记录,只需再次遍历列表即可轻松重播。

You can try to use a XMLSerialize to export a the whole class to a Structured XMl automatically. 您可以尝试使用XMLSerialize自动将整个类导出到Structured XMl。 It's kind of a way to make a full copy of the object in XML. 这是在XML中完整复制对象的一种方法。

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

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