private Move getMove(Piece start, int yEnd, int xEnd, PieceColor player) { List<Piece> removals = new List<Piece>(); List<Piece> additions = new List<Piece>(); if(multiJumpLoc != null) { if(multiJumpLoc.Equals(start.getCoordinates()) && moveIsJump(start.getCoordinates(), yEnd, xEnd)) { } else { throw new WrongMultiJumpPieceException(); } } System.Diagnostics.Debug.WriteLine("start vector is " + start.getCoordinates().ToString()); Vector startLoc = start.getCoordinates(); Vector endLoc = new Vector(yEnd, xEnd); Vector myMove = endLoc.subtract(startLoc); //jump logic goes here if (this.forceJumps && canJumpSomewhere()) { List<Vector> jumps = getDoableJumps(start); bool found = false; foreach (Vector jump in jumps) { if (jump.Equals(myMove)) { found = true; break; } } if (!found) { throw new PlayerMustJumpException(); } } System.Diagnostics.Debug.WriteLine("myMove is " + myMove.ToString()); bool foundValid = false; if(Math.Abs(myMove.getX()) == 1 && Math.Abs(myMove.getY()) == 1) { //move is not a jump System.Diagnostics.Debug.WriteLine("Move is not a jump."); Vector[] moves = getPossibleMoves(start.getColor(), start.getType()); foreach(Vector move in moves) { System.Diagnostics.Debug.WriteLine("testing possible move " + move.ToString()); if (myMove.Equals(move)) { removals.Add(start); additions.Add(givePieceNewLocationKingCheck(start, start.getCoordinates().add(myMove))); foundValid = true; break; } } } else if (Math.Abs(myMove.getX()) == 2 && Math.Abs(myMove.getY()) == 2) { //move is a jump Vector[] moves = getPossibleJumps(start.getColor(), start.getType()); foreach (Vector move in moves) { if (myMove.Equals(move)) { Vector jumpedLoc = start.getCoordinates().add(move.divideVector(2)); Piece jumpedPiece = board.getCellContents(jumpedLoc); if (jumpedPiece == null) { System.Diagnostics.Debug.WriteLine("cannot jump an empty square"); throw new InvalidMoveException(); } if (jumpedPiece.getColor() == getOppositeColor(start.getColor())) { removals.Add(start); removals.Add(jumpedPiece); additions.Add(givePieceNewLocationKingCheck(start, endLoc)); foundValid = true; } else { System.Diagnostics.Debug.WriteLine("cannot jump your own piece"); throw new InvalidMoveException(); } break; } } } else { System.Diagnostics.Debug.WriteLine("vector is wrong length"); throw new InvalidMoveException(); } if (!foundValid) { System.Diagnostics.Debug.WriteLine("Could not find match vector"); throw new InvalidMoveException(); } int myTurnNumber = turnNumber + 1; return new Move(myTurnNumber, removals, additions, player); }
private List<Vector> getDoableJumps(Piece p) { Vector[] jumps = getPossibleJumps(p.getColor(), p.getType()); List<Vector> doable = new List<Vector>(); foreach (Vector jump in jumps) { Vector endLoc = jump.add(p.getCoordinates()); Piece endP; try { endP = board.getCellContents(endLoc); } catch (CellOutOfBoundsException) { continue; } if (endP != null) { continue; } Vector middleLoc = p.getCoordinates().add(jump.divideVector(2)); Piece middleP; try { middleP = board.getCellContents(middleLoc); } catch (CellOutOfBoundsException) { continue; } if (middleP == null) { continue; } if (middleP.getColor() == p.getColor()) { continue; } doable.Add(new Vector(jump)); } return doable; }
private List<Vector> getDoableMoves(Piece p) { Vector[] moves = getPossibleMoves(p.getColor(), p.getType()); List<Vector> doable = new List<Vector>(); foreach (Vector move in moves) { Vector endLoc = move.add(p.getCoordinates()); Piece endP; try { endP = board.getCellContents(endLoc); } catch (CellOutOfBoundsException) { continue; } if (endP != null) { continue; } doable.Add(new Vector(move)); } return doable; }
public void addPieceToCell(Piece piece) { grid[piece.getCoordinates().getY(), piece.getCoordinates().getX()].addPiece(piece); //pieces.Add(piece); }
public void removePiece(Piece p) { if (p.getColor() == PieceColor.BLACK) { this.blackPieces--; } else { this.redPieces--; } board.removePieceFromCell(p); }
public void addPiece(Piece p) { if (p.getColor() == PieceColor.BLACK) { this.blackPieces++; } else { this.redPieces++; } board.addPieceToCell(p); }
public Piece givePieceNewLocationKingCheck(Piece currentP, Vector newLoc) { Piece newP = currentP.newLocation(newLoc); if (newLoc.getY() == 0 && newP.getColor() == PieceColor.BLACK) { newP = newP.newType(PieceType.KING); } else if (newLoc.getY() == board.getHeight() - 1 && newP.getColor() == PieceColor.RED) { newP = newP.newType(PieceType.KING); } return newP; }
public void addPiece(Piece piece) { if(this.filled) { throw new CellAlreadyFilledException(); } else { this.filled = true; } this.piece = piece; }
public Piece removePiece() { if( ! this.filled) { throw new CellEmptyException(); } else { this.filled = false; } Piece temp = this.piece; this.piece = null; return temp; }
public Cell() { this.filled = false; this.piece = null; }
public Piece(Piece copyable) { this.color = copyable.color; this.coordinates = copyable.coordinates; this.type = copyable.type; }
public MoveAttempt(Vector v, Piece p) : this(p.getCoordinates().getY(), p.getCoordinates().getX(), v.getY() + p.getCoordinates().getY(), v.getX() + p.getCoordinates().getX()) { }
public void removePieceFromCell(Piece piece) { grid[piece.getCoordinates().getY(), piece.getCoordinates().getX()].removePiece(); //pieces.Add(piece); }
private void createPieces() { logic = new GameLogic(row_W, row_W, MainPage.FORCE_JUMP); int row = 0, col = 0; for (int k = 0; k < 24; k++) { row = (k / 4) + ((k >= 12) ? 2 : 0); col = 2 * (k % 4) + (row % 2 == 0 ? 0 : 1); Checker c = new Checker(col, row, (k < 12) ? Colors.Red : DarkGrey, (k < 12) ? DarkRed : Colors.Black); Vector vect = new Vector(row, col); Piece piece = new Piece((k < 12) ? PieceColor.RED : PieceColor.BLACK, vect, PieceType.REGULAR); logic.addPiece(piece); spaces[col, row].setChecker(c); mainCanvas.Children.Add(spaces[col, row].getChecker().getEl2()); mainCanvas.Children.Add(spaces[col, row].getChecker().getEl1()); mainCanvas.Children.Add(spaces[col, row].getChecker().getCrown()); } }