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