private void shuffleToolStripMenuItem_Click(object sender, EventArgs e) { Form enterseed = new EnterSeed(); enterseed.Owner = this; enterseed.ShowDialog(); undolist.Addundo(puzzleState); if ("OK".Equals(this.Action)) { Puzzle shufflepuzzleState = new Puzzle(buttons); shufflepuzzleState.shuffle(this.Seed); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { buttons[j, i].BackColor = shufflepuzzleState.Colors[i, j]; } } this.toolStripStatusLabel1.Text = "Number of moves"; //set puzzlestate to the shuffled puzzle state and move to zero puzzleState.Colors = shufflepuzzleState.Colors; puzzleState.Moves = 0; } }
public void Addundo(Puzzle presentstate) { //clone the object Puzzle clone = new Puzzle(); clone.Clone(presentstate); //put the state onto the top of the stack Undostates.Push(clone); //if there are 5 objects in the stack //pull 4 of them out, clear the stack and push them back if (Undostates.Count == 5) { Stack<Puzzle> temp = new Stack<Puzzle>(); for (int i = 0; i < 4; i++) { temp.Push(Undostates.Pop()); } Undostates.Clear(); for (int i = 0; i < 4; i++) { Undostates.Push(temp.Pop()); } } }
public MainForm() { InitializeComponent(); //generate all buttons for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { buttons[i, j] = new Button(); buttons[i, j].Location = new Point(10 + 62 * i, 30 + 62 * j); buttons[i, j].BackColor = Color.Lime; buttons[i, j].Size = new Size(60, 60); buttons[i, j].ImageIndex = i * 3 + j; buttons[i, j].Click += new EventHandler(this.buttons_Click); this.Controls.Add(buttons[i, j]); } } puzzleState = new Puzzle(buttons); }
//for solving reference type problem //clone object public void Clone(Puzzle presentpuzzle) { Moves = presentpuzzle.Moves; Colors = new Color[PUZZLESIZE, PUZZLESIZE]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.Colors[i, j] = Color.Lime; this.Colors[i, j] = presentpuzzle.Colors[i, j]; } } }
public void Getundo() { //pull top object Getundostate = Undostates.Pop(); }