public override bool CanEat(CheckersPiece piece) { if (piece==null) return false; BoardPosition[][] possiblePos = this.PossiblePaths; for(int i =0; i<IncMov.Length; i++) { BoardPosition p = new BoardPosition(piece.X+IncMov[i].X, piece.Y+IncMov[i].Y); if (this.ParentBoard.IsInsideBoard(p.X, p.Y) && this.ParentBoard.IsEmptyCell(p.X, p.Y) && CanMoveTo(p.X, p.Y, possiblePos)) return true; } return false; }
public static bool IsEatMove(CheckersBoard board, BoardPosition[] movePath) { if(board!=null && movePath!=null && movePath.Length>=2) { int x1=movePath[0].X; int y1=movePath[0].Y; //System.Windows.Forms.MessageBox.Show(x1+" "+y1); if(board.IsInsideBoard(x1, y1)) { CheckersPiece piece=board.GetPieceAt(x1, y1) as CheckersPiece; if(piece!=null) { int x2=movePath[1].X; int y2=movePath[1].Y; //System.Windows.Forms.MessageBox.Show(x2+" "+y2); if(board.IsInsideBoard(x2, y2)) { if(Math.Abs(x1-x2)==Math.Abs(y1-y2)) { int incX=Inc(x1, x2); int incY=Inc(y1, y2); //System.Windows.Forms.MessageBox.Show(incX+" "+incY); x1+=incX; y1+=incY; //System.Windows.Forms.MessageBox.Show(x1+" "+y1); while(x1!=x2 || y1!=y2) { CheckersPiece piece2=board.GetPieceAt(x1, y1) as CheckersPiece; if(piece2!=null) { //System.Windows.Forms.MessageBox.Show(x1+" "+y1); //System.Windows.Forms.MessageBox.Show(piece.Color+" - "+piece2.Color); return piece.Color!=piece2.Color; } x1+=incX; y1+=incY; //System.Windows.Forms.MessageBox.Show(x1+" "+y1); } } return false; } else throw new ArgumentException(); } else throw new ArgumentException(); } else throw new ArgumentException(); } else throw new ArgumentException(); }
protected virtual void ConfigurePath(ArrayList listToFill, BoardPosition p) { if (listToFill != null) { for (int i = 0; i < listToFill.Count; i++) { ArrayList al = listToFill[i] as ArrayList; if (al != null) al.Insert(0, p); } } }
//public override bool CanMakeMove(CheckersMove move) //{ // BoardPosition[][] posiblePositions = this.PossiblePaths; // foreach (BoardPosition[] path in posiblePositions) // { // if (path!=null && IsIncluded(move.MovePath,path)) return true; // } // return false; //} protected override ArrayList MovementInDirection(Point increment ,ref bool outOfBoard) { var path = new ArrayList(); int top = Math.Min((Math.Sign(increment.X)<0)?this.X:(this.ParentBoard.Width-this.X) , (Math.Sign(increment.Y)<0)?this.Y:(this.ParentBoard.Height-this.Y)); bool foundPiece = false; BoardPosition pos = null; for(int i=1; i<=top; i++) { pos = new BoardPosition(this.X+i*increment.X,this.Y+i*increment.Y); if (this.ParentBoard.IsInsideBoard(pos.X,pos.Y)) { if(!this.ParentBoard.IsEmptyCell(pos.X,pos.Y)) { foundPiece = true; break; } path.Add(pos); } } outOfBoard = !foundPiece; return path; }
public bool IsMoveAvailable(BoardPosition currentPosition, MoveDirection intendedMove) { return(isPositionOnTheBoard(BoardUtil.GetNewBoardPosition(currentPosition, intendedMove))); }
private void AddMoveToPath(BoardPosition newpos) { var p = new BoardPosition(newpos.X, newpos.Y); var newPath = new List<BoardPosition>(_currentPath); newPath.Add(p); foreach (BoardPosition pos in _currentPath) _boardDrawer.Selected[pos.X, pos.Y] = false; do { newPath.RemoveAt(newPath.Count - 1); newPath.Add(p); if (Check(newPath)) { _currentPath = newPath; _moves.Add(newpos); _boardDrawer.Selected[newpos.X, newpos.Y] = true; break; } newPath.RemoveAt(newPath.Count - 1); } while (newPath.Count > 0); foreach (BoardPosition pos in _currentPath) _boardDrawer.Selected[pos.X, pos.Y] = true; }
private void MakeMove() { if (_moves.Count <= 1) ResetMove(); else { var pos = new BoardPosition[_moves.Count]; _moves.CopyTo(0, pos, 0, _moves.Count); bool eatMove = CheckersMove.IsEatMove(this.MyBoard, pos); var move = new CheckersMove(pos, eatMove); _lastMoveOk = true; ((HumanCheckersPlayer)CurrentPlayer).MakeAMove(move); if (_lastMoveOk) _lastMove = move; else _lastMove = null; CheckStatus(); ResetMove(); } }
static string ShowPath(BoardPosition[] dest) { string mov = ""; foreach (BoardPosition p in dest) mov += "[" + p.X + "," + p.Y + "],"; return mov; }
protected override ArrayList MovementInDirection(Point increment, ref bool outOfBoard) { var path = new ArrayList(); BoardPosition pos = null; pos = new BoardPosition(this.X + increment.X, this.Y + increment.Y); if (this.ParentBoard.IsInsideBoard(pos.X, pos.Y)) { if (!this.ParentBoard.IsEmptyCell(pos.X, pos.Y)) { outOfBoard = false; return path; } path.Add(pos); } outOfBoard = true; return path; }
private void Restriction1(ArrayList listToFill, ArrayList path, BoardPosition p, ref int countMovement) { ArrayList list = null; for (int i = 0; i < path.Count; i++) { list = new ArrayList(); list.Add(p); list.Add(path[i]); listToFill.Add(list); } countMovement++; }
public CheckersMove(BoardPosition[] positions, bool eatMove) { this.MovePath = positions; this.EatMove=eatMove; }
public void ExplodeMine(BoardPosition currentPosition) { _board[currentPosition.X, currentPosition.Y].Explode(); }
public bool IsMinePresent(BoardPosition currentPosition) { return(_board[currentPosition.X, currentPosition.Y].IsMine); }
public bool IsGoalReached(BoardPosition currentPosition) { return(currentPosition.Y == _numberOfRows - 1); }
/***************************************/ protected static bool IsIncluded(BoardPosition[] source, BoardPosition[] dest) { if (source==null || source.Length==0 || dest==null) return false; int startPos = 0; bool found = false; while (!found && startPos<dest.Length) { if (BoardPosition.Equals(dest[startPos++],source[0])) found = true; } //No encontre la primera coincidencia o los BoardPosition restantes son menos de los que estoy buscando if (!found || ((dest.Length-startPos)<(source.Length-1))) return false; if (source.Length==1) return true; int i=1; while(i<source.Length && startPos<dest.Length) if (BoardPosition.Equals(source[i],dest[startPos++])) i++; return (i>=source.Length); }
protected ArrayList PossibleMovements(bool eat) { int countMovement = 0; var possibleMovements = new ArrayList(); for (int i = 0; i < IncMov.Length; i++) { bool outOfBoard = false; ArrayList path = MovementInDirection(IncMov[i], ref outOfBoard); if (!eat && path != null && path.Count > 0) this.Restriction1(possibleMovements, path, new BoardPosition(this.X, this.Y), ref countMovement); if (outOfBoard) continue; if (!outOfBoard && path != null) { BoardPosition f = (path.Count == 0) ? (new BoardPosition(this.X + IncMov[i].X, this.Y + IncMov[i].Y)) : (new BoardPosition(((BoardPosition)path[path.Count - 1]).X + IncMov[i].X, ((BoardPosition)path[path.Count - 1]).Y + IncMov[i].Y)); if (!this.ParentBoard.IsInsideBoard(f.X, f.Y)) continue; CheckersPiece piece = this.ParentBoard.GetPieceAt(f.X, f.Y) as CheckersPiece; if (piece.Color != this.Color) { BoardPosition c = new BoardPosition(f.X + IncMov[i].X, f.Y + IncMov[i].Y); if ((this.ParentBoard.IsInsideBoard(c.X, c.Y) && this.ParentBoard.IsEmptyCell(c.X, c.Y))) { this.ParentBoard.RemovePiece(f); BoardPosition myPosition = new BoardPosition(this.X, this.Y); this.ParentBoard.MovePieceTo(this, c.X, c.Y); ArrayList tmp = this.PossibleMovements(true); this.ConfigurePath(tmp, myPosition); foreach (ArrayList al in tmp) possibleMovements.Add(al); this.ParentBoard.MovePieceTo(this, myPosition.X, myPosition.Y); this.ParentBoard.PutPieceAt(f.X, f.Y, piece); countMovement = (tmp.Count > 0) ? countMovement + 1 : countMovement; continue; } } } } if (countMovement == 0) possibleMovements.Add(new ArrayList(new BoardPosition[] { new BoardPosition(this.X, this.Y) })); return possibleMovements; }
protected void EatMoves(int x, int y, List<CheckersMove> moves) { bool wayFound = false; aux.Add(new BoardPosition(x, y)); for (int i = 0; i < XEat.Length; i++) { int newX = x + XEat[i]; int newY = y + YEat[i]; if (this.ParentBoard.IsInsideBoard(newX, newY)) { CheckersPiece piece = this.ParentBoard.GetPieceAt(newX, newY) as CheckersPiece; if (piece != null && piece.Color != this.Color) { int newX2 = newX + XEat[i]; int newY2 = newY + YEat[i]; if (aux.Count > 1) { if (aux[aux.Count - 2].X == newX2 && aux[aux.Count - 2].Y == newY2) continue; } if (this.ParentBoard.IsInsideBoard(newX2, newY2) && this.ParentBoard.GetPieceAt(newX2, newY2) == null) { wayFound = true; this.ParentBoard.RemovePiece(this); this.ParentBoard.PutPieceAt(newX2, newY2, this); this.ParentBoard.RemovePiece(piece); EatMoves(newX2, newY2, moves); this.ParentBoard.PutPieceAt(newX, newY, piece); this.ParentBoard.RemovePiece(this); this.ParentBoard.PutPieceAt(x, y, this); } } } } if (!wayFound && (this.X != aux[0].X || this.Y != aux[0].Y)) { BoardPosition[] arr = new BoardPosition[aux.Count]; for (int j = 0; j < aux.Count; j++) arr[j] = aux[j]; moves.Add(new CheckersMove(arr, true)); } aux.RemoveAt(aux.Count - 1); }
protected virtual bool CanMoveTo(int newx, int newy, BoardPosition[][] posiblePositions) { if (!this.ParentBoard.IsInsideBoard(newx, newy)) return false; foreach (BoardPosition[] path in posiblePositions) { if (path!=null) { for(int i=0; i<path.Length; i++) if (path[i].X==newx && path[i].Y==newy) return true; } } return false; }
static string ShowBoardPosition(BoardPosition[][] b) { string mov = "" + b.Length + "\n"; foreach (BoardPosition[] p in b) mov += ShowPath(p) + "\n"; return mov; }
protected virtual bool CanMoveTo(int newx, int newy, bool eat) { for(int i=0; i<IncMov.Length; i++) { bool found = false; BoardPosition f = FoundInDirecction(newx, newy, IncMov[i],ref found); if (found) return true; f = (f!=null) ? (new BoardPosition(this.X+IncMov[i].X, this.Y+IncMov[i].Y)) : (new BoardPosition(f.X+IncMov[i].X, f.Y+IncMov[i].Y)); if (!this.ParentBoard.IsInsideBoard(f.X,f.Y)) continue; CheckersPiece piece = this.ParentBoard.GetPieceAt(f.X, f.Y) as CheckersPiece; if (piece.Color!=this.Color) { BoardPosition c = new BoardPosition(f.X+IncMov[i].X, f.Y+IncMov[i].Y); if ((this.ParentBoard.IsInsideBoard(c.X, c.Y) && this.ParentBoard.IsEmptyCell(c.X, c.Y))) { this.ParentBoard.RemovePiece(f); BoardPosition myPosition = new BoardPosition(this.X, this.Y); this.ParentBoard.MovePieceTo(this, c.X,c.Y); if (this.CanMoveTo(newx, newy ,true))return true; this.ParentBoard.MovePieceTo(this, myPosition.X, myPosition.Y); this.ParentBoard.PutPieceAt(f.X, f.Y,piece); } } } return false; }
private void BoardPicturebox_MouseUp(object sender, MouseEventArgs e) { if (!(CurrentPlayer is HumanCheckersPlayer)) return; //else ... int xCell = (e.X) / CellWidth; int yCell = (e.Y) / CellHeight; bool xeven = (xCell % 2 == 0); bool yeven = (yCell % 2 == 0); if (!((xeven && yeven) || (!xeven && !yeven))) return; BoardPosition newpos = new BoardPosition(xCell, yCell); CheckersPiece piece = (CheckersPiece)MyBoard.GetPieceAt(newpos); //if the selected cell contains path piece of diferent color then return if ((piece != null) && piece.Color != CurrentPlayer.Color) return; //if the user is selected another piece of its color then clear the selection and select this piece if (piece != null && piece.Color == CurrentPlayer.Color && _moves.Count > 0 && !_moves[0].Equals(newpos)) { NewMove(piece,newpos); } else { //not double cell consecuttively if ((_moves.Count >= 1) && _moves[_moves.Count - 1].Equals(newpos)) return; if (_currentPath == null || _currentPath.Count == 0) { if (piece!=null) NewMove(piece, newpos); } else AddMoveToPath(newpos); } RepaintBoard(); //*Rayhand GC.Collect(); //*End }
protected void EatMoves(int x, int y, IList<CheckersMove> moves) { bool foodFound=false; aux.Add(new BoardPosition(x, y)); for(int i=0; i<4; i++) { //Initializing... int newX=x+Inc[i].X; int newY=y+Inc[i].Y; //Looking for piece... while(this.ParentBoard.IsInsideBoard(newX, newY) && this.ParentBoard.GetPieceAt(newX, newY)==null) { newX+=Inc[i].X; newY+=Inc[i].Y; } if (this.ParentBoard.IsInsideBoard(newX, newY) ) { CheckersPiece piece = this.ParentBoard.GetPieceAt(newX, newY) as CheckersPiece; if (piece.Color != this.Color) { //piece found... int newX2 = newX + Inc[i].X; int newY2 = newY + Inc[i].Y; int dx = Math.Abs(newX2 - x); int dy = Math.Abs(newY2 - y); while (this.ParentBoard.IsInsideBoard(newX2, newY2) && this.ParentBoard.GetPieceAt(newX2, newY2) == null) { foodFound = true; this.ParentBoard.RemovePiece(this); this.ParentBoard.PutPieceAt(newX2, newY2, this); this.ParentBoard.RemovePiece(piece); EatMoves(newX2, newY2, moves); this.ParentBoard.PutPieceAt(newX, newY, piece); this.ParentBoard.RemovePiece(this); this.ParentBoard.PutPieceAt(x, y, this); newX2 += Inc[i].X; newY2 += Inc[i].Y; } } } } if(!foodFound && (orgX!=x || orgY!=y)) { BoardPosition[] arr=new BoardPosition[aux.Count]; for(int z=0; z<aux.Count; z++) arr[z]=aux[z] as BoardPosition; moves.Add(new CheckersMove(arr, true)); } aux.RemoveAt(aux.Count-1); }
private void NewMove(CheckersPiece piece, BoardPosition position) { _moves.Clear(); _moves.Add(position); _posiblemoves = piece.PossibleMoves; _currentPath = new List<BoardPosition>(); _currentPath.Add(new BoardPosition(piece.X, piece.Y)); _boardDrawer.ClearSelection(); _boardDrawer.Selected[piece.X, piece.Y] = true; }
protected BoardPosition FoundInDirecction(int X, int Y, System.Drawing.Point increment, ref bool found) { int top = Math.Min((Math.Sign(increment.X)<0)?this.X:(this.ParentBoard.Width-this.X) , (Math.Sign(increment.Y)<0)?this.Y:(this.ParentBoard.Height-this.Y)); BoardPosition pos = null; for(int i=1; i<=top; i++) { pos = new BoardPosition(this.X+i*increment.X,this.Y+i*increment.Y); if (this.ParentBoard.IsInsideBoard(pos.X,pos.Y) && this.ParentBoard.IsEmptyCell(pos.X, pos.Y)) { if (pos.X==X && pos.Y==Y) { found =true; return null; } } else { found = false; return (this.ParentBoard.IsInsideBoard(pos.X,pos.Y)) ? pos : null; } } return null; }
public static bool Equals(BoardPosition bp1, BoardPosition bp2) { if (bp1 != null) return bp1.Equals(bp2); if (bp2 != null) return bp2.Equals(bp2); return (bp1 == bp2); }
private bool isPositionOnTheBoard(BoardPosition position) { return((position.X >= 0 && position.X < _numberOfColumns) && (position.Y >= 0 && position.Y < _numberOfRows)); }