@@ -8,11 +8,41 @@ namespace Procon2018 {
88Playground::Playground (const s3d::RectF & viewport)
99: FieldView(viewport, Field::RandomState())
1010, m_actions()
11- , m_dragState() {
11+ , m_dragState()
12+ , m_ai() {
13+ }
14+
15+ Playground::Playground (const s3d::RectF & viewport, SP<AI> ai0, SP<AI> ai1)
16+ : FieldView(viewport, Field::RandomState())
17+ , m_actions()
18+ , m_dragState()
19+ , m_ai{ai0, ai1} {
20+ if (ai0) {
21+ ai0->init (m_fld, PlayerId::A);
22+ ai0->forward ({});
23+ }
24+ if (ai1) {
25+ ai1->init (m_fld, PlayerId::B);
26+ ai1->forward ({});
27+ }
1228}
1329
1430void Playground::update () {
1531 FieldView::update ();
32+ if (m_fld.isEnd ()) return ;
33+
34+ bool forwards = s3d::KeyEnter.down ();
35+ bool validInput[4 ] = {};
36+ for (int i = 0 ; i < 2 ; i++) {
37+ if (m_ai[i] == nullptr ) {
38+ validInput[2 *i] = validInput[2 *i + 1 ] = true ;
39+ continue ;
40+ }
41+ auto move = m_ai[i]->getNextMove ();
42+ if (!move) continue ;
43+ m_actions[2 *i] = move->a0 ;
44+ m_actions[2 *i + 1 ] = move->a1 ;
45+ }
1646
1747 auto toGridPos = [&](s3d::Vec2 vec) {
1848 s3d::Vec2 pos = vec - m_v.tl ();
@@ -31,6 +61,7 @@ void Playground::update() {
3161 if (s3d::MouseL.down () || s3d::MouseR.down ()) {
3262 Point p = toGridPos (mousePos);
3363 for (int i = 0 ; i < 4 ; i++) {
64+ if (!validInput[i]) continue ;
3465 if (p == m_fld.agentPos ((AgentId)i)) {
3566 DragState s;
3667 if (s3d::MouseL.down ()) s.type = ActionType::Move;
@@ -79,14 +110,23 @@ void Playground::update() {
79110 drawAction (s3d::Line (gridCenter (p), gridCenter (trg)), m_actions[i]->type );
80111 }
81112
82- if (s3d::KeyEnter. down () ) {
113+ if (forwards && m_fld. resTurn () > 0 ) {
83114 forward (m_actions[0 ], m_actions[1 ], m_actions[2 ], m_actions[3 ]);
115+ std::cout << " resTurn: " << m_fld.resTurn () << std::endl;
116+ auto score = m_fld.calcScore ();
117+ std::cout << " score: (blue: " << score.first << " , red: " << score.second << " )" << std::endl;
118+
119+ if (m_fld.resTurn () > 0 ) {
120+ PlayerMove m0 = PlayerMove (m_actions[0 ], m_actions[1 ]);
121+ PlayerMove m1 = PlayerMove (m_actions[2 ], m_actions[3 ]);
122+ std::optional<std::pair<PlayerMove, PlayerMove>> moves = std::make_pair (m0, m1);
123+ for (int i = 0 ; i < 2 ; i++) {
124+ if (m_ai[i]) m_ai[i]->forward (moves);
125+ }
126+ }
84127 for (int i = 0 ; i < 4 ; i++) {
85128 m_actions[i].reset ();
86129 }
87- std::cout << " resTurn: " << m_fld.resTurn () << std::endl;
88- auto score = m_fld.calcScore ();
89- std::cout << " score: (" << score.first << " , " << score.second << " )" << std::endl;
90130 }
91131}
92132
0 commit comments