/* Creates and adds the tiles to the board */ private void generateGameBoard() { for (int row = 0; row < size; row ++) { //int tile = 4; Tile[] listOfTiles = new Tile[size]; //now add the tiles to the list for (int col = 0; col < size; col ++) { //create the tile based on col / row values Tile tile = new Tile(); tile.TileCoord = new Coord(col, row); // assign it its colour (based on position) if ((row % 2) == 0) { tile.TileIcon = ((col % 2) == 0) ? "white" : "black"; } else { tile.TileIcon = ((col % 2) == 0) ? "black" : "white"; } listOfTiles[col] = tile; } //add the tileList to the board at that row position internalBoard[row] = listOfTiles; //for each row, create size-cols } }
public Tuple<Piece, bool> movePiece(Tile fromTile, Tile toTile, List<Move> listOfMoves) { // IDENTIFIED MOVE NOT BEING SET // DEBUG // req lsOfMoves to identify the move which has // been made, (uses tileA and B to determine) // got from getValidAvailableMoves function Move identifiedMove = null; foreach (Move move in listOfMoves) { // a complex comparison requiring overload of the Equals method if (move.FromPos.Equals(fromTile.TileCoord) && move.ToPos.Equals(toTile.TileCoord)) { identifiedMove = move; break; } } if (identifiedMove == null) { System.Console.WriteLine("Critical Error (282)"); } // now copy the piece from fromTile to toTile toTile.OccupyingPiece = fromTile.OccupyingPiece; // make sure the piece now on toTile knows it is toTile.OccupyingPiece.updatePosition(toTile.TileCoord); // remove the old piece from the fromTile and // update its properties fromTile.OccupyingPiece = null; fromTile.IsOccupied = false; fromTile.GuiMustBeUpdated = true; // update the properties of the toTile toTile.IsOccupied = true; toTile.GuiMustBeUpdated = true; // now, using knowledge of the move, determine if a // piece has been captured Piece captured = null; // ============================ if (identifiedMove.MoveType == "jmp") { Tile jumpedTile = getTile(identifiedMove.JumpedPos); // ref captured = jumpedTile.OccupyingPiece; jumpedTile.OccupyingPiece = null; jumpedTile.IsOccupied = false; jumpedTile.GuiMustBeUpdated = true; } // now look at the piece and check whether its reached the king row bool pieceKinged = false; // ============================= bool onKingRow = isPieceOnKingRow(toTile.OccupyingPiece); if (toTile.OccupyingPiece.Ptype != "d_king" && onKingRow) { toTile.OccupyingPiece.upgradeToKing(); pieceKinged = true; } // returns a move outcome, contains whether a piece is // kinged or not (boolean) // and a piece (if it is captured otherwise null) Tuple<Piece, bool> moveOutcome = Tuple.Create(captured, pieceKinged); return moveOutcome; }
private List<Coord> processSecondClick(Tile tileClicked) { List<Coord> tilesWhichHaveChanged = new List<Coord>(); // can assume SELECTED holds a tile (with a valid piece on it) // and POTENTIALMOVES contain some // checks the second click is on a piece/tile thats highlighted (thus its in potential moves) if (tileClicked.IsHighlighted) { //System.Console.WriteLine("is highlighted!"); Tuple<Piece, bool> result = board.movePiece(data.Selected, tileClicked, data.Potentialmoves); List<Coord> theTiles = setHighlightsForTiles(data.Potentialmoves, false); tilesWhichHaveChanged.AddRange(theTiles); tilesWhichHaveChanged.Add(data.Selected.TileCoord); tilesWhichHaveChanged.Add(tileClicked.TileCoord); // check if a piece was captured if (result.Item1 != null) { Piece captured = result.Item1; tilesWhichHaveChanged.Add(captured.CurrentPosition); // the tile which was jumped must be updated data.Captured[captured.Player] += 1; data.Selected = tileClicked; // if piece was captured and move did not result in a king if (!result.Item2) { // then check for further moves List<Move> availableMoves = board.getValidAvailableMoves(tileClicked.TileCoord, true); if (availableMoves.Count > 0) { data.Potentialmoves = availableMoves; data.Stage = Data.Gamestage.OngoingCapture_NoClick; } } changeCapturedDisplay(data.Captured); // if NOT an ongoing capture, then change the player etc if (! (data.Stage == Data.Gamestage.OngoingCapture_NoClick) ) { data.Current_player = (data.Current_player == "red") ? "white" : "red"; changeDisplayMessage("Player " + data.Current_player + "'s turn"); data.Stage = Data.Gamestage.NoClick; } } // else a piece wasnt captured (just normal move) so end turn else { data.Current_player = (data.Current_player == "red") ? "white" : "red"; changeDisplayMessage("Player " + data.Current_player + "'s turn"); data.Stage = Data.Gamestage.NoClick; } } // else the player has clicked on a non highlighted one of their own pieces else if ((!tileClicked.IsHighlighted) && tileClicked.IsOccupied && tileClicked.OccupyingPiece.Player == data.Current_player) { // if previous clicked tile is not same as just clicked tile if (data.Selected.TileCoord != tileClicked.TileCoord) { // then just remove -> update the highlight (by calling this function again) List<Coord> theTiles = setHighlightsForTiles(data.Potentialmoves, false); //tilesWhichHaveChanged.AddRange(theTiles); data.Stage = Data.Gamestage.NoClick; // since a recursive call is made here, // it wont reach the updateGuiTiles normally, // so must call it manually here // this recursive call is needed, since in this situation when a user clicks off an already highlighted option // onto another option, it simply removes the initial highlight and reapplies the new highlight updateGuiTiles(theTiles); this.tileClickedHandler(null, tileClicked.TileCoord); // problem could be here } else { // just remove the highlight List<Coord> theTiles = setHighlightsForTiles(data.Potentialmoves, false); tilesWhichHaveChanged.AddRange(theTiles); data.Stage = Data.Gamestage.NoClick; } } // else player has clicked on a non-highlighted, non piece of theirs else { List<Coord> theTiles = setHighlightsForTiles(data.Potentialmoves, false); tilesWhichHaveChanged.AddRange(theTiles); data.Stage = Data.Gamestage.NoClick; } return tilesWhichHaveChanged; }
private List<Coord> processOngoingCaptureSecondClick(Tile tileClicked) { // this function almost identical to processSecondClick // but ENFORCES THE CONTINUED CAPTURE (WHICH IS IDENTIFIED IN THE FIRST CLICK) // input: // SELECTED := the posA tile (which has valid moves (represented by list of posB in POTENTIALMOVES)) // tileClicked := the posB tile // assert tileClicked is in Potnential moves, then make the move, then check for kinging / further jumps // if not in potential moves -> error message, firstclickongoingcapture List<Coord> tilesWhichHaveChanged = new List<Coord>(); if (tileClicked.IsHighlighted) // easier than searching through potentialmoves { //System.Console.WriteLine("is highlighted!"); Tuple<Piece, bool> result = board.movePiece(data.Selected, tileClicked, data.Potentialmoves); List<Coord> theTiles = setHighlightsForTiles(data.Potentialmoves, false); tilesWhichHaveChanged.AddRange(theTiles); tilesWhichHaveChanged.Add(data.Selected.TileCoord); tilesWhichHaveChanged.Add(tileClicked.TileCoord); if (result.Item1 != null) { Piece captured = result.Item1; tilesWhichHaveChanged.Add(captured.CurrentPosition); data.Captured[captured.Player] += 1; data.Selected = tileClicked; // resulted in a further capture.. // and if did not result in a king if (!result.Item2) { List<Move> availableMoves = board.getValidAvailableMoves(tileClicked.TileCoord, true); if (availableMoves.Count > 0) { data.Potentialmoves = availableMoves; data.Stage = Data.Gamestage.OngoingCapture_NoClick; } } changeCapturedDisplay(data.Captured); // if NOT an ongoing capture, then change the player etc if (!(data.Stage == Data.Gamestage.OngoingCapture_NoClick)) { data.Current_player = (data.Current_player == "red") ? "white" : "red"; changeDisplayMessage("Player " + data.Current_player + "'s turn"); data.Stage = Data.Gamestage.NoClick; } } // else a piece wasnt captured (just normal move) so end turn else { data.Current_player = (data.Current_player == "red") ? "white" : "red"; changeDisplayMessage("Player " + data.Current_player + "'s turn"); data.Stage = Data.Gamestage.NoClick; } } // else the player has clicked on a non highlighted tile // we must enforce capture // 1. player has reclicked on tileA // 2. player has clicked on a non highlighted tile (outside of the enforced capture) else if (tileClicked.TileCoord == data.Selected.TileCoord) { // simply toggle highlight for consistency and back to first click List<Coord> theTiles = setHighlightsForTiles(data.Potentialmoves, false); tilesWhichHaveChanged.AddRange(theTiles); data.Stage = Data.Gamestage.OngoingCapture_NoClick; } else // toggle highlight for consistency and enforce the capture { List<Coord> theTiles = setHighlightsForTiles(data.Potentialmoves, false); tilesWhichHaveChanged.AddRange(theTiles); data.Stage = Data.Gamestage.OngoingCapture_NoClick; changeDisplayMessage("enforce continued capture"); } return tilesWhichHaveChanged; }
private List<Coord> processOngoingCaptureFirstClick(Tile tileClicked) { List<Coord> tilesWhichHaveChanged = new List<Coord>(); if (tileClicked == data.Selected) { List<Coord> tilesToHighlight = new List<Coord>(); foreach (Move move in data.Potentialmoves) // remove the highlights { tilesToHighlight.Add(move.ToPos); // add those whows highlight value WILL be changed.. } board.setHighlightTag(tilesToHighlight, true); tilesWhichHaveChanged.AddRange(tilesToHighlight); data.Selected = tileClicked; // redundant? data.Stage = Data.Gamestage.OngoingCapture_OneClick; // PROBLEM LIKELY HERE // ONECLICK STATE ISNT ENTIRELY CORRECT, SINCE THERE IS A RESTRICTION THAT THE PIECE DOING SUCCESSIVE CAPTURES IN THE // 'SEQUENCE' MUST BE THE SAME PIECE ALL THE WAY THROUGH, EG TILECLICKED MUST BE THE PREV (SELECTED) // IF ONECLICK IS SET HERE THIS MEANS SECONDCLICKMADE FUNCTION IS CALLED AND IF AN ERROR IS DETECTED (CLICKING NOT ON THE HIGHLIGHTS) // SECONDCLICKMADE FUNCTION WILL HANDLE THE ERROR AND ALLOW THE USER TO SELECT ANY OF HIS PIECES AS A POTENTIAL VALID PIECE A // RATHER THAN RESTRICTING IT TO THE PREVIOUS PIECE (THE PNE IN THE SEQUENCE) } else { changeDisplayMessage("Player " + data.Current_player + ", continue the capture sequence"); } return tilesWhichHaveChanged; }
private List<Coord> processFirstClick(Tile tileClicked) { List<Coord> tilesWhichHaveChanged = new List<Coord>(); // enforce must jump rule if there are pieces with jumps available List<Tile> tilesContainingPlayerPiecesWithJumps = board.getTilesContainingPlayerPiecesWithValidMoves(data.Current_player, true); if (tilesContainingPlayerPiecesWithJumps.Count > 0 && !tilesContainingPlayerPiecesWithJumps.Contains(tileClicked)) { changeDisplayMessage("You must capture a piece if possible"); } else if (tileClicked.IsOccupied && tileClicked.OccupyingPiece.Player == data.Current_player) { // prioritise jumps List<Move> availableMoves = board.getValidAvailableMoves(tileClicked.TileCoord, true); if (availableMoves.Count == 0) { // then normal moves availableMoves = board.getValidAvailableMoves(tileClicked.TileCoord, false); } List<Coord> theTiles = setHighlightsForTiles(availableMoves, true); tilesWhichHaveChanged.AddRange(theTiles); // the model has been changed, and a record is kept of which corresponding tiles must be chaged in the gui // update the state data.Selected = tileClicked; data.Potentialmoves = availableMoves; data.Stage = Data.Gamestage.OneClick; } else { changeDisplayMessage("piece clicked is not of cur player"); } return tilesWhichHaveChanged; }