Преглед на файлове

proper teams implemented

Zoadian преди 10 години
родител
ревизия
3fb612df64
променени са 2 файла, в които са добавени 52 реда и са изтрити 32 реда
  1. 1 1
      playfield.cpp
  2. 51 31
      simulator.h

+ 1 - 1
playfield.cpp

@@ -51,7 +51,7 @@ void Playfield::paintEvent(QPaintEvent* event) {
 
         int xx = program.position.x * w;
         int yy = program.position.y * w;
-        painter.setPen(QPen(program.color, 3));
+        painter.setPen(QPen(program.team->color, 3));
         painter.setBrush(Qt::NoBrush);
         QRect rect(xx, yy,w+1, h+1);
         painter.drawEllipse(rect.center(), w/2.3, h/2.3);

+ 51 - 31
simulator.h

@@ -181,7 +181,32 @@ struct Instruction {
     }
 };
 
-using Bank = shared_ptr<vector<Instruction>>;
+struct Team {
+    QColor color = Qt::red;
+
+    Team(QColor color)
+        : color(color)
+    {
+    }
+};
+
+struct Bank {
+    shared_ptr<Team> team;
+    vector<Instruction> instructions;
+
+    Bank(shared_ptr<Team> team, const vector<Instruction>& instructions)
+        : team(team)
+        , instructions(instructions)
+    {
+    }
+
+    Bank(const Bank& bank)
+        : team(bank.team)
+        , instructions(bank.instructions)
+    {
+
+    }
+};
 
 
 using BankIndex = size_t;
@@ -207,15 +232,15 @@ struct Program {
     int creationCycle = 0;
     int generation = 0;
     Error error = NoError;
-    QColor color;
+    shared_ptr<Team> team;
     Position position;
     vector<Task> tasks;
     TaskIndex taskIndex = 0;
-    vector<Bank> banks;
+    vector<shared_ptr<Bank>> banks;
     array<int32_t, 20> vars{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
 
-    Program(QColor _color, Position _position, int32_t instructionSet, int32_t slotCount, int32_t mobile) {
-        color = _color;
+    Program(shared_ptr<Team> _team, Position _position, int32_t instructionSet, int32_t slotCount, int32_t mobile) {
+        team = _team;
         position = _position;
         banks.resize(slotCount);
         tasks.resize(1);
@@ -248,28 +273,23 @@ struct Simulator {
 
 
     void loadProgram(QColor color, size_t x, size_t y) {
-        programs.push_back(Program(color, Position{x, y}, 2, 50, 1));
-
-        {
-            Bank bank = make_shared<vector<Instruction>>();
-            bank->push_back(Instruction(BJUMP, LL, 1, 0));
-            programs.back().banks[0] = bank;
+        shared_ptr<Team> team = make_shared<Team>(color);
+
+        programs.push_back(Program(team, Position{x, y}, 2, 50, 1));
+
+        programs.back().banks[0] = make_shared<Bank>(team, vector<Instruction>{
+                                                              Instruction(BJUMP, LL, 1, 0)
+                                                          });
+
+        programs.back().banks[1] = make_shared<Bank>(team, vector<Instruction>{
+                                                              Instruction(SCAN, V, 1),
+                                                              Instruction(CREATE, LLL, 2, 50, 1),
+                                                              Instruction(TRANS, LL, 0, 0),
+                                                              Instruction(TRANS, LL, 1, 1),
+                                                              Instruction(TURN, L, 0),
+                                                              Instruction(AJUMP, L, 0)
+                                                          });
         }
-        {
-            Bank bank = make_shared<vector<Instruction>>();
-            bank->push_back(Instruction(SCAN, V, 1));
-            bank->push_back(Instruction(CREATE, LLL, 2, 50, 1));
-            bank->push_back(Instruction(TRANS, LL, 0, 0));
-            bank->push_back(Instruction(TRANS, LL, 1, 1));
-//            bank->push_back(Instruction(RANDOM, VLL, 5, -5, 5));
-            bank->push_back(Instruction(TURN, L, 0));
-            bank->push_back(Instruction(ADD, VL, 6, 1));
-//            bank->push_back(Instruction(IFG, VL, 6, 5));
-//            bank->push_back(Instruction(DIE));
-            bank->push_back(Instruction(AJUMP, L, 0));
-            programs.back().banks[1] = bank;
-        }
-    }
 
     Program* findProgram(Position position) {
         for(auto& program : programs) {
@@ -303,12 +323,12 @@ struct Simulator {
 
                 const auto bank_ptr = program.banks[task.bankIndex].get();
 
-                if(bank_ptr == nullptr || task.instIndex >= bank_ptr->size()) {
+                if(bank_ptr == nullptr || task.instIndex >= bank_ptr->instructions.size()) {
                     continue;
                 }
 
                 const auto& bank = *bank_ptr;
-                const auto& inst = bank[task.instIndex];
+                const auto& inst = bank.instructions[task.instIndex];
 
                 //prevent overrideing of instuctions...
 
@@ -455,7 +475,7 @@ struct Simulator {
                     auto remotePosition = calcPosition(program.position, task.direction, 1);
                     auto remoteProgram = findProgram(remotePosition);
                     if(!remoteProgram) {
-                        programs.push_back(Program(program.color, remotePosition, *task.p_a, *task.p_b, *task.p_c));
+                        programs.push_back(Program(program.team, remotePosition, *task.p_a, *task.p_b, *task.p_c));
                     }
                     task.instIndex += 1;
                     break;
@@ -526,7 +546,7 @@ struct Simulator {
                     // #a=2 ...friend
                     auto remotePosition = calcPosition(program.position, task.direction, 1);
                     auto remoteProgram = findProgram(remotePosition);
-                    *task.p_a = !remoteProgram ? 0 : remoteProgram->color == program.color ? 2: 1;
+                    *task.p_a = !remoteProgram ? 0 : remoteProgram->team == program.team ? 2: 1;
                     task.instIndex += 1;
                     break;
                 }
@@ -544,7 +564,7 @@ struct Simulator {
                         auto remotePosition = calcPosition(program.position, task.direction, i + 1);
                         auto remoteProgram = findProgram(remotePosition);
                         if(remoteProgram) {
-                            *task.p_a = remoteProgram->color == program.color ? 2: 1;
+                            *task.p_a = remoteProgram->team == program.team ? 2: 1;
                             *task.p_b = i;
                             break;
                         }