如何实现撤消功能
我正在编写一个名为 Flipper 的程序,其中包含 3x3 的拼图单元格。每个单元格(按钮)最初都是绿色的。当单击某个单元格时,该单元格及其相邻单元格翻转(改变颜色)。还有一个需求是undo功能,即回到上一阶段。我不知道如何实现这个。这些是游戏中发生的主要事情。
public Puzzle(Form1 form1)
{
buttons = new Button[3, 3] { { form1.button1, form1.button2, form1.button3 },
{ form1.button4, form1.button5, form1.button6 },
{ form1.button7, form1.button8, form1.button9 } };
//button reference from form1
}
public void reset()
{
//reset all the colors of buttons in the puzzle to lime
}
public void FlipCells(int row, int col)
{
//when certain button is clicked(this event is done in the form1.cs), say for (0,0) change color of cell (0,0),///(0,1) and (1,0) by calling changeColor method
}
public void changeColor(int row, int col)
{
//test current color of the cell, and change it
}
我要求在一个名为 Undo 的类中实现撤消功能。任何想法表示赞赏!
I'm writing a program called Flipper which has 3x3 cells for the puzzle. Each cell(button) has green color at initial. When Click certain cell, that cell and its adjacent cells flip(change color). And another requirement is the undo function, which is back to previous stage. I have no idea how to implement this. These are the main things happens in the game.
public Puzzle(Form1 form1)
{
buttons = new Button[3, 3] { { form1.button1, form1.button2, form1.button3 },
{ form1.button4, form1.button5, form1.button6 },
{ form1.button7, form1.button8, form1.button9 } };
//button reference from form1
}
public void reset()
{
//reset all the colors of buttons in the puzzle to lime
}
public void FlipCells(int row, int col)
{
//when certain button is clicked(this event is done in the form1.cs), say for (0,0) change color of cell (0,0),///(0,1) and (1,0) by calling changeColor method
}
public void changeColor(int row, int col)
{
//test current color of the cell, and change it
}
I'm asking to implement the undo function in a class called Undo. Any ideas is appreciated!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通过了解上次操作发生的变化,可以实现单次撤消。
事实证明,撤消翻转可以通过……再次翻转来完成。所以只要记住你的最后一个动作,然后重复它!
如果您记住每一步动作,您可以在游戏的初始状态下多次执行此操作。您可以通过创建一堆移动来实现此目的,在移动时推送这些移动,在撤消时弹出这些移动。
更一般而言,要撤消,您需要执行 3 件事:
有时,创建相反的动作是非常困难的。在这些情况下,在执行操作之前存储程序的状态,然后在用户想要撤消时重新加载它变得更容易。
A single undo can be implemented by knowing what changed with the last operation.
As it turns out, undoing a flip can be done by... flipping it again. So just remember your last move, and repeat it!
You can do this as many times as you like to the initial state of the game if you remember every move. You can do this by creating a stack of moves that you push as you move, and pop as you undo.
More generally, to undo, you need to do 3 things:
Sometimes, it is very difficult to create an inverse action. In these cases, it becomes easier to just store the state of the program before the action is executed, then reload it when the user wants an undo.
有很多用于实现撤消功能的选项。
由于这是家庭作业,我将向您介绍 Stack 数据结构。堆栈是后进先出的。把它想象成桌子上的一叠文件。
当玩家翻转一个方块时,您会记下它并将其添加到堆栈中(称为“推”)。玩家翻转另一个方块:您再做一个笔记,然后推到堆栈的顶部。
当请求“撤消”时,您要撤消哪个操作?最近的一个——所以你从堆栈的顶部提取(称为“pop”)。调用 Stack.Pop() 将从堆栈中返回最顶层(最近的)项目,并从堆栈中删除该项目。
这应该足以让你继续前进——弄清楚你在堆栈中需要什么,你就快完成了。
延伸阅读:
堆栈
:http://msdn。 microsoft.com/en-us/library/system.collections.stack.aspx堆栈
:http://msdn.microsoft .com/en-us/library/3278tedw.aspxThere are a lot of options for implementing the undo functionality.
Since this is homework, I'll just point you to the
Stack
data structure. Stack is last-in, first-out. Think of it as a stack of papers on a table.When a player flips a square, you make a note of it and add it to the stack (called "push"). The player flips another square: you make another note and then push to the top of the stack.
When "undo" is requested, which operation do you want to undo? The most recent one -- so you draw from the top of the stack (called "pop"). Calling Stack.Pop() will return topmost (most recent) item from the stack and remove that item from the stack.
That should be enough to get you going -- figure out what you need in the stack and you're almost done.
Further reading:
Stack
: http://msdn.microsoft.com/en-us/library/system.collections.stack.aspxStack<T>
: http://msdn.microsoft.com/en-us/library/3278tedw.aspx也许想想当你“撤销”一个动作时会发生什么。假设您只有两种颜色,则您刚刚翻转的所有颜色都会向后翻转(或再次翻转)
perhaps think about what would happen when you 'undo' a move. all the ones you just flipped are flipped back (or again) assuming you only have two colours that is
无论如何,在您的情况下,纪念品设计模式可能是矫枉过正的,它是实现撤消(和重做)功能的标准方法。
备忘录将保存对象的当前状态,并具有恢复功能,可反转其链接到的对象的更改。
因此,在您的情况下,备忘录将保存棋盘的状态(每个单元格的每种颜色),并且当应用于棋盘时,它将设置颜色。
All tough it is probably overkill in your situation the Memento Design Pattern is a standard way for implementing undo (and redo) functionality.
A Memento will save the current state of an object and will have a restore function that will reverse the changes on the object it's linked to.
So in your case a Memento would save the state of the board (each color for each cell) and when applied to a board it will set the colors.
实施命令模式。您实际上在某种字典或数组或堆栈中跟踪命令,然后撤消
访问此链接 http://www.codeproject.com/KB/architecture/sharped.aspx
Implement Command pattern. where you actually keep track of your commands in some sort of dictionary or array or stack and then undo
visit this link http://www.codeproject.com/KB/architecture/sharped.aspx
如果您只有两种颜色,那么撤消与您调用它的单元格的更改颜色没有什么不同。
因此,您所要做的就是存储(在成员变量中)调用changeColor 的最后一列和行,并在Undo 函数中使用存储的值再次调用changeColor。
If you have only two colors than undo is not different than changeColor for the cell that you lass called it for.
So all you have to do is to store (in member variables) the last column and row for which the changeColor was called and in the Undo function make another call to changeColor with the stored values.