/gridsum

Grid Sum

Primary LanguageGo

GridSum Game

A Sudoku-like number puzzle game where players must select cells in a grid to match target sums for each row and column.

Game Rules

  • The game board is a square grid of size NxN (where N is between 3 and 9)
  • Each cell contains a number between 1 and 9
  • Players must select cells in each row and column to match the target sums
  • A cell can be selected or deselected
  • The game is won when the sum of selected cells in each row and column matches their respective target sums

Difficulty Levels

The game offers four difficulty levels:

  1. Easy:

    • Each row and column has only one possible combination to reach the target sum
    • Target sums are achieved by selecting a single cell or a small number of cells
    • Perfect for beginners
  2. Medium:

    • Each row and column has 2-3 possible combinations to reach the target sum
    • Target sums require selecting multiple cells
    • Good for players who understand the basic mechanics
  3. Hard:

    • Each row and column has 3-5 possible combinations to reach the target sum
    • Target sums require selecting multiple cells with overlapping possibilities
    • Challenging for experienced players
  4. Very Hard:

    • Each row and column has 5+ possible combinations to reach the target sum
    • Target sums require selecting multiple cells with many overlapping possibilities
    • For expert players seeking maximum challenge

API Endpoints

Get New Game Board

GET /game/{size}/{difficulty}

Parameters:

  • size: Board size (3-9)
  • difficulty: Difficulty level (easy, medium, hard, very-hard)

Response:

{
  "board": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  "targetRowSums": [6, 15, 24],
  "targetColSums": [12, 15, 18],
  "boardSize": 3,
  "difficulty": "medium"
}

API Usage Examples

Starting the Server

go run main.go

The server will start on port 8080 by default.

API Examples

  1. Create a new 3x3 easy game:
curl http://localhost:8080/game/3/easy
  1. Create a new 5x5 medium game:
curl http://localhost:8080/game/5/medium
  1. Create a new 9x9 very hard game:
curl http://localhost:8080/game/9/very-hard

Example Response

{
  "board": [
    [7, 4, 2, 8, 1],
    [3, 9, 5, 6, 4],
    [8, 2, 7, 1, 9],
    [5, 1, 4, 3, 7],
    [2, 6, 9, 5, 8]
  ],
  "targetRowSums": [15, 18, 16, 13, 20],
  "targetColSums": [17, 16, 18, 14, 17],
  "boardSize": 5,
  "difficulty": "medium"
}

Error Responses

  1. Invalid board size:
{
  "error": "Invalid board size. Size must be between 3 and 9"
}
  1. Invalid difficulty level:
{
  "error": "Invalid difficulty level. Must be one of: easy, medium, hard, very-hard"
}

Game Flow

  1. Request a new game board using the API
  2. Present the board to the player
  3. Let the player select/deselect cells
  4. Calculate sums for each row and column
  5. Compare with target sums to check if the solution is correct

Notes

  • The server handles requests concurrently
  • Each request generates a unique board
  • All generated boards are guaranteed to have at least one valid solution
  • Target sums are always achievable using the numbers in the grid
  • The API response includes all necessary information to play the game

Performance Requirements

  • Board generation time should be under 100ms
  • Solution validation time should be under 100ms
  • The game must have at least one valid solution
  • Target sums must be achievable using the numbers in the grid