我正在编写一个名为Flipper的程序,它有3x3个单元用于拼图.每个单元格(按钮)在初始时都具有绿色.单击某个单元格时,该单元格及其相邻单元格会翻转(更改颜色).另一个要求是撤销功能,它回到了前一阶段.我不知道如何实现这一点.这些是游戏中发生的主要事情.
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
}
Run Code Online (Sandbox Code Playgroud)
我要求在名为Undo的类中实现撤销功能.任何想法都很赞赏!
小智 5
可以通过了解上次操作的更改来实现单个撤消.
事实证明,撤消翻转可以通过......再次翻转来完成.所以请记住你的最后一步,重复一遍!
如果你记得每一个动作,你可以根据自己的喜好多次完成游戏的初始状态.您可以通过创建在移动时按下的一组移动来执行此操作,并在撤消时弹出.
更一般地说,要撤消,您需要做3件事:
有时,创建反向动作非常困难.在这些情况下,在执行操作之前更容易存储程序的状态,然后在用户想要撤消时重新加载它.