Ejemplo n.º 1
0
        public void FindingFreeParametersShouldWork()
        {
            var l = new Line(new Point(0, 0, false), new Point(1, 1, false, true));

            var a = l.Where(p => p.free == true).ToArray();

            a.Length.Should().Be(1);
            a[0].GetHashCode().Should().Be(l.p2.y.GetHashCode());


        }
Ejemplo n.º 2
0
        private static async Task MainLoop( ConsoleColor bgc, ConsoleColor ec, ConsoleColor mc, int delay)
        {
            var r = new Random();
            int w = Console.WindowWidth, h = Console.WindowHeight;
            var chars = new Rc[h][];
            for ( int i = 0; i < h; i++ ) chars[ i ] = new Rc[w];
            for ( int i = 0; i < h; i++ ) {
                var c = chars[ i ];
                for ( int j = 0; j < w; j++ )
                    c[ j ] = new Rc { Char = (char) r.Next( 34, 127 ), Color = bgc };
            }
            //lines
            var lines = new Line[w];
            for ( int i = 0; i < w; i++ ) {
                var l = new Line { Height = r.Next( 4, h ) };
                l.Position = -h + r.Next( 0, h - l.Height + 1 );
                l.Speed = r.Next( 1, 4 );
                lines[ i ] = l;
            }
            var tid = 0UL;
            while ( true ) {
                var del = Task.Delay( delay );
                if ( Console.WindowHeight!=h || Console.WindowWidth!=w) {
                    return;
                }
                ++tid;
                //move
                var tid1 = tid;
                foreach (var line in lines.Where(line => tid1 % (ulong) line.Speed == 0UL))
                    line.Position = line.Position < h ? line.Position + 1 : -line.Height;

                //update chars
                for (var i = 0; i < lines.Length; i++ ) {
                    var line = lines[ i ];

                    var top = line.Position;
                    var bottom = top + line.Height - 1;

                    var topU = top < h;
                    var topD = top >= 0;
                    var topV = topU && topD;
                    var bottomU = bottom >= 0;
                    var bottomD = bottom < h;
                    var bottomV = bottomU && bottomD;
                    if ( bottomV || topV ) { //visible
                        if ( bottomV ) chars[ bottom ][ i ].Color = ec;
                        if ( topV ) chars[ top ][ i ].Color = ec;

                        var ms = Math.Max( 0, Math.Min( h, top+1 ) );
                        var me = Math.Max( 0, Math.Min( h, bottom ) );
                        for (var j = ms; j < me; j++ ) chars[ j ][ i ].Color = mc;

                        if ( topV )
                            for (var j = 0; j < top - 1; j++ )
                                chars[ j ][ i ].Color = bgc;
                        if (!bottomV) continue;
                        {
                            for ( var j = bottom+1; j < h; j++ )
                                chars[ j ][ i ].Color = bgc;
                        }
                    }
                    else
                        for (var j = 0; j < h; j++ )
                            chars[ j ][ i ].Color = bgc;
                }

                //render
                var cc = Console.ForegroundColor;
                int flushes = 0,
                    cf = 0,
                    pf = 0,
                    wf = 0;
                for ( var i = 0; i < h; i++ )
                {
                    var c = chars[ i ];
                    var pc = false;
                    for ( var j = 0; j < w; j++ ) {
                        var cur = c[ j ];
                        if ( cur.Changed && !(j==w-1&&i==h-1))
                        {
                            if ( !pc ) {
                                if ( !SetCursorPosition( j, i ) ) return;
                                flushes++;
                                pf++;
                            }
                            pc = true;
                            if (cc != cur.Color)
                            {
                                Console.ForegroundColor = cur.Color;
                                cc = cur.Color;
                                flushes++;
                                cf++;
                            }
                            {
                                Console.Write( cur.Char );
                                flushes++;
                                wf++;
                            }
                            cur.Changed = false;
                        }
                        else pc = false;
                    }
                    
                }
                await del.ConfigureAwait( false );
            }
        }