void spawnAll() { BoardPieces = new PieceIdentity[8, 8]; PromotionPieces = new PieceIdentity[2, 4]; spawnAt(0, givePos(4, 0)); spawnAt(1, givePos(3, 0)); spawnAt(2, givePos(2, 0)); spawnAt(2, givePos(5, 0)); spawnAt(3, givePos(1, 0)); spawnAt(3, givePos(6, 0)); spawnAt(4, givePos(0, 0)); spawnAt(4, givePos(7, 0)); spawnAt(6, givePos(4, 7)); spawnAt(7, givePos(3, 7)); spawnAt(8, givePos(2, 7)); spawnAt(8, givePos(5, 7)); spawnAt(9, givePos(1, 7)); spawnAt(9, givePos(6, 7)); spawnAt(10, givePos(0, 7)); spawnAt(10, givePos(7, 7)); for (int i = 0; i < 8; i++) { spawnAt(5, givePos(i, 1)); spawnAt(11, givePos(i, 6)); } spawnAt(1, giveAirPos(20, 7), 0);//To display White Promotion spawnAt(2, giveAirPos(21, 7), 0); spawnAt(3, giveAirPos(23, 7), 0); spawnAt(4, giveAirPos(24, 7), 0); spawnAt(7, giveAirPos(-20, 0), 0);//To display Black Promotion spawnAt(8, giveAirPos(-21, 0), 0); spawnAt(9, giveAirPos(-23, 0), 0); spawnAt(10, giveAirPos(-24, 0), 0); //Stalemate pieces... /* * spawnAt(0, givePos(4, 0)); * spawnAt(10, givePos(7, 0)); * //spawnAt(5, givePos(5, 3)); * spawnAt(10, givePos(7, 3)); * spawnAt(10, givePos(3, 4)); * spawnAt(10, givePos(5, 4));*/ //Promotion test pieces /* * spawnAt(5, givePos(3, 6)); * spawnAt(5, givePos(4, 6)); * spawnAt(11, givePos(3, 1)); * spawnAt(11, givePos(4, 1));*/ //Uncomment any one of above and comment rest of it to show a demo. }
void isCheck() { PieceIdentity KingPiece = BoardPieces[PieceIdentity.getKing(BoardPieces)[0, 0], PieceIdentity.getKing(BoardPieces)[1, 0]]; if (KingPiece.isTileUnderDanger(BoardPieces, PieceIdentity.getKing(BoardPieces)[0, 0], PieceIdentity.getKing(BoardPieces)[1, 0], isWhiteTurn)) { string player = (isWhiteTurn) ? "White" : "Black"; string opponent = (!isWhiteTurn) ? "White" : "Black"; checkBy.text = "You are under Check by " + opponent + " Team."; } else { checkBy.text = ""; } }
public PieceIdentity[,] getBoardAfter(PieceIdentity[,] currBoard, int x2, int y2)//overloaded function for current piece { PieceIdentity piece = currBoard[currX, currY]; PieceIdentity[,] nbd = new PieceIdentity[8, 8];//create a copy, not assign currBoard to nbd for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { nbd[i, j] = currBoard[i, j]; } } nbd[currX, currY] = null; nbd[x2, y2] = piece; return(nbd); }
public PieceIdentity[,] getBoardAfter(PieceIdentity[,] currBoard, int x1, int y1, int x2, int y2)//general function { PieceIdentity piece = currBoard[x1, y1]; PieceIdentity[,] nbd = new PieceIdentity[8, 8];//create a copy, not assign currBoard to nbd for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { nbd[i, j] = currBoard[i, j]; } } nbd[x1, y1] = null; nbd[x2, y2] = piece; return(nbd); }
void isCmOrSm() { bool noMoves = true; for (int i = 0; i < 8; i++)//checkMate or staleMate { for (int j = 0; j < 8; j++) { if (BoardPieces[i, j] == null) { continue; } if (BoardPieces[i, j].isWhitePiece != isWhiteTurn) { continue; } int[,] moves = BoardPieces[i, j].getCheckArray(BoardPieces[i, j].isAllowed(BoardPieces, true));//check moves if (BoardPieces[i, j].movePossible(moves)) { Debug.Log("noMoves false for ==" + i + "," + j); noMoves = false; break; } } if (!noMoves) { break; } } PieceIdentity KingPiece = BoardPieces[PieceIdentity.getKing(BoardPieces)[0, 0], PieceIdentity.getKing(BoardPieces)[1, 0]]; Debug.Log("noMoves==" + noMoves); if (noMoves) { if (KingPiece.isTileUnderDanger(BoardPieces, KingPiece.currX, KingPiece.currY, isWhiteTurn))//checkmate { win = (isWhiteTurn) ? -1 : 1; } else//stalemate { win = 2; } } }
// render moves in a grid // | ○ | // ---+---+--- // ─ | ☖| ─ // ---+---+--- // ○ | | ○ public void SetPiece(TaikyokuShogi game, PieceIdentity id) { var moves = game.GetMovement(id); headerText.Text = $"{Pieces.Name(id)}\n{Pieces.Kanji(id)} ({Pieces.Romanji(id)})"; ////// // // figure out the grid size // int maxMoves = 1; for (int i = 0; i < moves.StepRange.Length; ++i) { maxMoves = Math.Max(maxMoves, moves.StepRange[i] == Movement.Unlimited ? 2 : moves.StepRange[i]); } for (int i = 0; i < moves.JumpRange.Length; ++i) { var maxJump = moves.JumpRange[i].JumpDistances?.Max(); var jumpRange = maxJump == Movement.Unlimited ? 2 : maxJump + 1; jumpRange += moves.JumpRange[i].RangeAfter == Movement.Unlimited ? 1 : moves.JumpRange[i].RangeAfter; maxMoves = Math.Max(maxMoves, jumpRange ?? 0); } for (int i = 0; i < moves.RangeCapture.Count; ++i) { maxMoves = Math.Max(maxMoves, moves.RangeCapture[i] ? 2 : 0); } maxMoves = Math.Max(maxMoves, moves.LionMove ? 3 : 0); maxMoves = Math.Max(maxMoves, moves.HookMove switch { HookType.Orthogonal => 2, HookType.Diagonal => 2, HookType.ForwardDiagnal => 3, _ => 0 });
void selectPiece() { Debug.Log("outside2=" + BoardPieces[mouseOverX, mouseOverY]); PieceIdentity KingPiece = BoardPieces[PieceIdentity.getKing(BoardPieces)[0, 0], PieceIdentity.getKing(BoardPieces)[1, 0]]; Debug.Log("outside3=" + BoardPieces[mouseOverX, mouseOverY]); if (BoardPieces[mouseOverX, mouseOverY] != null) { if (isWhiteTurn == BoardPieces[mouseOverX, mouseOverY].isWhitePiece) { int[,] moves = BoardPieces[mouseOverX, mouseOverY].isAllowed(BoardPieces, true); moves = BoardPieces[mouseOverX, mouseOverY].getCheckArray(moves); if (BoardPieces[mouseOverX, mouseOverY].movePossible(moves)) { validMoves = moves; BoardSelection.BoardSelectionObject.createColorSelection(validMoves, mouseOverX, mouseOverY); currentPiece = BoardPieces[mouseOverX, mouseOverY]; } } } }
void manageMovement(int x, int y) { if (validMoves[x, y] > 0) { Debug.Log("current selection==" + (currentPiece.currX) + " , " + (currentPiece.currY)); oldSelection = currentPiece;//helpful in castling, first king then rook, when it comes for rook to move, oldSelection has king. translateReady = true; currentPiece.hasEverMoved = true; updateHistory(currentPiece.currX, currentPiece.currY, x, y);//keeping records of previous moves BoardPieces[currentPiece.currX, currentPiece.currY] = null; moveTo(currentPiece.currX, currentPiece.currY, x, y); if (validMoves[x, y] == 6)//castling { doneCastling = false; } didRotate = false; } else { currentPiece = null; } BoardSelection.BoardSelectionObject.removeColorSelection(); }
//FixedUpdate has fixed interval of updation usually 0.02 seconds private void FixedUpdate() { if (translateReady) { if (distCovered < dist) //initially distCovered and dist both are 0, so second condition { if ((distCovered / dist) <= 0.15) //smooth start, acceleration { speed += (0.05f * dist); } if ((distCovered / dist) > 0.65)//smooth end, deceleration { speed -= (0.02f * dist); killNow++; } distCovered += speed * Time.deltaTime; currentPiece.transform.Translate((dirMove) * speed * Time.deltaTime, Space.World);//Acc to Space.Self, it moves up in z axis } else { Debug.Log("end"); translateReady = false; distCovered = 0; dist = 0; killNow = -1; speed = startSpeed; currentPiece = null; if (validMoves[xc, yc] == 4 || validMoves[xc, yc] == 5)//promotion { Debug.Log("Promo"); choosePromotion(); } else if (validMoves[xc, yc] == 6 && !doneCastling)//fix here { Debug.Log("old selection+3==" + (oldSelection.currX + 1) + " , " + (oldSelection.currY)); currentPiece = BoardPieces[oldSelection.currX + 1, oldSelection.currY]; currentPiece.hasEverMoved = true; updateHistory(currentPiece.currX, currentPiece.currY, xc - 1, yc); //keeping records of previous moves BoardPieces[currentPiece.currX, currentPiece.currY] = null; moveTo(currentPiece.currX, currentPiece.currY, xc - 1, yc); doneCastling = true; } else { xc = yc = 0;//resetting xc and yc, we dont want to delete it if currently under promotion if (!didRotate) { CamRotator.fixangle += rotateBy; //rotate camera after every move didRotate = true; } isWhiteTurn = !isWhiteTurn; } isCmOrSm(); if (win == 0)//only show if game hasnt ended { isCheck(); } } if (killNow == 0)// set killNow such that this part executes only once { Debug.Log("Enters here"); currentPiece.setXY(xc, yc); if (validMoves[xc, yc] == 3) //en passant mode { activeChessman.Remove(BoardPieces[historyOfMoves[1, 3], historyOfMoves[1, 4]].gameObject); //moved from 0 to 1 Destroy(BoardPieces[historyOfMoves[1, 3], historyOfMoves[1, 4]].gameObject); } if (validMoves[xc, yc] == 2 || validMoves[xc, yc] == 5)//kill mode { Debug.Log("Kill" + validMoves[xc, yc]); if (BoardPieces[xc, yc].GetType() == typeof(King)) { win = BoardPieces[xc, yc].isWhitePiece ? -1 : 1; } activeChessman.Remove(BoardPieces[xc, yc].gameObject); Destroy(BoardPieces[xc, yc].gameObject); } BoardPieces[xc, yc] = currentPiece; } } if (bringPromotionOptions && win == 0) //dont promote if someone has already won { if (0.01f < pdist - pdistCovered && turnedOnPcome) //initially distCovered and dist both are 0, so second condition { pdistCovered += pspeed * Time.deltaTime; Debug.Log("3p --- here dist=" + pdist + " distCovered=" + pdistCovered + " speed=" + pspeed); for (int i = 0; i < 4; i++) { PromotionPieces[((isWhiteTurn) ? 0 : 1), i].transform.Translate((pdirMove) * pspeed * Time.deltaTime, Space.World);//Acc to Space.Self, it moves up in z axis Debug.Log("Promotion Piece " + ((isWhiteTurn) ? 0 : 1) + "," + i + "moved"); } } else { turnedOnPcome = false; if (!chosenPromotion) { if (Input.GetKeyDown(KeyCode.RightArrow)) { Debug.Log("Right"); if (choosePiece < 5) { choosePiece++; Debug.Log("choosePiece val=" + choosePiece); for (int i = 0; i < 4; i++) { int k = (i >= 2) ? 1 : 2; PromotionPieces[((isWhiteTurn) ? 0 : 1), i].transform.position = new Vector3(PromotionPieces[((isWhiteTurn) ? 0 : 1), i].transform.position.x + ((isWhiteTurn) ? -1 : 1), elevation, (isWhiteTurn) ? 7.5f : 0.5f); } } } else if (Input.GetKeyDown(KeyCode.LeftArrow))//else if so that only 1 key input is taken. { Debug.Log("Left"); if (choosePiece > 1) { choosePiece--; Debug.Log("choosePiece val=" + choosePiece); for (int i = 0; i < 4; i++) { int k = (i >= 2) ? 1 : 2; PromotionPieces[((isWhiteTurn) ? 0 : 1), i].transform.position = new Vector3(PromotionPieces[((isWhiteTurn) ? 0 : 1), i].transform.position.x - ((isWhiteTurn) ? -1 : 1), elevation, (isWhiteTurn) ? 7.5f : 0.5f); } } } else if (Input.GetKeyDown(KeyCode.Return)) { Debug.Log("Enter"); if (choosePiece != 3) { int k = (choosePiece > 3) ? choosePiece - 2 : choosePiece - 1; bkp = k; chosenPromotion = true; } } } else //take away tray if chosenPromotion { pspeed = -20; if (PromotionPieces[((isWhiteTurn) ? 0 : 1), bkp].transform.position.y > 0)//initially distCovered and dist both are 0, so second condition { pdistCovered += (pspeed) * Time.deltaTime; for (int i = 0; i < 4; i++) { if (i != bkp) { PromotionPieces[((isWhiteTurn) ? 0 : 1), i].transform.Translate((pdirMove) * pspeed * Time.deltaTime, Space.World); //Acc to Space.Self, it moves up in z axis } else if (PromotionPieces[((isWhiteTurn) ? 0 : 1), i].transform.position.y > 0) //i==bkp is obvious { PromotionPieces[((isWhiteTurn) ? 0 : 1), i].transform.Translate((new Vector3(0, -1, 0)) * 6 * Time.deltaTime, Space.World); } Debug.Log("Promotion Piece " + ((isWhiteTurn) ? 0 : 1) + "," + i + "moved"); } } else { pdistCovered = 0; int k = -2; for (int i = 0; i < 4; i++) { if (i == 2) { k++; } PromotionPieces[((isWhiteTurn) ? 0 : 1), i].transform.position = new Vector3((isWhiteTurn) ? 22.5f + k: -21.5f - k, elevation, (isWhiteTurn) ? 7.5f : 0.5f); k++; } bkp++; //1 for queen, 2 for bishop, 3 for knight, 4 for rook and interact with user to get his choice bkp += isWhiteTurn ? 0 : 6; //incrementing required values for black prefabs Debug.Log("bkp===" + bkp); activeChessman.Remove(BoardPieces[xc, yc].gameObject);//destroying pawn in these 2 lines Destroy(BoardPieces[xc, yc].gameObject); spawnAt(bkp, givePos(xc, yc)); Debug.Log("Promo End"); xc = yc = 0;//resetting xc and yc, we dont want to delete it if currently under promotion if (!didRotate) { CamRotator.fixangle += rotateBy;//rotate camera after every move didRotate = true; } bkp = 0; chosenPromotion = false; pdistCovered = 0; CamAdvanced.moveUp = false; isWhiteTurn = !isWhiteTurn; bringPromotionOptions = false; } } } } }
public void SetPiece(TaikyokuShogi game, PieceIdentity id) => pieceInfoDisplay.SetPiece(game, id);
public bool ShowDialog(TaikyokuShogi game, PieceIdentity idBefore, PieceIdentity idAfter) { originalPieceDisplay.SetPiece(game, idBefore); promotedPieceDisplay.SetPiece(game, idAfter); return(ShowDialog() ?? false); }
public Piece(Player owner, PieceIdentity id, bool promoted = false) => (Owner, Id, Promoted) = (owner, id, promoted);