This project is a multiplayer implementation of the classic board game Sequence. It features a backend built with Go to handle game logic, state management, and real-time communication via WebSockets. The frontend is a web client built with HTML, Tailwind CSS, Alpine.js, and JavaScript, allowing users to interact with the game in their browsers.
- Go Backend: Manages all game rules, player actions, and board state.
- WebSocket Communication: Enables real-time, bidirectional communication between the server and clients.
- HTML/CSS/JS Web Client: Provides the user interface for playing the game.
- Multiplayer Support: Allows multiple players to join and play a game concurrently.
- Core Sequence Rules Implemented:
- Card dealing and hand management.
- Placing chips on the board based on played cards.
- Special actions for Two-Eyed Jacks (wild placement) and One-Eyed Jacks (chip removal).
- Declaring "dead cards."
- Detection of sequences (5 in a row).
- Win condition checking.
- Static File Serving: The Go backend also serves the static HTML client.
- Card Emojis: Uses card suit emojis for a more visual representation on the board and in player hands.
- Valid Move Highlighting: The web client highlights possible valid moves on the board with a light background when a card is selected from the player's hand.
- Rejoin Support: Players can refresh or reconnect and will automatically rejoin their game and hand if their browser localStorage is intact.
- Backend:
- Go (Golang)
- Gorilla WebSocket (
github.com/gorilla/websocket
)
- Frontend:
- HTML5
- Tailwind CSS (via CDN)
- Alpine.js (via CDN)
- JavaScript (Vanilla)
.
├── main.go # Core Go backend server logic
├── static/
│ └── index.html # HTML web client
├── Makefile # Makefile for building, running, and cleaning the project
└── README.md # This file: Project overview, setup, and usage
-
Prerequisites:
- Ensure Go is installed on your system (version 1.18 or newer recommended).
- A modern web browser.
-
Get Dependencies: Open your terminal in the project's root directory and run:
go mod tidy
This will ensure all dependencies listed in
go.mod
are installed. -
Prepare Client File: Ensure the
static
directory exists in your project root and containsindex.html
. This file is required for the web client. -
Run the Backend Server: You can run the server in two ways:
- Using Go directly:
go run main.go
- Or, using the Makefile (recommended):
make run
You should see log messages indicating the server has started, typically on port
8008
. - Using Go directly:
-
Access the Game: Open your web browser and navigate to:
http://localhost:8008
This will load the web client, and you can start creating or joining games.
This project includes a Makefile
for convenient building and running:
-
Build the server:
make build
This compiles the Go backend and produces a
sequence-game
binary. -
Run the server:
make run
This builds (if needed) and runs the backend server. By default, it serves the web client at http://localhost:8008.
-
Clean build artifacts:
make clean
This removes the compiled binary.
- Data Structures:
Card
: Represents a playing card (Rank, Suit, ID, Emoji Display).Player
: Stores player-specific information (ID, Name, Hand, ChipColor, Connection).BoardSpace
: Represents a single cell on the game board (Card, OccupiedBy, IsCorner, IsLocked).Game
: Encapsulates the entire game state (Board, Players, DrawPile, CurrentTurn, etc.).
- Game Logic:
NewGame()
: Initializes a new game instance.initializeBoardLayout()
: Sets up the board usingboardCardDistribution
. Crucial for correct gameplay.parseCardID()
: Converts string representations fromboardCardDistribution
intoCard
objects.AddPlayer()
,StartGame()
: Manage player joining and game start.PlayAction()
,HandleDeadCard()
: Process player moves.checkForSequencesAfterPlay()
: Detects completed sequences.
- WebSocket Handling (
handleWebSocket
): Manages client connections, message routing, and game state broadcasts. - Static File Serving (
serveClient
): Serves theindex.html
client.
- WebSocket Connection: Establishes and maintains communication with the Go backend.
- UI Management:
- Game setup section (create/join game, player name).
- Game area display (board, player info, hand).
- Board Rendering: Dynamically creates the 10x10 game board based on data from the server.
- Hand Display: Shows the current player's cards.
- Action Handling:
handleCardInHandClick()
: Manages card selection from the hand.highlightValidMoves()
: Visually indicates where the selected card can be played (with a light background highlight).handleBoardCellClick()
: Sends play actions to the server when a board cell is clicked.- Handles "Declare Dead Card," "Create Game," "Join Game," and "Start Game" actions.
- State Synchronization: Updates the UI based on messages received from the server.
- Rejoin Logic: The client automatically attempts to rejoin the previous game and hand after a refresh or reconnect, using localStorage.
The accuracy of the boardCardDistribution
array in main.go
is critical for the game to function correctly according to standard Sequence rules. This array defines which card corresponds to each space on the board. Ensure it's verified and complete. The parseCardID
function is designed to handle suffixes (like _alt
) in this array for representing the second instance of a card.