public void Loop(IList <Tuple <string, IRobot> > robots, int round, int dN) { logpath = "../../log_" + round + ".txt"; File.WriteAllText(logpath, "round: " + round + Environment.NewLine, Encoding.UTF8); RoundConfig round_config = config.rounds[round]; state.robots.Clear(); for (int robot_id = 0; robot_id < robots.Count - dN; robot_id++) { RobotState robot = new RobotState { id = robot_id, X = SingleRandom.Instance.Next(0, round_config.width), Y = SingleRandom.Instance.Next(0, round_config.height), energy = round_config.max_energy, attack = 45, speed = 10, defence = 45, isAlive = true, kill = 0, kill_id = new int[robots.Count], name = robots[robot_id].Item2.Name, colour = robots[robot_id].Item2.Colour, dll_path = robots[robot_id].Item1 }; state.robots.Add(robot); } int robot_id_no_iter = robots.Count - dN; foreach (RobotState rs in future_robots) { for (int robot_id = robots.Count - dN; robot_id < robots.Count; robot_id++) { if ((rs.isAlive == true) && (robots[robot_id].Item2.Name == rs.name)) { RobotState rs_copy = rs.ShallowCopy(); rs_copy.id = robot_id_no_iter++; for (int kc = 0; kc < rs_copy.kill; kc++) { rs_copy.kill_id[kc] = 0; } rs_copy.kill = 0; state.robots.Add(rs_copy); break; } } } state.points.Clear(); for (int i = 0; i < round_config.nEnergy; i++) { Point point = new Point { X = SingleRandom.Instance.Next(0, round_config.width), Y = SingleRandom.Instance.Next(0, round_config.height), type = PointType.Energy }; state.points.Add(point); } for (int i = 0; i < round_config.nHealth; i++) { Point point = new Point { X = SingleRandom.Instance.Next(0, round_config.width), Y = SingleRandom.Instance.Next(0, round_config.height), type = PointType.Health }; state.points.Add(point); } for (int n = 0; n < round_config.steps; n++) { future_robots.Clear(); File.AppendAllText(logpath, "step: " + n + Environment.NewLine, Encoding.UTF8); for (int i = 0; i < robots.Count; i++) { IRobot robot = robots[i].Item2; Task <RobotAction> task = Task.Run(() => robot.Tick(i, round_config, state.DeepCopy())); try { UpdateState(round_config, i, task.Wait(round_config.timeout) ? task.Result : null); //File.AppendAllText(logpath, state.robots[i].id +": " + state.robots[i].X + "x" + state.robots[i].Y + "; health: " + (state.robots[i].attack + state.robots[i].defence + state.robots[i].speed) + "; energy: " + state.robots[i].energy + Environment.NewLine, Encoding.UTF8); } catch (Exception e) { if (task.IsFaulted) { File.AppendAllText(logpath, "robot \"" + robot.Name + "\" failed" + Environment.NewLine, Encoding.UTF8); } else { File.AppendAllText(logpath, "oh no" + Environment.NewLine, Encoding.UTF8); } File.AppendAllText(logpath, e.Message + Environment.NewLine, Encoding.UTF8); robots.RemoveAt(i); } } if (n < round_config.steps - 1) { state.robots.Clear(); foreach (RobotState rs in future_robots) { state.robots.Add(rs); } } else if (n == round_config.steps - 1) { for (int k = 0; k < future_robots.Count; k++) { if (future_robots[k].isAlive) { future_robots[k].energy += round_config.K * future_robots[k].kill; } else { future_robots[k].energy += round_config.K * future_robots[k].kill / 2; } } } Draw.Update(state.robots, state.points, round_config.width, round_config.height); for (int i = 0; i < future_robots.Count; i++) { if (future_robots[i].energy < 0) { future_robots[i].energy = 0; } if (future_robots[i].defence + future_robots[i].attack + future_robots[i].speed < 0) { future_robots[i].defence = 0; future_robots[i].attack = 0; future_robots[i].speed = 0; } /* * if (i % 2 == 0) * { * int energyValue = future_robots[i].energy / 10; * Form1.pbList[i].Value = energyValue; * } * else * { * Form1.pbList[i].Value = future_robots[i].defence + future_robots[i].attack + future_robots[i].speed; * } */ } } }
public void start() { label2.Text = "Раунд: " + (roundId + 1).ToString(); Draw.Start();//Draw.bmp, Draw.xList,Draw.yList); //функция старта, внутри нее отрисовка,и создание параметров поля,узнаем количество роботов if (config == null) { return; } if (robots != null) { dN = 0; pbList = new List <ProgressBar>(); lbList = new List <Label>(); robots_base = RobotLoader.LoadRobots(directoryPath); foreach (RobotState rs in game.future_robots) { if (rs.isAlive == true) { robots_base.Add(robots[rs.id]); dN++; } } robots.Clear(); robots.AddRange(robots_base); //createField //int n = /*game.future_robots.Count;*/robots.Count; ////listBox1.Height = 14 * n; ////Draw.param(100,100); //for (int i = 0; i < n; i++) //{ // // listBox1.Items.Add(/*game.future_robots[i].id*/robots[i]); // Label lb = new Label(); // lb.Size = label1.Size; // lb.Font = label1.Font; // lb.Name = "lb" + i.ToString(); // lb.Location = new System.Drawing.Point(764, 68 + 18 * i); // lb.Text = i.ToString()+". "+robots[i].Name; // this.Controls.Add(lb); // lbList.Add(lb); //} // ProgressBar pb = new ProgressBar(); // pb.Name = "pbH" + i.ToString(); // pb.Location = new System.Drawing.Point(890, 68 + 18 * i); // pb.Size = progressBar1.Size; // this.Controls.Add(pb); // pbList.Add(pb); // pb = new ProgressBar(); // pb.Name = "pbR" + i.ToString(); // pb.Location = new System.Drawing.Point(980, 68 + 18 * i);//подобрать значения // pb.Size = progressBar1.Size; // this.Controls.Add(pb); // pbList.Add(pb); //} Param p = new Param(); p.rId = roundId; p.d = dN; p.g = robots; var th = new Thread(newGame); th.Start(p); evStop.WaitOne(); gameStats(); evStop.Reset(); //game //game.Loop(robots, roundId, dN); //Thread.Sleep(5000); //clearField //listBox1.Items.Clear(); //for(int i = 0; i<pbList.Count;i++) //{ // pbList[i].Dispose(); //} //pbList.Clear(); //statView //Thread.Sleep(3000); //game.future_robots.Sort(); //Form form3 = new Form3(); //form3.Show(); //for (int j = 0; j < game.future_robots.Count; j++) //{ // Form3.dataGridView3.Rows.Add(); // Form3.dataGridView3.Rows[j].Cells[0].Value = game.future_robots[j].id; // Form3.dataGridView3.Rows[j].Cells[1].Value = game.future_robots[j].name; // Form3.dataGridView3.Rows[j].Cells[2].Value = game.future_robots[j].defence + game.future_robots[j].attack + game.future_robots[j].speed; // Form3.dataGridView3.Rows[j].Cells[3].Value = game.future_robots[j].energy; // Form3.dataGridView3.Rows[j].Cells[4].Value = game.future_robots[j].isAlive; //} roundId++; if (roundId > 4) { Application.Exit(); } } }