public void IntermediateBeatsBeginner() { int BotWins = 0; int Bot2Wins = 0; SquareValues winner = 0; var blackpieces = Pieces.BlackPlacements(); var whitepieces = Pieces.WhitePlacements(); var Board = new GameBoard(8, blackpieces, whitepieces); var Bot = new BotPlayer3(Board, Types[rnd.Next(Types.Count)]); var Bot2 = new BotPlayer1(Board, Board.OpponentType(Bot.Type)); //var Bot = new BotPlayerTempV(Board, SquareValues.Black, 3); //var Bot2 = new BotPlayerTempV(Board, SquareValues.White, 2); for (int i = 0; i < 100; i++) { winner = BotPlayer.PlayBots(Board, Bot, Bot2); if (winner == Bot.Type) { BotWins++; } else { Bot2Wins++; } } Assert.IsTrue(BotWins > Bot2Wins); }
/// <summary> /// Moves a Piece on the Board /// </summary> /// <param name="type"></param> /// <param name="oldCol"></param> /// <param name="oldRow"></param> /// <param name="newCol"></param> /// <param name="newRow"></param> public void MovePiece(SquareValues type, int oldCol, int oldRow, int newCol, int newRow) { Squares[oldCol, oldRow] = SquareValues.Empty; Squares[newCol, newRow] = type; if (type == SquareValues.Black && newRow == 0) { Squares[newCol, newRow] = SquareValues.BlackKing; } if (type == SquareValues.White && newRow == 7) { Squares[newCol, newRow] = SquareValues.WhiteKing; } if (newCol == oldCol + 2 && newRow == oldRow - 2) { Squares[oldCol + 1, oldRow - 1] = SquareValues.Empty; } if (newCol == oldCol + 2 && newRow == oldRow + 2) { Squares[oldCol + 1, oldRow + 1] = SquareValues.Empty; } if (newCol == oldCol - 2 && newRow == oldRow + 2) { Squares[oldCol - 1, oldRow + 1] = SquareValues.Empty; } if (newCol == oldCol - 2 && newRow == oldRow - 2) { Squares[oldCol - 1, oldRow - 1] = SquareValues.Empty; } }
/// <summary> /// Asserts whether a move on the checkers Board by any piece is Valid /// </summary> /// <param name="type"></param> /// <param name="oldCol"></param> /// <param name="oldRow"></param> /// <param name="newCol"></param> /// <param name="newRow"></param> /// <returns></returns> public bool IsValidMove(SquareValues type, int oldCol, int oldRow, int newCol, int newRow) { if (IsEmptySquare(newCol, newRow) == false) { return(false); } if (newCol == oldCol || newRow == oldRow) { return(false); } switch (type) { case SquareValues.Black: return(IsValidBlackMove(oldCol, oldRow, newCol, newRow)); case SquareValues.White: return(IsValidWhiteMove(oldCol, oldRow, newCol, newRow)); case SquareValues.BlackKing: return(IsValidBlackKingMove(oldCol, oldRow, newCol, newRow)); case SquareValues.WhiteKing: return(IsValidWhiteKingMove(oldCol, oldRow, newCol, newRow)); default: throw new Exception("Invalid SquareValue"); } }
public int Count(SquareValues type) { int count = 0; if (type == SquareValues.Black || type == SquareValues.BlackKing) { for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { var square = ReadSquare(col, row); if (square == SquareValues.Black || square == SquareValues.BlackKing) { count++; } } } } if (type == SquareValues.White || type == SquareValues.WhiteKing) { for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { var square = ReadSquare(col, row); if (square == SquareValues.White || square == SquareValues.WhiteKing) { count++; } } } } return(count); }
public void AdvancedVsAdvanced() { int BotWins = 0; int Bot2Wins = 0; SquareValues winner = 0; var blackpieces = Pieces.BlackPlacements(); var whitepieces = Pieces.WhitePlacements(); var Board = new GameBoard(8, blackpieces, whitepieces); var Bot = new BotPlayer5(Board, Types[rnd.Next(Types.Count)]); var Bot2 = new BotPlayer5(Board, Board.OpponentType(Bot.Type)); //var Bot = new BotPlayerTempV(Board, SquareValues.Black, 3); //var Bot2 = new BotPlayerTempV(Board, SquareValues.White, 2); for (int i = 0; i < 100; i++) { winner = BotPlayer.PlayBots(Board, Bot, Bot2); if (winner == Bot.Type) { BotWins++; } else { Bot2Wins++; } } Assert.IsFalse(Math.Abs(BotWins - Bot2Wins) > 20); }
/// <summary> /// Checks to see if a piece can become King on the next move /// </summary> /// <param name="newrow"></param> /// <param name="type"></param> /// <returns></returns> public bool CheckKing(int newrow, SquareValues type) { switch (type) { case SquareValues.Black: if (newrow == 0) { return(true); } else { return(false); } case SquareValues.White: if (newrow == 7) { return(true); } else { return(false); } default: return(false); } }
/// <summary> /// Checks if a move is safe /// </summary> public bool CheckingSpaces(int oldcol, int oldrow, int newcol, int newrow) { try { var type = Board.ReadSquare(oldcol, oldrow); a = Board.Squares[oldcol, oldrow]; b = Board.Squares[newcol, newrow]; Board.Squares[oldcol, oldrow] = SquareValues.Empty; Board.Squares[newcol, newrow] = type; if (!Board.CanBeJumped(newcol, newrow)) { Board.Squares[oldcol, oldrow] = a; Board.Squares[newcol, newrow] = b; return(true); } else { Board.Squares[oldcol, oldrow] = a; Board.Squares[newcol, newrow] = b; return(false); } } catch (Exception) { Board.Squares[oldcol, oldrow] = a; Board.Squares[newcol, newrow] = b; return(false); } }
public Move(Modality mode, SquareValues type, int inicol, int inirow, int fincol, int finrow) { Mode = mode; Type = type; IniCol = inicol; IniRow = inirow; FinCol = fincol; FinRow = finrow; }
/// <summary> /// Creates the Board in CLI /// </summary> public void PrintBoard() { int boardSize = Size; Console.WriteLine(); Console.WriteLine("The board looks like this: "); Console.WriteLine(); Console.Write(" "); for (int Column = 0; Column < boardSize; Column++) { Console.Write(" " + ColALphabet[Column] + " "); } Console.WriteLine(); for (int row = 0; row < boardSize; row++) { Console.Write(row + " "); for (int col = 0; col < boardSize; col++) { SquareValues square = ReadSquare(col, row); switch (square) { case SquareValues.Empty: Console.Write(' '); break; case SquareValues.Black: Console.Write('*'); break; case SquareValues.BlackKing: Console.Write('@'); break; case SquareValues.White: Console.Write('%'); break; case SquareValues.WhiteKing: Console.Write('/'); break; } if (col != boardSize - 1) { Console.Write(" | "); } } Console.WriteLine(); } Console.WriteLine(); }
public SquareValues OpponentType(SquareValues type) { switch (type) { case SquareValues.Black: return(SquareValues.White); case SquareValues.White: return(SquareValues.Black); case SquareValues.BlackKing: return(SquareValues.WhiteKing); case SquareValues.WhiteKing: return(SquareValues.BlackKing); default: return(SquareValues.Empty); } }
/// <summary> /// Checks if the Piecetype is correct relative to the SquareValue put in /// </summary> /// <param name="type"></param> /// <param name="oldCol"></param> /// <param name="oldRow"></param> /// <returns></returns> public bool NotYourPiece(SquareValues type, int oldCol, int oldRow) { if (type == SquareValues.Black) { if (Squares[oldCol, oldRow] != SquareValues.White && Squares[oldCol, oldRow] != SquareValues.WhiteKing) { return(false); } return(true); } if (type == SquareValues.White) { if (Squares[oldCol, oldRow] != SquareValues.Black && Squares[oldCol, oldRow] != SquareValues.BlackKing) { return(false); } return(true); } return(true); }
public void MovePiece(Move movement) { SquareValues type = movement.Type; int oldCol = movement.IniCol; int oldRow = movement.IniRow; int newCol = movement.FinCol; int newRow = movement.FinRow; Squares[oldCol, oldRow] = SquareValues.Empty; Squares[newCol, newRow] = type; if (type == SquareValues.Black && newRow == 0) { Squares[newCol, newRow] = SquareValues.BlackKing; } if (type == SquareValues.White && newRow == 7) { Squares[newCol, newRow] = SquareValues.WhiteKing; } if (newCol == oldCol + 2 && newRow == oldRow - 2) { Squares[oldCol + 1, oldRow - 1] = SquareValues.Empty; } if (newCol == oldCol + 2 && newRow == oldRow + 2) { Squares[oldCol + 1, oldRow + 1] = SquareValues.Empty; } if (newCol == oldCol - 2 && newRow == oldRow + 2) { Squares[oldCol - 1, oldRow + 1] = SquareValues.Empty; } if (newCol == oldCol - 2 && newRow == oldRow - 2) { Squares[oldCol - 1, oldRow - 1] = SquareValues.Empty; } }
/// <summary> /// Checks to see if a Piece can make any moves /// </summary> /// <param name="type"></param> /// <returns></returns> public bool CanMove(SquareValues type) { for (int oldrow = 0; oldrow < 8; oldrow++) { for (int oldcol = 0; oldcol < 8; oldcol++) { if (!IsEmptySquare(oldcol, oldrow) && !NotYourPiece(type, oldcol, oldrow)) { var realtype = Squares[oldcol, oldrow]; for (int newrow = 0; newrow < 8; newrow++) { for (int newcol = 0; newcol < 8; newcol++) { if (IsEmptySquare(newcol, newrow) && IsValidMove(realtype, oldcol, oldrow, newcol, newrow)) { return(true); } } } } } } return(false); }
public BotPlayer(GameBoard board, SquareValues type) { Type = type; Board = board; }
/// <summary> /// Checks to see if a piece has the option to jump and returns an int which tells of the direction of jump /// </summary> /// <param name="type"></param> /// <param name="currentCol"></param> /// <param name="currentRow"></param> /// <returns></returns> public int CanJump(SquareValues type, int currentCol, int currentRow) { var B = SquareValues.Black; var Bk = SquareValues.BlackKing; var W = SquareValues.White; var Wk = SquareValues.WhiteKing; var E = SquareValues.Empty; if (type == B || type == Bk) { if (IsInvalidEntry(currentRow - 2) == null) { if (IsInvalidEntry(currentCol + 2) == null) { if (Squares[currentCol + 1, currentRow - 1] == W || Squares[currentCol + 1, currentRow - 1] == Wk) { if (Squares[currentCol + 2, currentRow - 2] == E) { return(1); } } } if (IsInvalidEntry(currentCol - 2) == null) { if (Squares[currentCol - 1, currentRow - 1] == W || Squares[currentCol - 1, currentRow - 1] == Wk) { if (Squares[currentCol - 2, currentRow - 2] == E) { return(2); } } } } //-------------------------------------------------------------------- if (IsInvalidEntry(currentRow + 2) == null && type == Bk) { if (IsInvalidEntry(currentCol + 2) == null) { if (Squares[currentCol + 1, currentRow + 1] == W || Squares[currentCol + 1, currentRow + 1] == Wk) { if (Squares[currentCol + 2, currentRow + 2] == E) { return(3); } } } if (IsInvalidEntry(currentCol - 2) == null) { if (Squares[currentCol - 1, currentRow + 1] == W || Squares[currentCol - 1, currentRow + 1] == Wk) { if (Squares[currentCol - 2, currentRow + 2] == E) { return(4); } } } } //----------------------------------------------------------------------- return(0); } if (type == W || type == Wk) { if (IsInvalidEntry(currentRow + 2) == null) { if (IsInvalidEntry(currentCol + 2) == null) { if (Squares[currentCol + 1, currentRow + 1] == B || Squares[currentCol + 1, currentRow + 1] == Bk) { if (Squares[currentCol + 2, currentRow + 2] == E) { return(3); } } } if (IsInvalidEntry(currentCol - 2) == null) { if (Squares[currentCol - 1, currentRow + 1] == B || Squares[currentCol - 1, currentRow + 1] == Bk) { if (Squares[currentCol - 2, currentRow + 2] == E) { return(4); } } } } //----------------------------------------------------------- if (IsInvalidEntry(currentRow - 2) == null && type == Wk) { if (IsInvalidEntry(currentCol + 2) == null) { if (Squares[currentCol + 1, currentRow - 1] == B || Squares[currentCol + 1, currentRow - 1] == Bk) { if (Squares[currentCol + 2, currentRow - 2] == E) { return(1); } } } if (IsInvalidEntry(currentCol - 2) == null) { if (Squares[currentCol - 1, currentRow - 1] == B || Squares[currentCol - 1, currentRow - 1] == Bk) { if (Squares[currentCol - 2, currentRow - 2] == E) { return(2); } } } } //--------------------------------------------------------------- return(0); } return(0); }
public int CountOverlappingFabricSquares() { return(SquareValues.Where(c => c == 0).Count()); }
/// <summary> /// Checks if a jump is safe /// </summary> /// <param name="oldcol"></param> /// <param name="oldrow"></param> /// <param name="mode"></param> private void CheckingJumps(int oldcol, int oldrow, SquareValues type, CheckNo mode) { switch (mode) { case CheckNo.RightUp: try { a = Board.Squares[oldcol + 2, oldrow - 2]; b = Board.Squares[oldcol + 1, oldrow - 1]; Board.Squares[oldcol + 2, oldrow - 2] = type; Board.Squares[oldcol + 1, oldrow - 1] = SquareValues.Empty; if (!Board.CanBeJumped(oldcol + 2, oldrow - 2) || Board.CanJump(type, oldcol + 2, oldrow - 2) != 0) { Board.Squares[oldcol + 2, oldrow - 2] = a; Board.Squares[oldcol + 1, oldrow - 1] = b; Jumper(oldcol, oldrow); } else { Board.Squares[oldcol + 2, oldrow - 2] = a; Board.Squares[oldcol + 1, oldrow - 1] = b; } } catch (Exception) { try { Board.Squares[oldcol + 2, oldrow - 2] = a; Board.Squares[oldcol + 1, oldrow - 1] = b; } catch (Exception) { return; } return; } break; case CheckNo.LeftUp: try { a = Board.Squares[oldcol - 2, oldrow - 2]; b = Board.Squares[oldcol - 1, oldrow - 1]; Board.Squares[oldcol - 2, oldrow - 2] = type; Board.Squares[oldcol - 1, oldrow - 1] = SquareValues.Empty; if (!Board.CanBeJumped(oldcol - 2, oldrow - 2) || Board.CanJump(type, oldcol - 2, oldrow - 2) != 0) { Board.Squares[oldcol - 2, oldrow - 2] = a; Board.Squares[oldcol - 1, oldrow - 1] = b; Jumper(oldcol, oldrow); } else { Board.Squares[oldcol - 2, oldrow - 2] = a; Board.Squares[oldcol - 1, oldrow - 1] = b; } } catch (Exception) { try { Board.Squares[oldcol - 2, oldrow - 2] = a; Board.Squares[oldcol - 1, oldrow - 1] = b; } catch (Exception) { return; } return; } break; case CheckNo.RightDown: try { a = Board.Squares[oldcol + 2, oldrow + 2]; b = Board.Squares[oldcol + 1, oldrow + 1]; Board.Squares[oldcol + 2, oldrow + 2] = type; Board.Squares[oldcol + 1, oldrow + 1] = SquareValues.Empty; if (!Board.CanBeJumped(oldcol + 2, oldrow + 2) || Board.CanJump(type, oldcol + 2, oldrow + 2) != 0) { Board.Squares[oldcol + 2, oldrow + 2] = a; Board.Squares[oldcol + 1, oldrow + 1] = b; Jumper(oldcol, oldrow); } else { Board.Squares[oldcol + 2, oldrow + 2] = a; Board.Squares[oldcol + 1, oldrow + 1] = b; } } catch (Exception) { try { Board.Squares[oldcol + 2, oldrow + 2] = a; Board.Squares[oldcol + 1, oldrow + 1] = b; } catch (Exception) { return; } return; } break; case CheckNo.LeftDown: try { a = Board.Squares[oldcol - 2, oldrow + 2]; b = Board.Squares[oldcol - 1, oldrow + 1]; Board.Squares[oldcol - 2, oldrow + 2] = type; Board.Squares[oldcol - 1, oldrow + 1] = SquareValues.Empty; if (!Board.CanBeJumped(oldcol - 2, oldrow + 2) || Board.CanJump(type, oldcol - 2, oldrow + 2) != 0) { Board.Squares[oldcol - 2, oldrow + 2] = a; Board.Squares[oldcol - 1, oldrow + 1] = b; Jumper(oldcol, oldrow); } else { Board.Squares[oldcol - 2, oldrow + 2] = a; Board.Squares[oldcol - 1, oldrow + 1] = b; } } catch (Exception) { try { Board.Squares[oldcol - 2, oldrow + 2] = a; Board.Squares[oldcol - 1, oldrow + 1] = b; } catch (Exception) { return; } return; } break; } }
/// <summary> /// Checks to see if a move as well as a jump is safe before moving /// </summary> private void BotPlayer5() { var a = new KeyValuePair <int, int>(); var oldCol = 0; var oldRow = 0; var NewPlaces = new KeyValuePair <int, int>(); var newCol = 0; var newRow = 0; for (int oldrow = 0; oldrow < Board.Size; oldrow++) { for (int oldcol = 0; oldcol < Board.Size; oldcol++) { if (!Board.IsEmptySquare(oldcol, oldrow) && !Board.NotYourPiece(Type, oldcol, oldrow)) { var realtype = Board.Squares[oldcol, oldrow]; switch (Board.CanJump(realtype, oldcol, oldrow)) { case 0: break; case 1: CheckingJumps(oldcol, oldrow, realtype, CheckNo.RightUp); break; case 2: CheckingJumps(oldcol, oldrow, realtype, CheckNo.LeftUp); break; case 3: CheckingJumps(oldcol, oldrow, realtype, CheckNo.RightDown); break; case 4: CheckingJumps(oldcol, oldrow, realtype, CheckNo.LeftDown); break; default: break; } //Jumper(oldcol, oldrow); if (Board.IsEmptySquare(oldcol, oldrow)) { return; } } } } for (int oldrow = 0; oldrow < Board.Size; oldrow++) { for (int oldcol = 0; oldcol < Board.Size; oldcol++) { if (!Board.IsEmptySquare(oldcol, oldrow) && !Board.NotYourPiece(Type, oldcol, oldrow)) { var realtype = Board.Squares[oldcol, oldrow]; for (int newrow = 0; newrow < Board.Size; newrow++) { for (int newcol = 0; newcol < Board.Size; newcol++) { if (newrow == oldrow - 1 || newrow == oldrow + 1) { if (Board.IsEmptySquare(newcol, newrow) && Board.IsValidMove(realtype, oldcol, oldrow, newcol, newrow)) { if (CheckingSpaces(oldcol, oldrow, newcol, newrow)) { try { pos.Add(oldcol, oldrow); pos.Add(newcol, newrow); OldPosition = pos.First(); NewPosition = pos.Last(); pos.Clear(); BotPositions.Add(OldPosition, NewPosition); } catch (Exception) { List <KeyValuePair <int, int> > Options = new List <KeyValuePair <int, int> >(); Options.Add(BotPositions[OldPosition]); Options.Add(NewPosition); BotPositions.Remove(OldPosition); BotPositions.Add(OldPosition, Options[rnd.Next(2)]); } //Board.MovePiece(realtype, oldcol, oldrow, newcol, newrow); //return; } } } } } } } } if (BotPositions.Count != 0) { a = RandomValues(BotPositions); oldCol = a.Key; oldRow = a.Value; NewPlaces = BotPositions[a]; newCol = NewPlaces.Key; newRow = NewPlaces.Value; Board.MovePiece(Board.Squares[oldCol, oldRow], oldCol, oldRow, newCol, newRow); BotPositions.Clear(); return; } for (int oldrow = 0; oldrow < Board.Size; oldrow++) { for (int oldcol = 0; oldcol < Board.Size; oldcol++) { if (!Board.IsEmptySquare(oldcol, oldrow) && !Board.NotYourPiece(Type, oldcol, oldrow)) { var realtype = Board.Squares[oldcol, oldrow]; for (int newrow = 0; newrow < Board.Size; newrow++) { for (int newcol = 0; newcol < Board.Size; newcol++) { if (Board.IsEmptySquare(newcol, newrow) && Board.IsValidMove(realtype, oldcol, oldrow, newcol, newrow)) { try { pos.Add(oldcol, oldrow); pos.Add(newcol, newrow); OldPosition = pos.First(); NewPosition = pos.Last(); pos.Clear(); BotPositions.Add(OldPosition, NewPosition); } catch (Exception) { List <KeyValuePair <int, int> > Options = new List <KeyValuePair <int, int> >(); Options.Add(BotPositions[OldPosition]); Options.Add(NewPosition); BotPositions.Remove(OldPosition); BotPositions.Add(OldPosition, Options[rnd.Next(2)]); } //Board.MovePiece(realtype, oldcol, oldrow, newcol, newrow); //return; } } } } } } a = RandomValues(BotPositions); oldCol = a.Key; oldRow = a.Value; NewPlaces = BotPositions[a]; newCol = NewPlaces.Key; newRow = NewPlaces.Value; Board.MovePiece(Board.Squares[oldCol, oldRow], oldCol, oldRow, newCol, newRow); BotPositions.Clear(); return; }
public void SetPosition(int col, int row, SquareValues piecetype) { startCol = col; startRow = row; PieceType = piecetype; }
private void btnOK_Click(object sender, EventArgs e) { cancel = false; name1P = Name1p.Text; name2P1 = Name2P1.Text; name2P2 = Name2P2.Text; nameCG1 = NameCG1.Text; nameCG2 = NameCG2.Text; onlineid = OnlineID.Text; hostid = HostID.Text; port = Convert.ToInt32(PortNo.Text); try { playspeed = Convert.ToInt32(Convert.ToDouble(PlaySpeed.Text) * 1000); } catch (Exception) { playspeed = 800; } highlight = Highlight.Checked; joingame = JoinGame.Checked; hostgame = HostGame.Checked; if (Gamemode.SelectedTab == Tab1P) { gamemode = 0; } if (Gamemode.SelectedTab == Tab2P) { gamemode = 1; } if (Gamemode.SelectedTab == TabCG) { gamemode = 2; } if (Gamemode.SelectedTab == TabOnline) { gamemode = 3; } switch ((string)PlayerPieceType.SelectedItem) { case "Black": PType = SquareValues.Black; BotType = SquareValues.White; break; case "White": PType = SquareValues.White; BotType = SquareValues.Black; break; default: PType = SquareValues.Black; BotType = SquareValues.White; break; } if (gamemode == 3) { switch ((string)HostPiecetype.SelectedItem) { case "Black": PType = SquareValues.Black; break; case "White": PType = SquareValues.White; break; default: PType = SquareValues.Black; break; } } if (Difficulty.SelectedItem != null) { Bot = (BotPlayer)Difficulty.SelectedItem; //This is called casting Bot.Type = BotType; var BeginnerBot = new BotPlayer1(Board, BotType); var b = BeginnerBot.GetType(); //Bot = (BotPlayer)Activator.CreateInstance((Type)Difficulty.SelectedItem,Board,BotType); var c = Bot.GetType(); if (c == b) { beginner = true; } if (c != b) { beginner = false; } } if (Difficulty.SelectedItem == null) { Bot = new BotPlayer3(Board, BotType); } if (CG1Diff.SelectedItem != null) { Bot1 = (BotPlayer)CG1Diff.SelectedItem; //This is called casting Bot1.Type = SquareValues.Black; //Bot1 = (BotPlayer)Activator.CreateInstance((Type)CG1Diff.SelectedItem, Board, SquareValues.Black); } if (CG1Diff.SelectedItem == null) { Bot1 = new BotPlayer3(Board, SquareValues.Black); } if (CG2Diff.SelectedItem != null) { Bot2 = (BotPlayer)CG2Diff.SelectedItem; //This is called casting Bot2.Type = SquareValues.White; //Bot2 = (BotPlayer)Activator.CreateInstance((Type)CG2Diff.SelectedItem, Board, SquareValues.White); } if (CG2Diff.SelectedItem == null) { Bot2 = new BotPlayer3(Board, SquareValues.White); } }
/// <summary> /// Creates a list of possible jumps a selected piece can make /// </summary> /// <param name="type"></param> /// <param name="currentCol"></param> /// <param name="currentRow"></param> /// <returns></returns> public List <int> ListJumps(SquareValues type, int currentCol, int currentRow) { var B = SquareValues.Black; var Bk = SquareValues.BlackKing; var W = SquareValues.White; var Wk = SquareValues.WhiteKing; var E = SquareValues.Empty; var jumplist = new List <int>(); if (type == B || type == Bk) { if (IsInvalidEntry(currentRow - 2) == null) { if (IsInvalidEntry(currentCol + 2) == null) { if (Squares[currentCol + 1, currentRow - 1] == W || Squares[currentCol + 1, currentRow - 1] == Wk) { if (Squares[currentCol + 2, currentRow - 2] == E) { jumplist.Add(1); } } } if (IsInvalidEntry(currentCol - 2) == null) { if (Squares[currentCol - 1, currentRow - 1] == W || Squares[currentCol - 1, currentRow - 1] == Wk) { if (Squares[currentCol - 2, currentRow - 2] == E) { jumplist.Add(2); } } } } //-------------------------------------------------------------------- if (IsInvalidEntry(currentRow + 2) == null && type == Bk) { if (IsInvalidEntry(currentCol + 2) == null) { if (Squares[currentCol + 1, currentRow + 1] == W || Squares[currentCol + 1, currentRow + 1] == Wk) { if (Squares[currentCol + 2, currentRow + 2] == E) { jumplist.Add(3); } } } if (IsInvalidEntry(currentCol - 2) == null) { if (Squares[currentCol - 1, currentRow + 1] == W || Squares[currentCol - 1, currentRow + 1] == Wk) { if (Squares[currentCol - 2, currentRow + 2] == E) { jumplist.Add(4); } } } } //----------------------------------------------------------------------- jumplist.Add(0); } if (type == W || type == Wk) { if (IsInvalidEntry(currentRow + 2) == null) { if (IsInvalidEntry(currentCol + 2) == null) { if (Squares[currentCol + 1, currentRow + 1] == B || Squares[currentCol + 1, currentRow + 1] == Bk) { if (Squares[currentCol + 2, currentRow + 2] == E) { jumplist.Add(3); } } } if (IsInvalidEntry(currentCol - 2) == null) { if (Squares[currentCol - 1, currentRow + 1] == B || Squares[currentCol - 1, currentRow + 1] == Bk) { if (Squares[currentCol - 2, currentRow + 2] == E) { jumplist.Add(4); } } } } //----------------------------------------------------------- if (IsInvalidEntry(currentRow - 2) == null && type == Wk) { if (IsInvalidEntry(currentCol + 2) == null) { if (Squares[currentCol + 1, currentRow - 1] == B || Squares[currentCol + 1, currentRow - 1] == Bk) { if (Squares[currentCol + 2, currentRow - 2] == E) { jumplist.Add(1); } } } if (IsInvalidEntry(currentCol - 2) == null) { if (Squares[currentCol - 1, currentRow - 1] == B || Squares[currentCol - 1, currentRow - 1] == Bk) { if (Squares[currentCol - 2, currentRow - 2] == E) { jumplist.Add(2); } } } } //--------------------------------------------------------------- jumplist.Add(0); } jumplist.Add(0); return(jumplist); }
/// <summary> /// Checks if a jump is safe /// </summary> /// <param name="oldcol"></param> /// <param name="oldrow"></param> /// <param name="type"></param> /// <param name="mode"></param> /// <returns></returns> public bool CheckingJumps(int oldcol, int oldrow, SquareValues type, CheckNo mode) { switch (mode) { case CheckNo.RightUp: try { a = Board.Squares[oldcol + 2, oldrow - 2]; b = Board.Squares[oldcol + 1, oldrow - 1]; Board.Squares[oldcol + 2, oldrow - 2] = type; Board.Squares[oldcol + 1, oldrow - 1] = SquareValues.Empty; if (!Board.CanBeJumped(oldcol + 2, oldrow - 2) || Board.CanJump(type, oldcol + 2, oldrow - 2) != 0) { Board.Squares[oldcol + 2, oldrow - 2] = a; Board.Squares[oldcol + 1, oldrow - 1] = b; return(true); } else { Board.Squares[oldcol + 2, oldrow - 2] = a; Board.Squares[oldcol + 1, oldrow - 1] = b; return(false); } } catch (Exception) { try { Board.Squares[oldcol + 2, oldrow - 2] = a; Board.Squares[oldcol + 1, oldrow - 1] = b; return(false); } catch (Exception) { return(false); } } case CheckNo.LeftUp: try { a = Board.Squares[oldcol - 2, oldrow - 2]; b = Board.Squares[oldcol - 1, oldrow - 1]; Board.Squares[oldcol - 2, oldrow - 2] = type; Board.Squares[oldcol - 1, oldrow - 1] = SquareValues.Empty; if (!Board.CanBeJumped(oldcol - 2, oldrow - 2) || Board.CanJump(type, oldcol - 2, oldrow - 2) != 0) { Board.Squares[oldcol - 2, oldrow - 2] = a; Board.Squares[oldcol - 1, oldrow - 1] = b; return(true); } else { Board.Squares[oldcol - 2, oldrow - 2] = a; Board.Squares[oldcol - 1, oldrow - 1] = b; return(false); } } catch (Exception) { try { Board.Squares[oldcol - 2, oldrow - 2] = a; Board.Squares[oldcol - 1, oldrow - 1] = b; return(false); } catch (Exception) { return(false); } } case CheckNo.RightDown: try { a = Board.Squares[oldcol + 2, oldrow + 2]; b = Board.Squares[oldcol + 1, oldrow + 1]; Board.Squares[oldcol + 2, oldrow + 2] = type; Board.Squares[oldcol + 1, oldrow + 1] = SquareValues.Empty; if (!Board.CanBeJumped(oldcol + 2, oldrow + 2) || Board.CanJump(type, oldcol + 2, oldrow + 2) != 0) { Board.Squares[oldcol + 2, oldrow + 2] = a; Board.Squares[oldcol + 1, oldrow + 1] = b; return(true); } else { Board.Squares[oldcol + 2, oldrow + 2] = a; Board.Squares[oldcol + 1, oldrow + 1] = b; return(false); } } catch (Exception) { try { Board.Squares[oldcol + 2, oldrow + 2] = a; Board.Squares[oldcol + 1, oldrow + 1] = b; return(false); } catch (Exception) { return(false); } } case CheckNo.LeftDown: try { a = Board.Squares[oldcol - 2, oldrow + 2]; b = Board.Squares[oldcol - 1, oldrow + 1]; Board.Squares[oldcol - 2, oldrow + 2] = type; Board.Squares[oldcol - 1, oldrow + 1] = SquareValues.Empty; if (!Board.CanBeJumped(oldcol - 2, oldrow + 2) || Board.CanJump(type, oldcol - 2, oldrow + 2) != 0) { Board.Squares[oldcol - 2, oldrow + 2] = a; Board.Squares[oldcol - 1, oldrow + 1] = b; return(true);; } else { Board.Squares[oldcol - 2, oldrow + 2] = a; Board.Squares[oldcol - 1, oldrow + 1] = b; return(false); } } catch (Exception) { try { Board.Squares[oldcol - 2, oldrow + 2] = a; Board.Squares[oldcol - 1, oldrow + 1] = b; return(false); } catch (Exception) { return(false); } } default: return(false); } }
private static bool Move(GameBoard board, SquareValues type) { string[] ColAlphabet = new string[8] { "A", "B", "C", "D", "E", "F", "G", "H" }; Console.WriteLine("Select the Piece you would like to move"); var Problem = true; string msg = ""; while (Problem) { Console.Write("Put in the column and row of the piece you would like to move: "); string reply = Console.ReadLine(); if (reply.ToUpper() == "EXIT") { Problem = false; return(false); } var OldSquare = reply.ToUpper(); if (OldSquare.Length != 2) { Console.WriteLine(); Console.WriteLine("Incorrect number of values, only two is required"); Console.WriteLine(); continue; } int OldColumn = Array.IndexOf(ColAlphabet, Convert.ToString(OldSquare[0])); try { var a = Convert.ToInt32(Convert.ToString(OldSquare[1])); } catch (Exception) { Console.WriteLine("Incorrect value, Input letter and then number"); Console.WriteLine(); continue; } int OldRow = Convert.ToInt32(Convert.ToString(OldSquare[1])); msg = board.IsInvalidEntry(OldColumn); if (msg != null) { Console.WriteLine(msg); board.PrintBoard(); continue; } msg = board.IsInvalidEntry(OldRow); if (msg != null) { Console.WriteLine(msg); board.PrintBoard(); continue; } Console.WriteLine(); if (board.IsEmptySquare(OldColumn, OldRow)) { Console.WriteLine("This square is empty"); board.PrintBoard(); Console.WriteLine(); } else if (board.NotYourPiece(type, OldColumn, OldRow)) { Console.WriteLine("This is not your piece"); board.PrintBoard(); Console.WriteLine(); } else if (board.NotYourPiece(type, OldColumn, OldRow) == false) { var realtype = board.Squares[OldColumn, OldRow]; Console.Write("Put in the new Column and Row: "); string reply2 = Console.ReadLine(); if (reply2.ToUpper() == "EXIT") { Problem = false; return(false); } var NewSquare = reply2.ToUpper(); if (NewSquare.Length != 2) { Console.WriteLine("Incorrect number of values, only two is required"); continue; } int NewColumn = Array.IndexOf(ColAlphabet, Convert.ToString(NewSquare[0])); try { var a = Convert.ToInt32(Convert.ToString(NewSquare[1])); } catch (Exception) { Console.WriteLine("Incorrect value, Input letter and then number"); Console.WriteLine(); continue; } int NewRow = Convert.ToInt32(Convert.ToString(NewSquare[1])); msg = board.IsInvalidEntry(NewColumn); if (msg != null) { Console.WriteLine(msg); board.PrintBoard(); continue; } msg = board.IsInvalidEntry(NewRow); if (msg != null) { Console.WriteLine(msg); board.PrintBoard(); continue; } if (board.IsValidMove(realtype, OldColumn, OldRow, NewColumn, NewRow)) { board.MovePiece(realtype, OldColumn, OldRow, NewColumn, NewRow); if (board.HasJumped(OldColumn, OldRow, NewColumn, NewRow)) { if (board.IsValidMove(realtype, NewColumn, NewRow, NewColumn + 2, NewRow - 2) || board.IsValidMove(realtype, NewColumn, NewRow, NewColumn - 2, NewRow - 2)) { board.PrintBoard(); Console.WriteLine("Would you like to jump again: Yes/No"); string reply3 = Console.ReadLine(); if (reply3.ToUpper() == "EXIT") { Problem = false; return(false); } string ans = reply3.ToUpper(); char realans = ans[0]; if (realans == 'Y') { Console.WriteLine("Jump Again"); bool MultiJump = true; while (MultiJump) { OldColumn = NewColumn; OldRow = NewRow; NewColumn = Jumping(board, OldColumn, OldRow); if (board.ReadSquare(OldColumn, OldRow) == SquareValues.Empty) { board.PrintBoard(); if (board.CanJump(type, NewColumn, OldRow - 2) != 0) { Console.WriteLine("Would you like to jump again: Yes/No"); string reply4 = Console.ReadLine(); if (reply4.ToUpper() == "EXIT") { Problem = false; return(false); } string ans2 = reply4.ToUpper(); char realans2 = ans2[0]; if (realans2 == 'Y') { NewRow = OldRow - 2; continue; } if (realans2 != 'Y') { MultiJump = false; Problem = false; return(true); } } MultiJump = false; Problem = false; return(true); } if (board.ReadSquare(OldColumn, OldRow) != SquareValues.Empty && MultiJump) { continue; } } return(true); } } if (board.IsValidMove(realtype, NewColumn, NewRow, NewColumn + 2, NewRow + 2) || board.IsValidMove(realtype, NewColumn, NewRow, NewColumn - 2, NewRow + 2)) { board.PrintBoard(); Console.WriteLine("Would you like to jump again: Yes/No"); string reply3 = Console.ReadLine(); if (reply3.ToUpper() == "EXIT") { Problem = false; return(false); } string ans = reply3.ToUpper(); char realans = ans[0]; if (realans == 'Y') { Console.WriteLine("Jump Again"); bool MultiJump = true; while (MultiJump) { OldColumn = NewColumn; OldRow = NewRow; NewColumn = Jumping(board, OldColumn, OldRow); if (board.ReadSquare(OldColumn, OldRow) == SquareValues.Empty) { board.PrintBoard(); if (board.CanJump(type, NewColumn, OldRow + 2) != 0) { Console.WriteLine("Would you like to jump again: Yes/No"); string reply4 = Console.ReadLine(); if (reply4.ToUpper() == "EXIT") { Problem = false; return(false); } string ans2 = reply4.ToUpper(); char realans2 = ans2[0]; if (realans2 == 'Y') { NewRow = OldRow + 2; continue; } if (realans2 != 'Y') { MultiJump = false; Problem = false; return(true); } } MultiJump = false; Problem = false; return(true); } if (board.ReadSquare(OldColumn, OldRow) != SquareValues.Empty && MultiJump) { continue; } } return(true); } } } Problem = false; } else if (!board.IsValidMove(realtype, OldColumn, OldRow, NewColumn, NewRow)) { Console.WriteLine("Not a Valid Move"); board.PrintBoard(); Console.WriteLine(); } } } return(true); }
public Piece(int col = 0, int row = 0, SquareValues piecetype = 0) { SetPosition(col, row, piecetype); }
public BotPlayerTempV(GameBoard board, SquareValues type, int difficulty) { Type = type; Board = board; Difficuty = difficulty; }
public BotPlayer6(GameBoard board, SquareValues type) : base(board, type) { Board = board; Type = type; BotName = "Level 6"; }
/// <summary> /// Jumps over a Piece Whenever it can and can also double jump /// </summary> private void BotPlayer3() { var a = new KeyValuePair <int, int>(); var oldCol = 0; var oldRow = 0; var NewPlaces = new KeyValuePair <int, int>(); var newCol = 0; var newRow = 0; //int jumpNo = 0; for (int oldrow = 0; oldrow < Board.Size; oldrow++) { for (int oldcol = 0; oldcol < Board.Size; oldcol++) { if (!Board.IsEmptySquare(oldcol, oldrow) && !Board.NotYourPiece(Type, oldcol, oldrow)) { Jumper(oldcol, oldrow); if (Board.IsEmptySquare(oldcol, oldrow)) { return; } } } } for (int oldrow = 0; oldrow < Board.Size; oldrow++) { for (int oldcol = 0; oldcol < Board.Size; oldcol++) { if (!Board.IsEmptySquare(oldcol, oldrow) && !Board.NotYourPiece(Type, oldcol, oldrow)) { var realtype = Board.Squares[oldcol, oldrow]; for (int newrow = 0; newrow < Board.Size; newrow++) { for (int newcol = 0; newcol < Board.Size; newcol++) { if (Board.IsEmptySquare(newcol, newrow) && Board.IsValidMove(realtype, oldcol, oldrow, newcol, newrow)) { try { pos.Add(oldcol, oldrow); pos.Add(newcol, newrow); OldPosition = pos.First(); NewPosition = pos.Last(); pos.Clear(); BotPositions.Add(OldPosition, NewPosition); } catch (Exception) { List <KeyValuePair <int, int> > Options = new List <KeyValuePair <int, int> >(); Options.Add(BotPositions[OldPosition]); Options.Add(NewPosition); BotPositions.Remove(OldPosition); BotPositions.Add(OldPosition, Options[rnd.Next(2)]); } //Board.MovePiece(realtype, oldcol, oldrow, newcol, newrow); //return; } } } } } } a = RandomValues(BotPositions); oldCol = a.Key; oldRow = a.Value; NewPlaces = BotPositions[a]; newCol = NewPlaces.Key; newRow = NewPlaces.Value; Board.MovePiece(Board.Squares[oldCol, oldRow], oldCol, oldRow, newCol, newRow); BotPositions.Clear(); return; }