简体   繁体   English

如何在二维数组中留下某种标记,该标记会告诉程序在该位置执行某些操作

[英]How can I leave some kind of a mark in two-dimensional array that will tell the program to do something in this location

I am working on a little game that uses two-dimensional array for a board we play on. 我正在开发一款小游戏,该游戏在我们使用的棋盘上使用二维阵列。 I implemented Undo method that will go back in history step by step with every Button click. 我实现了“ Undo方法,每次单击“按钮”都会逐步返回历史记录。 The thing is in some cases the Undo method must do something else in a special situation and it might happen many times during the game. 问题是在某些情况下,“ Undo方法必须在特殊情况下执行其他操作,并且在游戏过程中可能会多次发生。

I know the one way would be to store 'x' and 'y' (which are responsible for position on the board) when I reach the place where it will be needed to do something else if we come back here later. 我知道一种方法是存储“ x”和“ y”(它们在板上的位置),当我到达那里时,如果我们稍后再回到这里,则需要做一些其他事情。 Then add if in Undo Action Listener that if position we come back to .equals any of stored x and y then do the thing. 然后在“ Undo Action Listener中添加if ,如果位置我们回到.equals存储的xy任何一个,则执行该操作。 But it is quite not efficient I would say and I am wondering if there is a way to leave a mark that will tell the program that when I come back here the Boolean I created will .equal true and do the thing. 但是我要说的是效率.equal ,我想知道是否有一种方法可以留下标记来告诉程序,当我回到这里时,我创建的Boolean.equal true并且可以执行该操作。

Here is the overview of the board: 这是董事会的概述: 董事会概况

I don't understand why an if statement will be inefficient. 我不明白为什么if语句效率低下。 I will add constant time (if only your equals method has constant time) to your undo operation, so the big-o time of your operation will not be modified. 我将在undo操作中添加恒定时间(如果只有equals方法具有恒定时间),因此操作的big-o时间不会被修改。

However, if you prefer another way to do that, then you could save an object in each cell. 但是,如果您更喜欢另一种方法,则可以在每个单元格中保存一个对象。 That object should have a boolean property that you will check when doing the undo operation. object应具有一个布尔属性,您在执行undo操作时将对其进行检查。

I would prefer using an if statement, but is up to you. 我更喜欢使用if语句,但是取决于您。

You are using a 2-dimensional array - an array of what? 您正在使用二维数组-什么是数组? Considering you have letters in your picture I presume something like String[][]. 考虑到您的图片中有字母,我假设使用String [] []之类的东西。

However you can also do an array of your own class. 但是,您也可以自己创建一个数组。 Something like a class "Cell". 类似于“单元”类。 Give this class two members - firstly the payload (aka the data you currently have) and secondly a boolean as your mark. 给这个类两个成员-首先是有效载荷(也就是您当前拥有的数据),其次是布尔值作为标记。

Separate concerns. 单独关注。 The board shouldn't have to also store its history. 董事会不必也保存其历史记录。 Keep a journal of actions and have a way to play them back in reverse. 记录动作,并有一种方法可以将它们反向播放。

This is exactly how relational databases cater for rollbacks; 关系数据库正是这样来满足回滚的。 they keep a log of every change and can undo everything in it. 他们会记录所有更改,并可以撤消其中的所有内容。 The log is usually a before snapshot of the state of the data being changed. 日志通常是更改数据状态的快照。 One simple way you could implement it in your case would be to store a snapshot of every board and undo would simply swap historic boards in for the current board. 在您的情况下,实现它的一种简单方法是存储每个板的快照,而撤消将简单地将历史板换成当前板。

This might seem “heavy”, but it's the way massive high transaction rate databases do it, and it's way simpler to implement and bulletproof, because you don't need to know how to undo a specific action. 这看似“沉重”,但它是大型高事务处理率数据库执行此操作的方式,并且实现起来更简单且防弹,因为您不需要知道如何撤消特定操作。 Further, it means boards need to know less, in fact they don't even need to know they can be undone. 此外,这意味着董事会需要了解的更少,实际上,他们甚至不需要知道可以撤消这些事务。

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

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