public List<Move> MakeTurn(Checker[,] field) { Func<Point, bool> InField = (point => point.X < 8 && point.X >= 0 && point.Y < 8 && point.Y >= 0); var answer = new List<Move>(); var valid = new Validator(); var way = (Color == Color.White) ? -1 : 1; var listOfMyCheckersWhoCanMove = new List<Move>(); var bindingMoves = valid.GetBindingMoves(field, Color); if (bindingMoves.Count > 0) { var tempMap = new Checker[8, 8]; for (var i = 0; i < 8; i++) for (var j = 0; j < 8; j++) if (field[i, j] != null) tempMap[i, j] = new Checker(field[i, j].Color, field[i, j].IsQueen); answer.Add(bindingMoves.ToArray()[Program.Rand.Next(0, bindingMoves.Count)]); valid.MakeMove(tempMap, answer[0]); var from = answer[0].To; var array = valid.GetBindingMoves(tempMap, Color).Where(x => x.From == from).ToArray(); var counter = array.Length; while (counter > 0) { var rand = Program.Rand.Next(0, counter); var move = new Move(from, array[rand].To); answer.Add(move); valid.MakeMove(tempMap, move); from = move.To; array = valid.GetBindingMoves(tempMap, Color).Where(x => x.From == from).ToArray(); counter = array.Length; } return answer; } for (var i = 0; i < 8; i++) // составляем список всех возможных фигур, которые могут ходить for (var j = 0; j < 8; j++) if (field[i, j] != null && field[i, j].Color == Color) { if (InField(new Point(i + 1, j + way)) && field[i + 1, j + way] == null) listOfMyCheckersWhoCanMove.Add(new Move(new Point(i, j), new Point(i + 1, j + way))); if (InField(new Point(i - 1, j + way)) && field[i - 1, j + way] == null) listOfMyCheckersWhoCanMove.Add(new Move(new Point(i, j), new Point(i - 1, j + way))); if (InField(new Point(i + 1, j - way)) && field[i + 1, j - way] == null && field[i, j].IsQueen) listOfMyCheckersWhoCanMove.Add(new Move(new Point(i, j), new Point(i + 1, j - way))); if (InField(new Point(i - 1, j - way)) && field[i - 1, j - way] == null && field[i, j].IsQueen) listOfMyCheckersWhoCanMove.Add(new Move(new Point(i, j), new Point(i - 1, j - way))); } if (listOfMyCheckersWhoCanMove.Count > 0) //если в этом списке что-то есть -- добавляем рандомный эл-т и заканчиваем ход { var rand = Program.Rand.Next(0, listOfMyCheckersWhoCanMove.Count); var move = listOfMyCheckersWhoCanMove[rand]; answer.Add(move); return answer; } MessageBox.Show(Color.ToString() + " lose"); Environment.Exit(0); return null; }
public MyForm(Checker[,] field) { black.Initialize(Color.Black); white.Initialize(Color.White); //to make a turn mark your fields to go one by one and press "T". To simulate a game //press space button. Happy gaming, your BandW404 team! FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.field = field; validator = new Validator(); StartPosition = FormStartPosition.CenterScreen; ClientSize = new Size(ElementSize * 8, ElementSize * 8); DoubleBuffered = true; Text = "Checkers"; tickCount = 0; this.KeyDown += MyForm_KeyDown; this.MouseClick += MyForm_MouseClick; }
public static void Gaming() { Logs.AddToLog("Game #" + GamesCount + ". " + firstPlayerFile + "(White) versus " + secondPlayerFile + "(Black). Let the battle begin!"); var movesCount = 0; var white = new MyRemotePlayer(firstPlayerFile, Color.White); var black = new MyRemotePlayer(secondPlayerFile, Color.Black); var validator = new Validator(); var field = new Game().CreateMap(); while (true) { movesCount++; if (movesCount > 150) { Logs.AddToLog("i'm done. it's a draw"); if (GamesCount != BestOf) { GamesCount++; var thr = new Thread(Gaming); thr.Start(); Thread.CurrentThread.Abort(); } else { Environment.Exit(0); Logs.Done(); } } if (usingTimer && movesCount > 1) { time.Reset(); time.Start(); } validator.IsCorrectMove(white.MakeTurn(field), field, Color.White); if (usingTimer && movesCount > 1) { time.Stop(); var ms = time.Elapsed.Milliseconds; Logs.AddToLog(ms.ToString()); MaxWhite = Math.Max(MaxWhite, ms); } if (usingForm) { Window.BeginInvoke(new Action<Checker[,]>(Window.Update), new object[] { field }); Thread.Sleep(TimeOutOfMove); } if (usingTimer && movesCount > 1) { time.Reset(); time.Start(); } validator.IsCorrectMove(black.MakeTurn(field), field, Color.Black); if (usingTimer && movesCount > 1) { time.Stop(); var ms = time.Elapsed.Milliseconds; Logs.AddToLog(ms.ToString()); MaxBlack = Math.Max(MaxBlack, ms); } if (usingForm) { Window.BeginInvoke(new Action<Checker[,]>(Window.Update), new object[] { field }); Thread.Sleep(TimeOutOfMove); } } }
void Test(List<Move> moves, Color color, bool answer, string mapname) { var validator = new Validator(); var field = GetMapFrom(mapname); if (!answer) try { validator.IsCorrectMove(moves, field, color); Assert.Fail(); } catch (NotImplementedException) { } else validator.IsCorrectMove(moves, field, color); }
public void SomeStrange3() { var field = GetMapFrom("Tests92.txt"); var validator = new Validator(); var moves = new List<Move>(); moves.Add(new Move(new Point(0, 7), new Point(7, 0))); validator.IsCorrectMove(moves, field, Color.Black); var newField = GetMapFrom("Tests93.txt"); Assert.AreEqual(Serializer.FieldToString(newField), Serializer.FieldToString(field)); }
public void SomeStrange() { var field = GetMapFrom("Tests88.txt"); var validator = new Validator(); var moves = new List<Move>(); moves.Add(new Move(new Point(0, 7), new Point(7, 0))); // дамка просто делает привязный ход и рубит другую дамку validator.IsCorrectMove(moves, field, Color.Black); //твой забаганный валидатор получает этот ход, поле и все такое. var newField = GetMapFrom("Tests89.txt"); //беру карту которая должна быть после хода (с дамкой) Assert.AreEqual(Serializer.FieldToString(newField), Serializer.FieldToString(field)); //обнаруживаю, что дамки ниха нет :С }
public void HashTest() { var moves = new HashSet<Move>(); var field = GetMapFrom("Tests12.txt"); var valid = new Validator(); moves = valid.GetBindingMoves(field, Color.White); Assert.AreEqual(0, moves.Count); }