//node constructors public Node(Node p, Move m, int a, int b) { //create a new board with the move provided CBoard = Move.CopyAndMove(p.CBoard, m); Alpha = a; Beta = b; }
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; }
//will return a new board with the provided move applied //will not alter the original board public static Piece[,] CopyAndMove(Piece[,] cboard, Move m) { var newBoard = new Piece[6,6]; for (var x = 0; x < 6; x++) { for (var y = 0; y < 6; y++) { if (cboard[x,y] != null) { newBoard[x,y] = new Piece(cboard[x,y]); } } } MakeMove(newBoard,m); return newBoard; }
public static Move Go(Square[,] board) { Move retVal = new Move( ); Dictionary<Players, List<Piece>> piecesOf = new Dictionary<Players, List<Piece>>( ); //A dictionary of lists of pieces. piecesOf[Colors.Red] contains a list of all red pieces. piecesOf.Add(Players.Black, new List<Piece>( )); //Initialize the list of black pieces in piecesOf. piecesOf.Add(Players.Red, new List<Piece>( )); //Sort the pieces and add them into the respective list in PiecesOf. foreach (Square oItem in board) //For each square. if (oItem.Piece != null) //If the square has a piece in it. piecesOf[oItem.Piece.Player].Add(oItem.Piece); //Add the piece to the respective list in piecesOf. //Find the cmove which jumps the most pieces. //Evaluate the moves of each of the computer's pieces. foreach (Piece currentPiece in piecesOf[Players.Black]) //For each of the computer player's pieces (the computer player is black). foreach (var item in Library.PossibleMoves(board, currentPiece)) { //For each of the squares that currentPiece can move to. Move currentMove = new Move(currentPiece, item.Key, item.Value, 0); currentMove.Rating = RateMove(currentMove); //Determine the human player's best response after the proposed move. Square[,] contingency = new Square[8, 8]; for (int row = 0; row < 8; row++) { for (int column = 0; column < 8; column++) { contingency[row, column] = board[row, column].Clone( ); } } contingency[item.Key.Left, item.Key.Top].Piece = contingency[currentPiece.Location.Left, currentPiece.Location.Top].Piece; //The piece being moved should be moved to the proposed destination. contingency[currentPiece.Location.Left, currentPiece.Location.Top].Piece = null; //Remove currentPiece from it's old location. Move bestResponse = new Move( ); foreach (Square iItem in contingency) { if (iItem.Piece != null && iItem.Piece.Player == Players.Red) { foreach (var iiItem in Library.PossibleMoves(contingency, iItem.Piece)) { int rating = RateMove(new Move(iItem.Piece, iiItem.Key, iiItem.Value, 0)); if (bestResponse.PieceMoved == null || rating > bestResponse.Rating) bestResponse = new Move(iItem.Piece, iiItem.Key, iiItem.Value, rating); } } } currentMove.Rating -= bestResponse.Rating; if (retVal.PiecesJumped == null || currentMove.Rating > retVal.Rating) //If retVal was never set before or currentMove jumps more pieces than the previously found best move. retVal = currentMove; } return retVal; }
private static int RateMove(Move move) { int retVal = 0; foreach (Piece item in move.PiecesJumped) { retVal += 1; if (item.King) retVal += 1; } if ((move.PieceMoved.Player == Players.Black && move.Destination.Top == 7) //If the piece is black and it is on the bottom of the board || (move.PieceMoved.Player == Players.Red && move.Destination.Top == 0)) //or if the piece is red and it is on the top of the board. retVal += 2; if (move.PieceMoved.Location.Top == (move.PieceMoved.Player == Players.Black ? 0 : 7)) retVal -= 3; else if (move.PieceMoved.Location.Left == 0 || move.PieceMoved.Location.Left == 7) retVal -= 1; if (move.Destination.Left == 0 || move.Destination.Left == 7) retVal += 1; return retVal; }
//will make a move, given a board and a move to do public static void MakeMove(Piece[,] cboard, Move m) { int startX = (int)m.Start.X; int startY = (int)m.Start.Y; int endX = (int)m.End.X; int endY = (int)m.End.Y; if (m.Jump) { int dx = startX - endX; int dy = startY - endY; cboard[startX - dx/2, startY - dy/2] = null; cboard[endX, endY] = cboard[startX, startY]; cboard[startX,startY] = null; } else { cboard[endX, endY] = cboard[startX, startY]; cboard[startX, startY] = null; } }
private bool IsRightQueenMove(Move move, Checker[,] field, Color playerColor) { var dx = new int[] { 1, -1, 1, -1 }; var dy = new int[] { -1, -1, 1, 1 }; var x = move.From.X; var y = move.From.Y; for (var i = 0; i < 4; i++) { var enemyFound = false; for (var delta = 1; delta < 8; delta++) if (!enemyFound) if (InField(new Point(x + dx[i] * delta, y + dy[i] * delta))) { if (field[x + dx[i] * delta, y + dy[i] * delta] != null) { enemyFound = true; continue; } if (move.To.X == x + dx[i] * delta && move.To.Y == y + dy[i] * delta) return true; } } return false; }
private bool IsQuennTurnCorrect(Checker[,] field, Color playerColor, Move turn) { var possibleAttack = Bind(field, new Point(turn.From.X, turn.From.Y), playerColor); return possibleAttack.Contains(turn) || IsRightQueenMove(turn, field, playerColor); }
private bool IsCheckerTurnCorrect(Checker[,] field, Color playerColor, Move turn) { if (field[turn.From.X, turn.From.Y].Color != playerColor) return false; var dx = new int[2]; var dy = new int[2]; if (playerColor == Color.White) { dx = new int[] { 1, -1 }; dy = new int[] { -1, -1 }; } else { dx = new int[] { -1, 1 }; dy = new int[] { 1, 1 }; } for (var i = 0; i < 2; i++) if (InField(new Point(turn.From.X + dx[i], turn.From.Y + dy[i]))) if (field[turn.From.X + dx[i], turn.From.Y + dy[i]] == null && turn.From.X + dx[i] == turn.To.X && turn.From.Y + dy[i] == turn.To.Y) return true;//ламповая проверка на возожность хода dx = new int[] { 2, -2, 2, -2 }; dy = new int[] { -2, -2, 2, 2 }; for (var i = 0; i < 4; i++) if (InField(new Point(turn.From.X + dx[i], turn.From.Y + dy[i]))) if (field[turn.From.X + dx[i], turn.From.Y + dy[i]] == null) { var decr = GetNextFreePlace(dx[i], dy[i]); if (field[turn.From.X + decr.X, turn.From.Y + decr.Y] != null && field[turn.From.X + decr.X, turn.From.Y + decr.Y].Color != playerColor) return true; } return false; }
private Tuple<int, int> GetDelta(Move move) { var dx = move.To.X > move.From.X ? 1 : -1; var dy = move.To.Y > move.From.Y ? 1 : -1; return new Tuple<int, int>(dx, dy); }
public void MakeMove(Checker[,] field, Move move) { var delta = GetDelta(move); var dx = delta.Item1; var dy = delta.Item2; var x = move.From.X; var y = move.From.Y; var checker = new Checker(field[x,y].Color, field[x,y].IsQueen); var color = field[x, y].Color; field[x, y] = null; for (var i = 0; i < 8; i++) if (InField(new Point(x + dx * i, y + dy * i))) { if (move.To.X == x + dx * i && move.To.Y == y + dy * i) { field[x + dx * i, y + dy * i] = checker; if (y + dy * i == 0 && checker.Color == Color.White) field[x + dx * i, y + dy * i] = new Checker(checker.Color, true); if (y + dy * i == 7 && checker.Color == Color.Black) field[x + dx * i, y + dy * i] = new Checker(checker.Color, true); break; } if (field[x + dx * i, y + dy * i] != null && field[x + dx * i, y + dy * i].Color != color) { checkersTODelete.Add(new Point(x + dx * i, y + dy * i)); field[x + dx * i, y + dy * i] = new Checker(Color.Beated, false); } } }
public void SelectChecker() { const string selectCheckerToMoveMessage = "Choose a checker: "; Move = new Move(); ClearMessageBar(); DisplayCurrentPlayerMessage(); Thread.Sleep(500); var moveStartCoordinate = GetCoordinate(selectCheckerToMoveMessage); var checkerToMove = GetChecker(moveStartCoordinate); while (!CanSelectChecker(checkerToMove)) { ClearMessageBar(); Console.Write("Error! Cannot select!"); Thread.Sleep(1000); moveStartCoordinate = GetCoordinate(selectCheckerToMoveMessage); checkerToMove = GetChecker(moveStartCoordinate); } Move.Coordinates.Add(moveStartCoordinate); }
public static bool VerifyMove(Move requestedMove,int botNumber) { bool moveValid = true; switch(botNumber) { case 1: switch(Board(requestedMove.Piece.X,requestedMove.Piece.Y)) { case Tile.RedChecker: moveValid = VerifyDown(requestedMove,botNumber); break; case Tile.KingedRedChecker: moveValid = VerifyDown(requestedMove,botNumber) || VerifyUp(requestedMove,botNumber); break; default: moveValid = false; break; } break; case 2: switch(Board(requestedMove.Piece.X,requestedMove.Piece.Y)) { case Tile.WhiteChecker: moveValid = VerifyUp(requestedMove,botNumber); break; case Tile.KingedWhiteChecker: moveValid = VerifyUp(requestedMove,botNumber) || VerifyDown(requestedMove,botNumber); break; default: moveValid = false; break; } break; } return moveValid; }
private static bool VerifyDown(Move requestedMove,int botNumber) { bool moveValid = true; Tile checkOne = Tile.White; Tile checkTwo = Tile.White; TileCheck(botNumber,ref checkOne,ref checkTwo); switch(requestedMove.MoveType) { case MoveType.LeftDown: if(Board(requestedMove.Piece.X - 1,requestedMove.Piece.Y + 1) != Tile.Black) { moveValid = false; } break; case MoveType.RightDown: if(Board(requestedMove.Piece.X + 1,requestedMove.Piece.Y + 1) != Tile.Black) { moveValid = false; } break; case MoveType.LeftDownJump: Tile halfWayPiece1 = Board(requestedMove.Piece.X - 1,requestedMove.Piece.Y + 1); if(halfWayPiece1 == checkOne || halfWayPiece1 == checkTwo) { if(Board(requestedMove.Piece.X - 2,requestedMove.Piece.Y - 2) != Tile.Black) { moveValid = false; } } else { moveValid = false; } break; case MoveType.RightDownJump: Tile halfWayPiece2 = Board(requestedMove.Piece.X + 1,requestedMove.Piece.Y + 1); if(halfWayPiece2 == checkOne || halfWayPiece2 == checkTwo) { if(Board(requestedMove.Piece.X + 2,requestedMove.Piece.Y + 2) != Tile.Black) { moveValid = false; } } else { moveValid = false; } break; default: moveValid = false; break; } return moveValid; }
public void SelectChecker() { Move = new Move(); Screen.ClearMessageBar(); Screen.DisplayCurrentPlayerMessage(CurrentPlayer); Thread.Sleep(500); var moveStartCoordinate = GetCoordinate(Screen.GetSelectCheckerMessage()); var checkerToMove = checkers.GetChecker(moveStartCoordinate); while (!CanSelectChecker(checkerToMove)) { Screen.ClearMessageBar(); Screen.DisplayCantSelectMessage(); Thread.Sleep(1000); moveStartCoordinate = GetCoordinate(Screen.GetSelectCheckerMessage()); checkerToMove = checkers.GetChecker(moveStartCoordinate); } Move.AddCoordinate(moveStartCoordinate); }