diff --git a/go/make_board/main.go b/go/make_board/main.go index 88e1de8..cb4d62f 100644 --- a/go/make_board/main.go +++ b/go/make_board/main.go @@ -1,28 +1,40 @@ package main import ( + "errors" "fmt" + "log" ) func main() { const boardY int = 7 const boardX int = 12 - board := makeBoard(boardY, boardX, "_") + board, err := makeBoard(boardY, boardX, "_") + + if err != nil { + log.Fatal(err) + } // Pretend that the player did something in the middle - playerMoveY := boardY / 2 - playerMoveX := boardX / 2 + playerY := boardY / 2 + playerX := boardX / 2 - board[playerMoveY][playerMoveX] = "@" + board[playerY][playerX] = "@" printBoard(board) } -func makeBoard(sizeY, sizeX int, symbol string) [][]string { +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++ { @@ -34,7 +46,7 @@ func makeBoard(sizeY, sizeX int, symbol string) [][]string { board[y][x] = symbol } } - return board + return board, nil } func printBoard(board [][]string) {