|
1 | 1 | package internal |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "encoding/json" |
6 | | - "fmt" |
7 | | - "io" |
8 | | - "net/http" |
9 | 4 | "os" |
10 | 5 | "path/filepath" |
11 | | - "time" |
12 | 6 |
|
13 | 7 | "github.com/codecrafters-io/tester-utils/test_case_harness" |
14 | 8 | "github.com/corentings/chess" |
15 | 9 | ) |
16 | 10 |
|
17 | | -func makeMove(fenStr string) (string, error) { |
18 | | - fen, err := chess.FEN(fenStr) |
19 | | - if err != nil { |
20 | | - return "", err |
21 | | - } |
22 | | - |
23 | | - game := chess.NewGame(fen) |
24 | | - |
25 | | - // Determine turn color |
26 | | - turn := "white" |
27 | | - if game.Position().Turn() == chess.Black { |
28 | | - turn = "black" |
29 | | - } |
30 | | - |
31 | | - // Prepare request body |
32 | | - type requestBody struct { |
33 | | - Fen string `json:"fen"` |
34 | | - Turn string `json:"turn"` |
35 | | - FailedMoves []string `json:"failed_moves"` |
36 | | - } |
37 | | - |
38 | | - body := requestBody{ |
39 | | - Fen: fenStr, |
40 | | - Turn: turn, |
41 | | - FailedMoves: []string{}, |
42 | | - } |
43 | | - |
44 | | - jsonBody, err := json.Marshal(body) |
45 | | - if err != nil { |
46 | | - return "", fmt.Errorf("error marshaling request body: %v", err) |
47 | | - } |
48 | | - |
49 | | - client := &http.Client{ |
50 | | - Timeout: 5 * time.Second, |
51 | | - } |
52 | | - |
53 | | - resp, err := client.Post( |
54 | | - "http://localhost:8000/move", |
55 | | - "application/json", |
56 | | - bytes.NewBuffer(jsonBody), |
57 | | - ) |
58 | | - if err != nil { |
59 | | - return "", fmt.Errorf("error making request: %v", err) |
60 | | - } |
61 | | - defer resp.Body.Close() |
62 | | - |
63 | | - responseBody, err := io.ReadAll(resp.Body) |
64 | | - if err != nil { |
65 | | - return "", fmt.Errorf("error reading response: %v", err) |
66 | | - } |
67 | | - |
68 | | - if resp.StatusCode != http.StatusOK { |
69 | | - return "", fmt.Errorf("failed to make move, got status %d: %s", resp.StatusCode, string(responseBody)) |
70 | | - } |
71 | | - |
72 | | - return string(responseBody), nil |
73 | | -} |
74 | | - |
75 | 11 | func checkFEN(FEN string) bool { |
76 | 12 | _, err := chess.FEN(FEN) |
77 | 13 | if err != nil { |
|
0 commit comments