Esempio n. 1
0
    public void shiftRows(Lobby lobby, bool bottomColumnFilled)
    {
        int[,] board = lobby.game.board.board;
        Prints prints = new Prints();

        Console.WriteLine("APPLIED BOARD");
        prints.PrintMultiDimArr(lobby.game.board.board);
        int rows = board.GetLength(0);
        int cols = board.GetLength(1);
        // shift code
        List <int[]> SquaresList = new List <int[]>();

        for (int j = 0; j < cols; j++)
        {
            int shiftAmount = (board[rows - 1, j] == 0) ? 1 : 0;
            for (int i = board.GetLength(0) - 2; i >= 0; i--)
            {
                if (board[i, j] > 0)
                {
                    int[] temp = { i, j, shiftAmount };
                    SquaresList.Add(temp);
                }
                else
                {
                    shiftAmount++;
                }
            }
        }

        if (!bottomColumnFilled)
        {
            for (int j = 0; j < cols; j++)
            {
                if (board[rows - 1, j] != 0)
                {
                    int[] temp = { rows - 1, j, 0 };
                    SquaresList.Add(temp);
                }
            }
        }

        int[,] newBoard = new int[board.GetLength(0), board.GetLength(1)];

        for (int a = 0; a < SquaresList.Count; a++)
        {
            int[] temp = SquaresList[a];
            newBoard[temp[0] + temp[2], temp[1]] = board[temp[0], temp[1]];
        }
        Console.WriteLine("NEW BOARDTM");
        prints.PrintMultiDimArr(newBoard);
        lobby.game.board.board = newBoard;
        Console.WriteLine("SHIFTED BOARD");
        prints.PrintMultiDimArr(lobby.game.board.board);
    }
Esempio n. 2
0
        public void SpecificShiftSituation()
        {
            lobbyManager.createLobby(4, "bob", 5, "no", (WebSocketSharp.WebSocket)null);
            Lobby lobby = new Lobby("1", 0);

            lobby.game             = new GameState(10, 10);
            lobby.game.board.board = new int[, ] {
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 1, 3, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
                { 1, 1, 1, 2, 3, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            };
            lobbyManager.shiftRows(lobby, true);
            int[,] resBoard  = lobby.game.board.board;
            int[,] testBoard = new int[, ] {
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 1, 3, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
                { 1, 1, 1, 2, 3, 0, 0, 0, 0, 0 }
            };
            bool isSameBoard = true;

            for (int i = 0; i < testBoard.GetLength(0); i++)
            {
                for (int j = 0; j < testBoard.GetLength(1); j++)
                {
                    if (resBoard[i, j] != testBoard[i, j])
                    {
                        isSameBoard = false;
                    }
                }
            }
            Prints prints = new Prints();

            Console.WriteLine("RESULTING BOARD");
            prints.PrintMultiDimArr(lobby.game.board.board);
            Assert.That(isSameBoard, Is.EqualTo(true));
        }
Esempio n. 3
0
        public void RemovePowerCubeSecondRowColumn()
        {
            lobbyManager.createLobby(4, "bob", 5, "no", (WebSocketSharp.WebSocket)null);
            Lobby lobby = new Lobby("1", 0);

            lobby.game             = new GameState(8, 8);
            lobby.game.board.board = new int[, ] {
                { 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 0, 0, 0, 0, 0, 0 },
                { 1, 2, 1, 1, 1, 1, 1, 1 },
                { 1, 1, 1, 1, 0, 1, 1, 1 }
            };
            lobbyManager.checkRows(lobby);
            int[,] resBoard  = lobby.game.board.board;
            int[,] testBoard = new int[, ] {
                { 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0 },
                { 1, 0, 1, 1, 0, 1, 1, 1 }
            };
            bool isSameBoard = true;

            for (int i = 0; i < testBoard.GetLength(0); i++)
            {
                for (int j = 0; j < testBoard.GetLength(1); j++)
                {
                    if (resBoard[i, j] != testBoard[i, j])
                    {
                        isSameBoard = false;
                    }
                }
            }
            Prints prints = new Prints();

            Console.WriteLine("RESULTING BOARD");
            prints.PrintMultiDimArr(lobby.game.board.board);
            Assert.That(isSameBoard, Is.EqualTo(true));
        }
Esempio n. 4
0
    /**
     * #function SingleBot::GetSingleMove |
     * @author JavaComSci |
     * @desc need desc here TODO |
     * @header public override List<...> GetSingleMove(Board, List<Block>, bool) |
     * @param int[][] board : current enviornment |
     * @param List<Block> blocks : contains the list of all the blocks to try to fit in this location |
     * @returns List<...> bestPiecePlacementOfCurrentBlock : contains the list of the indicies
     * of where the piece would be on the board |
     */
    public List <Tuple <int, int> > GetSingleMove1(Board board, List <List <Block> > allBotBlocks, bool allRotations = false)
    {
        List <Block> blocks = allBotBlocks[0];

        Console.WriteLine("BOARD");
        botInfoPrinter.PrintMultiDimArr(board.board);
        Console.WriteLine("RECIEVED PIECE");
        botInfoPrinter.PrintJaggedArr(blocks[0].data);

        // get the max height of each column of the baord
        board.FindMaxHeights();

        // has the information about all the compatible pieces for a given block
        Dictionary <Block, List <Tuple <int, List <Tuple <int, int> >, int, int> > > allBlocksPossible = new Dictionary <Block, List <Tuple <int, List <Tuple <int, int> >, int, int> > >();

        int blockCount = 0;

        if (blocks.Count >= 1)
        {
            shapeColor = blocks[0].color;
        }

        // test out each of the pieces
        foreach (Block block in blocks)
        {
            // check to make sure that each block is valid
            bool blockValid = block.CheckValidity();

            // block is invalid
            if (!blockValid)
            {
                throw new Exception("Shape formation is incorrect");
            }

            // compatible pieces for a single block
            // has all the positions compatible for this piece with the rotation, location on board, area covered, and number of lines that can be cleared
            List <Tuple <int, List <Tuple <int, int> >, int, int> > compatiblePieces = new List <Tuple <int, List <Tuple <int, int> >, int, int> >();

            // get the fit of the board with the area and whether a piece can clear a line
            compatiblePieces.AddRange(getFit(board, block, 1));
            block.data = block.RotateMatrix();
            compatiblePieces.AddRange(getFit(board, block, 2));
            block.data = block.RotateMatrix();
            compatiblePieces.AddRange(getFit(board, block, 3));
            block.data = block.RotateMatrix();
            compatiblePieces.AddRange(getFit(board, block, 4));

            if (allRotations)
            {
                block.data = block.RotateMatrix();
            }

            // compatible pieces has all the pieces that are compatible with the board and has the information about the rotation, location on board, area covered, and if that piece can clear a line
            compatiblePieces.Sort((x, y) => {
                // sort by whether a line has been cleared and puts those first if there is
                int result = y.Item4.CompareTo(x.Item4);
                // sort by the area covered
                return(result == 0 ? y.Item3.CompareTo(x.Item3) : result);
            });

            // the current piece cannot even be placed so must return null
            if (compatiblePieces.Count == 0 && blockCount == 0)
            {
                return(null);
            }
            blockCount++;

            allBlocksPossible[block] = compatiblePieces;
        }

        // get the best piece of the block that is the first one
        Tuple <int, List <Tuple <int, int> >, int, int> bestPieceOfCurrentBlock = allBlocksPossible[blocks[0]][0];

        // placement of this block that is the best of the current block
        List <Tuple <int, int> > bestPiecePlacementOfCurrentBlock = bestPieceOfCurrentBlock.Item2;

        // if the best current piece doesnt clear any lines, need to find one that does clear lines
        if (bestPieceOfCurrentBlock.Item4 == 0 && blocks.Count > 1)
        {
            // does not clear any lines so need to look for another candidate and choose the next best spot for this block

            // find the next piece that clears lines
            Tuple <int, List <Tuple <int, int> >, int, int> bestPieceOfNextBlock = allBlocksPossible[blocks[1]][0];

            // next next piece that clears lines
            Tuple <int, List <Tuple <int, int> >, int, int> bestPieceOfNextNextBlock = allBlocksPossible.Count > 2 ? allBlocksPossible[blocks[2]][0] : null;
            // Console.WriteLine("NEXT PIECE " + bestPieceOfNextNextBlock.Item4);

            // the next piece is able to clear lines
            if (bestPieceOfNextBlock.Item4 > 0)
            {
                // Console.WriteLine("I AM HEREHEHEH LOOKING AT THE NEXT PIECE!\n");
                // create a new copy of the board so that it can run through the steps for clearing lines again
                int [,] copiedBoard = board.CopyBoard(board.board);

                // add the pieces to this board to where it has the new information
                copiedBoard = board.FillBoardWithPieceConsidered(copiedBoard, bestPieceOfNextBlock.Item2);

                // check to see if there is anything the current piece can place so do another search with the new info
                List <Tuple <int, List <Tuple <int, int> >, int, int> > compatiblePiecesForCurrentPieceAfterPiece1 = new List <Tuple <int, List <Tuple <int, int> >, int, int> >();

                // set up new board
                Board cBoard = new Board(board.board.GetLength(0), board.board.GetLength(1));
                cBoard.board = copiedBoard;
                cBoard.FindMaxHeights();

                // get the fit of the board with the area and whether a piece can clear a line
                compatiblePiecesForCurrentPieceAfterPiece1.AddRange(getFit(cBoard, blocks[0], 1));
                blocks[0].data = blocks[0].RotateMatrix();
                compatiblePiecesForCurrentPieceAfterPiece1.AddRange(getFit(cBoard, blocks[0], 2));
                blocks[0].data = blocks[0].RotateMatrix();
                compatiblePiecesForCurrentPieceAfterPiece1.AddRange(getFit(cBoard, blocks[0], 3));
                blocks[0].data = blocks[0].RotateMatrix();
                compatiblePiecesForCurrentPieceAfterPiece1.AddRange(getFit(cBoard, blocks[0], 4));

                if (allRotations)
                {
                    blocks[0].data = blocks[0].RotateMatrix();
                }

                // compatible pieces has all the pieces that are compatible with the board and has the information about the rotation, location on board, area covered, and if that piece can clear a line
                compatiblePiecesForCurrentPieceAfterPiece1.Sort((x, y) => {
                    // sort by whether a line has been cleared and puts those first if there is
                    int result = y.Item4.CompareTo(x.Item4);
                    // sort by the area covered
                    return(result == 0 ? y.Item3.CompareTo(x.Item3) : result);
                });

                // placement of this block that is the best of the current block
                bestPiecePlacementOfCurrentBlock = compatiblePiecesForCurrentPieceAfterPiece1[0].Item2;
            }
            else if (bestPieceOfNextNextBlock != null && bestPieceOfNextNextBlock.Item4 > 0)
            {
                // Console.WriteLine("I AM A NEXT NEXT SHAPE\n\n\n\n");

                // check 2 shapes from the current shape

                // create a new copy of the board so that it can run through the steps for clearing lines again
                int [,] copiedBoard = board.CopyBoard(board.board);

                // add the pieces to this board to where it has the new information
                copiedBoard = board.FillBoardWithPieceConsidered(copiedBoard, bestPieceOfNextNextBlock.Item2);

                // check to see if there is anything the current piece can place so do another search with the new info
                List <Tuple <int, List <Tuple <int, int> >, int, int> > compatiblePiecesForCurrentPieceAfterPiece2 = new List <Tuple <int, List <Tuple <int, int> >, int, int> >();

                // set up new board
                Board cBoard = new Board(board.board.GetLength(0), board.board.GetLength(1));
                cBoard.board = copiedBoard;
                cBoard.FindMaxHeights();

                // get the fit of the board with the area and whether a piece can clear a line
                compatiblePiecesForCurrentPieceAfterPiece2.AddRange(getFit(cBoard, blocks[0], 1));
                blocks[0].data = blocks[0].RotateMatrix();
                compatiblePiecesForCurrentPieceAfterPiece2.AddRange(getFit(cBoard, blocks[0], 2));
                blocks[0].data = blocks[0].RotateMatrix();
                compatiblePiecesForCurrentPieceAfterPiece2.AddRange(getFit(cBoard, blocks[0], 3));
                blocks[0].data = blocks[0].RotateMatrix();
                compatiblePiecesForCurrentPieceAfterPiece2.AddRange(getFit(cBoard, blocks[0], 4));

                if (allRotations)
                {
                    blocks[0].data = blocks[0].RotateMatrix();
                }

                // compatible pieces has all the pieces that are compatible with the board and has the information about the rotation, location on board, area covered, and if that piece can clear a line
                compatiblePiecesForCurrentPieceAfterPiece2.Sort((x, y) => {
                    // sort by whether a line has been cleared and puts those first if there is
                    int result = y.Item4.CompareTo(x.Item4);
                    // sort by the area covered
                    return(result == 0 ? y.Item3.CompareTo(x.Item3) : result);
                });

                // placement of this block that is the best of the current block
                bestPiecePlacementOfCurrentBlock = compatiblePiecesForCurrentPieceAfterPiece2[0].Item2;
            }
        }

        // Console.WriteLine("BEST PIECE FOR BOARD");
        // botInfoPrinter.PrintPositions(bestPiecePlacementOfCurrentBlock);
        Console.WriteLine("BOARD WITH PIECE");
        botInfoPrinter.PrintBoardWithPiece(board.board, bestPiecePlacementOfCurrentBlock);

        return(bestPiecePlacementOfCurrentBlock);
    }
Esempio n. 5
0
    /**
     * #function DoubleBot::GetMove |
     * @author JavaComSci |
     * @desc gets the fit of a both block onboard |
     * @header public override List<Tuple<int, int>> GetMove(Board board, List<List<Block>> allBotBlocks, bool allRotations = false)  |
     * @param Board board: board to do placing on |
     * @param List<List<Block>> allBotBlocks: blocks to be placed|
     * @returns List<List<Tuple<int, int>>> : contains position for both blocks |
     */
    public override List <List <Tuple <int, int> > > GetMove(Board board, List <List <Block> > allBotBlocks, bool allRotations = false)
    {
        // get each of the bot's blocks
        List <Block> bot1Blocks = allBotBlocks[0];
        List <Block> bot2Blocks = allBotBlocks[1];

        // check to make sure that each block is valid
        bool blockValid1 = bot1Blocks[0].CheckValidity();

        // block is invalid
        if (!blockValid1)
        {
            throw new Exception("Shape formation is incorrect");
        }
        bool blockValid2 = bot2Blocks[0].CheckValidity();

        // block is invalid
        if (!blockValid2)
        {
            throw new Exception("Shape formation is incorrect");
        }

        // get the max height of each column of the baord
        board.FindMaxHeights();

        // print the information
        // Console.WriteLine("BOARD");
        botInfoPrinter.PrintMultiDimArr(board.board);
        // Console.WriteLine("PIECE 1 BOT 1");
        botInfoPrinter.PrintJaggedArr(bot1Blocks[0].data);
        // Console.WriteLine("PIECE 2 BOT 2");
        botInfoPrinter.PrintJaggedArr(bot2Blocks[0].data);


        // all orientations of the 2 blocks in each one going first on the board
        List <Tuple <Block, Block, int> > allOrientations = GetAllOrientations(bot1Blocks[0], bot2Blocks[0]);

        if (allOrientations == null)
        {
            return(null);
        }

        // Console.WriteLine("ALL ORIENTATIONS");
        botInfoPrinter.PrintAllOrientationsAsList(allOrientations);

        // get the best fit of blocks
        List <Tuple <CompatiblePiece, CompatiblePiece> > allCompatiblePieces = GetFitBothBlocks(board, allOrientations);

        if (allCompatiblePieces == null || allCompatiblePieces.Count == 0)
        {
            // Console.WriteLine("I AM RETURING WITH NO PIECES");
            return(null);
        }

        Tuple <CompatiblePiece, CompatiblePiece> bestPieces = GetBestFit(allCompatiblePieces);

        // Console.WriteLine("BEST FIETS " + bestPieces);
        if (bestPieces == null || bestPieces.Item1 == null || bestPieces.Item2 == null)
        {
            return(null);
        }

        // printing the info
        // Console.WriteLine("BEST COMPATIBLE PIECES ON BOARD");
        List <Tuple <CompatiblePiece, CompatiblePiece> > bestPiecesList = new List <Tuple <CompatiblePiece, CompatiblePiece> >();

        bestPiecesList.Add(bestPieces);
        botInfoPrinter.PrintAllCompatiblePieces(board.board, bestPiecesList);


        // return setup
        List <Tuple <int, int> >         compatiblePiece1 = bestPieces.Item1.locationOnBoard;
        List <Tuple <int, int> >         compatiblePiece2 = bestPieces.Item2.locationOnBoard;
        List <List <Tuple <int, int> > > allMoves         = new List <List <Tuple <int, int> > >();

        allMoves.Add(compatiblePiece1);
        allMoves.Add(compatiblePiece2);
        if (compatiblePiece1 == null || compatiblePiece2 == null)
        {
            return(null);
        }
        return(allMoves);
    }
Esempio n. 6
0
    /**
     * #function TripleBot::GetMove |
     * @author JavaComSci |
     * @desc gets the fit of a three block onboard |
     * @header public override List<Tuple<int, int>> GetMove(Board board, List<List<Block>> allBotBlocks, bool allRotations = false)  |
     * @param Board board: board to do placing on |
     * @param List<List<Block>> allBotBlocks: blocks to be placed|
     * @returns List<List<Tuple<int, int>>> : contains position for both blocks |
     */
    public override List <List <Tuple <int, int> > > GetMove(Board board, List <List <Block> > allBotBlocks, bool allRotations = false)
    {
        try {
            CheckBlockValididty(allBotBlocks);
        } catch (Exception e) {
            throw e;
        }

        // get each of the bot's blocks
        List <Block> bot1Blocks = allBotBlocks[0];
        List <Block> bot2Blocks = allBotBlocks[1];
        List <Block> bot3Blocks = allBotBlocks[2];

        // get the max height of each column of the baord
        board.FindMaxHeights();

        // print the information
        Console.WriteLine("BOARD");
        botInfoPrinter.PrintMultiDimArr(board.board);
        Console.WriteLine("PIECE 1 BOT 1");
        botInfoPrinter.PrintJaggedArr(bot1Blocks[0].data);
        Console.WriteLine("PIECE 1 BOT 2");
        botInfoPrinter.PrintJaggedArr(bot2Blocks[0].data);
        Console.WriteLine("PIECE 1 BOT 3");
        botInfoPrinter.PrintJaggedArr(bot3Blocks[0].data);

        // get all the orientations
        List <Tuple <Block, Block, Block> > allOrientations = GenerateAllOrientations(allBotBlocks);

        if (allOrientations == null)
        {
            return(null);
        }
        // Console.WriteLine("ALL ORIENTATIONS");
        botInfoPrinter.PrintAllOrientationsThreeBlocksAsList(allOrientations);

        // get fit three blocks
        List <Tuple <CompatiblePiece, CompatiblePiece, CompatiblePiece> > allCompatiblePieces = GetFitThreeBlocks(board, allOrientations);

        if (allCompatiblePieces == null || allCompatiblePieces.Count == 0)
        {
            return(null);
        }
        Tuple <CompatiblePiece, CompatiblePiece, CompatiblePiece> bestPieces = GetBestFit(allCompatiblePieces);

        if (bestPieces == null || bestPieces.Item1 == null || bestPieces.Item2 == null || bestPieces.Item3 == null)
        {
            return(null);
        }

        // printing the info
        Console.WriteLine("BEST COMPATIBLE PIECES ON BOARD");
        List <Tuple <CompatiblePiece, CompatiblePiece, CompatiblePiece> > bestPiecesList = new List <Tuple <CompatiblePiece, CompatiblePiece, CompatiblePiece> >();

        bestPiecesList.Add(bestPieces);
        botInfoPrinter.PrintAllCompatiblePieces(board.board, bestPiecesList);


        // return setup
        List <Tuple <int, int> >         compatiblePiece1 = bestPieces.Item1.locationOnBoard;
        List <Tuple <int, int> >         compatiblePiece2 = bestPieces.Item2.locationOnBoard;
        List <Tuple <int, int> >         compatiblePiece3 = bestPieces.Item3.locationOnBoard;
        List <List <Tuple <int, int> > > allMoves         = new List <List <Tuple <int, int> > >();

        allMoves.Add(compatiblePiece1);
        allMoves.Add(compatiblePiece2);
        allMoves.Add(compatiblePiece3);
        if (compatiblePiece1 == null || compatiblePiece2 == null || compatiblePiece3 == null)
        {
            return(null);
        }
        return(allMoves);
    }