Skip to content

gregpuzzles1/latin_squares

Repository files navigation

Latin Squares App

A Flutter mobile application that generates and displays 9x9 Latin squares with customizable starting orders.

Description

This app showcases mathematical Latin squares - 9x9 grids where each number from 1 to 9 appears exactly once in every row and column. Users can:

  • View a default Latin square on app launch
  • Enter a custom starting order (1-9) to generate different Latin square patterns
  • See the square displayed in a responsive grid with clear borders

Algorithm

The app uses a row rotation algorithm for generating Latin squares:

  1. First Row: Starting with the chosen order k, create a sequence [k, k+1, ..., 9, 1, ..., k-1]
  2. Subsequent Rows: Each row is a left-rotation of the previous row by one position

Example (Starting Order = 3):

Row 0: 3 4 5 6 7 8 9 1 2
Row 1: 4 5 6 7 8 9 1 2 3
Row 2: 5 6 7 8 9 1 2 3 4
...

This guarantees:

  • ✅ Every row contains 1-9 exactly once
  • ✅ Every column contains 1-9 exactly once
  • ✅ Deterministic: same starting order always produces the same square
  • ✅ Performance: O(81) operations, computed instantly

Features

Default Square: Launches with starting order 1 Custom Starting Order: Input any number 1-9 to regenerate Input Validation: Clear error messages for invalid inputs Responsive Grid: Adapts to different screen sizes and orientations Instant Feedback: Square regenerates immediately on valid input Updated Heading: The blue bar at the top of the page now displays "Latin Squares Demonstration" for clarity

Setup Instructions

Prerequisites

  • Flutter SDK 3.0.0 or higher
  • Dart SDK 3.0.0 or higher
  • iOS 12+ or Android 6.0+ device/emulator

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd latin_squares
  2. Install dependencies:

    flutter pub get
  3. Run the app:

    flutter run
  4. Run tests:

    flutter test
  5. Run with coverage:

    flutter test --coverage

Project Structure

lib/
├── main.dart                    # App entry point and UI
├── models/
│   ├── latin_square.dart        # Immutable Latin square data model
│   └── latin_square_generator.dart  # Generator with row rotation algorithm
├── widgets/
│   └── latin_square_grid.dart   # Responsive 9x9 grid widget
└── utils/
    └── starting_order_input.dart # Input parsing and validation

test/
├── models/                       # Unit tests for models and generator
├── widgets/                      # Widget tests for UI components
└── utils/                        # Validation logic tests

integration_test/
└── app_test.dart                 # End-to-end integration tests

Architecture

The app follows a library-first design principle:

  • Pure Dart Models (lib/models/): Business logic with zero Flutter dependencies
  • Flutter UI Layer (lib/widgets/, lib/main.dart): UI components that consume models
  • TDD Approach: All code test-driven (37 unit/widget tests, 100% pass rate)

Testing

Unit Tests (21 tests)

  • test/models/latin_square_test.dart: Model creation, valueAt, getRow, getColumn, equality
  • test/models/latin_square_generator_test.dart: Determinism, row/column uniqueness, value range, algorithm correctness

Widget Tests (5 tests)

  • test/widgets/latin_square_grid_test.dart: GridView presence, 81 cells, correct values, borders, responsive sizing

Input Validation Tests (12 tests)

  • test/utils/starting_order_input_test.dart: Valid inputs (1-9), invalid inputs (0, 10, non-numbers), error messages

Integration Tests (5 scenarios)

  • integration_test/app_test.dart: Full user flows, default square, custom input, validation, determinism

Test Coverage: >80% (all critical paths covered)

Known Limitations

  • Grid Size: Fixed 9x9 (not configurable)
  • Values: Numbers only (no symbols, letters, or custom values)
  • Platforms: Mobile (iOS/Android) and Web (Flutter web)
  • Storage: No persistence (squares regenerated on restart)

Performance

  • App Launch: Latin square displays within <1 second ✅
  • Regeneration: Completes within <2 seconds of valid input ✅
  • App Size: <50MB ✅
  • UI: 60fps smooth scrolling/resizing ✅

Validation Guarantees

All generated squares are mathematically guaranteed to be valid Latin squares through contract tests:

  1. Determinism: Same order → same square (always)
  2. Row Uniqueness: Each row has 1-9 exactly once
  3. Column Uniqueness: Each column has 1-9 exactly once
  4. Completeness: All 81 cells populated
  5. Value Range: All values in [1,9]

Constitution Compliance

This project follows the Latin Squares Constitution v1.0.0:

  • Principle I: Library-first design (pure Dart models, Flutter UI layer)
  • Principle II: TDD approach (37 tests, Red-Green-Refactor cycles)
  • Principle III: Algorithm correctness (contract tests, mathematical guarantees)
  • Principle IV: Documentation (dartdoc on all public APIs)
  • Principle V: Simplicity (zero external dependencies, <1000 LOC)

About

A latin squares project coded with spec-kit

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors