]> git.sesse.net Git - stockfish/blob - src/client.cpp
Return all candidate moves from the probe.
[stockfish] / src / client.cpp
1 #include <iostream>
2 #include <memory>
3 #include <string>
4
5 #include <grpc++/grpc++.h>
6
7 #include "hashprobe.grpc.pb.h"
8 #include "types.h"
9 #include "uci.h"
10
11 using grpc::Channel;
12 using grpc::ClientContext;
13 using grpc::Status;
14
15 std::string FormatMove(Move move) {
16   if (move == MOVE_NULL) {
17     return "Null-move";
18   } else if (move == MOVE_NONE) {
19     return "MOVE_NONE";
20   } else {
21     return UCI::square(from_sq(move)) + UCI::square(to_sq(move));
22   }
23 }
24
25 int main(int argc, char** argv) {
26   std::shared_ptr<Channel> channel(grpc::CreateChannel(
27       "localhost:50051", grpc::InsecureChannelCredentials()));
28   std::unique_ptr<HashProbe::Stub> stub(HashProbe::NewStub(channel));
29
30   for ( ;; ) {
31     char buf[256];
32     if (fgets(buf, sizeof(buf), stdin) == nullptr || buf[0] == '\n') {
33       exit(0);
34     }
35
36     char *ptr = strchr(buf, '\n');
37     if (ptr != nullptr) *ptr = 0;
38
39     HashProbeRequest request;
40     request.set_fen(buf);
41
42     HashProbeResponse response;
43     ClientContext context;
44     Status status = stub->Probe(&context, request, &response);
45
46     if (status.ok()) {
47       for (const HashProbeMove &hpmove : response.move()) {
48         std::cout << FormatMove(Move(hpmove.move())) << " ";
49         std::cout << hpmove.found() << " ";
50         std::cout << FormatMove(Move(hpmove.pv_move())) << " ";
51         switch (hpmove.bound()) {
52         case HashProbeMove::BOUND_NONE:
53           std::cout << "?";
54           break;
55         case HashProbeMove::BOUND_EXACT:
56           std::cout << "==";
57           break;
58         case HashProbeMove::BOUND_UPPER:
59           std::cout << "<=";
60           break;
61         case HashProbeMove::BOUND_LOWER:
62           std::cout << ">=";
63           break;
64         } 
65         std::cout << " " << UCI::value(Value(hpmove.value())) << " ";
66         std::cout << hpmove.depth() << std::endl;
67       }
68       std::cout << "END" << std::endl;
69     } else {
70       std::cout << "ERROR" << std::endl;
71     }
72   }
73
74   return 0;
75 }