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
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) {