Skip to content

An exemplar project to illustrate drawing a grid in a WPF application

Notifications You must be signed in to change notification settings

MrHallKSC/GridExemplarWPF

Repository files navigation

Grid Exemplar WPF - Understanding Core Concepts for A-Level NEA Projects

Important Note: This entire project, including all code and this README documentation, was generated by Claude AI (Anthropic) as an educational resource for A-Level Computer Science teachers and students. It is intended to demonstrate concepts and techniques, not to serve as a template for student NEA projects.

Overview

This exemplar WPF application demonstrates fundamental programming concepts required for AQA 7517 A-Level Computer Science NEA projects. It illustrates how to create a grid-based application using WPF and C#, showing key techniques that students should understand when developing their own projects.

Important: This exemplar is designed to teach concepts, not to be used as a starting point for your NEA project. Your NEA must be your own original work, addressing a problem you have identified and designed yourself.

Project Structure

The project consists of three main files:

  1. Square.cs - A custom class representing a single grid cell (AI-generated)
  2. MainWindow.xaml - The user interface layout definition (AI-generated)
  3. MainWindow.xaml.cs - The code-behind containing the application logic (AI-generated)

Understanding AQA 7517 Technical Skills

This exemplar demonstrates specific skills from the AQA specification that are commonly needed in grid-based or maze-style projects. Understanding these concepts will help you implement similar features in your own, original project.

Skills from Group B (Table 1, Section 4.14.3.4.1)

This exemplar demonstrates the following Group B technical skills:

Data Structures:

  • Multi-dimensional arrays: The Square[,] grid array stores the grid structure
  • Records/Classes: The Square class encapsulates cell properties
  • Simple OOP model: Custom class with properties and methods

Algorithms:

  • Simple user-defined algorithms: Coordinate conversion, wall randomization, click detection
  • File handling concepts: Though not implemented here, the structure supports reading/writing grid configurations

Skills from Group A (Table 1, Section 4.14.3.4.1)

To achieve Group A level in your NEA, you would need to incorporate more advanced techniques such as:

Data Structures:

  • Complex data models (e.g., interlinked database tables)
  • Advanced structures (stacks, queues, graphs, trees)
  • Complex OOP with inheritance, polymorphism, interfaces

Algorithms:

  • Graph/tree traversal algorithms
  • Recursive algorithms
  • Complex user-defined algorithms (optimization, pathfinding, scheduling)
  • Advanced sorting (merge sort or similar)

This exemplar focuses on Group B level skills. Your NEA project should aim higher.

Core Programming Concepts Explained

1. Object-Oriented Programming

The Square class demonstrates encapsulation - bundling data and methods that operate on that data:

public class Square
{
    // Properties store the state
    public int X { get; set; }
    public bool NorthWall { get; set; }
    
    // Methods operate on that state
    public bool ContainsPoint(double pointX, double pointY)
    {
        return pointX >= X && pointX < X + Size &&
               pointY >= Y && pointY < Y + Size;
    }
}

Key Concept: Each object knows its own data and how to work with it. This makes code more maintainable and easier to reason about.

2. 2D Arrays for Grid Representation

A 2D array naturally represents a grid structure:

private Square[,] grid;
grid = new Square[gridRows, gridCols];

Why this matters:

  • Direct mapping between logical structure (rows/columns) and data structure
  • Easy access to adjacent cells: grid[row-1, col] is the cell above
  • Essential for algorithms that need to navigate or analyze grid patterns
  • Common in maze generation, pathfinding, game boards, cellular automata

3. Coordinate System Conversion

Converting between logical (grid) and physical (pixel) coordinates:

int x = col * SQUARE_SIZE;  // Grid position → Screen position
int y = row * SQUARE_SIZE;

Key Concept: Programs often need to work with multiple representations of the same data. Understanding how to convert between them is crucial.

4. Modular Wall-Based Design

Each square has four independent walls:

public bool NorthWall { get; set; }
public bool EastWall { get; set; }
public bool SouthWall { get; set; }
public bool WestWall { get; set; }

Why this design choice:

  • Maze algorithms work by removing walls between cells
  • Allows precise control over cell boundaries
  • More flexible than drawing complete rectangles
  • Demonstrates data modeling for specific problem domains

5. Event-Driven Programming

WPF applications respond to events:

private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Point clickPoint = e.GetPosition(DrawingCanvas);
    // Respond to user action
}

Key Concept: The program waits for user actions rather than executing sequentially from start to finish. This is fundamental to interactive applications.

6. Input Validation

Safely handling user input:

if (int.TryParse(WidthTextBox.Text, out int newWidth))
{
    if (newWidth >= 1 && newWidth <= 50)
    {
        // Safe to use newWidth
    }
}

Key Concept: Never trust user input. Always validate before using it. This prevents crashes and security issues.

WPF Fundamentals

What is WPF?

Windows Presentation Foundation (WPF) is a framework for creating desktop applications. It separates the visual design (XAML) from the logic (C#).

Key WPF Concepts Demonstrated

1. Layout Containers

  • DockPanel: Arranges children by docking them to edges (top, bottom, left, right)
  • StackPanel: Stacks children vertically or horizontally
  • Canvas: Allows precise positioning using coordinates

2. Controls

  • Button: Triggers actions when clicked
  • TextBox: Accepts user input
  • TextBlock: Displays read-only text
  • Menu/MenuItem: Provides organized commands

3. Event Handlers

Events link user actions to code:

<Button Content="Apply" Click="ApplyGridSize_Click"/>

When clicked, the ApplyGridSize_Click method in the code-behind executes.

4. Drawing on Canvas

Line line = new Line
{
    X1 = startX, Y1 = startY,
    X2 = endX, Y2 = endY,
    Stroke = Brushes.Black,
    StrokeThickness = 2
};
Canvas.SetLeft(line, 0);
Canvas.SetTop(line, 0);
DrawingCanvas.Children.Add(line);

Key Concept: Graphics are created as objects and added to the canvas. You have full control over each visual element.

Algorithm Analysis

Linear Search for Click Detection

for (int row = 0; row < gridRows; row++)
{
    for (int col = 0; col < gridCols; col++)
    {
        if (grid[row, col].ContainsPoint(clickPoint.X, clickPoint.Y))
        {
            // Found the clicked square
            return;
        }
    }
}

Analysis:

  • Time Complexity: O(n) where n is the total number of squares
  • This is acceptable for small grids but could be optimized for larger ones
  • Alternative: Calculate grid position directly from coordinates - O(1)

Random Number Generation

private Random random = new Random();

public void RandomiseWalls(Random random)
{
    NorthWall = random.Next(2) == 1;  // 50% probability
}

Key Concept: Create Random once and reuse it. Creating multiple instances quickly produces identical sequences.

Coding Style Characteristics (Table 2, Section 4.14.3.4.2)

Basic Characteristics (Present in this exemplar)

  • ✓ Meaningful identifier names (gridRows, DrawSquare)
  • ✓ Effective annotation where code is complex
  • ✓ Consistent style throughout

Good Characteristics (Present in this exemplar)

  • ✓ Well-designed user interface (menu bar, control panel, canvas)
  • ✓ Modularisation of code (separate methods for distinct tasks)
  • ✓ Good use of local variables
  • ✓ Minimal use of global variables
  • ✓ Use of constants (SQUARE_SIZE)
  • ✓ Appropriate indentation
  • ✓ Self-documenting code (clear variable names, logical structure)

Excellent Characteristics (Partially demonstrated)

  • ✓ Modules (methods) with appropriate parameters
  • ✓ Cohesive methods (each does one thing well)
  • ~ Defensive programming (input validation present)
  • ~ Exception handling (basic error checking, could be more comprehensive)

Key Takeaways for Your NEA Project

1. Choose Appropriate Data Structures

This exemplar uses a 2D array because it naturally represents a grid. For your project:

  • Consider what data structure best represents your problem
  • A list? A tree? A graph? A database?
  • The choice should make your algorithms simpler, not more complex

2. Encapsulation Simplifies Code

The Square class knows how to check if it contains a point. This is better than having that logic scattered throughout the code.

Principle: Objects should manage their own data and behavior.

3. Separate UI from Logic

WPF separates the visual design (XAML) from the code (C#). This makes both easier to modify independently.

Principle: Keep different concerns separate.

4. Plan for User Input

Users make mistakes. Your program should:

  • Validate all input
  • Provide clear error messages
  • Prevent invalid states

5. Document Your Thinking

Comments should explain why you made a choice, not just what the code does:

// Using Canvas because we need precise pixel positioning for drawing lines
// StackPanel or Grid would not give us this control

Understanding vs. Copying

Important for NEA Success:

This exemplar teaches concepts you should understand and apply in your own way:

  • ✓ Understand how 2D arrays represent grids
  • ✓ Understand event-driven programming
  • ✓ Understand coordinate system conversion
  • ✗ Don't copy this code into your NEA
  • ✗ Don't use this as a template

Your NEA must:

  • Solve a problem you have identified
  • Be designed by you
  • Use these concepts in your own way
  • Demonstrate your own problem-solving skills

Running the Project

  1. Open the solution in Visual Studio 2022 or later
  2. Ensure you have .NET Desktop Development workload installed
  3. Build and run the project (F5)
  4. Experiment with the features to understand how they work
  5. Read the code with the comments to understand the implementation

AQA 7517 Specification Alignment

This exemplar addresses content from:

  • Section 4.1: Fundamentals of Programming
  • Section 4.2: Fundamentals of Data Structures (2D arrays)
  • Section 4.4.1: Abstraction and automation
  • Section 4.13: Systematic approach to problem solving

The technical skills demonstrated align with Group B of Table 1 (Section 4.14.3.4.1). To achieve higher marks in your NEA, you should incorporate Group A skills appropriate to your chosen problem.

Further Reading

About This Resource

Created by: Claude AI (Anthropic)
Purpose: Educational exemplar for AQA 7517 A-Level Computer Science
Date: November 2024
License: Provided for educational purposes for AQA 7517 A-Level Computer Science students and teachers

This resource was created as a teaching tool to demonstrate programming concepts and techniques. It is not intended to be submitted as student work or used as a template for NEA projects.


Remember: The purpose of this exemplar is to help you understand concepts. Your NEA project must be your own original work addressing your own problem. Use these concepts as building blocks, but create your own solution.

About

An exemplar project to illustrate drawing a grid in a WPF application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages