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 List <MoveAttempt> getAllDoableMoveAttempts() { PieceColor jumperColor = this.whoseMove(); List <MoveAttempt> allMoves = new List <MoveAttempt>(); if (multiJumpLoc != null) { return(allMoves); } for (int y = 0; y < board.getHeight(); y++) { for (int x = 0; x < board.getHeight(); x++) { Piece p = board.getCellContents(y, x); if (p == null || (p.getColor() != jumperColor)) { continue; } List <Vector> doable = getDoableMoves(p); if (doable != null && doable.Count != 0) { foreach (Vector v in doable) { allMoves.Add(new MoveAttempt(y, x, y + v.getY(), x + v.getX())); } } } } System.Diagnostics.Debug.WriteLine("all moves returning" + ((allMoves.Count > 0) ? "true" : "false")); return(allMoves); }
public MoveAttempt getAnyDoableMoveAttempt() { if (multiJumpLoc != null) { return(null); } PieceColor jumperColor = this.whoseMove(); for (int y = 0; y < board.getHeight(); y++) { for (int x = 0; x < board.getHeight(); x++) { Piece p = board.getCellContents(y, x); if (p == null) { continue; } if (p.getColor() != jumperColor) { continue; } List <Vector> vectors = getDoableMoves(p); if (vectors.Count > 0) { System.Diagnostics.Debug.WriteLine("can jump somewhere returning moveattempt for" + y + " " + x); return(new MoveAttempt(y, x, y + vectors[0].getY(), x + vectors[0].getX())); } } } System.Diagnostics.Debug.WriteLine("can move somewhere returning false"); return(null); }
public string getBoardText() { string t = ""; for (int y = 0; y < board.getHeight(); y++) { for (int x = 0; x < board.getWidth(); x++) { Piece p = board.getCellContents(y, x); if (p == null) { t += "n"; } else { if (p.getColor() == PieceColor.BLACK) { t += "b"; } else { t += "r"; } } } t += "\n"; } return(t); }
public bool isSelectable(int y, int x) { System.Diagnostics.Debug.WriteLine("y is " + y + " and x is " + x); Piece p = board.getCellContents(y, x); System.Diagnostics.Debug.WriteLine("piece is " + p.ToString()); return(p.getColor() == this.whoseMove()); }
public void addPiece(Piece p) { if (p.getColor() == PieceColor.BLACK) { this.blackPieces++; } else { this.redPieces++; } board.addPieceToCell(p); }
public void removePiece(Piece p) { if (p.getColor() == PieceColor.BLACK) { this.blackPieces--; } else { this.redPieces--; } board.removePieceFromCell(p); }
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); }
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); }
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> 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; }
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; }
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); }
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)); }