Skip to content

edgarmuyomba/tictactoeGameServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TicTacToe Game Server

This is a simple implementation for a multiplayer game (TicTacToe) server using python websockets. It handles the game-logic and communication between clients and the server to ensure real-time game play. It also provides the option to play against an AI agent. The front-end segment for this game can be found here.

Events

  1. Connect

    This is used to establish a connection between a client and a server

    Request

     {
         "type": "connect"
     }

    Response

     {
         "type": "connect"
     }
  2. New Game

    This is used by the client to create a new game on the web server

    Request

        {
            "type": "new_game",
        }

    Response

        {
            "type": "new_game",
            "turn": "<game_session.current_turn>",
            "game_id": "<game_session.game_id>",
            "mark": "<player's_mark>",
            "player_id": "<player's_id>"
        }
  3. AI

    This triggers the creation of a new game against the AI agent

    Request

        {
            "type": "ai",
        }

    Response

        {
            "type": "new_game",
            "turn": "<game_session.current_turn>",
            "game_id": "<game_session.game_id>",
            "mark": "X",
            "player_id": "<player's_id>"
        }
  4. Play Move

    This is used to trigger a game play move in the specific game session. The event is used to update the game state on the server and if playing against an AI agent, a response is obtained and sent back otherwise, the event is sent to the second player in the game session ( if any ).

    Request

        {
            "type": "play_move",
            "game_id": "<game_session.game_id>",
            "player_id": "<player's_id>",
            "index": "<move_index>"
        }

    Response

    • If the move that has been played results in a win
          {
              "type": "win",
              "winner": "<game_session.current_turn>",
              "game_state": "<game_session.game_state>"
          }
    • If the move resulted in a draw
          {
              "type": "draw",
              "game_state": "<game_session.game_state>"
          }
    • Otherwise a normal move is sent
          {
              "type": "play_move",
              "turn": "<game_session.current_turn>",
              "game_state": "<game_session.game_state>"
          }
  5. Join Game

    This is used by a player to join an existing game session using the game_id

    Request

     {
         "type": "join_game",
         "game_id": "<game_session.game_id>"
     }

    Response

    • If the game_id is valid and the game session exists?
      {
              "type": "new_game",
              "turn": "<game_session.current_turn>",
              "game_id": "<game_session.game_id>",
              "mark": "<player's_mark>",
              "player_id": "<player's_id>",
              "game_state": "<game_session.game_state>"
      }

Error Handling

  • In case of any error, a specialised event is sent out and displayed to the client in the form

    {
        "type": "error",
        "message": "<error_message>"
    }

    These can occur in cases such as

    • A client trying to join a game session with maximum number of players
    • A client trying to join a game session that doesnt exist
    • A client trying to play a move when its not their turn
  • In the event that duing a Two-player game session one of the clients leaves, still, an event is sent out to the second player and the game_session terminated.

    {
        "type": "player_left"
    }

About

Game Server for Tic Tac Toe

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages