コード例 #1
0
ファイル: Board.cs プロジェクト: SinaC/TetriNET
 public void CommitPiece(IPiece piece)
 {
     //if (piece.PosX < 1 || piece.PosX > Width)
     //    return;
     //if (piece.PosY < 1 || piece.PosY > Height)
     //    return;
     for (int i = 1; i <= piece.TotalCells; i++)
     {
         // Get piece position in board
         int x, y;
         piece.GetCellAbsolutePosition(i, out x, out y);
         // Check out of board
         if (x < 1)
         {
             return;
         }
         if (x > Width)
         {
             return;
         }
         if (y < 1)
         {
             return;
         }
         if (y > Height)
         {
             return;
         }
         // Add piece in board
         this[x, y] = CellHelper.SetColor(piece.Value); // indexer will handle cell out of board
     }
 }
コード例 #2
0
        public void DrawPiece(IPiece piece, int boardHeight)
        {
            // Clear
            foreach (Rectangle rect in _grid)
            {
                rect.Fill = TransparentColor;
            }

            if (piece == null)
            {
                return;
            }
            // Draw
            IPiece temp = piece.Clone();
            int    minX, minY, maxX, maxY;

            temp.GetAbsoluteBoundingRectangle(out minX, out minY, out maxX, out maxY);
            // Move to top, left
            temp.Translate(-minX, 0);
            temp.Translate(0, boardHeight - maxY);
            Pieces cellPiece = temp.Value;

            for (int i = 1; i <= temp.TotalCells; i++)
            {
                int x, y;
                temp.GetCellAbsolutePosition(i, out x, out y); // 1->Width x 1->Height
                int cellY = boardHeight - y;
                int cellX = x;

                Rectangle uiPart = GetControl(cellX, cellY);
                uiPart.Fill = TextureManager.TextureManager.Instance.GetBigPiece(cellPiece);
            }
        }
コード例 #3
0
ファイル: ConsoleUI.cs プロジェクト: SinaC/TetriNET
 private void HideNextPieceColor()
 {
     lock (_lock)
     {
         // hide next piece
         if (_client.NextPiece != null)
         {
             Console.ResetColor();
             IPiece temp = _client.NextPiece.Clone();
             int    minX, minY, maxX, maxY;
             temp.GetAbsoluteBoundingRectangle(out minX, out minY, out maxX, out maxY);
             // Move to top, left
             temp.Translate(-minX, 0);
             if (maxY > _client.Board.Height)
             {
                 temp.Translate(0, _client.Board.Height - maxY);
             }
             // hide piece
             for (int i = 1; i <= temp.TotalCells; i++)
             {
                 int x, y;
                 temp.GetCellAbsolutePosition(i, out x, out y);
                 Console.SetCursorPosition(x, _client.Board.Height - y);
                 Console.Write(" ");
             }
         }
     }
 }
コード例 #4
0
ファイル: ConsoleUI.cs プロジェクト: SinaC/TetriNET
 private void DisplayNextPieceColor()
 {
     lock (_lock)
     {
         // draw next piece
         if (_client.NextPiece != null)
         {
             IPiece temp = _client.NextPiece.Clone();
             int    minX, minY, maxX, maxY;
             temp.GetAbsoluteBoundingRectangle(out minX, out minY, out maxX, out maxY);
             // Move to top, left
             temp.Translate(-minX, 0);
             if (maxY > _client.Board.Height)
             {
                 temp.Translate(0, _client.Board.Height - maxY);
             }
             // Display piece
             Pieces cellPiece = temp.Value;
             Console.BackgroundColor = GetPieceColor(cellPiece);
             for (int i = 1; i <= temp.TotalCells; i++)
             {
                 int x, y;
                 temp.GetCellAbsolutePosition(i, out x, out y);
                 Console.SetCursorPosition(x, _client.Board.Height - y);
                 Console.Write(" ");
             }
         }
     }
 }
コード例 #5
0
ファイル: TestView.xaml.cs プロジェクト: SinaC/TetriNET
        private void DrawPiece(IPiece piece, int size, int topX, int topY)
        {
            IPiece temp      = piece.Clone();
            Pieces cellPiece = temp.Value;

            for (int i = 1; i <= temp.TotalCells; i++)
            {
                int x, y;
                temp.GetCellAbsolutePosition(i, out x, out y); // 1->Width x 1->Height

                Rectangle rectangle = new Rectangle
                {
                    Width  = size,
                    Height = size,
                    Fill   = TextureManager.TextureManager.Instance.GetBigPiece(cellPiece)
                };
                Canvas.Children.Add(rectangle);
                Canvas.SetLeft(rectangle, topX + x * size);
                Canvas.SetTop(rectangle, topY + y * size);
            }

            Rectangle anchor = new Rectangle
            {
                Width  = size,
                Height = size,
                Fill   = PieceAnchorColor
            };

            Canvas.Children.Add(anchor);
            Canvas.SetLeft(anchor, topX);
            Canvas.SetTop(anchor, topY);
        }
コード例 #6
0
ファイル: PlayerGridView.xaml.cs プロジェクト: SinaC/TetriNET
        //public void ToggleDropLocation()
        //{
        //    _displayDropLocation = !_displayDropLocation;
        //    if (_displayDropLocation)
        //        DrawDropLocation();
        //}

        private void DrawPiece(IBoard board, IPiece piece, Brush brush)
        {
            for (int i = 1; i <= piece.TotalCells; i++)
            {
                int x, y;
                piece.GetCellAbsolutePosition(i, out x, out y); // 1->Width x 1->Height
                int cellY = board.Height - y;
                int cellX = x - 1;

                Rectangle uiPart = GetControl(cellX, cellY);
                if (uiPart != null)
                {
                    uiPart.Fill = brush;
                }
            }
            // Draw piece center
            if (ClientOptionsViewModel.Instance.DisplayPieceAnchor && ClientOptionsViewModel.Instance.IsDeveloperModeActivated)
            {
                int cellY = board.Height - piece.PosY;
                int cellX = piece.PosX - 1;

                Rectangle uiPart = GetControl(cellX, cellY);
                if (uiPart != null)
                {
                    uiPart.Fill = PieceAnchorColor;
                }
            }
        }
コード例 #7
0
        private void DrawPiece(Pieces piece)
        {
            Canvas.Children.Clear();

            IPiece temp = Piece.CreatePiece(piece, 0, 0, 1, 0);
            int    minX, minY, maxX, maxY;

            temp.GetAbsoluteBoundingRectangle(out minX, out minY, out maxX, out maxY);
            temp.Translate(-minX, -minY);
            for (int i = 1; i <= temp.TotalCells; i++)
            {
                int x, y;
                temp.GetCellAbsolutePosition(i, out x, out y); // 1->Width x 1->Height

                // TODO: move into screen  posX + x must be == 0  same for y

                Rectangle rectangle = new Rectangle
                {
                    Width  = CellSize,
                    Height = CellSize,
                    Fill   = PieceColor
                };
                Canvas.Children.Add(rectangle);
                Canvas.SetLeft(rectangle, x * (CellSize + 1));
                Canvas.SetTop(rectangle, y * (CellSize + 1));
            }

            Canvas.Width  = (CellSize + 1) * (maxX - minX + 1);
            Canvas.Height = (CellSize + 1) * (maxY - minY + 1);
        }
コード例 #8
0
ファイル: BoardHelper.cs プロジェクト: SinaC/TetriNET
        // The following counts the number of cells (0..4) of a piece that would
        // be eliminated by dropping the piece.
        public static int CountPieceCellsEliminated(IBoard board, IPiece piece, bool alreadyDropped)
        {
            // Copy piece and board so that this measurement is not destructive.
            IBoard copyOfBoard = alreadyDropped ? board : board.Clone();
            IPiece copyOfPiece = alreadyDropped ? piece : piece.Clone();

            if (!alreadyDropped)
            {
                // Drop copy of piece on to the copy of the board
                copyOfBoard.DropAndCommit(copyOfPiece);
            }

            // Scan rows.  For each full row, check all board Y values for the
            // piece.  If any board Y of the piece matches the full row Y,
            // increment the total eliminated cells.
            int countPieceCellsEliminated = 0;

            for (int y = 1; y <= copyOfBoard.Height; y++)
            {
                bool fullRow = true; // hypothesis
                for (int x = 1; x <= copyOfBoard.Width; x++)
                {
                    byte cellValue = copyOfBoard[x, y];
                    if (cellValue == 0)
                    {
                        fullRow = false;
                        break;
                    }
                }

                if (fullRow)
                {
                    // Find any matching board-relative Y values in dropped copy of piece.
                    for (int cellIndex = 1; cellIndex <= piece.TotalCells; cellIndex++)
                    {
                        int boardX;
                        int boardY;
                        copyOfPiece.GetCellAbsolutePosition(cellIndex, out boardX, out boardY);
                        if (boardY == y)
                        {
                            countPieceCellsEliminated++;  // Moohahahaaa!
                        }
                    }
                }
            }

            return(countPieceCellsEliminated);
        }
コード例 #9
0
ファイル: Board.cs プロジェクト: SinaC/TetriNET
 public bool CheckNoConflict(IPiece piece, bool checkTop = false)
 {
     //if (piece.PosX < 1)
     //    return false;
     //if (piece.PosX > Width)
     //    return false;
     //if (piece.PosY < 1)
     //    return false;
     //if (checkTop && piece.PosY > Height)
     //    return false;
     for (int i = 1; i <= piece.TotalCells; i++)
     {
         // Get piece position in board
         int x, y;
         piece.GetCellAbsolutePosition(i, out x, out y);
         // Check out of board
         if (x < 1)
         {
             return(false);
         }
         if (x > Width)
         {
             return(false);
         }
         if (y < 1)
         {
             return(false);
         }
         if (checkTop && y > Height)
         {
             return(false);
         }
         // Check if piece already occupied
         if (this[x, y] != CellHelper.EmptyCell)
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #10
0
ファイル: Board.cs プロジェクト: SinaC/TetriNET2
 public void CommitPiece(IPiece piece)
 {
     //if (piece.PosX < 1 || piece.PosX > Width)
     //    return;
     //if (piece.PosY < 1 || piece.PosY > Height)
     //    return;
     for (int i = 1; i <= piece.TotalCells; i++)
     {
         // Get piece position in board
         int x, y;
         piece.GetCellAbsolutePosition(i, out x, out y);
         // Check out of board
         if (x < 1)
             return;
         if (x > Width)
             return;
         if (y < 1)
             return;
         if (y > Height)
             return;
         // Add piece in board
         this[x, y] = CellHelper.SetColor(piece.Value); // indexer will handle cell out of board
     }
 }
コード例 #11
0
ファイル: Board.cs プロジェクト: SinaC/TetriNET2
 public bool CheckNoConflict(IPiece piece, bool checkTop = false)
 {
     //if (piece.PosX < 1)
     //    return false;
     //if (piece.PosX > Width)
     //    return false;
     //if (piece.PosY < 1)
     //    return false;
     //if (checkTop && piece.PosY > Height)
     //    return false;
     for (int i = 1; i <= piece.TotalCells; i++)
     {
         // Get piece position in board
         int x, y;
         piece.GetCellAbsolutePosition(i, out x, out y);
         // Check out of board
         if (x < 1)
             return false;
         if (x > Width)
             return false;
         if (y < 1)
             return false;
         if (checkTop && y > Height)
             return false;
         // Check if piece already occupied
         if (this[x, y] != CellHelper.EmptyCell)
             return false;
     }
     return true;
 }
コード例 #12
0
        public bool GetBestMove(IBoard board, IPiece current, IPiece next, out int bestRotationDelta, out int bestTranslationDelta, out bool rotationBeforeTranslation)
        {
            int    currentBestTranslationDelta = 0;
            int    currentBestRotationDelta    = 0;
            double currentBestRating           = -1.0e+20; // Really bad!
            int    currentBestPriority         = 0;

            //current.Translate(0, -1);

            IBoard tempBoard = board.Clone();
            IPiece tempPiece = current.Clone();

            // Consider all possible rotations
            for (int trialRotationDelta = 0; trialRotationDelta < current.MaxOrientations; trialRotationDelta++)
            {
                // Copy piece
                tempPiece.CopyFrom(current);
                // Rotate
                tempPiece.Rotate(trialRotationDelta);

                // Get translation range
                bool isMovePossible;
                int  minDeltaX;
                int  maxDeltaX;
                BoardHelper.GetAccessibleTranslationsForOrientation(board, tempPiece, out isMovePossible, out minDeltaX, out maxDeltaX);

                StringBuilder sb = new StringBuilder();
                for (int i = 1; i <= tempPiece.TotalCells; i++)
                {
                    int x, y;
                    tempPiece.GetCellAbsolutePosition(i, out x, out y);
                    sb.Append(String.Format("[{0}->{1},{2}]", i, x - tempPiece.PosX, y - tempPiece.PosY));
                }
                //Log.Log.Default.WriteLine("{0} {1} -> {2}  {3}", trialRotationDelta, minDeltaX, maxDeltaX, sb.ToString());
                if (isMovePossible)
                {
                    // Consider all allowed translations
                    for (int trialTranslationDelta = minDeltaX; trialTranslationDelta <= maxDeltaX; trialTranslationDelta++)
                    {
                        // Evaluate this move

                        // Copy piece
                        tempPiece.CopyFrom(current);
                        // Rotate
                        tempPiece.Rotate(trialRotationDelta);
                        // Translate
                        tempPiece.Translate(trialTranslationDelta, 0);

                        // Check if move is acceptable
                        if (board.CheckNoConflict(tempPiece))
                        {
                            // Copy board
                            tempBoard.CopyFrom(board);
                            // Drop piece
                            tempBoard.DropAndCommit(tempPiece);

                            // Evaluate
                            double trialRating;
                            int    trialPriority;
                            EvaluteMove(tempBoard, tempPiece, out trialRating, out trialPriority);

                            //Log.Log.Default.WriteLine("R:{0:0.0000} P:{1} R:{2} T:{3}", trialRating, trialPriority, trialRotationDelta, trialTranslationDelta);

                            // Check if better than previous best
                            if (trialRating > currentBestRating || (Math.Abs(trialRating - currentBestRating) < 0.0001 && trialPriority > currentBestPriority))
                            {
                                currentBestRating           = trialRating;
                                currentBestPriority         = trialPriority;
                                currentBestTranslationDelta = trialTranslationDelta;
                                currentBestRotationDelta    = trialRotationDelta;
                            }
                        }
                    }
                }
            }

            // commit to this move
            rotationBeforeTranslation = true;
            bestTranslationDelta      = currentBestTranslationDelta;
            bestRotationDelta         = currentBestRotationDelta;

            //Console.SetCursorPosition(0, _client.Board.Height+1);
            // Console.WriteLine("{0} {1} {2:0.000} {3}", bestRotationDelta, bestTranslationDelta, currentBestRating, currentBestPriority);

            return(true);
        }