예제 #1
0
    public void V256_ShiftRight()
    {
        Assert.AreEqual(
            new V256(
                247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247,
                247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 7),
            V256.FromByte(254) >> 5);

        V256 x = new V256(
            255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240,
            239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224);

        Assert.AreEqual(
            "F7 EF E7 DF D7 CF C7 BF B7 AF A7 9F 97 8F 87 7F 77 6F 67 5F 57 4F 47 3F 37 2F 27 1F 17 0F 07 07",
            (x >> 5).ToString());

        V256 y = new V256(
            0xF1, 0x18, 0x80, 0x01, 0x18, 0x80, 0x01, 0x18,
            0x80, 0x01, 0x18, 0x80, 0x01, 0x18, 0x80, 0x01,
            0x18, 0x80, 0x01, 0x18, 0x80, 0x01, 0x18, 0x80,
            0x01, 0x18, 0x80, 0x01, 0x18, 0x80, 0xFF, 0x0F);

        Assert.AreEqual(
            "01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 F8 FF 00 00",
            (y >> 12).ToString());
    }
예제 #2
0
    public void V256_ShiftLeft()
    {
        Assert.AreEqual(
            new V256(
                192, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223,
                223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223),
            V256.FromByte(254) << 5);

        V256 x = new V256(
            255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240,
            239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224);

        Assert.AreEqual(
            "E0 DF BF 9F 7F 5F 3F 1F FF DE BE 9E 7E 5E 3E 1E FE DD BD 9D 7D 5D 3D 1D FD DC BC 9C 7C 5C 3C 1C",
            (x << 5).ToString());


        V256 y = new V256(
            0xF1, 0x18, 0x80, 0x01, 0x18, 0x80, 0x01, 0x18,
            0x80, 0x01, 0x18, 0x80, 0x01, 0x18, 0x80, 0x01,
            0x18, 0x80, 0x01, 0x18, 0x80, 0x01, 0x18, 0x80,
            0x01, 0x18, 0x80, 0x01, 0x18, 0x80, 0xFF, 0x0F);

        Assert.AreEqual(
            "00 10 8F 01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 18 80 01 F8",
            (y << 12).ToString());
    }
예제 #3
0
파일: Tetris.cs 프로젝트: lightln2/Tetriss
    public static void RemoveFullLines(ref V256 board, ref int score)
    {
        // fast check if board does not have full lines
        V256 test = board;

        test &= (test << 5);
        test &= (test << 2);
        test &= (test << 1);
        test &= (test << 1);
        // now each eleventh bit of 12-bit line is set iff line is full
        if ((test & checkLineMask) == 0)
        {
            return;
        }

        int  scoreBoost    = 1;
        V256 lineMask      = fullLine >> width;
        V256 underLineMask = fullLine;

        while (lineMask != 0)
        {
            while ((board & lineMask) == lineMask)
            {
                score       += scoreBoost;
                scoreBoost <<= 1;
                // remove line from the board OR-ing lower part of board (under the line) and shifted upper part of the board
                board = (board & underLineMask) | ((board & ~underLineMask & ~lineMask) << width) | emptyLine;
            }
            underLineMask |= lineMask;
            lineMask     >>= width;
        }
    }
예제 #4
0
    public void Tetris_NoLinesToRemoveOEmptyBoard()
    {
        V256 board = Tetris.board;
        int  score = 0;

        Tetris.RemoveFullLines(ref board, ref score);
        Assert.AreEqual(0, score);
    }
예제 #5
0
 public void V256_Shuffle() => Assert.AreEqual(
     new V256(
         0, 1, 8, 4, 0, 1, 8, 4, 0, 1, 8, 4, 0, 1, 8, 4,
         0, 17, 24, 20, 0, 17, 24, 20, 0, 17, 24, 20, 0, 17, 24, 20),
     new V256(
         1, 2, 3, 4, 5, 6, 7, 8,
         9, 10, 11, 12, 13, 14, 15, 16,
         17, 18, 19, 20, 21, 22, 23, 24,
         25, 26, 27, 28, 29, 30, 31, 32).Shuffle(V256.FromUInt(0x030700FF)));
예제 #6
0
파일: Tetris.cs 프로젝트: lightln2/Tetriss
    static string GetLine(V256 field)
    {
        // convert lowest 12 bits of line into 12 bytes: bit 0 -> empty tile; bit 1 -> filled tile
        V256 line = field.Shuffle(lineToStringShuffler) & lineToStringMask;

        line = line.Min(V256.ONE);
        line = tile.Shuffle(line);
        return(Encoding.ASCII.GetString(line.ToArray <byte>(), 0, width));
    }
예제 #7
0
    static void Solve(V256 board, int step, int score)
    {
        processedMoves++;
        if (processedMoves % 1_000_000_000 == 0)
        {
            double millionMovesPerSecond = processedMoves / timer.Elapsed.TotalSeconds / 1000000;
            Console.WriteLine($"Processed {processedMoves:N0} moves at {timer.Elapsed} ({millionMovesPerSecond:0.00} M moves/second)");
        }
        if (step > maxStepFound)
        {
            maxStepFound = step;
            Console.WriteLine($"new max depth={maxStepFound} after processing {processedMoves:N0} moves at {timer.Elapsed}");
        }
        if (step >= pieceOrder.Length)
        {
            return;
        }

        int pieceIndex = pieceOrder[step];

        V256[] currentPiece = Tetris.PIECES[pieceIndex];

        movesLength += 2;
        for (int rotationIndex = 0; rotationIndex < currentPiece.Length; rotationIndex++)
        {
            V256 piece = currentPiece[rotationIndex];
            moves[movesLength - 2] = rotationIndex;
            if ((piece & board) != 0)
            {
                break;                       // can't rotate anymore
            }
            moves[movesLength - 1] = 0;
            Solve(board, piece, step, score);

            for (int i = 1; i <= 5; i++)
            {
                moves[movesLength - 1] = i;
                if (((piece << i) & board) != 0)
                {
                    break;                              // can't move anymore
                }
                Solve(board, piece << i, step, score);
            }
            for (int i = 1; i <= 5; i++)
            {
                moves[movesLength - 1] = -i;
                if (((piece >> i) & board) != 0)
                {
                    break;                              // can't move anymore
                }
                Solve(board, piece >> i, step, score);
            }
        }
        movesLength -= 2;
    }
예제 #8
0
 static void Solve(V256 board, V256 piece, int step, int score)
 {
     while ((board & (piece << Tetris.width)) == 0)
     {
         piece <<= Tetris.width;
     }
     board |= piece;
     Tetris.RemoveFullLines(ref board, ref score);
     if (score > maxScoreFound)
     {
         maxScoreFound = score;
         Console.WriteLine($"new max score={maxScoreFound} at depth {maxStepFound} after processing {processedMoves:N0} moves at {timer.Elapsed}");
         Console.WriteLine($"  {GetMovesAsString()}");
     }
     Solve(board, step + 1, score);
 }
예제 #9
0
파일: Tetris.cs 프로젝트: lightln2/Tetriss
    static void DisplayField()
    {
        Console.BackgroundColor = ConsoleColor.Blue;
        Console.ForegroundColor = ConsoleColor.Gray;
        DisplaySystemInfo();
        Console.SetCursorPosition(0, 2);
        Console.ForegroundColor = ConsoleColor.White;

        V256 field = board | FallingPiece;

        while (field != 0)
        {
            Console.WriteLine("   " + GetLine(field));
            field >>= width;
        }
        Console.WriteLine($"\n\n   Score: {score}\n");
    }
예제 #10
0
    public void Tetris_RemoveOneLine()
    {
        V256 board = Tetris.board;
        V256 piece = Tetris.PIECES[0][0];

        piece >>= 3;
        while ((board & (piece << Tetris.width)) == 0)
        {
            piece <<= Tetris.width;
        }
        board  |= piece;
        piece   = Tetris.PIECES[0][0];
        piece <<= 3;
        while ((board & (piece << Tetris.width)) == 0)
        {
            piece <<= Tetris.width;
        }
        board  |= piece;
        piece   = Tetris.PIECES[0][1];
        piece >>= 1;
        while ((board & (piece << Tetris.width)) == 0)
        {
            piece <<= Tetris.width;
        }
        board |= piece;
        piece  = Tetris.PIECES[0][1];
        while ((board & (piece << Tetris.width)) == 0)
        {
            piece <<= Tetris.width;
        }
        board |= piece;

        int score = 0;

        Tetris.RemoveFullLines(ref board, ref score);
        Assert.AreEqual(1, score);
    }
예제 #11
0
파일: Tetris.cs 프로젝트: lightln2/Tetriss
 static bool Intersects(V256 piece) => (piece & board) != 0;
예제 #12
0
파일: Tetris.cs 프로젝트: lightln2/Tetriss
 static void AddPieceToBoard() => board |= FallingPiece;