private bool moveIsJump(Vector start, int yEnd, int xEnd) { return (Math.Abs(start.getX() - xEnd) == 2 && Math.Abs(start.getY() - yEnd) == 2); }
public Piece getCellContents(Vector v) { return getCellContents(v.getY(), v.getX()); }
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); }
public Move makeMove(int yStart, int xStart, int yEnd, int xEnd) { Piece start = board.getCellContents(yStart, xStart); Piece end = board.getCellContents(yEnd, xEnd); if (start == null) { //there is no piece here throw new CellEmptyException(); } if (end != null) { throw new CellFullException(); } if (start.getColor() != whoseMove()) { throw new PieceWrongColorException(); } PieceType originalPieceType = start.getType(); System.Diagnostics.Debug.WriteLine("makeMove called"); Move myMove = getMove(start, yEnd, xEnd, this.whoseMove()); doMove(myMove); successMoves.Add(new MoveAttempt(yStart, xStart, yEnd, xEnd)); movesMade.Add(myMove); Piece add = myMove.getAdditions()[0]; if (originalPieceType != add.getType()) { lastAdvantage = turnNumber; } if (myMove.getRemovals().Count > 1) { //that means a piece has been taken lastAdvantage = turnNumber; } if (originalPieceType == add.getType() && myMove.getRemovals().Count == 2 && getDoableJumps(add).Count != 0) { this.multiJumpLoc = myMove.getAdditions()[0].getCoordinates(); //don't change turnNumber } else { this.turnNumber++; this.multiJumpLoc = null; } this.moveNumber++; //getOptimizedHeuristic(0, 3, new GameLogic(this)); return myMove; }
public void skipMultiJump() { if (this.forceJumps) { throw new PlayerMustJumpException(); } else { turnNumber++; multiJumpLoc = null; } }
public GameLogic(GameLogic g) { //does a deep copy of GameLogic this.forceJumps = g.forceJumps; this.board = g.getBoardCopy(); this.moveNumber = g.moveNumber; this.blackPieces = g.blackPieces; this.redPieces = g.redPieces; if (multiJumpLoc != null) { this.multiJumpLoc = new Vector(g.multiJumpLoc); } else { this.multiJumpLoc = null; } this.turnNumber = g.turnNumber; }
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 Vector add(Vector move) { return new Vector(this.y + move.y, this.x + move.x); }
public Vector subtract(Vector move) { return new Vector(this.y - move.y, this.x - move.x); }
//copy Constructor: public Vector(Vector copyable) { this.x = copyable.getX(); this.y = copyable.getY(); }
public Piece newLocation(Vector loc) { return new Piece(this.color, loc, this.type); }
public Piece(Piece copyable) { this.color = copyable.color; this.coordinates = copyable.coordinates; this.type = copyable.type; }
public Piece(PieceColor color, Vector coordinates, PieceType type) { this.color = color; this.coordinates = coordinates; this.type = type; }
public MoveAttempt(Vector v, Piece p) : this(p.getCoordinates().getY(), p.getCoordinates().getX(), v.getY() + p.getCoordinates().getY(), v.getX() + p.getCoordinates().getX()) { }