public async Task Move(MainViewModel viewModel) { await Task.Delay(viewModel.MoveDelay * 1000); //Wybór znaku którego jeszcze nie było var notSelectedColor = viewModel.Colors.FirstOrDefault(p => !viewModel.BoardItems.Any(q => q.Color != null && q.Color == p.Index)); if (notSelectedColor != null) { viewModel.SelectedColor = notSelectedColor; return; } //Pierwszy znak nie powodujący przegranej foreach (var color in viewModel.Colors) { viewModel.SelectedBoardItem.Color = color.Index; if (!TwinsChecker.CheckTwins(viewModel.BoardItems)) { viewModel.SelectedBoardItem.Color = null; viewModel.SelectedColor = color; return; } } //Każdy wybór jest przygrywający viewModel.SelectedColor = viewModel.Colors[0]; }
public static SecondPlayerMove MinMove(List <BoardItem> board, int colorCount, int maxSize) { if (board.Count() == maxSize || TwinsChecker.CheckTwins(board)) { // Should never happend throw new Exception(); } var bestMove = new SecondPlayerMove() { Rank = int.MaxValue }; var positions = Enumerable.Range(0, board.Count); foreach (var position in positions) { var newBoard = board.ConvertAll(_ => new BoardItem(_.Color)).ToList(); //Wastawiamy element var item = new BoardItem(); newBoard.Insert(position, item); // Gra pierwszy gracz (minimalizujemy) FirstPlayerMove firstPlayerMove = MaxMove(newBoard, position, colorCount, maxSize); if (bestMove.Rank > firstPlayerMove.Rank) { bestMove = new SecondPlayerMove() { Position = position, Rank = firstPlayerMove.Rank };; } } return(bestMove); }
public async Task Move(MainViewModel viewModel) { await Task.Delay(viewModel.MoveDelay * 1000); //Znaki nie powodujące przegranej var list = new List <Color>(); foreach (var color in viewModel.Colors) { viewModel.SelectedBoardItem.Color = color.Index; if (!TwinsChecker.CheckTwins(viewModel.BoardItems)) { viewModel.SelectedBoardItem.Color = null; list.Add(color); } } //Każdy wybór jest przygrywający if (!list.Any()) { viewModel.SelectedColor = viewModel.Colors[0]; } else { //Losowy wybór nie przegrywający var random = new Random(DateTime.Now.Millisecond); var colorIndex = random.Next(list.Count); viewModel.SelectedColor = viewModel.Colors[colorIndex]; } }
public static FirstPlayerMove MaxMove(List <BoardItem> board, int position, int colorCount, int maxSize) { if (TwinsChecker.CheckTwins(board)) { // Should never happend throw new Exception(); } var bestMove = new FirstPlayerMove() { Rank = int.MinValue }; var colors = Enumerable.Range(0, colorCount); foreach (var color in colors) { var newBoard = board.ConvertAll(_ => new BoardItem(_.Color)).ToList(); newBoard[position].Color = color; // Sprawdzamy czy gra się zakończyła if (TwinsChecker.CheckTwins(newBoard)) // Są ciasne bliźniaki (przegrywamy) { if (bestMove.Rank < newBoard.Count) { bestMove = new FirstPlayerMove() { Color = color, Rank = newBoard.Count }; } } else if (newBoard.Count == maxSize) // Doszliśmy do końca (wygrywamy) { return(new FirstPlayerMove() { Color = color, Rank = int.MaxValue }); } else // Gra się nie skończyła { // Gra drugi gracz (maksymalizujemy) SecondPlayerMove secondPlayerMove = MinMoveCached(newBoard, colorCount, maxSize); if (bestMove.Rank < secondPlayerMove.Rank) { bestMove = new FirstPlayerMove() { Color = color, Rank = secondPlayerMove.Rank }; } } } return(bestMove); }