// Draw some piece with color and position public void DrawPieceAt(Position pos, ConsoleColor color, Piece opponent) { if (!positionsOnBoard[pos.x, pos.y] && !opponent.positionsOnBoard[pos.x, pos.y]) { Console.ForegroundColor = color; Console.SetCursorPosition(positions[pos.x, pos.y].x - (PieceBody.Length / 2), positions[pos.x, pos.y].y); Console.Write(PieceBody); } else { throw new ArgumentException("Please try again!"); } }
// Generate input string to move some piece public string GenerateInputForMoving(Piece player) { List<int[]> pieces = new List<int[]>(); for (int i = 0; i < this.piece.positionsOnBoard.GetLength(0); i++) { for (int j = 0; j < this.piece.positionsOnBoard.GetLength(1); j++) { if (this.piece.positionsOnBoard[i, j]) { pieces.Add(new int[] { i, j }); } } } while (true) { int generatePiece = generator.Next(0, pieces.Count); int gen = 0; bool checkRectangleCorners = true; bool checkBorderRectangles = true; bool checkMiddleRectangle = true; #region Check all rectangle corners switch (int.Parse(pieces[generatePiece][0].ToString() + pieces[generatePiece][1].ToString())) { case 0: case 10: case 20: gen = (generator.Next(0, 2) == 0) ? 1 : 3; break; case 2: case 12: case 22: gen = (generator.Next(0, 2) == 0) ? 1 : 4; break; case 5: case 15: case 25: gen = (generator.Next(0, 2) == 0) ? 3 : 6; break; case 7: case 17: case 27: gen = (generator.Next(0, 2) == 0) ? 4 : 6; break; default: checkRectangleCorners = false; break; } if (checkRectangleCorners) { if (!player.positionsOnBoard[0, gen] && !this.piece.positionsOnBoard[0, gen]) { return coords[pieces[generatePiece][0], pieces[generatePiece][1]] + coords[pieces[generatePiece][0], gen]; } } #endregion #region Check all middle pieces on border rectangles switch (int.Parse(pieces[generatePiece][0].ToString() + pieces[generatePiece][1].ToString())) { case 1: case 21: gen = (generator.Next(0, 2) == 0) ? 0 : 2; break; case 3: case 23: gen = (generator.Next(0, 2) == 0) ? 0 : 5; break; case 4: case 24: gen = (generator.Next(0, 2) == 0) ? 2 : 7; break; case 6: case 26: gen = (generator.Next(0, 2) == 0) ? 5 : 7; break; default: checkBorderRectangles = false; break; } if (checkBorderRectangles) { if (!player.positionsOnBoard[0, gen] && !this.piece.positionsOnBoard[0, gen]) { return coords[pieces[generatePiece][0], pieces[generatePiece][1]] + coords[pieces[generatePiece][0], gen]; } if (!player.positionsOnBoard[1, pieces[generatePiece][1]] && !this.piece.positionsOnBoard[1, pieces[generatePiece][1]]) { return coords[pieces[generatePiece][0], pieces[generatePiece][1]] + coords[1, pieces[generatePiece][1]]; } } #endregion #region Check all middle pieces on middle rectangle switch (int.Parse(pieces[generatePiece][0].ToString() + pieces[generatePiece][1].ToString())) { case 11: gen = (generator.Next(0, 2) == 0) ? 0 : 2; break; case 13: gen = (generator.Next(0, 2) == 0) ? 0 : 5; break; case 14: gen = (generator.Next(0, 2) == 0) ? 2 : 7; break; case 16: gen = (generator.Next(0, 2) == 0) ? 5 : 7; break; default: checkMiddleRectangle = false; break; } if (checkMiddleRectangle) { if (!player.positionsOnBoard[0, gen] && !this.piece.positionsOnBoard[0, gen]) { return coords[pieces[generatePiece][0], pieces[generatePiece][1]] + coords[pieces[generatePiece][0], gen]; } if (!player.positionsOnBoard[0, pieces[generatePiece][1]] && !this.piece.positionsOnBoard[0, pieces[generatePiece][1]]) { return coords[pieces[generatePiece][0], pieces[generatePiece][1]] + coords[0, pieces[generatePiece][1]]; } if (!player.positionsOnBoard[2, pieces[generatePiece][1]] && !this.piece.positionsOnBoard[2, pieces[generatePiece][1]]) { return coords[pieces[generatePiece][0], pieces[generatePiece][1]] + coords[2, pieces[generatePiece][1]]; } } #endregion } }
// Generate input string to remove some piece from the board internal string GenerateInputForRemoving(byte[, ,] allMills, Piece player) { for (int n = 0; n < allMills.GetLength(0); n++) { for (int j = 0; j < allMills.GetLength(1); j++) { if (player.positionsOnBoard[allMills[n, j, 0], allMills[n, j, 1]]) { return coords[allMills[n, j, 0], allMills[n, j, 1]]; } } } throw new Exception("There is not piece for removing!"); }
// Constructor public Robot(Piece robotPieces, string robotName = "PC") : base(robotPieces, robotName) { generator = new Random(); }
// Generate input string to place some piece on the board public string GenerateInputForPlacing(byte[, ,] allMills, Piece player) { #region Check for free mill for (int n = 0; n < allMills.GetLength(0); n++) { bool[] pieces = new bool[3]; for (int j = 0; j < allMills.GetLength(1); j++) { if (this.piece.positionsOnBoard[allMills[n, j, 0], allMills[n, j, 1]]) { pieces[j] = true; } } if (pieces.Where(m => m == true).Count() == 2) { for (int i = 0; i < 3; i++) { if (!pieces[i] && !this.piece.positionsOnBoard[allMills[n, i, 0], allMills[n, i, 1]] && !player.positionsOnBoard[allMills[n, i, 0], allMills[n, i, 1]]) { return coords[allMills[n, i, 0], allMills[n, i, 1]]; } } } } #endregion #region If the number of occupied pieces for mill is 2 for (int n = 0; n < allMills.GetLength(0); n++) { bool[] pieces = new bool[3]; for (int j = 0; j < allMills.GetLength(1); j++) { if (player.positionsOnBoard[allMills[n, j, 0], allMills[n, j, 1]]) { pieces[j] = true; } } if (pieces.Where(m => m == true).Count() == 2) { for (int i = 0; i < 3; i++) { if (!pieces[i] && !this.piece.positionsOnBoard[allMills[n, i, 0], allMills[n, i, 1]]) { return coords[allMills[n, i, 0], allMills[n, i, 1]]; } } } } #endregion #region If the number of occupied pieces for mill is 1 for (int n = 0; n < allMills.GetLength(0); n++) { bool[] pieces = new bool[3]; for (int j = 0; j < allMills.GetLength(1); j++) { if (player.positionsOnBoard[allMills[n, j, 0], allMills[n, j, 1]]) { pieces[j] = true; } } if (pieces.Where(m => m == true).Count() == 1) { for (int i = 0; i < 3; i++) { switch (i) { case 0: if (pieces[i] && !this.piece.positionsOnBoard[allMills[n, 2, 0], allMills[n, 2, 1]]) { return coords[allMills[n, 2, 0], allMills[n, 2, 1]]; } break; case 1: int gen = (generator.Next(0, 2) == 0) ? 0 : 2; if (pieces[i] && !this.piece.positionsOnBoard[allMills[n, gen, 0], allMills[n, gen, 1]]) { return coords[allMills[n, gen, 0], allMills[n, gen, 1]]; } break; case 2: if (pieces[i] && !this.piece.positionsOnBoard[allMills[n, 0, 0], allMills[n, 0, 1]]) { return coords[allMills[n, 0, 0], allMills[n, 0, 1]]; } break; default: break; } } } } #endregion // If there aren't any opponent pieces on the bord return coords[generator.Next(0, 3), generator.Next(0, 8)]; }
// Constructor public RealPlayer(Piece playersPieces, string playersName = "Player", int wins = 0) : base(playersPieces, playersName) { this.winsCount = wins; }
// Check is the piece movable private bool CanBeMoved(Position fromPos, Position toPos, Piece opponent) { if ((fromPos.x != toPos.x || fromPos.y != toPos.y) && positionsOnBoard[fromPos.x, fromPos.y]) { bool result = false; if (fromPos.x == toPos.x) { switch (fromPos.y) { case 0: if (toPos.y == fromPos.y + 1 && !positionsOnBoard[fromPos.x, fromPos.y + 1] && !opponent.positionsOnBoard[fromPos.x, fromPos.y + 1]) { result = true; } else if (toPos.y == fromPos.y + 3 && !positionsOnBoard[fromPos.x, fromPos.y + 3] && !opponent.positionsOnBoard[fromPos.x, fromPos.y + 3]) { result = true; } break; case 1: case 6: if (toPos.y == fromPos.y + 1 && !positionsOnBoard[fromPos.x, fromPos.y + 1] && !opponent.positionsOnBoard[fromPos.x, fromPos.y + 1]) { result = true; } else if (toPos.y == fromPos.y - 1 && !positionsOnBoard[fromPos.x, fromPos.y - 1] && !opponent.positionsOnBoard[fromPos.x, fromPos.y - 1]) { result = true; } break; case 2: if (toPos.y == fromPos.y - 1 && !positionsOnBoard[fromPos.x, fromPos.y - 1] && !opponent.positionsOnBoard[fromPos.x, fromPos.y - 1]) { result = true; } else if (toPos.y == fromPos.y + 2 && !positionsOnBoard[fromPos.x, fromPos.y + 2] && !opponent.positionsOnBoard[fromPos.x, fromPos.y + 2]) { result = true; } break; case 3: if (toPos.y == fromPos.y - 3 && !positionsOnBoard[fromPos.x, fromPos.y - 3] && !opponent.positionsOnBoard[fromPos.x, fromPos.y - 3]) { result = true; } else if (toPos.y == fromPos.y + 2 && !positionsOnBoard[fromPos.x, fromPos.y + 2] && !opponent.positionsOnBoard[fromPos.x, fromPos.y + 2]) { result = true; } break; case 4: if (toPos.y == fromPos.y - 2 && !positionsOnBoard[fromPos.x, fromPos.y - 2] && !opponent.positionsOnBoard[fromPos.x, fromPos.y - 2]) { result = true; } else if (toPos.y == fromPos.y + 3 && !positionsOnBoard[fromPos.x, fromPos.y + 3] && !opponent.positionsOnBoard[fromPos.x, fromPos.y + 3]) { result = true; } break; case 5: if (toPos.y == fromPos.y - 2 && !positionsOnBoard[fromPos.x, fromPos.y - 2] && !opponent.positionsOnBoard[fromPos.x, fromPos.y - 2]) { result = true; } else if (toPos.y == fromPos.y + 1 && !positionsOnBoard[fromPos.x, fromPos.y + 1] && !opponent.positionsOnBoard[fromPos.x, fromPos.y + 1]) { result = true; } break; case 7: if (toPos.y == fromPos.y - 1 && !positionsOnBoard[fromPos.x, fromPos.y - 1] && !opponent.positionsOnBoard[fromPos.x, fromPos.y - 1]) { result = true; } else if (toPos.y == fromPos.y - 3 && !positionsOnBoard[fromPos.x, fromPos.y - 3] && !opponent.positionsOnBoard[fromPos.x, fromPos.y - 3]) { result = true; } break; default: result = false; break; } if (result) { return true; } } else { if (fromPos.y == 3 || fromPos.y == 4 || fromPos.y == 1 || fromPos.y == 6) { switch (fromPos.x) { case 0: if (toPos.x == fromPos.x + 1 && !positionsOnBoard[fromPos.x + 1, fromPos.y] && !opponent.positionsOnBoard[fromPos.x + 1, fromPos.y]) { result = true; } break; case 1: if (toPos.x == fromPos.x + 1 && !positionsOnBoard[fromPos.x + 1, fromPos.y] && !opponent.positionsOnBoard[fromPos.x + 1, fromPos.y]) { result = true; } else if (toPos.x == fromPos.x - 1 && !positionsOnBoard[fromPos.x - 1, fromPos.y] && !opponent.positionsOnBoard[fromPos.x - 1, fromPos.y]) { result = true; } break; case 2: if (toPos.x == fromPos.x - 1 && !positionsOnBoard[fromPos.x - 1, fromPos.y] && !opponent.positionsOnBoard[fromPos.x - 1, fromPos.y]) { result = true; } break; default: result = false; break; } if (result) { return true; } } } } return false; }
// Remove some piece from the board public void RemovePiece(Position remove, Piece opponent) { if (positionsOnBoard[remove.x, remove.y]) { this.count--; positionsOnBoard[remove.x, remove.y] = false; DrawPieceAt(remove, ConsoleColor.Gray, opponent); } else { throw new Exception("Piece is missing on this position!"); } }
// Move the pieces public void Move(string input, Piece opponent, Board board) { Position fromPos = GetPositionFromInput(input[0].ToString() + input[1].ToString()); Position toPos = GetPositionFromInput(input[2].ToString() + input[3].ToString()); if (CanBeMoved(fromPos, toPos, opponent)) { positionsOnBoard[fromPos.x, fromPos.y] = false; // освобождаване на старата позиция DrawPieceAt(fromPos, board.PiecePlaceColor, opponent); DrawPieceAt(toPos, PiecesColor, opponent); positionsOnBoard[toPos.x, toPos.y] = true; // поставяне на новата позиция } else { throw new ArgumentException("Please try again!"); } }
// Return true if mills are happen private static bool CheckForMills(Piece player, bool[,] playerPositionTemp) { for (int n = 0; n < AllPossibleMills.GetLength(0); n++) { if ((player.positionsOnBoard[AllPossibleMills[n, 0, 0], AllPossibleMills[n, 0, 1]] && player.positionsOnBoard[AllPossibleMills[n, 1, 0], AllPossibleMills[n, 1, 1]] && player.positionsOnBoard[AllPossibleMills[n, 2, 0], AllPossibleMills[n, 2, 1]]) && !(playerPositionTemp[AllPossibleMills[n, 0, 0], AllPossibleMills[n, 0, 1]] && playerPositionTemp[AllPossibleMills[n, 1, 0], AllPossibleMills[n, 1, 1]] && playerPositionTemp[AllPossibleMills[n, 2, 0], AllPossibleMills[n, 2, 1]])) { return true; } } return false; }
// Constructor public Player(Piece piece, string name) { this.piece = piece; this.Name = name; }