Created
September 23, 2012 04:29
-
-
Save sevvie/3768874 to your computer and use it in GitHub Desktop.
Having fun with Go and the Gracious Benefactor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package doomed | |
import ("math/rand"; "fmt") | |
// UGH WHY AM I DOING IT LIKE THIS | |
const ( | |
Unused = -1 | |
NotGenerated = 0 | |
Mountain = 1 | |
Arid = 2 | |
Water = 3 | |
Desert = 4 | |
Volcano = 5 | |
ScorchedEarth = 6 | |
) | |
func RangedRandom(x, y int) int { | |
return rand.Intn(y - x) + x | |
} | |
type Vector struct { | |
x, y int | |
} | |
type Tile struct { | |
terrain int | |
x, y int | |
} | |
type Board struct { | |
grid [][]Tile | |
} | |
func (b *Board) CheckNeighboringTiles(x, y int) int { | |
// The six cardinal directions are: | |
// [-1, -1] [-1, 0] [0, -1] [0, +1] [+1, -1], [+1, 0] | |
score := 0 | |
score += b.grid[y-1][x-1].terrain | |
score += b.grid[y-1][x].terrain | |
score += b.grid[y][x-1].terrain | |
score += b.grid[y][x+1].terrain | |
score += b.grid[y+1][x-1].terrain | |
score += b.grid[y+1][x].terrain | |
return score | |
} | |
func (b *Board) Generate() { | |
height := len(b.grid) | |
width := len(b.grid[0]) | |
for y := range b.grid { | |
for x := range b.grid[y] { | |
b.grid[y][x] = Tile{NotGenerated, x, y} | |
} | |
} | |
// Here's where the fun starts. | |
bucket := make([]Tile, width*height) | |
bucket_counter := 0 | |
b.grid[height/2][width/2] = Tile{Volcano, width/2, height/2} | |
bucket[bucket_counter] = b.grid[height/2][width/2]; bucket_counter++ | |
for i := 0; i < 1; i++ { | |
lx := RangedRandom(0,16) | |
ly := RangedRandom(0,16) | |
if b.grid[ly][lx].terrain != 0 { i--; continue } | |
if b.CheckNeighboringTiles(lx, ly) != 0 { i--; continue } | |
b.grid[ly][lx].terrain = Water | |
bucket[bucket_counter] = b.grid[ly][lx]; bucket_counter++ | |
} | |
for i := 0; i < 7; i++ { | |
lx := RangedRandom(0,16) | |
ly := RangedRandom(0,16) | |
if(b.grid[ly][lx].terrain != 0) { i--; continue } | |
if b.CheckNeighboringTiles(lx, ly) != 0 { i--; continue } | |
lt := RangedRandom(0,6) | |
b.grid[ly][lx].terrain = lt | |
bucket[bucket_counter] = b.grid[ly][lx]; bucket_counter++ | |
} | |
fmt.Printf("...\n"); | |
} | |
func (b *Board) SetTileType(pos Vector) { | |
} | |
func NewBoard(x, y int) *Board { | |
b := new(Board) | |
b.grid = make([][]Tile, y) | |
for i := range b.grid { | |
b.grid[i] = make([]Tile, x) | |
} | |
return b | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment