Exemplo n.º 1
0
        private async void UpdateTask()
        {
            // キャラクタの行動
            var jsonMapData = histories.Last();

            Func <ParallelQuery <ActionData> > asyncJob = () =>
            {
                var tasks = Players.Where(p => p.isAlive).AsParallel().Select(p => p.Action(jsonMapData));
                return(tasks.AsParallel().Where(t => t.Wait((int)(my_timer.Interval * 0.8))).Select(t => t.Result));
            };

            var actions = await Task.Run(asyncJob);

            foreach (var action in actions)
            {
                EvalPutBombAction(action);
                EvalMoveAction(action);
            }

            turn    += 1;
            showTurn = turn;

            // 壁が落ちてくる
            if (turn >= 360)
            {
                int i = turn - 360;
                if (i < Utils.FALLING_WALL.Length)
                {
                    Position p = new Position(Utils.FALLING_WALL[i][0], Utils.FALLING_WALL[i][1]);
                    walls.Add(p);
                    blocks.RemoveAll(block => block.pos.Equals(p));
                    items.RemoveAll(item => item.pos.Equals(p));
                    bombs.RemoveAll(b =>
                    {
                        if (!b.pos.Equals(p))
                        {
                            return(false);
                        }

                        b.owner.setBombCount--;
                        return(true);
                    });
                }
            }

            // if (turn == 600) {
            //     int max = 0;
            //     Player winner = Players.get(0);
            //     for(Player p : Players){
            //         if (max < p.totalSetBombCount) {
            //             max = p.totalSetBombCount;
            //             winner = p;
            //         }
            //     }
            //     final Player winner2 = winner;
            //     Players.forEach(p->{
            //             if (!(p == winner2)){
            //                 p.ch = '墓';
            //                 p.isAlive = false;
            //             }
            //         });
            // }

            foreach (Bomb b in bombs)
            {
                b.timer -= 1;
            }

            // get item
            List <Item> usedItems = new List <Item>();

            foreach (Player p in Players)
            {
                foreach (Item i in items)
                {
                    if (p.pos.Equals(i.pos))
                    {
                        i.Effect(p);
                        usedItems.Add(i);
                    }
                }
            }
            items.RemoveAll(i => usedItems.Contains(i));

            // bomb explosion
            fires = new List <Position>();
            List <Bomb> explodeBombs = new List <Bomb>();

            foreach (Bomb b in bombs)
            {
                if (b.timer <= 0)
                {
                    explodeBombs.Add(b);
                }
            }
            // chaining
            while (explodeBombs.Count() != 0)
            {
                explodeBombs.ForEach(b => b.owner.setBombCount -= 1);
                fires.AddRange(Explodes(explodeBombs));
                bombs.RemoveAll(b => explodeBombs.Contains(b));
                explodeBombs = new List <Bomb>();
                foreach (Bomb b in bombs)
                {
                    foreach (Position p in fires)
                    {
                        if (b.pos.Equals(p))
                        {
                            explodeBombs.Add(b);
                            break;
                        }
                    }
                }
            }
            fires = RemoveDuplicates(fires);

            // item burning
            items.RemoveAll(i => fires.Contains(i.pos));

            // block burning
            var burningBlocks = blocks.Where(b => fires.Contains(b.pos));

            items.AddRange(burningBlocks.Where(b => b.GetItem() != null).Select(b => b.GetItem()));
            blocks.RemoveAll(b => burningBlocks.Contains(b));

            foreach (var p in Players)
            {
                if (!p.isAlive)
                {
                    continue;
                }
                if (fires.Contains(p.pos) || walls.Contains(p.pos))
                {
                    p.ch      = '墓';
                    p.isAlive = false;
                }
            }

            mapData = new MapData(turn, walls, blocks, Players, bombs, items, fires);
            var mapJson = Utils.ObjectToJson(mapData);

            histories.Add(mapJson);

            ShowMap(mapData, mapJson);

            var living = Players.Where(p => p.isAlive);

            if (living.Count() == 1)
            {
                textArea.AppendText("TURN " + turn + " "
                                    + living.First().Name
                                    + "の勝ちです!\n");
                if (checkBox1.Checked)
                {
                    my_timer.Stop();
                }


                // debugようこーど
                // try{
                //     Thread.sleep(5000);
                //     newGame();
                // }catch(InterruptedException e){}
            }
            else if (living.Count() == 0)
            {
                textArea.AppendText("引き分けです!\n");
                if (checkBox1.Checked)
                {
                    my_timer.Stop();
                }

                // debugようこーど

                // try{
                //     Thread.sleep(5000);
                //     newGame();
                // }catch(InterruptedException e){}
            }
        }
Exemplo n.º 2
0
        public void NewGame()
        {
            if (Players != null)
            {
                DisposePlayers();
            }

            // 同一階層か二つ上の階層でconfigファイルを探す
            string configPath = null;

            if (System.IO.File.Exists(System.IO.Path.GetFullPath("./config.config")))
            {
                configPath = System.IO.Path.GetFullPath("./config.config");
            }
            else if (System.IO.File.Exists(System.IO.Path.GetFullPath("../../config.config")))
            {
                configPath = System.IO.Path.GetFullPath("../../config.config");
            }
            else
            {
                throw new InvalidOperationException("Please set file as \"config.config\" at the same directory.");
            }

            string[] lines = System.IO.File.ReadAllLines(configPath);

            var tmp = new List <string>();

            lines.Take(4).ToList().ForEach(line => tmp.Add(line.Substring(line.IndexOf("=") + 1)));
            tmp.RemoveAll((s => s.Trim() == ""));

            Players = new List <Player>();
            tmp.ForEach(s => Players.Add(new ExAI(s)));

            if (Players.Count() < 4)
            {
                you = new You("あなた");
                Players.Add(you);
            }

            while (Players.Count() < 4)
            {
                Players.Add(new AIPlayer("敵"));
            }

            // プレイヤーを初期位置に移動
            // order players rondomly
            Players = Players.OrderBy(p => Guid.NewGuid()).ToList();
            for (var i = 0; i < Players.Count(); i++)
            {
                Players[i].pos = Utils.INIT_POSITIONS[i];
                Players[i].SetId(i);
            }

            foreach (var p in Players)
            {
                textArea.AppendText($"{p.Id}:{p.Name}\n");
            }

            walls = new List <Position>();
            for (int x = 0; x < Utils.WIDTH; x++)
            {
                for (int y = 0; y < Utils.HEIGHT; y++)
                {
                    if (Utils.MAP_ARRAY[y][x] == '■')
                    {
                        walls.Add(new Position(x, y));
                    }
                }
            }

            blocks = new List <Block>();
            while (blocks.Count() < 90)
            {
                Block newBlock = new Block(RandomPosition());
                if (!Utils.IsNearInitPosition(newBlock.pos) &&
                    !walls.Contains(newBlock.pos) &&
                    !blocks.Any(b => b.pos.Equals(newBlock.pos)))
                {
                    blocks.Add(newBlock);
                }
            }

            int index = 0;

            for (; index < Utils.ITEM_COUNT / 2; index++)
            {
                Block b = blocks[index];
                b.SetItem(new Item('力', b.pos));
            }
            for (; index < Utils.ITEM_COUNT; index++)
            {
                Block b = blocks[index];
                b.SetItem(new Item('弾', b.pos));
            }

            turn      = 0;
            showTurn  = 0;
            bombs     = new List <Bomb>();
            items     = new List <Item>();
            fires     = new List <Position>();
            mapData   = new MapData(turn, walls, blocks, Players, bombs, items, fires);
            histories = new List <string>();
            var mapJson = Utils.ObjectToJson(mapData);

            histories.Add(mapJson);
            ShowMap(mapData, mapJson);
            textArea.AppendText("TURN 0: ゲームが開始されました\n");

            // 最初の開始時には1秒待つ
            System.Threading.Thread.Sleep(1000);

            update_delegate   = new Update_label_delegate(fnUpdate_Label);
            my_timer          = new System.Timers.Timer(Utils.DEFAULT_SLEEP_TIME);
            my_timer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
            my_timer.Start();
        }