Example #1
0
        protected void SetupPlayerSort()
        {
            var playerIds         = Enumerable.Range(0, Settings.Players);
            var shuffledPlayerIds = playerIds.OrderBy(x => rand.Next()).ToList();

            if ((Settings.FixedPixels.Count > 0) && (Settings.PlayerSort != PlayerSort.Random))
            {
                logger.Warn("Fixed pixels are only used when player sort is random");
            }

            if (Settings.PlayerSort == PlayerSort.Random)
            {
                int pixelIndex = 0, playerIndex = 0, currentButton = 1;

                // Go through the list of pixels from left to right, top to bottom
                // and assign players in semi-random order (grouped).
                while (pixelIndex < pixels.Count)
                {
                    var currentPixel = pixels[pixelIndex];
                    pixelIndex++;

                    // Skip fixed pixels
                    if (Settings.FixedPixels.Any(p => (p.X == currentPixel.TileLeft) &&
                                                 (p.Y == currentPixel.TileTop)))
                    {
                        currentPixel.Red   = 0;
                        currentPixel.Green = 0;
                        currentPixel.Blue  = 0.8;
                        currentPixel.State = PixelState.Fixed;
                        continue;
                    }

                    currentPixel.PlayerId = shuffledPlayerIds[playerIndex];
                    currentPixel.Button   = currentButton;

                    if (++playerIndex >= shuffledPlayerIds.Count)
                    {
                        // Reshuffle player order and continue to next button
                        playerIndex       = 0;
                        shuffledPlayerIds = playerIds.OrderBy(x => rand.Next()).ToList();
                        currentButton++;
                    }
                }
            }
            else if (Settings.PlayerSort == PlayerSort.Diffuse)
            {
                // Make a kinda-latin square
                int startingPlayerId = 0;

                for (int tileTop = 0; tileTop < tiles; tileTop++)
                {
                    var playerId = startingPlayerId;

                    // Repeat this row's ordering
                    for (int tileLeft = 0; tileLeft < tiles; tileLeft++)
                    {
                        var pixelIndex   = (tileTop * tiles) + tileLeft;
                        var currentPixel = pixels[pixelIndex];

                        currentPixel.PlayerId = playerId;
                        playerId = (playerId + 1) % Settings.Players;
                    }

                    // Shift the starting player id forward by two for each row
                    startingPlayerId = (startingPlayerId + 2) % Settings.Players;
                }

                // Assign buttons from left to right, top to bottom
                var playerButtons = Enumerable.Range(0, Settings.Players)
                                    .Select(i => 1)
                                    .ToList();

                foreach (var pixel in pixels)
                {
                    pixel.Button = playerButtons[pixel.PlayerId]++;
                }
            }
            else if (Settings.PlayerSort == PlayerSort.Clustered)
            {
                var board = ClusteredBoard.Create(Settings.Players, tiles);

                // Assign buttons from left to right, top to bottom
                var playerButtons = Enumerable.Range(0, Settings.Players)
                                    .Select(i => 1)
                                    .ToList();

                foreach (var pixel in pixels)
                {
                    pixel.PlayerId = (int)board[pixel.TileLeft, pixel.TileTop];
                    pixel.Button   = playerButtons[pixel.PlayerId]++;
                }
            }
        }
Example #2
0
        public static void Main(string[] args)
        {
            if (!log4net.LogManager.GetRepository().Configured)
            {
                log4net.Config.BasicConfigurator.Configure();
            }

            log4net.ILog logger = log4net.LogManager.GetLogger("Pixel.Program");

            logger.Info("Starting up Pixel");

            var settings = new PixelSettings()
            {
                IsDebugMode  = false,
                ScreenWidth  = DisplayDevice.Default.Width,
                ScreenHeight = DisplayDevice.Default.Height,
                Fullscreen   = false,
                MaxSize      = 0, Players = 0, Port = 0, PlayerSort = PlayerSort.Random
            };

            logger.Debug("Parsing command-line options");

            // Parse command-line options
            bool showHelp = false, doClusterTest = false;

            var options = new OptionSet()
            {
                { "h|?|help", "Show this help message",
                  v => showHelp = !string.IsNullOrEmpty(v) },

                { "debug", "Enable debug mode (random commands can be issued with 'R')",
                  v => settings.IsDebugMode = !string.IsNullOrEmpty(v) },

                { "screen-width=", "Screen width in pixels (default: current)",
                  v => settings.ScreenWidth = Convert.ToInt32(v) },

                { "screen-height=", "Screen heigh in pixels (default: current)",
                  v => settings.ScreenHeight = Convert.ToInt32(v) },

                { "full-screen", "Enables full-screen mode",
                  v => settings.Fullscreen = !string.IsNullOrEmpty(v) },

                { "max-size=", "Maximum number of tiles in a row or column",
                  v => settings.MaxSize = Convert.ToInt32(v) },

                { "players=", "Number of players (required)",
                  v => settings.Players = Convert.ToInt32(v) },

                { "port=", "Network port of input server",
                  v => settings.Port = Convert.ToInt32(v) },

                { "data-file=", "Path to the output data file",
                  v => settings.DataFilePath = v },

                { "player-sort=", "Initial arrangement of players (Random, Clustered, Diffuse)",
                  v => settings.PlayerSort = (PlayerSort)Enum.Parse(typeof(PlayerSort), v) },

                { "initial-state=", "Initial state of pixels (Random, On, Off)",
                  v => settings.InitialState = (InitialState)Enum.Parse(typeof(InitialState), v) },

                { "cluster-test", "Tests the effectiveness of the clustering algorithm",
                  v => doClusterTest = !string.IsNullOrEmpty(v) },

                { "fixed-pixels=", "List of fixed pixels in the format [X1,Y1][X2,Y2]...",
                  v => settings.FixedPixels.AddRange(CLIObject.FromString <FixedPixel>(v, "X", "Y")) },

                { "description=", "Text to display at the top of the screen during gameplay",
                  v => settings.GameDescription = v }
            };

            options.Parse(args);

            if (showHelp)
            {
                options.WriteOptionDescriptions(Console.Out);
                return;
            }

            if (doClusterTest)
            {
                for (int players = 2; players <= settings.Players; players++)
                {
                    var tiles     = (int)Math.Floor(Math.Sqrt(players * PixelSettings.PlayerButtonCount));
                    var testBoard = ClusteredBoard.Create(players, tiles);

                    logger.InfoFormat("{0} player(s): {1} stragglers", players,
                                      ClusteredBoard.GetStragglerCount(ref testBoard));
                }

                return;
            }

            if (settings.Players < 1)
            {
                logger.Fatal("players option is required");
                return;
            }

            if (settings.IsDebugMode)
            {
                logger.Info("Debug mode is enabled");
            }

            using (var game = new PixelWindow(settings))
            {
                // Start game
                logger.Debug("Running game loop");
                game.Run(0, 0);
            }
        }