diff --git a/Chess/Creator.cs b/Chess/Creator.cs new file mode 100644 index 0000000..5dd5384 --- /dev/null +++ b/Chess/Creator.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Chess +{ + abstract class ChessFigureFactory + { + public abstract ChessFigure ChessFigureCreator(Coordinates position, bool color); + + } + + class PawnCreator : ChessFigureFactory + { + public PawnCreator() { } + public override ChessFigure ChessFigureCreator(Coordinates position, bool color) + { + return new Pawn(position, color); + } + } + class CastleCreator : ChessFigureFactory + { + public CastleCreator() + { } + public override ChessFigure ChessFigureCreator(Coordinates position, bool color) + { + return new Castle(position, color); + } + } + class HorseCreator : ChessFigureFactory +{ + public HorseCreator() + { } + public override ChessFigure ChessFigureCreator(Coordinates position, bool color) + { + return new Horse(position, color); + } + } + class KingCreator : ChessFigureFactory +{ + public KingCreator() + { } + public override ChessFigure ChessFigureCreator(Coordinates position, bool color) + { + return new King(position, color); + } + } + class ElephantCreator : ChessFigureFactory +{ + public ElephantCreator() + { } + public override ChessFigure ChessFigureCreator(Coordinates position, bool color) + { + return new Elephant(position, color); + } + } + class QueenCreator : ChessFigureFactory +{ + public QueenCreator() + { } + public override ChessFigure ChessFigureCreator(Coordinates position, bool color) + { + return new Queen(position,color); + } + } + +} diff --git a/Chess/Game.cs b/Chess/Game.cs index 878dec1..b4167df 100644 --- a/Chess/Game.cs +++ b/Chess/Game.cs @@ -1,94 +1,90 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Chess -{ +{ class Game { - - private Gamer _currentPlayer; + static List listFigures = new List(); + private static Gamer _currentPlayer; - private Gamer _blackPlayer; - private Gamer _whitePlayer; + private static Gamer _blackPlayer; + private static Gamer _whitePlayer; public static void Main(string[] param) { - + InitializeFigures(); + InitializePlayers(); + PassStepTo(true); + Console.ReadKey(); } public Game() { + } - public void PassStepTo(bool color) + private static void PassStepTo(bool color) { - - if (color) _currentPlayer = _whitePlayer; else _currentPlayer = _blackPlayer; - + Console.WriteLine($"Ходит игрок: {_currentPlayer.Name}"); _currentPlayer.Step(); - - } + private static void InitializePlayers() + { + _whitePlayer = new Gamer("Ilya", listFigures.Where(figure => figure.Color), PassStepTo); + _blackPlayer = new Gamer("Bagadat", listFigures.Where(figure => !figure.Color), PassStepTo); + } - private void SetFigureStartPosition(ChessFigure figure, bool color) + + private static void InitializeFigures() { - if (figure is Elephant) - { - Elephant elephantc1 = new Elephant(new Coordinates('c', 1), true); - Elephant elephantf1 = new Elephant(new Coordinates('f', 1), true); - Elephant elephantc8 = new Elephant(new Coordinates('c', 8), false); - Elephant elephantf8 = new Elephant(new Coordinates('f', 1), false); - } - else if (figure is Castle) - { - Castle castlea1 = new Castle(new Coordinates('a', 1), true); - Castle castleh1 = new Castle(new Coordinates('h', 1), true); - Castle castlea8 = new Castle(new Coordinates('a', 8), false); - Castle castleh8 = new Castle(new Coordinates('h', 8), false); - } - else if (figure is Horse) - { - Horse horseb1 = new Horse(new Coordinates('b', 1), true); - Horse horseg1 = new Horse(new Coordinates('g', 1), true); - Horse horseb8 = new Horse(new Coordinates('b', 8), false); - Horse horseg8 = new Horse(new Coordinates('g', 8), false); + // Почитай про фабрику и попробуй реализовать!!! - - } - else if (figure is King) + // ChessFigureFactory creator = new PawnCreator(); + // ChessFigure figure = creator.ChessFigureCreator(); + listFigures = new List() { - King kinge1 = new King(new Coordinates('e', 1), true); - King kinge8 = new King(new Coordinates('e', 8), false); - } - else if (figure is Queen) - { - Queen queend1 = new Queen(new Coordinates('d', 1), true); - Queen queend8 = new Queen(new Coordinates('d', 8), false); - } - else if (figure is Pawn) - { - Pawn pawna2 = new Pawn(new Coordinates('a', 2), true); - Pawn pawnb2 = new Pawn(new Coordinates('b', 2), true); - Pawn pawnc2 = new Pawn(new Coordinates('c', 2), true); - Pawn pawnd2 = new Pawn(new Coordinates('d', 2), true); - Pawn pawne2 = new Pawn(new Coordinates('e', 2), true); - Pawn pawnf2 = new Pawn(new Coordinates('f', 2), true); - Pawn pawng2 = new Pawn(new Coordinates('g', 2), true); - Pawn pawnh2 = new Pawn(new Coordinates('h', 2), true); - Pawn pawna7 = new Pawn(new Coordinates('a', 7), false); - Pawn pawnb7 = new Pawn(new Coordinates('b', 7), false); - Pawn pawnc7 = new Pawn(new Coordinates('c', 7), false); - Pawn pawnd7 = new Pawn(new Coordinates('d', 7), false); - Pawn pawne7 = new Pawn(new Coordinates('e', 7), false); - Pawn pawnf7 = new Pawn(new Coordinates('f', 7), false); - Pawn pawng7 = new Pawn(new Coordinates('g', 7), false); - Pawn pawnh7 = new Pawn(new Coordinates('h', 7), false); - } + new Elephant(new Coordinates('c', '1'), true), + new Elephant(new Coordinates('f', '1'), true), + new Elephant(new Coordinates('c', '8'), false), + new Elephant(new Coordinates('f', '8'), false), + new Castle(new Coordinates('a', '1'), true), + new Castle(new Coordinates('h', '1'), true), + new Castle(new Coordinates('a', '8'), false), + new Castle(new Coordinates('h', '8'), false), + new Horse(new Coordinates('b', '1'), true), + new Horse(new Coordinates('g', '1'), true), + new Horse(new Coordinates('b', '8'), false), + new Horse(new Coordinates('g', '8'), false), + new King(new Coordinates('e', '1'), true), + new King(new Coordinates('e', '8'), false), + new Queen(new Coordinates('d', '1'), true), + new Queen(new Coordinates('d', '8'), false), + new Pawn(new Coordinates('a', '2'), true), + new Pawn(new Coordinates('b', '2'), true), + new Pawn(new Coordinates('c', '2'), true), + new Pawn(new Coordinates('d', '2'), true), + new Pawn(new Coordinates('e', '2'), true), + new Pawn(new Coordinates('f', '2'), true), + new Pawn(new Coordinates('g', '2'), true), + new Pawn(new Coordinates('h', '2'), true), + new Pawn(new Coordinates('a', '7'), false), + new Pawn(new Coordinates('b', '7'), false), + new Pawn(new Coordinates('c', '7'), false), + new Pawn(new Coordinates('d', '7'), false), + new Pawn(new Coordinates('e', '7'), false), + new Pawn(new Coordinates('f', '7'), false), + new Pawn(new Coordinates('g', '7'), false), + new Pawn(new Coordinates('h', '7'), false), + }; } + } + } diff --git a/Chess/Gamer.cs b/Chess/Gamer.cs index e6594a6..27a1e11 100644 --- a/Chess/Gamer.cs +++ b/Chess/Gamer.cs @@ -1,55 +1,78 @@ using System; using System.Collections.Generic; using System.Linq; - -using System.Text; -using System.Threading.Tasks; +using System.Threading; namespace Chess { class Gamer { - Game _passToStep; + private bool _flagFinish=false; TimeSpan _gamersTime = new TimeSpan(0, 5, 0); + private Action PassStepTo; public string Name { get; } - List _figures { get; } + IEnumerable _figures { get; } - public Gamer(string name, List figures) + public Gamer(string name, IEnumerable figures, Action passStepTo) { _figures = figures; Name = name; + PassStepTo = passStepTo; } public void Step() { - DateTime start = DateTime.Now; + // Засечь время (таймер или поток) + TimerCallback timerCallback = new TimerCallback(Hello); + Timer timer = new Timer(timerCallback, null, _gamersTime, new TimeSpan(1,0,0)); + string gamerStep = Console.ReadLine(); + //Заюзать флаг окончания игры + + + timer.Dispose(); + string[] startToEnd = gamerStep.Split('-'); foreach (var figure in _figures) { + + if (figure.CheckCurrentPosition(startToEnd[0])) { + startToEnd[1] = startToEnd[1].ToLower(); - figure.Move(startToEnd[1][0], startToEnd[1][1]); + if (!figure.Move(startToEnd[1][0], startToEnd[1][1])) + { + Console.WriteLine("Фигура не может так ходить"); + + } + else + Console.WriteLine("Фигура может так ходить"); + break; } + + + // Обработать случаи: + // 1) когда фигуры найдено не было + // 2) когда фигурой сходить не получилось + // Только в случае успешного хода мы передаем ход } + // Console.Clear(); DateTime stop = DateTime.Now; - TimeSpan delta = start - stop; - long seconds = delta.Seconds; - - TimeSpan newTime = new TimeSpan(seconds); - _gamersTime = _gamersTime.Subtract(newTime); - if (_gamersTime != null) - { - Action method = _passToStep.PassStepTo; - method(GetColor()); - } - + TimeSpan delta = stop.Subtract(start); + _gamersTime = _gamersTime.Subtract(delta); + PassStepTo.Invoke(!GetColor()); + } + private void Hello(object state) + { + if (_gamersTime.Minutes == 0) ; + Console.WriteLine("Время истекло"); + } public bool GetColor()