go:make_board: Changed playermovex and y to just playerx and y; added error handling for negative numbers

This commit is contained in:
Logen Kain 2017-10-03 02:21:32 -07:00
parent 672cbdfb99
commit 58a8413177

View File

@ -1,28 +1,40 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"log"
) )
func main() { func main() {
const boardY int = 7 const boardY int = 7
const boardX int = 12 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 // Pretend that the player did something in the middle
playerMoveY := boardY / 2 playerY := boardY / 2
playerMoveX := boardX / 2 playerX := boardX / 2
board[playerMoveY][playerMoveX] = "@" board[playerY][playerX] = "@"
printBoard(board) 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 //Make an array of empty strings
// Rows // 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) board := make([][]string, sizeY)
for y := 0; y < sizeY; y++ { for y := 0; y < sizeY; y++ {
@ -34,7 +46,7 @@ func makeBoard(sizeY, sizeX int, symbol string) [][]string {
board[y][x] = symbol board[y][x] = symbol
} }
} }
return board return board, nil
} }
func printBoard(board [][]string) { func printBoard(board [][]string) {