/// <summary> /// Returns an array of Booleans with possible targets for a 'one square around moving' <see cref="Piece"/>. /// </summary> /// <param name="p">The 'one square around moving' piece.</param> /// <param name="board">The board where the piece is.</param> /// <returns>An array of Booleans with possible targets for the <paramref name="p"/>-piece, where true means a valid target and false means an invalid target.</returns> public static bool[,] OneSquareAroundMove(Piece p, Board.Board board) { var possibleMovements = new bool[board.Rows, board.Columns]; var position = p.GetPosition(); #region One Square Around // 012 // 3P5 // 678 for (int row = position.Y - 1; row <= position.Y + 1; row++) { if (row < 0 || row >= board.Rows) { continue; } for (int column = position.X - 1; column <= position.X + 1; column++) { if (column < 0 || column >= board.Columns) { continue; } var watchedTarget = board.GetPiece(row, column); if (watchedTarget is null || watchedTarget.Team != p.Team) { possibleMovements[row, column] = true; } } } #endregion return(possibleMovements); }
// // Summary: // Writes the column letters of the board. // // Parameters: // board: // Is the board from where the columns are. private static void WriteColumnLetters(Board.Board board) { // // Creates the space before the letters begin. string columnLetters = new string(' ', board.Height.ToString().Length + 1); // // Adds the letters. for (int i = 0; i < board.Width; i++) { columnLetters += (char)(i + 'a'); } // // Writes the result. Console.WriteLine(columnLetters); }
public static void ReplayLog(string path, Board.Board board, bool showcase) { try { string logString; using (System.IO.StreamReader file = new System.IO.StreamReader(path)) { logString = file.ReadToEnd(); } List <(int, int)> moveList = board.ParseLog(logString); board.Replay(moveList, showcase); } catch (Exception e) { Console.WriteLine("Recreading board position failed"); } }
/// <summary> /// Returns an array of Booleans with possible targets for a "L-shape"-moving <see cref="Piece"/>. /// </summary> /// <remarks> /// This movement consists of moving the piece two squares vertically and then one square horizontally (or vice versa). /// </remarks> /// <param name="p">The "L-shape"-moving piece.</param> /// <param name="board">The board where the piece is.</param> /// <returns>An array of Booleans with possible targets for the "L-shape"-moving <paramref name="p"/>-piece, where true means a valid target and false means an invalid target.</returns> public static bool[,] LShapeMove(Piece p, Board.Board board) { var possibleMovements = new bool[board.Rows, board.Columns]; var position = p.GetPosition(); #region Gathering Possible Targets /* 7 0 * 6 1 * P * 5 2 * 4 3 */ var targetsSet = new HashSet <TwoDimensionPosition> { new TwoDimensionPosition(position.X + 1, position.Y - 2), // 0 new TwoDimensionPosition(position.X + 2, position.Y - 1), // 1 new TwoDimensionPosition(position.X + 2, position.Y + 1), // 2 new TwoDimensionPosition(position.X + 1, position.Y + 2), // 3 new TwoDimensionPosition(position.X - 1, position.Y + 2), // 4 new TwoDimensionPosition(position.X - 2, position.Y + 1), // 5 new TwoDimensionPosition(position.X - 2, position.Y - 1), // 6 new TwoDimensionPosition(position.X - 1, position.Y - 2) // 7 }; #endregion #region Filtering Targets foreach (var target in targetsSet) { if (target.Y >= 0 && target.Y < board.Rows && target.X >= 0 && target.X < board.Columns) { var watchedPiece = board.GetPiece(target); if (watchedPiece is null || watchedPiece.Team != p.Team) { possibleMovements[target.Y, target.X] = true; } } } #endregion return(possibleMovements); }
/// <summary> /// Prints an board to the console with all letter and row indicators. /// </summary> /// <param name="board">Board to be printed.</param> /// <param name="highlightedSquares">An array of booleans where true positions means a board square to be highlighted.</param> /// <param name="spacesAtLeft">Distance between <see cref="System.Console.CursorLeft"/> and the column of the buffer area where each <paramref name="board"/>'s row begins to be written.</param> public static void PrintBoard(Board.Board board, int spacesAtLeft) { PrintBoard(board, new bool[board.Rows, board.Columns], spacesAtLeft); }
static void Main(string[] args) { Board.Board board = new Board.Board(11, 11); board.Initialize(); board.Print(); #region test IAgent player0 = new ConsolePlayer(); //AlphaBetaTT(1, new WeightedEvaluation(200, 150, 70, 20, 4));//WeightedEvaluation());//ConsolePlayer();//RandomAgent(); IAgent player1 = new AlphaBetaTT(2, new WeightedEvaluation(150, 200, 50, 20, 4)); //EvaluationMaterialBalance()); IAgent[] players = new IAgent[2]; players[0] = player0; players[1] = player1; #endregion test #region replay if (false) { ReplayLog(DEFAULT_LOG_PATH, board, true); Environment.Exit(0); } #endregion int player = 0; #region restore position from log if (false) { ReplayLog(DEFAULT_LOG_PATH, board, false); } #endregion while (player == 0 || player == 1) { (int, int)nextMove = players[player].GetNextMove(board); board.Move(nextMove); Console.WriteLine($"Next Move: {board.SerializeMove(board.log.Last.Value)}"); board.Print(); player = board.activePlayer; using (System.IO.StreamWriter file = new System.IO.StreamWriter(DEFAULT_LOG_PATH, true)) { file.WriteLine($"{board.SerializeMove(nextMove)}"); } if (board.CheckTerminalPosition() == 0) { player = -2; } else if (board.CheckTerminalPosition() == 1) { player = -1; } } if (player == -1) { Console.WriteLine("Silver player won"); } else if (player == -2) { Console.WriteLine("Gold player won"); } LinkedList <(int, int)> possibleMoves = board.GetLegalMoves(); Console.WriteLine("END!"); }
// // Summary: // The basic contructor of the Pawn that only pass its own the // parameters to its own superclass. // // Parameters: // boardPosition: // The position of this piece in the board. // // board: // The board on which the piece belongs. // // team: // The team of the piece. public Pawn(BoardPosition boardPosition, Board.Board board, Team team) : base("P", boardPosition, board, team) { }
public BoardChangedEventArgs(Board.Board board) { this.board = board; }
private static void LoadOther(Game game, StreamReader reader, Board.Board board) { int n = int.Parse(reader.ReadLine()); for (int i = 0; i < n; i++) { string line = reader.ReadLine(); string[] data = line.Split(','); switch (data[0]) { case "monster": int x = int.Parse(data[1]); int y = int.Parse(data[2]); Orientation o = Orientation.H; Orientation.TryParse(data[4], true, out o); Monster monster = GetMonster(int.Parse(data[3])); monster.Orientation = o; board.PlaceFigure(monster, new Point(x, y)); break; case "door": RuneKey color; RuneKey.TryParse(data[12], out color); Orientation orientation; Orientation.TryParse(data[11], out orientation); board.AddDoor(new Door(int.Parse(data[1]), new Point(int.Parse(data[2]), int.Parse(data[3])), new Point(int.Parse(data[4]), int.Parse(data[5])), int.Parse(data[6]), new Point(int.Parse(data[7]), int.Parse(data[8])), new Point(int.Parse(data[9]), int.Parse(data[10])), orientation, color, game.Content.Load<Texture2D>("Images/Board/door-" + color.ToString()))); break; default: board[int.Parse(data[1]), int.Parse(data[2])].Marker = GetMarker(data[0], data[3]); break; } } System.Diagnostics.Debug.WriteLine("Markers loaded successfully!"); FullModel.board = board; LoadChests(game, reader); }
public static void LoadMap(Game game) { StreamReader reader = new StreamReader(TitleContainer.OpenStream("quest1.map")); int height = int.Parse(reader.ReadLine()); int width = int.Parse(reader.ReadLine()); Board.Board board = new Board.Board(width, height, game.Content.Load<Texture2D>("Images/Board/floor")); for (int y = 0; y < height; y++) { char[] c = reader.ReadLine().ToCharArray(); for (int x = 0; x < c.Length; x++) { switch (c[x]) { case ' ': board[x, y] = null; break; default: board[x, y] = new Square(int.Parse(c[x] + string.Empty)); break; } } } System.Diagnostics.Debug.WriteLine("Map loaded successfully!"); LoadOther(game, reader, board); }
public void Copy(Board other) { this.basemap = other.basemap; this.nowmap = other.nowmap; this.maxid = other.maxid; this.pass = other.pass; this.name = other.name; this.time = other.time; Invalidate(); }
// // Summary: // Writes the board with row and column indicators, board limits, marking the targets and marking the actual selected piece. // // Parameters: // board: // The board to be printed. // // targets: // An array marking the positions of the board to be highlighted. // // selectedPiece: // A piece to be highlighted with dark yellow. public static void WriteLineBoard(Board.Board board, bool[,] targets, Piece selectedPiece) { // // Output example with a 10x10 board: // abcdefghij // ╔══════════╗ // 10║-P-P-P-P-P║10 // 9 ║P-P-P-P-P-║ 9 // 8 ║-P-P-P-P-P║ 8 // 7 ║P-P-P-P-P-║ 7 // 6 ║----------║ 6 // 5 ║----------║ 5 // 4 ║-P-P-P-P-P║ 4 // 3 ║P-P-P-P-P-║ 3 // 2 ║-P-P-P-P-P║ 2 // 1 ║P-P-P-P-P-║ 1 // ╚══════════╝ // abcdefghij // // abcdefghij WriteColumnLetters(board); // // ╔══════════╗ Console.WriteLine( new string(' ', board.Height.ToString().Length) + '╔' + new string('═', board.Width) + "╗"); // // xx║----------║xx // x ║----------║ x for (int arrRow = 0; arrRow < board.Height; arrRow++) { // // xx║ // Writes the left number and the '║'. Console.Write( FromArrayRowToBoardRow(arrRow, board.Height) + new string(' ', board.Height.ToString().Length - FromArrayRowToBoardRow(arrRow, board.Height).ToString().Length) + '║'); // // ---------- // Writes the pieces. for (int arrColumn = 0; arrColumn < board.Width; arrColumn++) { if (board.Pieces(arrRow, arrColumn) == selectedPiece) { Console.BackgroundColor = ConsoleColor.DarkYellow; } if (targets[arrRow, arrColumn]) { Console.BackgroundColor = ConsoleColor.Blue; } if (board.Pieces(arrRow, arrColumn) == null) { Console.Write('-'); } else { WritePiece(board.Pieces(arrRow, arrColumn)); } Console.BackgroundColor = ConsoleColor.Black; } // // ║xx // Writes the right number. Console.WriteLine( '║' + new string(' ', board.Height.ToString().Length - FromArrayRowToBoardRow(arrRow, board.Height).ToString().Length) + FromArrayRowToBoardRow(arrRow, board.Height)); } // // ╚══════════╝ Console.WriteLine( new string(' ', board.Height.ToString().Length) + '╚' + new string('═', board.Width) + '╝'); // // abcdefghij WriteColumnLetters(board); }
/// <summary> /// Prints an board to the console with all letter, row indicators and highlighting selected board squares. /// </summary> /// <param name="board">Board to be printed.</param> /// <param name="highlightedSquares">An array of booleans where true positions means a board square to be highlighted.</param> /// <param name="spacesAtLeft">Distance between <see cref="System.Console.CursorLeft"/> and the column of the buffer area where each <paramref name="board"/>'s row begins to be written.</param> public static void PrintBoard(Board.Board board, in bool[,] highlightedSquares, int spacesAtLeft)
/// <summary> /// Returns an array of Booleans with possible targets for a forward-moving <see cref="Piece"/>. /// </summary> /// <remarks> /// This movement consists of: /// <list type="bullet"> /// <item>Both front diagonals.</item> /// <description>When the capture is available.</description> /// <item>A step forward.</item> /// <description>If the front board square is empty.</description> /// <item>Two steps forward.</item> /// <description>When both first step and second step are empty and it is the first movement of the piece.</description> /// </list> /// </remarks> /// <param name="p">The forward-moving piece.</param> /// <param name="board">The board where the piece is.</param> /// <returns>An array of Booleans with possible targets for the forward-moving <paramref name="p"/>-piece, where true means a valid target and false means an invalid target.</returns> public static bool[,] ForwardMove(Piece p, Board.Board board) { var possibleMovements = new bool[board.Rows, board.Columns]; var position = p.GetPosition(); #region Direction Modifier /* Set the directionModifier, it'll say which direction to follow * in the Piece.Board array, e.g.: * - A forward-moving Piece which is from the top of the board must * always move positively along the Piece.Board array rows. * - On the other hand, a forward-moving piece from the bottom of the * board always will move negatively along the Piece.Board array rows. */ var directionModifier = p.Side switch { BoardSide.Top => 1, BoardSide.Bottom => - 1, _ => throw new NotImplementedException("The Piece.Side is invalid.") }; #endregion #region Front Diagonals // 0 1 // P TwoDimensionPosition watchedPosition; for (int columnModifier = 0; columnModifier <= 2; columnModifier += 2) { watchedPosition = new TwoDimensionPosition(position.X - 1 + columnModifier, position.Y + directionModifier); if (watchedPosition.X >= 0 && watchedPosition.X < board.Columns && watchedPosition.Y >= 0 && watchedPosition.Y < board.Rows) { var watchedPiece = board.GetPiece(watchedPosition); if (!(watchedPiece is null) && watchedPiece.Team != p.Team) { possibleMovements[watchedPosition.Y, watchedPosition.X] = true; } } } #endregion #region First step and Second Step Forward // 2 // P watchedPosition = new TwoDimensionPosition(position.X, position.Y + directionModifier); if (watchedPosition.Y >= 0 && watchedPosition.Y < board.Rows && board.GetPiece(watchedPosition) is null) { possibleMovements[watchedPosition.Y, watchedPosition.X] = true; // 3 // // P watchedPosition = new TwoDimensionPosition(watchedPosition.X, watchedPosition.Y + directionModifier); if (watchedPosition.Y >= 0 && watchedPosition.Y < board.Rows && p.TimesMoved == 0 && board.GetPiece(watchedPosition.Y, watchedPosition.X) is null) { possibleMovements[watchedPosition.Y, watchedPosition.X] = true; } } #endregion return(possibleMovements); }
/// <summary> /// Returns an array of Booleans with possible targets for a straight-moving <see cref="Piece"/>. /// </summary> /// <param name="p">The straight-moving piece.</param> /// <param name="board">The board where the piece is.</param> /// <returns>An array of Booleans with possible targets for the straight-moving <paramref name="p"/>-piece, where true means a valid target and false means an invalid target.</returns> public static bool[,] StraightMove(Piece p, Board.Board board) { var possibleMovements = new bool[board.Rows, board.Columns]; var position = p.GetPosition(); #region Top // Checks the top of the piece: // [...] // 1 // 0 // P for (int row = position.Y - 1; row >= 0; row--) { var watchedTarget = board.GetPiece(row, position.X); if (watchedTarget is null) { possibleMovements[row, position.X] = true; } else if (watchedTarget.Team != p.Team) { possibleMovements[row, position.X] = true; break; } else { break; } } #endregion #region Right // Checks the right side of the piece: // P01[...] for (int column = position.X + 1; column < board.Columns; column++) { var watchedTarget = board.GetPiece(position.Y, column); if (watchedTarget is null) { possibleMovements[position.Y, column] = true; } else if (watchedTarget.Team != p.Team) { possibleMovements[position.Y, column] = true; break; } else { break; } } #endregion #region Bottom // Checks the bottom side of the piece: // P // 0 // 1 // [...] for (int row = position.Y + 1; row < board.Rows; row++) { var watchedTarget = board.GetPiece(row, position.X); if (watchedTarget is null) { possibleMovements[row, position.X] = true; } else if (watchedTarget.Team != p.Team) { possibleMovements[row, position.X] = true; break; } else { break; } } #endregion #region Left // Checks the left side of the piece: // [...]10P for (int column = position.X - 1; column >= 0; column--) { var watchedTarget = board.GetPiece(position.Y, column); if (watchedTarget is null) { possibleMovements[position.Y, column] = true; } else if (watchedTarget.Team != p.Team) { possibleMovements[position.Y, column] = true; break; } else { break; } } #endregion return(possibleMovements); }
// // Summary: // The basic contructor of the Dame that only pass its own the // parameters to its own superclass. // // Parameters: // boardPosition: // The position of this piece in the board. // // board: // The board on which the piece belongs. // // team: // The team of the piece. public Dame(BoardPosition boardPosition, Board.Board board, Team team) : base("D", boardPosition, board, team) { }
/// <summary> /// Returns an array of Booleans with possible targets for a diagonal-moving <see cref="Piece"/>. /// </summary> /// <param name="p">The diagonal-moving piece.</param> /// <param name="board">The board where the piece is.</param> /// <returns>An array of Booleans with possible targets for the diagonal-moving <paramref name="p"/>-piece, where true means a valid target and false means an invalid target.</returns> public static bool[,] DiagonalMove(Piece p, Board.Board board) { var possibleMovements = new bool[board.Rows, board.Columns]; var position = p.GetPosition(); #region Top right // Checks the top right diagonal of the piece: // [..] // 1 // 0 // P for (int row = position.Y - 1, column = position.X + 1; row >= 0 && column < board.Columns; row--, column++) { var watchedTarget = board.GetPiece(row, column); if (watchedTarget is null) { possibleMovements[row, column] = true; } else if (watchedTarget.Team != p.Team) { possibleMovements[row, column] = true; break; } else { break; } } #endregion #region Bottom right // Checks the bottom right diagonal of the piece: // P // 0 // 1 // [..] for (int row = position.Y + 1, column = position.X + 1; row < board.Rows && column < board.Columns; row++, column++) { var watchedTarget = board.GetPiece(row, column); if (watchedTarget is null) { possibleMovements[row, column] = true; } else if (watchedTarget.Team != p.Team) { possibleMovements[row, column] = true; break; } else { break; } } #endregion #region Bottom left // Checks the bottom left diagonal of the piece: // P // 0 // 1 // [..] for (int row = position.Y + 1, column = position.X - 1; row < board.Rows && column >= 0; row++, column--) { var watchedTarget = board.GetPiece(row, column); if (watchedTarget is null) { possibleMovements[row, column] = true; } else if (watchedTarget.Team != p.Team) { possibleMovements[row, column] = true; break; } else { break; } } #endregion #region Top left // Checks the top left diagonal of the piece: // [..] // 1 // 0 // P for (int row = position.Y - 1, column = position.X - 1; row >= 0 && column >= 0; row--, column--) { var watchedTarget = board.GetPiece(row, column); if (watchedTarget is null) { possibleMovements[row, column] = true; } else if (watchedTarget.Team != p.Team) { possibleMovements[row, column] = true; break; } else { break; } } #endregion return(possibleMovements); }