Snake and Ladder is a classic board game that involves rolling a dice, moving across a numbered board, and using ladders to climb up while avoiding snakes that send players down. In this article, we’ll build a simple Snake and Ladder game using Golang, making it a great project for beginners to learn about structuring a Go application, handling randomness, and implementing game logic.
Table of contents
Open Table of contents
Setting Up the Go Project
Before we start coding, ensure you have Go installed on your system. If not, download and install it from golang.org.
Now, let’s create a new Go project:
mkdir snake_ladder_game
cd snake_ladder_game
touch main.go
Writing the Go Code
Step 1: Defining Structures
We define the Player
and Game
structures to store relevant data.
package main
import (
"fmt"
"math/rand"
"time"
)
type Player struct {
Name string
Position int
}
type Game struct {
Snakes map[int]int
Ladders map[int]int
Players []*Player
Winner *Player
}
Step 2: Initializing the Game
We create a function to initialize the board with snakes, ladders, and players.
func NewGame(players []string) *Game {
snakes := map[int]int{99: 10, 80: 20, 54: 34, 40: 3, 25: 5}
ladders := map[int]int{2: 38, 7: 14, 8: 31, 15: 26, 21: 42}
var playerList []*Player
for _, name := range players {
playerList = append(playerList, &Player{Name: name, Position: 0})
}
return &Game{Snakes: snakes, Ladders: ladders, Players: playerList}
}
Step 3: Implementing the Dice Roll
We simulate a dice roll using Go’s random number generator.
func RollDice() int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(6) + 1
}
Step 4: Handling Player Movement
We update a player’s position based on dice rolls while checking for snakes and ladders.
func (g *Game) MovePlayer(player *Player) {
roll := RollDice()
newPos := player.Position + roll
if newPos > 100 {
fmt.Printf("%s rolled %d, but stays at %d\n", player.Name, roll, player.Position)
return
}
if newPos, ok := g.Snakes[newPos]; ok {
fmt.Printf("%s landed on a snake! Moving to %d\n", player.Name, newPos)
} else if newPos, ok := g.Ladders[newPos]; ok {
fmt.Printf("%s climbed a ladder! Moving to %d\n", player.Name, newPos)
} else {
fmt.Printf("%s moved to %d\n", player.Name, newPos)
}
player.Position = newPos
if player.Position == 100 {
g.Winner = player
}
}
Step 5: Running the Game Loop
We iterate through players until one reaches the finish line.
func (g *Game) Start() {
for g.Winner == nil {
for _, player := range g.Players {
g.MovePlayer(player)
if g.Winner != nil {
fmt.Printf("🎉 %s wins the game! 🎉\n", g.Winner.Name)
return
}
}
}
}
Step 6: Main Function to Start the Game
func main() {
players := []string{"Alice", "Bob"}
game := NewGame(players)
game.Start()
}
Running the Game
To run the game, execute:
go run main.go
Example Output
Alice rolled 5 and moved to 5
Bob rolled 3 and moved to 3
Alice rolled 6 and moved to 11
Bob rolled 4 and moved to 7
Bob climbed a ladder! Moving to 14
...
🎉 Bob wins the game! 🎉
Enhancements
- Multiplayer Support: Extend to allow more players.
- Web Interface: Use Go with WebSockets for an interactive experience.
- Database Storage: Store player stats and game history.
Conclusion
This project is a great introduction to Go’s structuring, randomness handling, and game logic implementation. As a beginner, try adding more features like configurable board sizes or special rules.
Happy Coding! 🚀