示例#1
0
文件: Engine.cs 项目: ao1415/Othello
        public async Task <char> GameTaskAsync(CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return(Config.Draw);
            }

            string size = Config.Size.ToString();
            string time = lifeTime.ToString();

            bool check = true;

            check &= InitProcess();

            player01.name = await ReadlineAsync(player01);

            player02.name = await ReadlineAsync(player02);

            replayLog += player01.name + Environment.NewLine;
            replayLog += player02.name + Environment.NewLine;
            replayLog += lifeTime + Environment.NewLine;

            check &= CheckPlayerName();

            char result = Config.Draw;

            if (check)
            {
                Writeline(player01, size);
                Writeline(player01, time);
                Writeline(player01, Config.Black.ToString());

                Writeline(player02, size);
                Writeline(player02, time);
                Writeline(player02, Config.White.ToString());

                Stopwatch player01Timer = new Stopwatch();
                Stopwatch player02Timer = new Stopwatch();

                GameEngine gameEngine = new GameEngine();

                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        result = Config.Draw;
                        break;
                    }

                    var nextPlayer = gameEngine.NextColor;
                    var nextTable  = gameEngine.NextTable;

                    string command;
                    if (nextPlayer == Config.Black)
                    {
                        player01Timer.Start();

                        long timer = lifeTime - player01Timer.ElapsedMilliseconds;

                        //入力
                        Writeline(player01, timer.ToString());
                        foreach (var str in nextTable)
                        {
                            Writeline(player01, str);
                        }
                        Writeline(player01, Config.End);

                        //出力
                        command = await ReadlineAsync(player01);

                        player01Timer.Stop();

                        replayLog += command + " " + (lifeTime - player01Timer.ElapsedMilliseconds).ToString() + Environment.NewLine;

                        if (player01Timer.ElapsedMilliseconds > lifeTime)
                        {
                            result = Config.White;
                            break;
                        }
                    }
                    else if (nextPlayer == Config.White)
                    {
                        player02Timer.Start();

                        long timer = lifeTime - player02Timer.ElapsedMilliseconds;

                        //入力
                        Writeline(player02, timer.ToString());
                        foreach (var str in nextTable)
                        {
                            Writeline(player02, str);
                        }
                        Writeline(player02, Config.End);

                        //出力
                        command = await ReadlineAsync(player02);

                        player02Timer.Stop();

                        replayLog += command + " " + (lifeTime - player02Timer.ElapsedMilliseconds).ToString() + Environment.NewLine;

                        if (player02Timer.ElapsedMilliseconds > lifeTime)
                        {
                            result = Config.Black;
                            break;
                        }
                    }
                    else
                    {
                        result = gameEngine.Winner;
                        break;
                    }

                    if (gameEngine.CheckPosition(command, nextPlayer))
                    {
                        gameEngine.Put(command, nextPlayer);
                    }
                    else
                    {
                        if (nextPlayer == Config.Black)
                        {
                            result = Config.White;
                            break;
                        }
                        else if (nextPlayer == Config.White)
                        {
                            result = Config.Black;
                            break;
                        }
                    }
                }

                FileWrite(result);
            }

            return(result);
        }
示例#2
0
        private void MakeReplayData(string filename)
        {
            string[] replayFileLines = File.ReadAllLines(filename);

            name01 = replayFileLines[0];
            name02 = replayFileLines[1];
            string LifeTime = replayFileLines[2];

            GameEngine gameEngine = new GameEngine();

            int  turn   = 3;
            char result = Config.Draw;

            while (true)
            {
                var nextPlayer = gameEngine.NextColor;
                var nextTable  = gameEngine.NextTable;

                if (nextPlayer == Config.Draw)
                {
                    result = gameEngine.Winner;
                    break;
                }

                var coms = replayFileLines[turn].Split(' ');
                turn++;

                string command = coms[0] + " " + coms[1];

                Data data = new Data();

                data.turn  = nextPlayer;
                data.table = (char[, ])gameEngine.NextTableChar.Clone();

                int blackCount = 0;
                int whiteCount = 0;
                for (int y = 0; y < Config.Size; y++)
                {
                    for (int x = 0; x < Config.Size; x++)
                    {
                        if (data.table[y, x] == Config.Black)
                        {
                            blackCount++;
                        }
                        else if (data.table[y, x] == Config.White)
                        {
                            whiteCount++;
                        }
                    }
                }

                data.pos.X      = int.Parse(coms[0]);
                data.pos.Y      = int.Parse(coms[1]);
                data.blackCount = blackCount;
                data.whiteCount = whiteCount;

                int time = int.Parse(coms[2]);
                if (nextPlayer == Config.Black)
                {
                    data.blackTimer = coms[2];

                    if (replayDatas.Count == 0)
                    {
                        data.whiteTimer = LifeTime;
                    }
                    else
                    {
                        data.whiteTimer = replayDatas.Last().whiteTimer;
                    }

                    if (time < 0)
                    {
                        result = Config.White;
                        break;
                    }
                }
                else if (nextPlayer == Config.White)
                {
                    data.whiteTimer = coms[2];
                    data.blackTimer = replayDatas.Last().blackTimer;

                    if (time < 0)
                    {
                        result = Config.Black;
                        break;
                    }
                }

                replayDatas.Add(data);

                if (gameEngine.CheckPosition(command, nextPlayer))
                {
                    gameEngine.Put(command, nextPlayer);
                }
                else
                {
                    if (nextPlayer == Config.Black)
                    {
                        result = Config.White;
                        break;
                    }
                    else if (nextPlayer == Config.White)
                    {
                        result = Config.Black;
                        break;
                    }
                }
            }

            Data endData = new Data();

            endData.table = gameEngine.NextTableChar;

            int endBlackCount = 0;
            int endWhiteCount = 0;

            for (int y = 0; y < Config.Size; y++)
            {
                for (int x = 0; x < Config.Size; x++)
                {
                    if (endData.table[y, x] == Config.Black)
                    {
                        endBlackCount++;
                    }
                    else if (endData.table[y, x] == Config.White)
                    {
                        endWhiteCount++;
                    }
                }
            }

            endData.blackCount = endBlackCount;
            endData.whiteCount = endWhiteCount;

            endData.blackTimer = replayDatas.Last().blackTimer;
            endData.whiteTimer = replayDatas.Last().whiteTimer;

            replayDatas.Add(endData);
        }