public BlackPc(Player pl, Vector3 pos) : base(pl, pos) { color = new Vector3(0.2f); possMoves = new Vector[] { new Vector(-1, 1), new Vector(1, 1) }; possAttacks = new Vector[] { new Vector(-2, 2), new Vector(2, 2) }; }
public WhitePc(Player pl, Vector3 pos) : base(pl, pos) { color = new Vector3(1, 1, 0.9f); possMoves = new Vector[] { new Vector(-1, -1), new Vector(1, -1) }; possAttacks = new Vector[] { new Vector(-2, -2), new Vector(2, -2) }; }
public King(Player pl, Vector3 pos) : base(pl, pos) { possAttacks = new Vector[24]; possMoves = new Vector[28]; int m = 0; int i = 7; while (m < 28 && i > 0) { if (i != 1 && m < 24) { possAttacks[m] = new Vector(-i, -i); possAttacks[m + 1] = new Vector(i, i); possAttacks[m + 2] = new Vector(-i, i); possAttacks[m + 3] = new Vector(i, -i); } possMoves[m] = new Vector(-i, -i); possMoves[m + 1] = new Vector(i, i); possMoves[m + 2] = new Vector(-i, i); possMoves[m + 3] = new Vector(i, -i); m = m + 4; i--; } }
public Player(PlayerSettings p, int i) { if (p.name == null) name = "Player " + (i + 1); else name = p.name; nameTag = new Text3D(name, Font3D.getFont("arial"), null); if (p.avatar == null) avatar = Texture2D.FromStream(Program.game.GraphicsDevice, System.IO.File.OpenRead("default.png")); else avatar = p.avatar; cam = new Camera(new Vector3(0.7f, i == 1 ? 0 : MathHelper.Pi, 0), 80, Vector3.Zero, Vector3.Up); selection = new Vector(0, i == 1 ? 0 : 7); index = i; }
public void applyAction(Vector src, Vector dest) { if(isPossAttack(src, dest)) { attackPc(src, dest); if (isMultipleAttack(dest)) attackAgain = dest; else attackAgain = null; } else if (isPossMove(src, dest)) { fRule(); movePc(src, dest); } else { GamePlay.currPl.dest = null; throw new GameplayException("Invalid Action !"); } }
public bool validateMove(Piece srcPc, Vector src, Vector possMove) { return isKingClear(possMove.stepBack(), src); }
public void validateSrcPc(Vector v) { Piece temp = tiles[v.x][v.y].p; if(temp == null) throw new GameplayException("The selected Tile is Empty !"); if(!temp.isHis(GamePlay.currPl)) throw new GameplayException("It's not your Piece !"); }
public bool validateAttack(Piece srcPc, Vector src, Vector possAttack) { Vector tmp = findEnemy(possAttack, src); //Finds the enemy if(tmp != null) //If the enemy exists. return isKingClear(tmp.stepBack(), src); //Check if the path between enemy and king is clear. return false; //Else return false cause there is no enemy ! }
public void validateDestPc(Vector v) { if (tiles[v.x][v.y].p != null) throw new GameplayException("The destination Tile is Occupied !"); }
//Rule number (f) in the .pdf :) public void fRule() { Tile[] delete = new Tile[12]; //12 penalties at maximum. int count = 0; for(int i = 0; i < 8; i++) for(int j = 0; j < 8; j++) { Piece tmpPc = tiles[i][j].p; if(tmpPc != null && tmpPc.isHis(GamePlay.currPl)) { Vector src = new Vector(i, j); for(int k = 0; k < tmpPc.possAttacks.Length; k++) { Vector dest = Vector.add(src, tmpPc.possAttacks[k]); if(dest.x>=0 && dest.x<=7 && dest.y>=0 && dest.y<=7 && isEmpty(dest) && validateAttack(tmpPc, src, tmpPc.possAttacks[k])) { delete[count] = tiles[i][j]; //add the tile containing the coward piece in an array. count++; //increment the number of cowards :D break; //to avoid penalizing the same piece twice. } } } } //nullify the coward pieces ! for(int i = 0; i < count; i++) delete[i].p = null; if (count != 0) { fruled = true; //a flag that should be true if at least one piece was penalized. attackAgain = null; } }
public void movePc(Vector src, Vector dest) { tiles[dest.x][dest.y].p = tiles[src.x][src.y].p; tiles[src.x][src.y].p = null; if (tiles[dest.x][dest.y].p != null) tiles[dest.x][dest.y].p.setTarget(dest); makeKing(dest); }
public virtual void getInput() { int delta = index == 0 ? 1 : -1; if (Input.isPressed(Keys.Up)) selection.y -= delta; if (Input.isPressed(Keys.Down)) selection.y += delta; if (Input.isPressed(Keys.Right)) selection.x += delta; if (Input.isPressed(Keys.Left)) selection.x -= delta; if (selection.x > 7) selection.x = 7; if (selection.x < 0) selection.x = 0; if (selection.y > 7) selection.y = 7; if (selection.y < 0) selection.y = 0; if (Input.isPressed(Keys.Enter)) { if (src == null) { GamePlay.board.validateSrcPc(selection); src = selection.clone(); } else if (Vector.isEqual(src, selection)) src = null; else { GamePlay.board.validateDestPc(selection); dest = selection.clone(); } } }
public static Vector sub(Vector l, Vector m) { return new Vector(l.x - m.x, l.y - m.y); }
public bool isMultipleAttack(Vector src) { Piece srcPc = getPc(src); for (int i = 0; i < srcPc.possAttacks.Length; i++) { Vector dest = Vector.add(src, srcPc.possAttacks[i]); if (dest.x >= 0 && dest.x <= 7 && dest.y >= 0 && dest.y <= 7 && isEmpty(dest) && validateAttack(srcPc, src, srcPc.possAttacks[i])) return true; } return false; }
public bool isPossAttack(Vector src, Vector dest) { //do checking for possible attacks here ... Piece srcPc = tiles[src.x][src.y].p; for(int i = 0; i < srcPc.possAttacks.Length; i++) if(Vector.isEqual(Vector.add(src, srcPc.possAttacks[i]), dest)) return validateAttack(srcPc, src, srcPc.possAttacks[i]); return false; }
//Checks if the path for the King is Clear. public bool isKingClear(Vector curr, Vector src) { if (curr.x == 0 && curr.y == 0) //Reached the attacking King ? return true; Vector add = Vector.add(curr, src); if (!isEmpty(add)) //Is something blocking the way ? return false; return true && isKingClear(curr.stepBack(), src); }
public bool isEmpty(Vector v) { return tiles[v.x][v.y].p == null; }
public Piece getPc(Vector v) { return tiles[v.x][v.y].p; }
public void attackPc(Vector src, Vector dest) { Vector enemyPos = Vector.add(findEnemy(Vector.sub(dest, src), src), src); tiles[enemyPos.x][enemyPos.y].killPc = true; movePc(src, dest); makeKing(dest); }
public static bool isEqual(Vector l, Vector m) { return (l.x == m.x) && (l.y == m.y); }
public bool isPossMove(Vector src, Vector dest) { Piece srcPc = tiles[src.x][src.y].p; for(int i = 0; i < srcPc.possMoves.Length; i++) if(Vector.isEqual(Vector.add(src, srcPc.possMoves[i]), dest)) return validateMove(srcPc, src, srcPc.possMoves[i]); return false; }
public Vector findEnemy(Vector curr, Vector src) { if (curr.x == 0 && curr.y == 0) //Reached the attacking King ? return null; Vector add = Vector.add(curr, src); //it is the actual current position if (!isEmpty(add)) //if you found a piece if (!getPc(add).isHis(GamePlay.currPl)) //Found the enemy ? return curr; else return null; //Your Piece is blocking the way between the dest and enemy (if it existed) return findEnemy(curr.stepBack(), src); }
public void setTarget(Vector v) { moving = true; targetPos = new Vector3(v.x * 5, 0, v.y * 5); halfWay = (targetPos - currentPos) / 2f + currentPos; }
public bool isStuck() { for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) { Piece tmpPc = tiles[i][j].p; if (tmpPc != null && tmpPc.isHis(GamePlay.currPl)) { Vector src = new Vector(i, j); for (int k = 0; k < tmpPc.possMoves.Length; k++) { Vector dest = Vector.add(src, tmpPc.possMoves[k]); if (dest.x >= 0 && dest.x <= 7 && dest.y >= 0 && dest.y <= 7 && tiles[dest.x][dest.y].p == null && validateMove(tmpPc, src, tmpPc.possMoves[k])) return false; } for (int k = 0; k < tmpPc.possAttacks.Length; k++) { Vector dest = Vector.add(src, tmpPc.possAttacks[k]); if (dest.x >= 0 && dest.x <= 7 && dest.y >= 0 && dest.y <= 7 && tiles[dest.x][dest.y].p == null && validateAttack(tmpPc, src, tmpPc.possAttacks[k])) return false; } } } return true; }
//Check for promotion to King and apply it public void makeKing(Vector v) { if (!isEmpty(v)) tiles[v.x][v.y].p = getPc(v).promote(v.y); }
public static Vector add(Vector l, Vector m) { return new Vector(l.x + m.x, l.y + m.y); }