Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Game3.cs
- using System;
- using System.Collections.Generic;
- namespace Pyatnyasky_v2
- {
- class Game3 : Game2
- {
- Stack<Points> undo = new Stack<Points>();
- Stack<Points> redo = new Stack<Points>();
- public Game3(int[] point) : base(point)
- {
- }
- public void History(int value)
- {
- undo.Push(GetLocation(value));
- }
- public void Undo()
- {
- if (undo.Count > 0)
- {
- var point = undo.Pop();
- this.Move(point.x, point.y], this); //Вот здесь среда показывает синтаксическа ошибка
- redo.Push(point);
- }
- else throw new Exception("Unable to cancel");
- }
- public void Redo()
- {
- if (redo.Count > 0)
- {
- var point = redo.Pop();
- this.Move(point.x, point.y], this); //Вот здесь среда показывает синтаксическа ошибка
- undo.Push(point);
- }
- }
- }
- }
- //Game.cs
- using System;
- namespace Pyatnyasky
- {
- class Points
- {
- public int x, y;
- public Points(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- class Game
- {
- public int[] point = new int[16];
- public int Length = 0;
- public static int[] ArrayText = new int[16];
- public const int width = 4, height = 4;
- public int[,] field = new int[width, height];
- public Points[] FieldValue = new Points[16];
- public Game(int[] point)
- {
- int r = 0;
- Length = width * height;
- for (int j = 0; j < height; j++)
- {
- for (int i = 0; i < width; i++)
- {
- field[j, i] = point[r];
- FieldValue[point[r]] = new Points(j, i);
- r++;
- }
- }
- }
- public int this[int x, int y]
- {
- get
- {
- if (x < 0 || x >= width * height || y < 0 || y >= width * height)
- {
- throw new ArgumentOutOfRangeException("the indexes do not fit");
- }
- return field[x, y];
- }
- }
- public Points GetLocation(int value)
- {
- return FieldValue[value];
- }
- public void drawField()
- {
- Console.WriteLine("____________________________________");
- for (int i = 0; i < width; i++)
- {
- for (int j = 0; j < height; j++)
- {
- Console.Write(field[i, j] + "t");
- }
- Console.WriteLine();
- }
- Console.WriteLine("____________________________________");
- }
- public void Move(int value, Game3 obj)
- {
- try
- {
- if (value > 15 || value < 0)
- {
- throw new ArgumentException();
- }
- int x = GetLocation(0).x;
- int y = GetLocation(0).y;
- int X = GetLocation(value).x;
- int Y = GetLocation(value).y;
- if ((X == x && (Y == y - 1 || Y == y + 1)) || (Y == y && (X == x - 1 || X == x + 1)))
- {
- field[x, y] = value;
- field[X, Y] = 0;
- var vere = FieldValue[0];
- FieldValue[0] = FieldValue[value];
- FieldValue[value] = vere;
- obj.History(value);
- }
- else
- {
- throw new Exception();
- }
- }
- catch (ArgumentException)
- {
- Console.WriteLine("There is no such number: ");
- }
- catch (Exception)
- {
- Console.WriteLine("Along with this number is not 0: ");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment