Algorithm Game Introduction
The main goal of this site is efficiency. We hope readers can quickly master algorithms and pass interview and written tests.
Besides that, I also hope the learning process can be interesting. If you only do problems for interviews, it can get boring.
If readers can really feel the charm of algorithms and think of learning as an exploration, then you will have inner motivation. This not only makes you learn better, but also adds some fun.
Finding a job these days is stressful. Algorithm problems are now a must for most tech jobs. So why not make it a bit more fun?
After some research, I think combining algorithm learning with games is a good idea. It’s interesting, and it also helps you see how algorithms work in real situations. You get both fun and knowledge.
Gamification vs Visualization
If we already have Visualization Panels, why do we need algorithm games?
I have thought about this. I think both are needed because they solve different problems.
The visualization panel answers questions like: What happens step by step in this code? Why do we do n + 1
and not n + 2
?
Algorithm games answer different questions: In what real scenarios can we use this algorithm? How do we use it?
So, in my game designs, it’s not just about showing how the algorithm runs. The game combines real scenarios, and asks players to use data structures and algorithms together to finish tasks. You can see how algorithms work in the game.
Below is a simple guide to using the game panel on this site.
How to Use the Game Panel
At the top right of the panel, there is a full-screen
On the left is the game screen. On the right, the top part is the problem description, and the bottom part is the code editor. You need to write code in the editor to solve the problem. Then click "Submit" to run your code in the game engine. You can interact with the game to see if your code works.
Below is a "Snake" game panel. If you click "Begin", the snake won’t move at first. This problem is about how double linked lists work in real life. You need to add the logic so the snake can move.
Write the right code in the editor, click "Submit" to send it to the game engine, then click "Begin". Now the game will run, and you have a playable mini-game:
Sample solution:
// The game panel only supports submitting JavaScript code
// Java solution is provided to help readers understand the algorithm logic
public void move(List<Point> snake, String direction, Point foodPosition) {
Point currentHead = snake.get(0);
// Calculate new head position
Point newHeadPos = new Point(currentHead.x, currentHead.y);
switch (direction) {
case "right":
newHeadPos.x += 1;
break;
case "left":
newHeadPos.x -= 1;
break;
case "up":
newHeadPos.y += 1;
break;
case "down":
newHeadPos.y -= 1;
break;
default:
throw new IllegalArgumentException("Invalid direction");
}
// Add new head node
snake.add(0, newHeadPos);
if (!newHeadPos.equals(foodPosition)) {
// Remove tail node if not eating food
snake.remove(snake.size() - 1);
}
}
// The game panel only supports submitting JavaScript code
// C++ solution is provided to help readers understand the algorithm logic
void move(vector<Point>& snake, const string& direction, const Point& foodPosition) {
Point currentHead = snake.front();
// Calculate new head position
Point newHeadPos(currentHead.x, currentHead.y);
if (direction == "right") {
newHeadPos.x += 1;
} else if (direction == "left") {
newHeadPos.x -= 1;
} else if (direction == "up") {
newHeadPos.y += 1;
} else if (direction == "down") {
newHeadPos.y -= 1;
} else {
throw invalid_argument("Invalid direction");
}
// Add new head node
snake.insert(snake.begin(), newHeadPos);
if (!newHeadPos.equals(foodPosition)) {
// Remove tail node if not eating food
snake.pop_back();
}
}
# The game panel only supports submitting JavaScript code
# Python solution is provided to help readers understand the algorithm logic
def move(snake, direction, food_position):
current_head = snake[0]
# Calculate new head position
new_head_pos = Point(current_head.x, current_head.y)
if direction == 'right':
new_head_pos.x += 1
elif direction == 'left':
new_head_pos.x -= 1
elif direction == 'up':
new_head_pos.y += 1
elif direction == 'down':
new_head_pos.y -= 1
else:
raise ValueError('Invalid direction')
# Add new head node
snake.insert(0, new_head_pos)
if not new_head_pos.equals(food_position):
# Remove tail node if not eating food
snake.pop()
// The game panel only supports submitting JavaScript code
// Go solution is provided to help readers understand the algorithm logic
func move(snake *[]Point, direction string, foodPosition Point) {
currentHead := (*snake)[0]
// Calculate new head position
newHeadPos := Point{X: currentHead.X, Y: currentHead.Y}
switch direction {
case "right":
newHeadPos.X += 1
case "left":
newHeadPos.X -= 1
case "up":
newHeadPos.Y += 1
case "down":
newHeadPos.Y -= 1
default:
panic("Invalid direction")
}
// Add new head node
*snake = append([]Point{newHeadPos}, *snake...)
if !newHeadPos.Equals(foodPosition) {
// Remove tail node if not eating food
*snake = (*snake)[:len(*snake)-1]
}
}
var move = function(snake, direction, foodPosition) {
const currentHead = snake[0]
// Calculate new head position
const newHeadPos = new Point(currentHead.x, currentHead.y)
switch (direction) {
case 'right':
newHeadPos.x += 1
break
case 'left':
newHeadPos.x -= 1
break
case 'up':
newHeadPos.y += 1
break
case 'down':
newHeadPos.y -= 1
break
default:
throw new Error('Invalid direction')
}
// Add new head node
snake.unshift(newHeadPos)
if (!newHeadPos.equals(foodPosition)) {
// Remove tail node if not eating food
snake.pop()
}
}
Game Collection
Game chapters are spread throughout this site (look for sections marked with
The game features are still being tested. I have many fun ideas, but only some are live now. More games are coming soon~
Snake Game
This game tests how to use doubly linked lists in practice. See Implement Snake Game:
Match-Three Game
This game tests the double pointer technique for arrays. See Implement Match-Three Game:
Huarong Dao
Huarong Dao is an advanced puzzle for the BFS algorithm framework. It is very interesting. I suggest you play music and try to solve it. For solutions, see Implement Huarong Dao Game:
Connect-Two Game
This game tests the BFS algorithm. See Implement Connect-Two Game:
One Stroke Puzzle
This game tests Eulerian graphs. See Eulerian Graphs and One-Stroke Puzzle:
Minesweeper Game
The minesweeper game tests several algorithms.
First, it tests random algorithms. You need to know the shuffling and reservoir sampling algorithms. See Implement Minesweeper Game:
Usually, we use the Monte Carlo method to check if the random algorithm is fair. The site has a Monte Carlo checker. You can use it to test your minesweeper random map algorithm:
Minesweeper can also test DFS algorithms. See Implement Minesweeper Game II:
You can also try to write an algorithm to solve minesweeper, making a "minesweeper cheat tool":
Sudoku Game
Use backtracking to solve any sudoku puzzle, any difficulty. See Implement Sudoku Game:
Maze Game
Use DFS or BFS to solve mazes. See Finish Maze Game:
Maze Map Generation
Use random algorithms to generate maze maps. You can use DFS, minimum spanning tree, or recursive split algorithm:
Game of Life
Game of Life tests basic array skills and bit manipulation. See Implement Game of Life: