Exercise 1
Game Concept:
Match Blocks: A classic match-3 puzzle game where players swap adjacent blocks to form lines of three or more identical blocks. The game includes power-ups that can be purchased via in-app purchases to clear larger sections or shuffle the board.
Design/Implementation Challenge:
Implement a board shuffling algorithm that ensures the new board after shuffling has at least one possible match.
Key Concepts:
- Algorithms: Board generation and shuffling
- Design Patterns: Singleton for game manager
- Event Logic: Triggering events post-shuffle
Solution Strategies:
Use the Fisher-Yates shuffle algorithm to randomize the board. After shuffling, iterate through the board to check for any existing matches. If no matches are found, reshuffle until a valid board is created. Implement this within a GameManager using the Singleton pattern to manage game states and events.
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
private int width = 8;
private int height = 8;
private GameObject[,] board;
void Awake()
{
Instance = this;
}
public void ShuffleBoard()
{
do
{
// Shuffle logic here
} while (!HasPossibleMatch());
}
bool HasPossibleMatch()
{
// Check for possible matches
return true; // Placeholder
}
}
Exercise 2
Game Concept:
Physics Puzzle: Players must navigate a ball through various obstacles by tilting their device. Each level presents different physics-based challenges, such as moving platforms and barriers. In-app purchases offer level skips or additional levels.
Design/Implementation Challenge:
Create a tilt-controlled movement system using Unity’s physics to allow smooth ball navigation.
Key Concepts:
- Simple Physics: Rigidbody and collision handling
- Event Logic: Device tilt input
- Mathematics: Vector calculations for movement
Solution Strategies:
Attach a Rigidbody to the ball and use Input.acceleration to detect device tilt. Convert the acceleration vector to force inputs applied to the Rigidbody, ensuring smooth and responsive movement. Clamp the force values to prevent excessive speed.
public class BallController : MonoBehaviour
{
public float speed = 10f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector3 tilt = Input.acceleration;
Vector3 force = new Vector3(tilt.x, 0, tilt.y) * speed;
rb.AddForce(force);
}
}
Exercise 3
Game Concept:
Number Slider: A puzzle game where players slide numbered tiles on a grid to arrange them in sequential order. In-app purchases provide hints or additional challenging levels.
Design/Implementation Challenge:
Implement an efficient algorithm to determine if the current puzzle state is solvable.
Key Concepts:
- Algorithms: Inversion count for solvability
- Mathematics: Permutation parity
- Design Patterns: MVC for separating logic
Solution Strategies:
Calculate the number of inversions in the tile arrangement. For odd grid widths, a puzzle is solvable if the number of inversions is even. Implement this check in the game’s logic controller before presenting the puzzle to the player.
public bool IsSolvable(int[] tiles)
{
int inversions = 0;
for(int i = 0; i < tiles.Length; i++)
{
for(int j = i + 1; j < tiles.Length; j++)
{
if(tiles[i] > tiles[j]) inversions++;
}
}
return inversions % 2 == 0;
}
Exercise 4
Game Concept:
Color Connect: Players connect same-colored dots with lines without overlapping. The game increases in difficulty with more colors and obstacles. In-app purchases offer special tools or color palettes.
Design/Implementation Challenge:
Create an algorithm to prevent overlapping lines and ensure valid connections between dots.
Key Concepts:
- Algorithms: Line intersection detection
- Event Logic: User input handling
- Design Patterns: Observer for updating connections
Solution Strategies:
Implement a line-drawing system where each new line checks for intersections with existing lines using line segment intersection algorithms. If an overlap is detected, prevent the connection. Use the Observer pattern to update connected lines dynamically as the player interacts.
public bool CheckIntersection(LineRenderer newLine, List<LineRenderer> existingLines)
{
foreach(var line in existingLines)
{
if (LinesIntersect(newLine, line))
return true;
}
return false;
}
bool LinesIntersect(LineRenderer line1, LineRenderer line2)
{
// Implement line intersection logic
return false; // Placeholder
}
Exercise 5
Game Concept:
Puzzle Drops: Players drop colored tiles into a grid, aiming to create specific patterns or matches. Successful patterns clear tiles and earn points. In-app purchases allow players to buy special tiles or additional attempts.
Design/Implementation Challenge:
Develop an efficient matching system that detects and clears patterns in real-time as tiles are placed.
Key Concepts:
- Algorithms: Pattern matching and flood fill
- Mathematics: Grid-based coordinate handling
- Event Logic: Real-time update and tile clearing
Solution Strategies:
Use a flood fill algorithm to detect contiguous matching tiles whenever a new tile is placed. Iterate through the grid to identify patterns that meet the clearing criteria. Once detected, trigger a tile-clearing event and update the grid accordingly. Optimize by only checking areas affected by the latest tile placement.
public void CheckAndClearMatches(int x, int y)
{
List<Tile> matchedTiles = FloodFill(x, y);
if(matchedTiles.Count >= 3)
{
foreach(var tile in matchedTiles)
Destroy(tile.gameObject);
// Trigger scoring event
}
}
List<Tile> FloodFill(int x, int y)
{
// Implement flood fill to find matching tiles
return new List<Tile>(); // Placeholder
}