public void BoardLogicRemovePiece()
        {
            PieceLogic[] expectedPieces = new PieceLogic[24];
            for (int i = 0; i < 12; i++)
            {
                PieceLogic p = new PieceLogic(i, "Red");
                expectedPieces[i] = p;
            }
            for (int i = 11; i < 24; i++)
            {
                PieceLogic p = new PieceLogic(i, "Black");
                expectedPieces[i] = p;
            }
            BoardLogic board = new BoardLogic(expectedPieces);

            int idToRemove = 1;

            // test RemovePiece
            board.RemovePiece(idToRemove);
            PieceLogic[] actualPieces = board.GetPieces();
            for (int i = 0; i < actualPieces.Length; i++)
            {
                if (actualPieces[i].GetId() == idToRemove)
                {
                    int[] actualLocation   = actualPieces[i].GetLocation();
                    int[] expectedLocation = new int[] { -1, -1 };
                    Assert.AreEqual(actualLocation[0], expectedLocation[0]);
                }
            }
        }
    //private Client _client = new Client();



    ///////////////////////////////////////////////////////////////////
    //////////////////////////// Constructor //////////////////////////
    ///////////////////////////////////////////////////////////////////
    /// <summary>
    /// Constructor - sets gameId, playerColor,  and board according to parameters.
    /// Sets request status to None.  Still need to set Game Status, Player Turn, and Valid Moves
    /// </summary>
    public GameController(int gameId, string playerColor)
    {
        if (playerColor.ToLower() == "red")
        {
            _playerColor = "Red";
        }
        else
        {
            _playerColor = "Black";
        }

        _gameid        = gameId;
        _requestStatus = "None";
        _playerTurn    = "Black";

        // initialize the pieces
        int[,] initialLocations = new int[24, 2] {
            { 0, 0 }, { 0, 2 }, { 0, 4 }, { 0, 6 }, { 1, 1 }, { 1, 3 }
            , { 1, 5 }, { 1, 7 }, { 2, 0 }, { 2, 2 }, { 2, 4 }, { 2, 6 }, { 5, 1 }, { 5, 3 }, { 5, 5 }, { 5, 7 }, { 6, 0 }, { 6, 2 }, { 6, 4 }, { 6, 6 }, { 7, 1 }, { 7, 3 }, { 7, 5 }, { 7, 7 }
        };
        PieceLogic[] initialPieces = new PieceLogic[24];
        for (int i = 0; i < 12; i++)
        {
            initialPieces[i] = new PieceLogic(i, "Black");
            initialPieces[i].SetLocation(new int[] { initialLocations[i, 0], initialLocations[i, 1] });
            initialPieces[i + 12] = new PieceLogic(i + 12, "Red");
            initialPieces[i + 12].SetLocation(new int[] { initialLocations[i + 12, 0], initialLocations[i + 12, 1] });
        }

        _board = new BoardLogic(initialPieces);


        //  _client = new Client();
    }
 /// <summary>
 /// A piece becomes a king if it reaches the enemy's side.
 /// </summary>
 public void BecomeKing(PieceLogic piece)
 {
     if (piece.GetColor() == "Red" && piece.GetLocation()[0] == 0)
     {
         piece.KingMe();
     }
     if (piece.GetColor() == "Black" && piece.GetLocation()[0] == 7)
     {
         piece.KingMe();
     }
 }
Ejemplo n.º 4
0
        public GameController BuildGameControllerBlackPlayerWithJumps()
        {
            PieceLogic[] testPieces = new PieceLogic[24];
            int[,] locations = new int[24, 2] {
                { 0, 0 }, { 0, 2 }, { 0, 4 }, { 0, 6 }, { 1, 1 }, { 1, 3 }
                , { 1, 5 }, { 1, 7 }, { 2, 0 }, { 3, 3 }, { 2, 4 }, { 3, 5 }, { 4, 2 }, { 4, 4 }, { 4, 6 }, { 5, 7 }, { 6, 0 }, { 6, 2 }, { 6, 4 }, { 6, 6 }, { 7, 1 }, { 7, 3 }, { 7, 5 }, { 7, 7 }
            };
            for (int i = 0; i < 12; i++)
            {
                testPieces[i] = new PieceLogic(i, "Red");
                testPieces[i].SetLocation(new int[] { locations[i, 0], locations[i, 1] });
                testPieces[i + 12] = new PieceLogic(i + 12, "Black");
                testPieces[i + 12].SetLocation(new int[] { locations[i + 12, 0], locations[i + 12, 1] });
            }
            BoardLogic     testBoard = new BoardLogic(testPieces);
            GameController testGc    = new GameController(2, "Black", testBoard);

            testGc.SetGameStatus("Ongoing");
            return(testGc);
        }
        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);
        }
        public void BoardLogicGetSet()
        {
            PieceLogic[] expectedPieces = new PieceLogic[24];
            for (int i = 0; i < 12; i++)
            {
                PieceLogic p = new PieceLogic(i, "Red");
                expectedPieces[i] = p;
            }
            for (int i = 11; i < 24; i++)
            {
                PieceLogic p = new PieceLogic(i, "Black");
                expectedPieces[i] = p;
            }
            BoardLogic board = new BoardLogic(expectedPieces);

            PieceLogic[] actualPieces = board.GetPieces();

            // test GetPieces
            Assert.AreEqual(expectedPieces, actualPieces);

            for (int i = 0; i < 12; i++)
            {
                PieceLogic p = new PieceLogic(i, "Black");
                expectedPieces[i] = p;
            }
            for (int i = 11; i < 24; i++)
            {
                PieceLogic p = new PieceLogic(i, "Red");
                expectedPieces[i] = p;
            }

            // test SetPieces
            board.SetPieces(expectedPieces);
            actualPieces = board.GetPieces();
            Assert.AreEqual(expectedPieces, actualPieces);
        }
Ejemplo n.º 7
0
	/*
	 * Check if given piece is placeable onto grid.
	 */
	public bool IsPlaceable(PieceLogic piece)
	{
		int pHeight = piece.GetHeight();
		int pWidth = piece.GetWidth();
		int or = (int)piece.GetOrigin().y;
		int oc = (int)piece.GetOrigin().x;
		for(int r = 0; r < pHeight; r++)
			for(int c = 0; c < pWidth; c++)
			{
				// if cell is NoColor it's not necessary to check it.
				if(piece.GetCell(r,c).Match(CellColor.NoColor))
					continue;
				// piece cell position on grid 
				int row = or - r;
				int col = oc + c;
				// if cell outbounds of the grid, return false.
				if(row < 0 || row >= HEIGHT || col < 0 || col >= WIDTH)
					return false;	
				// if that cell is already taken, return false.
				if(!cells[row][col].Match(CellColor.NoColor))
					return false;
			}
		return true;
	}
Ejemplo n.º 8
0
    // 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;
            }
        }
    }
Ejemplo n.º 9
0
	public void Copy(PieceLogic root)
	{
		SetWidth(root.GetWidth());
		SetHeight(root.GetHeight());
		ResetCells();
		for(int r = 0; r < height; r++)
			for(int c = 0; c < width; c++)
			{
				cells[r][c].SetColor(root.GetCell(r,c).GetColor());
				cells[r][c].SetFeature(root.GetCell(r,c).GetFeature());
			}
	}
Ejemplo n.º 10
0
	/*
	 * Put given piece onto grid.
	 * The piece will be colidable when consolidated.
	 */
	public void ConsolidatePiece(PieceLogic piece)
	{
		int pHeight = piece.GetHeight();
		int pWidth = piece.GetWidth();
		int oc = (int)piece.GetOrigin().x;
		int or = (int)piece.GetOrigin().y;
		// Printing piece onto grid
		for(int r = 0; r < pHeight; r++)
		for(int c = 0; c < pWidth; c++)
		{
			// sanity check for when piece is not completely shown.
			if(or-r < 0)	continue;

			if(!piece.GetCell(r,c).Match(CellColor.NoColor))
			{
				cells[or-r][oc+c].SetColor(piece.GetCell(r,c).GetColor());
				cells[or-r][oc+c].SetFeature(piece.GetCell(r,c).GetFeature());
			}
		}
	}