Home >Backend Development >Golang >A simple Go simulation - concurrency issues
php Editor Shinichi brings you a simple but interesting Go simulation game called "Concurrency Problem". This game takes concurrent programming as its theme, allowing players to experience the charm of concurrent programming in a virtual world. In the game, players need to write code to handle the simultaneous execution of multiple tasks and test their concurrent programming abilities. The game interface is concise and clear, and the operation is simple, suitable for beginners to get started, and it also provides multiple difficulty and challenge modes for players to choose from. Whether you are a beginner or an experienced developer, you can enjoy the fun of concurrent programming in this simulation game.
I am a student from Poland and this semester I started a concurrent programming course (Go, Ada and some theoretical and CSP languages in the future). To be honest, Golang looks interesting but I'm a bit confused. The bottom line is that in my experience I would call myself a below average programmer. Basically, my task is to create a simulation, which I will describe like this:
There is an n*m grid
Travelers can be generated randomly, up to k travelers, each traveler has a unique ID (1, 2, 3, etc., up to k)
At a random moment, if the space is free (I'm sure the free space is 0), the traveler can move up, left, right or down on the grid
There is also a camera that sometimes prints the current state of the mesh as well as recent movements (not implemented yet)
Unofficially, I've heard that I should use channels, whatever that means
My idea is to create a structure with the id and coordinates of each traveler and send their id to a channel that represents willingness to move, and then I will randomly choose the direction of movement.
I'm a little confused about concurrency - not only if and where should I use wgs and mutexes, but also if for example if I do go func(){} should the loop be inside or outside. I would be very happy to appreciate any tips, help or fixes/ideas to fix my code because currently, as you guessed, it is not working properly (e.g. when the camera prints the grid, sometimes there are more than k travelers , where multiple travelers share the same number and sometimes they seem to disappear). Hope everyone has a great day and I'd really appreciate any help :)
package main; import( "fmt" "os" "strconv" "math/rand" //"sync" "time" ) type traveler struct{ id int; x int; y int; } func main(){ //command line n, err := strconv.Atoi(os.Args[1]); m, err := strconv.Atoi(os.Args[2]); k, err := strconv.Atoi(os.Args[3]); if err != nil{ panic(err) return } //board var grid [][]int; grid = make([][]int, n) for i:=0; i<n; i++{ grid[i] = make([]int, m) } //array of travelers, channel for moves and id travelers := make([]traveler, k) no_of_travelers := 0; move := make(chan int, k); id := 1; //var wg sync.WaitGroup go camera(grid); go func() { for i:=0; i<len(travelers); i++ { if no_of_travelers<k{ travelers[i] = spawnTraveler(&id,grid); no_of_travelers++; } } }() go func() { for{ a:= rand.Intn(k); sendMoveTraveler(&travelers[a], move); } }() receiveMoveTraveler(travelers, move, grid); } func spawnTraveler(id *int, grid [][]int) traveler{ x:=-1; y:=-1; for{ x = rand.Intn(len(grid)); y = rand.Intn(len(grid)); if(grid[x][y]==0){ break; } } t := traveler{id: *id, x: x, y:y}; grid[x][y] = *id; *id++; return t; } func sendMoveTraveler(t *traveler, move chan int){ move <- t.id } func receiveMoveTraveler(travelers []traveler, move chan int, grid [][]int){ for{ id := <- move for i:=0; i<len(travelers); i++{ if travelers[i].id == id{ direction := rand.Intn(4); //1-left 2-up 3-right 4-down switch direction { case 0: if travelers[i].x>0 && grid[travelers[i].x-1][travelers[i].y] == 0{ grid[travelers[i].x-1][travelers[i].y] = grid[travelers[i].x][travelers[i].y]; grid[travelers[i].x][travelers[i].y] = 0; travelers[i].x = travelers[i].x-1; travelers[i].y = travelers[i].y; } case 1: if travelers[i].y>0 && grid[travelers[i].x][travelers[i].y-1] == 0{ grid[travelers[i].x][travelers[i].y-1] = grid[travelers[i].x][travelers[i].y]; grid[travelers[i].x][travelers[i].y] = 0; travelers[i].x = travelers[i].x; travelers[i].y = travelers[i].y-1; } case 2: if travelers[i].x<len(grid)-1 && grid[travelers[i].x+1][travelers[i].y] == 0{ grid[travelers[i].x+1][travelers[i].y] = grid[travelers[i].x][travelers[i].y]; grid[travelers[i].x][travelers[i].y] = 0; travelers[i].x = travelers[i].x+1; travelers[i].y = travelers[i].y; } case 3: if travelers[i].y<len(grid)-1 && grid[travelers[i].x][travelers[i].y+1] == 0{ grid[travelers[i].x][travelers[i].y+1] = grid[travelers[i].x][travelers[i].y]; grid[travelers[i].x][travelers[i].y] = 0; travelers[i].x = travelers[i].x; travelers[i].y = travelers[i].y+1; } } //fmt.Println("Ściagnalem ruch", travelers[i].id); } } } } func camera(grid [][]int){ for{ for i:=0; i<len(grid); i++{ for j:=0; j<len(grid); j++{ if grid[i][j]!= 0{ fmt.Printf("%02d ", grid[i][j]); } else{ fmt.Printf("-- "); } } fmt.Println(); } fmt.Println(); time.Sleep(time.Second * 3); } }
I'm a little overwhelmed by all the ideas - wgs, mutexes, atoms, etc.
That being said:
The above is the detailed content of A simple Go simulation - concurrency issues. For more information, please follow other related articles on the PHP Chinese website!