60 lines
1016 B
Go
Raw Normal View History

2017-09-27 06:19:46 -07:00
package main
import (
"errors"
2017-09-27 06:19:46 -07:00
"fmt"
"log"
2017-09-27 06:19:46 -07:00
)
func main() {
const boardY int = 7
const boardX int = 12
2017-09-27 06:19:46 -07:00
board, err := makeBoard(boardY, boardX, "_")
if err != nil {
log.Fatal(err)
}
2017-09-27 06:19:46 -07:00
// Pretend that the player did something in the middle
playerY := boardY / 2
playerX := boardX / 2
2017-09-27 06:19:46 -07:00
board[playerY][playerX] = "@"
2017-09-27 06:19:46 -07:00
printBoard(board)
2017-09-27 06:19:46 -07:00
}
func makeBoard(sizeY, sizeX int, symbol string) ([][]string, error) {
2017-09-27 06:19:46 -07:00
//Make an array of empty strings
// Rows
if sizeY < 1 || sizeX < 1 {
err := errors.New("Board 'x' and 'y' values must both be greater than 1!")
return nil, err
}
board := make([][]string, sizeY)
2017-09-27 06:19:46 -07:00
for y := 0; y < sizeY; y++ {
2017-09-27 06:19:46 -07:00
//Make each array an array of empty strings
//cols
board[y] = make([]string, sizeX)
2017-09-27 06:19:46 -07:00
// and populate em
for x := 0; x < sizeX; x++ {
board[y][x] = symbol
2017-09-27 06:19:46 -07:00
}
}
return board, nil
2017-09-27 06:19:46 -07:00
}
func printBoard(board [][]string) {
2017-09-27 06:19:46 -07:00
for y := 0; y < len(board); y++ {
for x := 0; x < len(board[y]); x++ {
fmt.Print(board[y][x])
}
fmt.Println()
}
}