60 lines
1016 B
Go
60 lines
1016 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
func main() {
|
|
const boardY int = 7
|
|
const boardX int = 12
|
|
|
|
board, err := makeBoard(boardY, boardX, "_")
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Pretend that the player did something in the middle
|
|
playerY := boardY / 2
|
|
playerX := boardX / 2
|
|
|
|
board[playerY][playerX] = "@"
|
|
|
|
printBoard(board)
|
|
|
|
}
|
|
|
|
func makeBoard(sizeY, sizeX int, symbol string) ([][]string, error) {
|
|
//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)
|
|
|
|
for y := 0; y < sizeY; y++ {
|
|
//Make each array an array of empty strings
|
|
//cols
|
|
board[y] = make([]string, sizeX)
|
|
// and populate em
|
|
for x := 0; x < sizeX; x++ {
|
|
board[y][x] = symbol
|
|
}
|
|
}
|
|
return board, nil
|
|
}
|
|
|
|
func printBoard(board [][]string) {
|
|
for y := 0; y < len(board); y++ {
|
|
for x := 0; x < len(board[y]); x++ {
|
|
fmt.Print(board[y][x])
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|