Example #1
0
        public void PlayGame(DebugBreak debugBreak)
        {
            _playerCount = _consoles.Length;
            // initialize the player consoles
            _processes          = new Process[_playerCount];
            _errorStreamThreads = new Thread[_playerCount];
            try
            {
                for (int i = 0; i < _playerCount; i++)
                {
                    Process p = CreatePlayerProcess(_consoles[i]);
                    _processes[i] = p;
                    p.Start();
                    _errorStreamThreads[i] = ErrorStream(p, i);
                }

                if (debugBreak != null)
                {
                    debugBreak();
                }

                // create the player structure
                InitReferee(_playerCount);

                // send initialization data to the player consolose
                for (int i = 0; i < _playerCount; i++)
                {
                    string[] initLines = GetInitInputForPlayer(i);
                    for (int j = 0; j < initLines.Length; j++)
                    {
                        WriteLine(_processes[i], initLines[j]);
                    }
                }
                try
                {
                    int turnCounter  = 0;
                    int frameCounter = 0;
                    AddFrame();
                    while (turnCounter < Settings.MaxRounds)
                    {
                        string[] playerResponse = new string[_playerCount];
                        for (int i = 0; i < _playerCount; i++)
                        {
                            // initialize the turn
                            string[] turnLines = GetInitInputForPlayer(turnCounter, i);
                            for (int j = 0; j < turnLines.Length; j++)
                            {
                                WriteLine(_processes[i], turnLines[j]);
                            }
                            // and wait for the player to respond

                            int?timeout = Settings.UseTimeOut ? (turnCounter == 0 ? Settings.FirstTimeout : Settings.Timeout) : (int?)null;
                            playerResponse[i] = ReadLine(_processes[i], timeout, i);
                        }
                        // process the player responses

                        for (int i = 0; i < _playerCount; i++)
                        {
                            HandlePlayerOutput(frameCounter++, turnCounter, i, new string[] { playerResponse[i] });
                        }
                        UpdateGame(turnCounter);
                        turnCounter++;
                        AddFrame();

                        Console.WriteLine(turnCounter);
                    }
                    AddFinishFrame();
                }
                catch (Exception ex)
                {
                    if (ex is GameOverException)
                    {
                        var goe = (GameOverException)ex;
                    }
                    else if (ex is InvalidInputException)
                    {
                        var iie = (InvalidInputException)ex;
                    }
                    else if (ex is LostException)
                    {
                        var le = (LostException)ex;
                    }
                    else
                    {
                    }
                    AddFinishFrame();
                }
            }
            finally
            {
                for (int i = 0; i < _playerCount; i++)
                {
                    try
                    {
                        _processes[i].Kill();
                        _errorStreamThreads[i].Abort();
                    }
                    catch
                    { }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Scans a literal string, contained between "(" and ")".
        /// </summary>
        public Symbol ScanLiteralString()
        {
            // Reference: 3.2.3  String Objects / Page 53
            // Reference: TABLE 3.32  String Types / Page 157

            Debug.Assert(_currChar == Chars.ParenLeft);
            _token = new StringBuilder();
            int  parenLevel = 0;
            char ch         = ScanNextChar(false);

            // Phase 1: deal with escape characters.
            while (ch != Chars.EOF)
            {
                switch (ch)
                {
                case '(':
                    parenLevel++;
                    break;

                case ')':
                    if (parenLevel == 0)
                    {
                        ScanNextChar(false);
                        // Is goto evil? We could move Phase 2 code here or create a subroutine for Phase 1.
                        goto Phase2;
                    }
                    parenLevel--;
                    break;

                case '\\':
                {
                    ch = ScanNextChar(false);
                    switch (ch)
                    {
                    case 'n':
                        ch = Chars.LF;
                        break;

                    case 'r':
                        ch = Chars.CR;
                        break;

                    case 't':
                        ch = Chars.HT;
                        break;

                    case 'b':
                        ch = Chars.BS;
                        break;

                    case 'f':
                        ch = Chars.FF;
                        break;

                    case '(':
                        ch = Chars.ParenLeft;
                        break;

                    case ')':
                        ch = Chars.ParenRight;
                        break;

                    case '\\':
                        ch = Chars.BackSlash;
                        break;

                    // AutoCAD PDFs my contain such strings: (\ )
                    case ' ':
                        ch = ' ';
                        break;

                    case Chars.CR:
                    case Chars.LF:
                        ch = ScanNextChar(false);
                        continue;

                    default:
                        if (char.IsDigit(ch))              // First octal character.
                        {
                            // Octal character code.
                            if (ch >= '8')
                            {
                                ParserDiagnostics.HandleUnexpectedCharacter(ch);
                            }

                            int n = ch - '0';
                            if (char.IsDigit(_nextChar))              // Second octal character.
                            {
                                ch = ScanNextChar(false);
                                if (ch >= '8')
                                {
                                    ParserDiagnostics.HandleUnexpectedCharacter(ch);
                                }

                                n = n * 8 + ch - '0';
                                if (char.IsDigit(_nextChar))              // Third octal character.
                                {
                                    ch = ScanNextChar(false);
                                    if (ch >= '8')
                                    {
                                        ParserDiagnostics.HandleUnexpectedCharacter(ch);
                                    }

                                    n = n * 8 + ch - '0';
                                }
                            }
                            ch = (char)n;
                        }
                        else
                        {
                            //TODO
                            // Debug.As sert(false, "Not implemented; unknown escape character.");
                            ParserDiagnostics.HandleUnexpectedCharacter(ch);
                        }
                        break;
                    }
                    break;
                }

                default:
                    break;
                }

                _token.Append(ch);
                ch = ScanNextChar(false);
            }

            // Phase 2: deal with UTF-16BE if necessary.
            // UTF-16BE Unicode strings start with U+FEFF ("þÿ"). There can be empty strings with UTF-16BE prefix.
Phase2:
            if (_token.Length >= 2 && _token[0] == '\xFE' && _token[1] == '\xFF')
            {
                // Combine two ANSI characters to get one Unicode character.
                StringBuilder temp   = _token;
                int           length = temp.Length;
                if ((length & 1) == 1)
                {
                    // TODO What does the PDF Reference say about this case? Assume (char)0 or treat the file as corrupted?
                    temp.Append(0);
                    ++length;
                    DebugBreak.Break();
                }
                _token = new StringBuilder();
                for (int i = 2; i < length; i += 2)
                {
                    _token.Append((char)(256 * temp[i] + temp[i + 1]));
                }
                return(_symbol = Symbol.UnicodeString);
            }
            // Adobe Reader also supports UTF-16LE.
            if (_token.Length >= 2 && _token[0] == '\xFF' && _token[1] == '\xFE')
            {
                // Combine two ANSI characters to get one Unicode character.
                StringBuilder temp   = _token;
                int           length = temp.Length;
                if ((length & 1) == 1)
                {
                    // TODO What does the PDF Reference say about this case? Assume (char)0 or treat the file as corrupted?
                    temp.Append(0);
                    ++length;
                    DebugBreak.Break();
                }
                _token = new StringBuilder();
                for (int i = 2; i < length; i += 2)
                {
                    _token.Append((char)(256 * temp[i + 1] + temp[i]));
                }
                return(_symbol = Symbol.UnicodeString);
            }
            return(_symbol = Symbol.String);
        }
Example #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            _outputEvents = new ConcurrentQueue <ConsoleOutputEventArgs>();
            string filePath = textBox1.Text;

            if (!File.Exists(filePath))
            {
                MessageBox.Show("Player program not found");
                return;
            }


            string opponentAI = ConfigurationManager.AppSettings[$"AI{cbLevel.SelectedIndex}"];

            if (!File.Exists(opponentAI))
            {
                MessageBox.Show("Oponent program not found");
                return;
            }


            string[] playerNames = new string[2];
            string[] players     = new string[2];
            if (rbtnBlue.Checked)
            {
                players[0]     = filePath;
                playerNames[0] = "User application";
                players[1]     = opponentAI;
                playerNames[1] = "Boss program";
            }
            else
            {
                players[1]     = filePath;
                playerNames[1] = "User application";
                players[0]     = opponentAI;
                playerNames[0] = "Boss program";
            }

            int index = Leage.SelectedIndex;

            if (index == 2)
            {
                index = 3;
            }

            HackathonWork.Settings.SetLeageLevel(index);

            Referee referee = new Referee(players);
            int     seed;

            if (int.TryParse(SeedTb.Text, out seed))
            {
                referee.Seed = seed;
            }


            DebugBreak debugMethod = null;

            if (chbxDebug.Checked)
            {
                HackathonWork.Settings.UseTimeOut = false;
                debugMethod = this.Debug;
            }
            else
            {
                HackathonWork.Settings.UseTimeOut = true;
            }
            try
            {
                if (rbtnBlue.Checked)
                {
                    referee.ConsoleErrorOutputPlayer1 += ConsoleErrorOutputPlayer;
                }
                else
                {
                    referee.ConsoleErrorOutputPlayer2 += ConsoleErrorOutputPlayer;
                }
                referee.ConsoleOutputPlayer += ConsoleErrorOutputPlayer;

                referee.PlayGame(debugMethod);
            }
            catch (Exception ex)
            {
                // who throw the exception?
            }

            _frames = referee.GetFrames();


            MatchData md = new MatchData()
            {
                Frames      = _frames,
                PlayerNames = playerNames,
            };

            Viewer v = new Viewer(md);

            v.Show();

            PrintConsoleOUtput();
        }