Добавить использование шаблонов проектирования в игру "Пятнашки" - C#
Формулировка задачи:
Есть реализация игры пятнашки, нужно сделать откат предыдущего хода, выбор уровня сложности (рохмир игрового поля 3х3, 4х4), с использованием паттернов Синглетон, Фабричный метод, Команда, Хранитель, помогите.
using System; //Game.cs namespace puzzle15 { class Points { public readonly int x, y; public Points(int x, int y) { this.x = x; this.y = y; } } class Game { public int Length=0; const int nw = 4, height = 4; int[,] field = new int[nw, height]; Points[] FieldValue = new Points[16]; public Game(int[] point) { int r = 0; string[] file = new string[4]; Length = 16; mixer(point); for (int j = 0; j < height; j++) { for (int i = 0; i < nw; i++) { field[j, i] = point[r]; FieldValue[point[r]] = new Points(j, i); r++; } } } public void mixer(int[] p) { int tmp = 0; Random rnd = new Random(); for (int i = 0; i < 16; i++) { bool isExist = false; do { isExist = false; tmp = rnd.Next(0, 16); for (int j = 0; j < i; j++) { if (tmp == p[j]) { isExist = true; } } } while (isExist); p[i] = tmp; } } private Points GetLocation(int value) { return FieldValue[value]; } public void drawField() { Console.WriteLine("----------------------------"); for (int i = 0; i < nw; i++) { for (int j = 0; j < height; j++) { Console.Write(field[i, j] + "\t"); } Console.WriteLine(); } Console.WriteLine("----------------------------"); } public bool repeat(double Length,int[] point) { for (int i = 0; i < Length; ++i) { for (int y = i + 1; y < Length; ++y) { if (point[i] == point[y]) { Console.WriteLine(point[i] + " ==" + point[y]); throw new ArgumentException("Numbers should not be repeated"); } } } return true; } public Boolean finish() { bool temp = false; int value = 1; for (int i = 0; i < nw; ++i) { for (int j = 0; j < height; ++j) { if (field[i, j] == value) { temp = true; ++value; if (value == Length) { value = 0; } } else { return false; } } } return temp; } public void Move(int value) { try { Console.WriteLine(value); if (value > 15 || value < 0) { throw new ArgumentException(); } int x = GetLocation(0).x; int y = GetLocation(0).y; int ValueX = GetLocation(value).x; int ValueY = GetLocation(value).y; if ((ValueX == x && (ValueY == y - 1 || ValueY == y + 1))||(ValueY == y && (ValueX == x - 1 || ValueX == x + 1))) { field[x, y] = value; field[ValueX, ValueY] = 0; var vere = FieldValue[0]; FieldValue[0] = FieldValue[value]; FieldValue[value] = vere; } } catch (ArgumentException) { Console.WriteLine("There is no such number, try again: "); } catch (Exception) { Console.WriteLine("Next to this number is not 0, try again: "); } } } //-------------------------------------------------------------------------------------------------------------------------------------------------------- class Points2 { .............................................................................................. } class Game2 { public int Length = 0; const int nw = 3, height = 3; int[,] field = new int[nw, height]; Points[] FieldValue = new Points[9]; public Game2(int[] point) { int r = 0; string[] file = new string[3]; Length = 9; mixer(point); for (int j = 0; j < height; j++) { for (int i = 0; i < nw; i++) { field[j, i] = point[r]; FieldValue[point[r]] = new Points(j, i); r++; } } } ................................................................................................. } } //Program.cs using System; using System.Collections.Generic; namespace puzzle15 ............................................................................................... //вот код начало хранителя class GameHistory { public Stack<Game> History { get; private set; } public GameHistory() { History = new Stack<Game>(); } } }
Решение задачи: «Добавить использование шаблонов проектирования в игру "Пятнашки"»
textual
Листинг программы
//Program.cs using System; namespace _15puzzle { class Program { static void Main(string[] args) { int i; int[] p = new int[100]; for (i = 0; i < 16; i++) { p[i] = i + 1; } p[15] = 0; Console.WriteLine("15 puzzle"); Game MyGame = new Game(p); int score = 0; MyGame.mixer(p); for (; ; ) { MyGame.drawField(); Console.WriteLine("Select number: "); int a=Convert.ToInt32(Console.ReadLine()); MyGame.Move(a); if (MyGame.finish()) { MyGame.drawField(); Console.WriteLine("Winner!"); Console.WriteLine("Game end"); break; } score++; Console.WriteLine("Count: " + score); } } } } //Game.cs using System; using System.IO; namespace _15puzzle { class Game { int[] point=new int[100]; public int Length=0; public static int[] ArrayText=new int [100]; const int nw = 4, height = 4; int[,] field = new int[nw, height]; Points[] FieldValue = new Points[100]; public Game(int[] point) { int ruru = 0; string[] file = new string[4]; Length = TextSCV(); mixer(ArrayText); for (int j = 0; j < height; j++) { for (int i = 0; i < nw; i++) { field[j, i] = ArrayText[ruru]; FieldValue[ArrayText[ruru]] = new Points(j, i); ruru++; } } } public void mixer(int[] p) { int tmp = 0; Random rnd = new Random(); for (int i = 0; i < 16; i++) { bool isExist = false; do { isExist = false; tmp = rnd.Next(0, 16); for (int j = 0; j < i; j++) { if (tmp == p[j]) { isExist = true; } } } while (isExist); p[i] = tmp; } } public int this[int x, int y] { get { return field[x, y]; } set { field[x, y] = value; } } private Points GetLocation(int value) { return FieldValue[value]; } public void drawField() { Console.Write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "\n"); for (int i = 0; i < nw; i++) { for (int j = 0; j < height; j++) { Console.Write(field[i, j] + "\t"); } Console.WriteLine(); } Console.Write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "\n"); } public bool repeat(double Length,int[] ArrayText) { for (int i = 0; i < Length; ++i) { for (int y = i + 1; y < Length; ++y) { if (ArrayText[i] == ArrayText[y]) { Console.WriteLine(ArrayText[i] + " ==" + ArrayText[y]); throw new ArgumentException("Number not repet! "); } } } return true; } public Boolean finish() { bool temp = false; int value = 1; for (int i = 0; i < nw; ++i) { for (int j = 0; j < height; ++j) { if (field[i, j] == value) { temp = true; ++value; if (value == Length) { value = 0; } } else { return false; } } } return temp; } public void Move(int value) { try { Console.WriteLine(value); if (value > 15 || value < 0) { throw new ArgumentException(); } int x = GetLocation(0).x; int y = GetLocation(0).y; int ValueX = GetLocation(value).x; int ValueY = GetLocation(value).y; if ((ValueX == x && (ValueY == y - 1 || ValueY == y + 1))||(ValueY == y && (ValueX == x - 1 || ValueX == x + 1))) { field[x, y] = value; field[ValueX, ValueY] = 0; var vere = FieldValue[0]; FieldValue[0] = FieldValue[value]; FieldValue[value] = vere; } else { throw new Exception(); //Console.WriteLine("Некуда двигать. "); } } catch (ArgumentException) { Console.WriteLine("Numer is not: "); } catch (Exception) { Console.WriteLine("Number: "); } } public static int TextSCV() { int ruru = 0; string[] text = File.ReadAllLines(@"text.csv"); Char place = ' '; for (int i = 0; i < text.Length; ++i) { string[] row = text[i].Split(place); foreach (var substring in row) { ArrayText[ruru] = Convert.ToInt32(substring); //Console.WriteLine(ArrayText[ruru]); ++ruru; } } return ruru; } } } //Point.cs namespace _15puzzle { class Points { public readonly int x, y; public Points(int x, int y) { this.x = x; this.y = y; } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д