public void TestPieceLogic() { int expectedID = 2; string expectedColor = "red"; int[] expectedLocation = new int[] { 0, 0 }; bool expectedIsKing = false; PieceLogic p = new PieceLogic(expectedID, expectedColor); p.SetLocation(expectedLocation); int actualID = p.GetId(); string actualColor = p.GetColor(); int[] actualLocation = p.GetLocation(); bool actualIsKing = p.IsKing(); // test GetID Assert.AreEqual(expectedID, actualID); // test setLocation getLocation Assert.AreEqual(expectedLocation, actualLocation); // test getColor Assert.AreEqual(expectedColor, actualColor); //test IsKing Assert.AreEqual(expectedIsKing, actualIsKing); // test KingMe p.KingMe(); expectedIsKing = true; actualIsKing = p.IsKing(); Assert.AreEqual(expectedIsKing, actualIsKing); }
// given an x, y and x, y attempts to move the piece to the given location // uses move validation to determine if move is valid and if the turn should change // also determines win condition private void TryMove(int xstart, int ystart, int xend, int yend) { // location of the starting piece startDrag = new Vector2(xstart, ystart); // the UI piece at the starting position selectedPiece = pieces[xstart, ystart]; //Debug.Log("xstart: " + xstart + " ystart: " + ystart + " xend: " + xend + " yend: " + yend); // Check if the piece is null if (selectedPiece != null) { // move the piece to the given end location // this provides the visual of the user placing // the piece and it *slingshotting* back to it original // location if the move is invalid MovePiece(selectedPiece, xend, yend); // confirm move using logic controller if (black_game.IsMoveValid(new int[2, 2] { { ystart, xstart }, { yend, xend } })) { // set the end location to the given UI piece pieces[xend, yend] = selectedPiece; // null the given start location pieces[xstart, ystart] = null; // clear the drag vector startDrag = Vector2.down; // check if the move was a jump if (Math.Abs(xstart - xend) == 2) { // create a temp piece Piece p = pieces[((xstart + xend) / 2), ((ystart + yend) / 2)]; if (p != null) { // set jump checker to true for use in double jumps jumped = true; // remove the jumped piece from the UI array pieces[((xstart + xend) / 2), ((ystart + yend) / 2)] = null; // remove the piece from the actual UI DestroyImmediate(p.gameObject); } } // perform the move on the logic side black_game.MakeMove(new int[2, 2] { { ystart, xstart }, { yend, xend } }); // get the logical piece at the end location PieceLogic pl = black_game.FindPiece(yend, xend); // check if the piece is a king (logical) and isn't flipped (UI king) if (pl.IsKing() && !selectedPiece.isFlipped) { //Debug.Log("King ME"); // logical side gives a piece a king after moving // if the piece is a king and isn't flipped in the UI // it needs to be flipped selectedPiece.isFlipped = true; selectedPiece.transform.Rotate(Vector3.right * 180); } // generate the next table of valid moves for checking for second jumps black_game.FindValidMoves(); //Debug.Log("contined" + black_game.HasContinuedTurn()); //Debug.Log("jumped: " + jumped); // continued turn is a logical indicator the pieces can make another move // jumped checks that the piece that moved made a jump (so this move is a second jump) if (black_game.HasContinuedTurn() && jumped) { // keeps the player the same and they have to make the next jump return; } // no second turn else { // reset jumped jumped = false; //Debug.Log("Swapping"); // Change the UI indicator that shows the player Turn if (black_game.GetPlayerColor() == "Black") { blackPanel.SetActive(false); redPanel.SetActive(true); } else { blackPanel.SetActive(true); redPanel.SetActive(false); } // Change the logical turn black_game.SwitchTurn(); } // null the piece (reset) selectedPiece = null; // check if there is a winner string winner = black_game.WhoWon(); if (winner == "NoOne") { // continue if no one has won return; } else if (winner == "Red") { // show winner panel for Red if red won redWonPanel.SetActive(true); } else { // show winner panel for Black if black won blackWonPanel.SetActive(true); } } // if piece is null then reset the piece, the vector, and the UI piece global else { MovePiece(selectedPiece, xstart, ystart); startDrag = Vector2.zero; selectedPiece = null; //Debug.Log("Invalid Move"); return; } } }