krouzek:uloha:sachy1

Šachy

Pro zadanou šachovnici

  1. vygenerujte všechny kombinace tahů daného hráče v jednoduchém případě (neuvažujte rošády, braní mimochodem, výměnu pěšců za figury)
  2. vytvořte funkci která pro zadanou pozici detekuje šach
  3. pozice kdy je šach a všechny tahy vedou do šachu ⇒ šach mat (konec hry)
  4. pozice kdy není šach, ale všechny tahy vedou do šachu ⇒ pat (remíza)
  5. omezte vygenerované kombinace z 1 = nepovolte žádné tahy, které vedou do šachu
  6. nechte počítač hrát proti počítači (tj. střídání tahů bílý - černý), zcela náhodné tahy. Nějak si hru zobrazte.
    • Jsou všechny tahy platné? Pokud ne, opravte.
    • Končí hra vždy výhrou nějaké strany?
  • Můžete svůj program dělat v NodeJS - textový vstup a výstup.
  • Můžete svůj program dělat v klasické stránce - výstup můžete rovnou „rendrovat“ na stránku - tj. po každém tahu aktualizovat hodnoty „šachovnice“ - HTML tabulky (nejsnazší)

Nejrobustnější: Můžeme program napsat v NodeJS a potom jeho výstup poslat do klienta (jako JSON). Klient potom výstup zobrazí. Tímto komplikovanějším řešením umožníme budoucí síťovou hru více hráčů proti sobě. Také nás to donutí striktně oddělit logiku (v nodejs projektu) od zobrazení a UI (ve stránce)

Zajímavou možností pak je rozdělit práci bez problémů na dva programátory = jeden dělá zobrazovadlo a klikadlo, druhý implementuje pravidla šachů - a shodnete se na nějakém rozhraní.

Vygenerovaný příklad takového klient-server rozhraní najdete na konci stránky.

- **Game State Object**:
  {
    "gameId": "unique-game-id",
    "board": {/* Current board state */},
    "currentPlayer": "white" /* Player color */,
    "gameStatus": "active", /* or "checkmate," "stalemate," etc. */,
    "moves": ["e4", "e5", /* List of moves made in the game */],
    "players": {
      "white": "Player1",
      "black": "Player2"
    }
  }
- **Active Game List Object**:
  {
    "games": [
      {
        "gameId": "game-1",
        "players": ["Player1", "Player2"],
        "status": "active"
      },
      {
        "gameId": "game-2",
        "players": ["Player3", "Player4"],
        "status": "active"
      }
    ]
  }

This is a basic outline of a RESTful API for a multiplayer chess game. You may need to expand on this depending on your specific requirements, including game authentication, handling game events, and implementing additional features like chat, time control, and more.

Create a JSON formatted example input of a chess solving program. Use all chess figure types in the example.
{
  "board": {
    "a8": "r",
    "b8": "n",
    "c8": "b",
    "d8": "q",
    "e8": "k",
    "f8": "b",
    "g8": "n",
    "h8": "r",
    "a7": "p",
    "b7": "p",
    "c7": "p",
    "d7": "p",
    "e7": "p",
    "f7": "p",
    "g7": "p",
    "h7": "p",
    "a2": "P",
    "b2": "P",
    "c2": "P",
    "d2": "P",
    "e2": "P",
    "f2": "P",
    "g2": "P",
    "h2": "P",
    "a1": "R",
    "b1": "N",
    "c1": "B",
    "d1": "Q",
    "e1": "K",
    "f1": "B",
    "g1": "N",
    "h1": "R"
  },
  "turn": "white",
  "castling_rights": "KQkq",
  "en_passant_target": "e3",
  "halfmove_clock": 25,
  "fullmove_number": 12
}

In this JSON input:

  • The board field represents the current state of the chessboard. Each square is represented by its coordinates (e.g., „a8“ for the square in the bottom-left corner) and the piece occupying it. Lowercase letters represent black pieces, and uppercase letters represent white pieces. Here, „r“ is for a black rook, „n“ for a black knight, „b“ for a black bishop, „q“ for a black queen, „k“ for a black king, „p“ for black pawns, and their uppercase counterparts for white pieces.
  • The turn field indicates whose turn it is to move, which is „white“ in this example.
  • The castling_rights field indicates the available castling rights. „KQkq“ means both white and black can still castle kingside and queenside.
  • The en_passant_target field specifies the target square for en passant captures. In this case, it's „e3“.
  • The halfmove_clock field represents the number of half-moves since the last pawn move or capture.
  • The fullmove_number field indicates the number of the current full move, where a full move consists of both players making a move.
  • castling = rošáda
  • en passant = tah mimochodem

ChatGPT nejspíš vychází z https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation a podobných zdrojů

{
   "chess_pieces": [
    { "type": "rook", "color": "black", "position": "a8" },
    { "type": "knight", "color": "black", "position": "b8" },
    { "type": "bishop", "color": "black", "position": "c8" },
    { "type": "queen", "color": "black", "position": "d8" },
    { "type": "king", "color": "black", "position": "e8" },
    { "type": "bishop", "color": "black", "position": "f8" },
    { "type": "knight", "color": "black", "position": "g8" },
    { "type": "rook", "color": "black", "position": "h8" },
    { "type": "pawn", "color": "black", "position": "a7" },
    { "type": "pawn", "color": "black", "position": "b7" },
    { "type": "pawn", "color": "black", "position": "c7" },
    { "type": "pawn", "color": "black", "position": "d7" },
    { "type": "pawn", "color": "black", "position": "e7" },
    { "type": "pawn", "color": "black", "position": "f7" },
    { "type": "pawn", "color": "black", "position": "g7" },
    { "type": "pawn", "color": "black", "position": "h7" },
    { "type": "pawn", "color": "white", "position": "a2" },
    { "type": "pawn", "color": "white", "position": "b2" },
    { "type": "pawn", "color": "white", "position": "c2" },
    { "type": "pawn", "color": "white", "position": "d2" },
    { "type": "pawn", "color": "white", "position": "e2" },
    { "type": "pawn", "color": "white", "position": "f2" },
    { "type": "pawn", "color": "white", "position": "g2" },
    { "type": "pawn", "color": "white", "position": "h2" },
    { "type": "rook", "color": "white", "position": "a1" },
    { "type": "knight", "color": "white", "position": "b1" },
    { "type": "bishop", "color": "white", "position": "c1" },
    { "type": "queen", "color": "white", "position": "d1" },
    { "type": "king", "color": "white", "position": "e1" },
    { "type": "bishop", "color": "white", "position": "f1" },
    { "type": "knight", "color": "white", "position": "g1" },
    { "type": "rook", "color": "white", "position": "h1" }
  ]
}

A ještě lépe parsovatelná pozice:

{
  "chess_pieces": [
    { "type": "rook", "color": "black", "position": { "x": 0, "y": 7 } },
    { "type": "knight", "color": "black", "position": { "x": 1, "y": 7 } },
    { "type": "bishop", "color": "black", "position": { "x": 2, "y": 7 } },
    { "type": "queen", "color": "black", "position": { "x": 3, "y": 7 } },
    { "type": "king", "color": "black", "position": { "x": 4, "y": 7 } },
    { "type": "bishop", "color": "black", "position": { "x": 5, "y": 7 } },
    { "type": "knight", "color": "black", "position": { "x": 6, "y": 7 } },
    { "type": "rook", "color": "black", "position": { "x": 7, "y": 7 } },
    { "type": "pawn", "color": "black", "position": { "x": 0, "y": 6 } },
    { "type": "pawn", "color": "black", "position": { "x": 1, "y": 6 } },
    { "type": "pawn", "color": "black", "position": { "x": 2, "y": 6 } },
    { "type": "pawn", "color": "black", "position": { "x": 3, "y": 6 } },
    { "type": "pawn", "color": "black", "position": { "x": 4, "y": 6 } },
    { "type": "pawn", "color": "black", "position": { "x": 5, "y": 6 } },
    { "type": "pawn", "color": "black", "position": { "x": 6, "y": 6 } },
    { "type": "pawn", "color": "black", "position": { "x": 7, "y": 6 } },
    { "type": "pawn", "color": "white", "position": { "x": 0, "y": 1 } },
    { "type": "pawn", "color": "white", "position": { "x": 1, "y": 1 } },
    { "type": "pawn", "color": "white", "position": { "x": 2, "y": 1 } },
    { "type": "pawn", "color": "white", "position": { "x": 3, "y": 1 } },
    { "type": "pawn", "color": "white", "position": { "x": 4, "y": 1 } },
    { "type": "pawn", "color": "white", "position": { "x": 5, "y": 1 } },
    { "type": "pawn", "color": "white", "position": { "x": 6, "y": 1 } },
    { "type": "pawn", "color": "white", "position": { "x": 7, "y": 1 } },
    { "type": "rook", "color": "white", "position": { "x": 0, "y": 0 } },
    { "type": "knight", "color": "white", "position": { "x": 1, "y": 0 } },
    { "type": "bishop", "color": "white", "position": { "x": 2, "y": 0 } },
    { "type": "queen", "color": "white", "position": { "x": 3, "y": 0 } },
    { "type": "king", "color": "white", "position": { "x": 4, "y": 0 } },
    { "type": "bishop", "color": "white", "position": { "x": 5, "y": 0 } },
    { "type": "knight", "color": "white", "position": { "x": 6, "y": 0 } },
    { "type": "rook", "color": "white", "position": { "x": 7, "y": 0 } }
  ]
}

Create an interface for a multiplayer chess game between the client (visualizing the game, allowing the players to select next move) and the server (implementing the chess game logic). RESTApi

Poznámky:

  • Všimněte si, že návrh vlastně neřeší že hrajeme šachy, ale věnuje se spíš správě herních sessions
  • Pravděpodobně by podobná šablona šla použít na libovolnou tahovou hru
  • Není řešena autentizace uživatelů, ale samozřejmě by bylo potřeba jí pak mít
  • Problém který není řešen: endpoint GET /api/games/{gameId}/allowed-moves - pokud nechceme v klientovi opakovat kód pro logiku tvoření tahů, musí se dozvědět co je přípustné a co ne (aby umožnil uživateli pouze přípustné tahy)

1. Create a New Game

  1. Endpoint: `POST /api/games`
  2. Description: Create a new chess game. This endpoint initializes the game state and returns a unique game ID for reference.
  3. Request: None
  4. Response: `{„gameId“: „unique-game-id“}`

2. Get Game State

  1. Endpoint: `GET /api/games/{gameId}`
  2. Description: Retrieve the current state of the game, including the board configuration, whose turn it is, and other game-specific details.
  3. Request: None
  4. Response: Detailed game state JSON

3. Make a Move

  1. Endpoint: `POST /api/games/{gameId}/move`
  2. Description: Allows a player to make a move in the game. The request should include the player's move details, such as the starting and ending positions of the piece.
  3. Request: `{ „playerColor“: „white“, „from“: { „x“: X1, „y“: Y1 }, „to“: { „x“: X2, „y“: Y2 } }`
  4. Response: `{„success“: true, „message“: „Move successful“}`

4. List Active Games

  1. Endpoint: `GET /api/games`
  2. Description: Retrieve a list of all active games, including their IDs and details.
  3. Request: None
  4. Response: List of active game objects

5. Join a Game

  1. Endpoint: `POST /api/games/{gameId}/join`
  2. Description: Allows a player to join an existing game.
  3. Request: `{ „playerName“: „PlayerName“, „playerColor“: „black“ }`
  4. Response: `{„success“: true, „message“: „Joined the game“}`

6. Resign from the Game

  1. Endpoint: `POST /api/games/{gameId}/resign`
  2. Description: Allows a player to resign from the game.
  3. Request: None
  4. Response: `{„success“: true, „message“: „Resigned from the game“}`
  • krouzek/uloha/sachy1.txt
  • Poslední úprava: 2023/11/15 20:54
  • autor: 127.0.0.1