Flutter ffi with stockfish chess engine

360 views
Skip to first unread message

Muhammad Aliah

unread,
Aug 31, 2020, 11:52:36 AM8/31/20
to Dart FFI
Hi.
I'm working on chess app using flutter.
I have already added  Stockfish source code c++ to my flutter project,
but the problem is it's not clear to me how to open stream in flutter to deal with stockfish uci.
I'll appreciate any help .

Thank you in advance.

Daco Harkes

unread,
Aug 31, 2020, 11:59:02 AM8/31/20
to Muhammad Aliah, Dart FFI
Hi Muhammad,

Nice to meet you.

Can you be more specific?
What data are you trying to get from where to where?
Do you have a code sample or API?

Kind regards,

 •  Daco Harkes
 •  Software Engineer
 •  dacoh...@google.com 


--
You received this message because you are subscribed to the Google Groups "Dart FFI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dart-ffi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dart-ffi/76045e2d-c705-4fc2-8b4d-0af334527e36n%40googlegroups.com.

Muhammad Aliah

unread,
Aug 31, 2020, 12:32:07 PM8/31/20
to Dart FFI
I use c++ sample code,
 I need to get data from this code,

void UCI::command(const string& cmd) {
  istringstream is(cmd);

  string token;
  is >> skipws >> token;

  if (token == "quit" || token == "stop" || token == "ponderhit")
  {
      // The GUI sends 'ponderhit' to tell us to ponder on the same move the
      // opponent has played. In case Signals.stopOnPonderhit is set we are
      // waiting for 'ponderhit' to stop the search (for instance because we
      // already ran out of time), otherwise we should continue searching but
      // switch from pondering to normal search.
      if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
      {
          Search::Signals.stop = true;
          Threads.main()->notify_one(); // Could be sleeping
      }
      else
          Search::Limits.ponder = false;
  }
  else if (token == "perft" || token == "divide")
  {
      int depth;
      stringstream ss;

      is >> depth;
      ss << Options["Hash"]    << " "
         << Options["Threads"] << " " << depth << " current " << token;

      benchmark(currentPos, ss);
  }
  else if (token == "key")
      sync_cout << hex << uppercase << setfill('0')
                << "position key: "   << setw(16) << currentPos.key()
                << "\nmaterial key: " << setw(16) << currentPos.material_key()
                << "\npawn key:     " << setw(16) << currentPos.pawn_key()
                << dec << nouppercase << setfill(' ') << sync_endl;

  else if (token == "uci")
      sync_cout << "id name " << engine_info(true)
                << "\n"       << Options
                << "\nuciok"  << sync_endl;

  else if (token == "eval")
  {
      Search::RootColor = currentPos.side_to_move(); // Ensure it is set
      sync_cout << Eval::trace(currentPos) << sync_endl;
  }
  else if (token == "ucinewgame") TT.clear();
  else if (token == "go")         go(currentPos, is);
  else if (token == "position")   position(currentPos, is);
  else if (token == "setoption")  setoption(is);
  else if (token == "flip")       currentPos.flip();
  else if (token == "bench")      benchmark(currentPos, is);
  else if (token == "d")          sync_cout << currentPos.pretty() << sync_endl;
  else if (token == "isready")    sync_cout << "readyok" << sync_endl;
  else
      sync_cout << "Unknown command: " << cmd << sync_endl;
}

Daco Harkes

unread,
Aug 31, 2020, 12:51:52 PM8/31/20
to Muhammad Aliah, Dart FFI
Hey Muhammad,

C++ strings do not have a predictable memory layout, so we only support plain C char*. That means you'll have to write a small wrapper function with char* as argument type.

How to link the native C/C++ code into your Flutter app you can read here. (Did you manage to invoke a simple hello_world() in C from Dart through the FFI? That way you know that your application setup is working, before looking at connecting to stockfish.)

Kind regards,

 •  Daco Harkes
 •  Software Engineer
 •  dacoh...@google.com 

Reply all
Reply to author
Forward
0 new messages