From b5a7a5d15108188ef51bd43ab75b40404699e987 Mon Sep 17 00:00:00 2001 From: Grigoriy Krasnochub Date: Sun, 22 Mar 2020 15:14:44 +0300 Subject: [PATCH] realize work with gameMap throw interface --- game/console/console_game.go | 10 +- game/game.go | 101 +++++----- game/game_is_win_test.go | 306 +++++++++++++++++-------------- game/gmmap/game_map_interface.go | 8 + game/gmmap/in_memory_game_map.go | 37 ++++ 5 files changed, 275 insertions(+), 187 deletions(-) create mode 100644 game/gmmap/game_map_interface.go create mode 100644 game/gmmap/in_memory_game_map.go diff --git a/game/console/console_game.go b/game/console/console_game.go index a57968d..4c60d38 100644 --- a/game/console/console_game.go +++ b/game/console/console_game.go @@ -11,9 +11,9 @@ import ( ) func PrintMap(userGame game.Game) { - - verticallyLen := len(userGame.GameMap) - horizontallyLen := len(userGame.GameMap[0]) + gameMap := userGame.GetMap() + verticallyLen := len(gameMap) + horizontallyLen := len(gameMap[0]) var horizontallyCharsIndentCount int = (horizontallyLen / 10) + 2 var verticallyCharsIndentCount int = (verticallyLen / 10) + 1 @@ -25,7 +25,7 @@ func PrintMap(userGame game.Game) { fmt.Print("\n") //end print first line - for i, gameMapRow := range userGame.GameMap { + for i, gameMapRow := range gameMap { fmt.Printf("%*d", verticallyCharsIndentCount, i) for _, gameMapCell := range gameMapRow { switch gameMapCell { @@ -54,7 +54,7 @@ func DoConsoleGame() { errorMessage = "" } - fmt.Println("Please write game map size in format \"xMapSize yMapSize\" (without semicolons)") + fmt.Println("Please write game gmmap size in format \"xMapSize yMapSize\" (without semicolons)") text, _ := reader.ReadString('\n') userMapSize := regex.FindStringSubmatch(text) if userMapSize == nil || userMapSize[1] == "" || userMapSize[2] == "" { diff --git a/game/game.go b/game/game.go index 2ef8eda..9c9083d 100644 --- a/game/game.go +++ b/game/game.go @@ -1,29 +1,25 @@ package game -import "errors" +import ( + "errors" + "goTicTacToe/game/gmmap" +) const emptyCellValue = 0 type Game struct { - GameMap [][]int UserTurn bool GameFinished bool + gmmap.GameMapInterface } func New(mapSizeX int, mapSizeY int) (*Game, error) { - if mapSizeX <= 0 || mapSizeY <= 0 { - return nil, errors.New("size matters, the sizeNumbers should be bigger") + userGame := &Game{UserTurn: true, GameFinished: false, GameMapInterface: &gmmap.InMemoryGameMap{}} + newMapError := userGame.NewMap(mapSizeX, mapSizeY) + if newMapError != nil { + return nil, newMapError } - //Создаем пустой 2d срез - gameMap := make([][]int, 0) - - for i := 0; i < mapSizeX; i++ { - //создаем пустой срез - tmp := make([]int, mapSizeY) - //допихиваем в 2d срез обычный срез - gameMap = append(gameMap, tmp) - } - userGame := &Game{GameMap: gameMap, UserTurn: true, GameFinished: false} + return userGame, nil } @@ -31,38 +27,47 @@ func (game Game) IsGameEnded() bool { return !isNextTurnAvailable(game) || game.GameFinished } -func (game *Game) MakeTurn (x int, y int) (bool, error) { - if x > len(game.GameMap) || x < 0 || y > len(game.GameMap[0]) || y < 0 { +func (game *Game) MakeTurn(x int, y int) (bool, error) { + gameMap := game.GetMap() + if x > len(gameMap) || x < 0 || y > len(gameMap[0]) || y < 0 { return false, errors.New("cell coordinates do not exist") } - if game.GameMap[x][y] != emptyCellValue { + if gameMap[x][y] != emptyCellValue { return false, errors.New("cell is not empty") } + var writeValueToCellError error if game.UserTurn { - game.GameMap[x][y] = 1 + writeValueToCellError = game.WriteValueToCell(1, x, y) } else { - game.GameMap[x][y] = 2 + writeValueToCellError = game.WriteValueToCell(2, x, y) + } + + if writeValueToCellError != nil { + return false, writeValueToCellError } + game.UserTurn = !game.UserTurn return true, nil } func (game *Game) IsWinningCombinationExistForCell(x int, y int) bool { - cellValue := game.GameMap[x][y] - gameMapXLen := len(game.GameMap) - gameMapYLen := len(game.GameMap[0]) + gameMap := game.GetMap() + + cellValue := gameMap[x][y] + gameMapXLen := len(gameMap) + gameMapYLen := len(gameMap) if x >= 0 && x < gameMapXLen && y >= 0 && y < gameMapYLen && cellValue != emptyCellValue { //TODO написать более изысканную проверку - verticalCellValueStrike := getVerticallyValueStrikeForCell(*game, x, y) + verticalCellValueStrike := getVerticallyValueStrikeForCell(gameMap, x, y) - horizontalCellValueStrike := getHorizontalValueStrikeForCell(*game, x, y) + horizontalCellValueStrike := getHorizontalValueStrikeForCell(gameMap, x, y) - backSlashValueStrike := getBackSlashValueStrikeForCell(*game, x, y) + backSlashValueStrike := getBackSlashValueStrikeForCell(gameMap, x, y) - slashValueStrike := getSlashValueStrikeForCell(*game, x, y) + slashValueStrike := getSlashValueStrikeForCell(gameMap, x, y) cellValueStrike := getMaxInt(verticalCellValueStrike, horizontalCellValueStrike, backSlashValueStrike, slashValueStrike) @@ -74,16 +79,16 @@ func (game *Game) IsWinningCombinationExistForCell(x int, y int) bool { return false } -func getVerticallyValueStrikeForCell(game Game, x int, y int) int { - cellValue := game.GameMap[x][y] +func getVerticallyValueStrikeForCell(gameMap [][]int, x int, y int) int { + cellValue := gameMap[x][y] cellValueStrike := 0 holdValue := false - for i := 0; i < len(game.GameMap); i++ { + for i := 0; i < len(gameMap); i++ { if i == x { holdValue = true } - if game.GameMap[i][y] == cellValue { + if gameMap[i][y] == cellValue { cellValueStrike++ } else { if !holdValue { @@ -98,16 +103,16 @@ func getVerticallyValueStrikeForCell(game Game, x int, y int) int { return cellValueStrike } -func getHorizontalValueStrikeForCell(game Game, x int, y int) int { - cellValue := game.GameMap[x][y] +func getHorizontalValueStrikeForCell(gameMap [][]int, x int, y int) int { + cellValue := gameMap[x][y] cellValueStrike := 0 holdValue := false - for i := 0; i < len(game.GameMap[0]); i++ { + for i := 0; i < len(gameMap[0]); i++ { if i == y { holdValue = true } - if game.GameMap[x][i] == cellValue { + if gameMap[x][i] == cellValue { cellValueStrike++ } else { if !holdValue { @@ -125,8 +130,8 @@ func getHorizontalValueStrikeForCell(game Game, x int, y int) int { /* \ */ -func getBackSlashValueStrikeForCell(game Game, x int, y int) int { - cellValue := game.GameMap[x][y] +func getBackSlashValueStrikeForCell(gameMap [][]int, x int, y int) int { + cellValue := gameMap[x][y] cellValueStrike := 0 holdValue := false @@ -142,12 +147,12 @@ func getBackSlashValueStrikeForCell(game Game, x int, y int) int { } horizontalCellCoordinate := startCell[1] - mapHorizontalLen := len(game.GameMap[0]) - for i := startCell[0]; i < len(game.GameMap); i++ { + mapHorizontalLen := len(gameMap[0]) + for i := startCell[0]; i < len(gameMap); i++ { if i == x && horizontalCellCoordinate == y { holdValue = true } - if game.GameMap[i][horizontalCellCoordinate] == cellValue { + if gameMap[i][horizontalCellCoordinate] == cellValue { cellValueStrike++ } else { if !holdValue { @@ -171,8 +176,8 @@ func getBackSlashValueStrikeForCell(game Game, x int, y int) int { /* / */ -func getSlashValueStrikeForCell(game Game, x int, y int) int { - cellValue := game.GameMap[x][y] +func getSlashValueStrikeForCell(gameMap [][]int, x int, y int) int { + cellValue := gameMap[x][y] cellValueStrike := 0 holdValue := false @@ -180,7 +185,7 @@ func getSlashValueStrikeForCell(game Game, x int, y int) int { var startCell [2]int startCell[0] = 0 startCell[1] = y + x - mapHorizontalLen := len(game.GameMap[0]) + mapHorizontalLen := len(gameMap[0]) if startCell[1] >= mapHorizontalLen { startCell[0] = startCell[1] - mapHorizontalLen + 1 startCell[1] = mapHorizontalLen - 1 @@ -188,11 +193,11 @@ func getSlashValueStrikeForCell(game Game, x int, y int) int { horizontalCellCoordinate := startCell[1] - for i := startCell[0]; i < len(game.GameMap); i++ { + for i := startCell[0]; i < len(gameMap); i++ { if i == x && horizontalCellCoordinate == y { holdValue = true } - if game.GameMap[i][horizontalCellCoordinate] == cellValue { + if gameMap[i][horizontalCellCoordinate] == cellValue { cellValueStrike++ } else { if !holdValue { @@ -214,8 +219,9 @@ func getSlashValueStrikeForCell(game Game, x int, y int) int { } func getGameCellsForWinCount(game Game) int { - verticalLen := len(game.GameMap) - horizontalLen := len(game.GameMap[0]) + gameMap := game.GetMap() + verticalLen := len(gameMap) + horizontalLen := len(gameMap[0]) if verticalLen <= horizontalLen { return verticalLen @@ -225,7 +231,8 @@ func getGameCellsForWinCount(game Game) int { } func isNextTurnAvailable(game Game) bool { - for _, gameMapRow := range game.GameMap { + gameMap := game.GetMap() + for _, gameMapRow := range gameMap { for _, gameMapCell := range gameMapRow { if gameMapCell == emptyCellValue { return true diff --git a/game/game_is_win_test.go b/game/game_is_win_test.go index 5b496bd..ec8a0c6 100644 --- a/game/game_is_win_test.go +++ b/game/game_is_win_test.go @@ -3,144 +3,161 @@ package game import ( "fmt" "github.com/stretchr/testify/assert" + "goTicTacToe/game/gmmap" "testing" ) type userGames struct { - userGame Game + userGame Game + gameMap [][]int cellCoordinate [2]int - result bool + result bool } -func funcToTestsIsWinningCombinationExistForCell(t *testing.T, winCombinations []userGames){ +func funcToTestsIsWinningCombinationExistForCell(t *testing.T, winCombinations []userGames) { a := assert.New(t) for i, testGameWin := range winCombinations { + testGameWin.userGame.LoadGameMapFromSlice(testGameWin.gameMap) if testGameWin.result { - a.True(testGameWin.userGame.IsWinningCombinationExistForCell(testGameWin.cellCoordinate[0], testGameWin.cellCoordinate[1]),fmt.Sprintf("%s %d, want %t, got %t", "Test Data №", i, true, false)) - } else{ - a.False(testGameWin.userGame.IsWinningCombinationExistForCell(testGameWin.cellCoordinate[0], testGameWin.cellCoordinate[1]),fmt.Sprintf("%s %d, want %t, got %t", "Test Data №", i, false, true)) + a.True(testGameWin.userGame.IsWinningCombinationExistForCell(testGameWin.cellCoordinate[0], testGameWin.cellCoordinate[1]), fmt.Sprintf("%s %d, want %t, got %t", "Test Data №", i, true, false)) + } else { + a.False(testGameWin.userGame.IsWinningCombinationExistForCell(testGameWin.cellCoordinate[0], testGameWin.cellCoordinate[1]), fmt.Sprintf("%s %d, want %t, got %t", "Test Data №", i, false, true)) } } } func TestGame_IsWinningCombinationExistForCellBackSlash(t *testing.T) { - testGameWins :=[]userGames{ + testGameWins := []userGames{ { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, - }, - UserTurn: false, - GameFinished: false, }, [2]int{0, 0}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false,}, + [][]int{ {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 1, 0}, }, - UserTurn: false, - GameFinished: false,}, [2]int{0, 1}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 0, 0}, - }, - UserTurn: false, - GameFinished: false, }, [2]int{1, 0}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 0, 0}, - }, - UserTurn: false, - GameFinished: false, }, [2]int{1, 0}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false,}, + [][]int{ {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, }, - UserTurn: false, - GameFinished: false,}, [2]int{1, 0}, true, }, { - Game {GameMap: [][]int{ - {0,1,0,0,0}, - {0,0,0,0,0}, - {0,0,0,1,0}, - }, - UserTurn: false, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, GameFinished: false, }, - [2]int{0,1}, + [][]int{ + {0, 1, 0, 0, 0}, + {0, 0, 0, 0, 0}, + {0, 0, 0, 1, 0}, + }, + [2]int{0, 1}, false, }, } - funcToTestsIsWinningCombinationExistForCell(t,testGameWins) + funcToTestsIsWinningCombinationExistForCell(t, testGameWins) } func TestGame_IsWinningCombinationExistForCellSlash(t *testing.T) { testGameWins := []userGames{ { - Game{GameMap: [][]int{ - {0, 0, 1}, - {0, 1, 0}, - {1, 0, 0}, - }, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, UserTurn: false, GameFinished: false, - }, [2]int{1, 1}, + }, [][]int{ + {0, 0, 1}, + {0, 1, 0}, + {1, 0, 0}, + }, [2]int{1, 1}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {0, 0, 0, 1, 0}, {0, 0, 1, 0, 0}, {0, 1, 0, 0, 0}, }, - UserTurn: false, - GameFinished: false, - }, [2]int{1, 2}, + [2]int{1, 2}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 1, 0, 0}, }, - UserTurn: false, - GameFinished: false, - }, [2]int{1, 2}, + [2]int{1, 2}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, @@ -148,13 +165,15 @@ func TestGame_IsWinningCombinationExistForCellSlash(t *testing.T) { {1, 0, 0, 0}, {0, 0, 0, 0}, }, - UserTurn: false, - GameFinished: false, - }, [2]int{1, 3}, + [2]int{1, 3}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}, @@ -163,173 +182,190 @@ func TestGame_IsWinningCombinationExistForCellSlash(t *testing.T) { {1, 0, 0, 0}, {0, 0, 0, 0}, }, - UserTurn: false, - GameFinished: false, - }, [2]int{2, 3}, + [2]int{2, 3}, true, }, } - funcToTestsIsWinningCombinationExistForCell(t,testGameWins) + funcToTestsIsWinningCombinationExistForCell(t, testGameWins) } func TestGame_IsWinningCombinationExistForCellVertical(t *testing.T) { testGameWins := []userGames{ { - Game {GameMap: [][]int{ - {0,1,0}, - {0,1,0}, - {0,1,0}, - }, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, UserTurn: false, GameFinished: false, - }, [2]int{2,1}, + }, + [][]int{ + {0, 1, 0}, + {0, 1, 0}, + {0, 1, 0}, + }, + [2]int{2, 1}, true, }, { - Game {GameMap: [][]int{ - {0,1,0,0}, - {0,1,0,0}, - {0,1,0,0}, - {0,1,0,0}, - }, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, UserTurn: false, GameFinished: false, - }, [2]int{2,1}, + }, + [][]int{ + {0, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 0, 0}, + }, + [2]int{2, 1}, true, }, { - Game {GameMap: [][]int{ - {0,0,0,0,1}, - {0,0,0,0,1}, - {0,0,0,0,1}, - {0,0,0,0,1}, - {0,0,0,0,1}, - }, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, UserTurn: false, GameFinished: false, - }, [2]int{2,4}, + }, + [][]int{ + {0, 0, 0, 0, 1}, + {0, 0, 0, 0, 1}, + {0, 0, 0, 0, 1}, + {0, 0, 0, 0, 1}, + {0, 0, 0, 0, 1}, + }, + [2]int{2, 4}, true, }, { - Game {GameMap: [][]int{ - {1,0,0,0,0}, - {1,0,2,0,0}, - {1,0,2,0,0}, - {1,0,2,0,0}, - {1,0,2,0,0}, - }, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, UserTurn: false, GameFinished: false, - }, [2]int{3,0}, + }, + [][]int{ + {1, 0, 0, 0, 0}, + {1, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + }, + [2]int{3, 0}, true, }, { - Game {GameMap: [][]int{ - {1,0,0,0,0}, - {1,0,2,0,0}, - {1,0,2,0,0}, - {1,0,2,0,0}, - {1,0,2,0,0}, - }, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, UserTurn: false, GameFinished: false, - }, [2]int{3,0}, + }, + [][]int{ + {1, 0, 0, 0, 0}, + {1, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + }, + [2]int{3, 0}, true, }, { - Game {GameMap: [][]int{ - {1,0,0,0,0}, - {1,0,2,0,0}, - {2,0,2,0,0}, - {1,0,2,0,0}, - {1,0,2,0,0}, - }, - UserTurn: false, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, GameFinished: false, }, - [2]int{3,0}, + [][]int{ + {1, 0, 0, 0, 0}, + {1, 0, 2, 0, 0}, + {2, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + }, + [2]int{3, 0}, false, }, { - Game {GameMap: [][]int{ - {1,0,0,0,0}, - {1,0,2,0,0}, - {1,0,2,0,0}, - {2,0,2,0,0}, - {1,0,2,0,0}, - }, - UserTurn: false, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, GameFinished: false, }, - [2]int{3,0}, + [][]int{ + {1, 0, 0, 0, 0}, + {1, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + {2, 0, 2, 0, 0}, + {1, 0, 2, 0, 0}, + }, + [2]int{3, 0}, false, }, } - funcToTestsIsWinningCombinationExistForCell(t,testGameWins) + funcToTestsIsWinningCombinationExistForCell(t, testGameWins) } func TestGame_IsWinningCombinationExistForCellHorizontal(t *testing.T) { testGameWins := []userGames{ { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {1, 1, 1, 1, 1}, {0, 0, 2, 0, 0}, {0, 0, 2, 0, 0}, {0, 0, 2, 0, 0}, {0, 0, 2, 0, 0}, }, - UserTurn: false, - GameFinished: false, - }, [2]int{0, 0}, + [2]int{0, 0}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {0, 0, 0, 0, 0}, {1, 0, 2, 0, 0}, {1, 1, 1, 1, 1}, {2, 0, 2, 0, 0}, {2, 0, 2, 0, 0}, }, - UserTurn: false, - GameFinished: false, - }, [2]int{2, 0}, + [2]int{2, 0}, true, }, { - Game{GameMap: [][]int{ + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, + GameFinished: false, + }, + [][]int{ {0, 0, 0, 0, 0}, {1, 0, 2, 0, 0}, {0, 0, 0, 0, 0}, {2, 0, 2, 0, 0}, {1, 1, 1, 1, 1}, }, - UserTurn: false, - GameFinished: false, - }, [2]int{4, 0}, + [2]int{4, 0}, true, }, { - Game {GameMap: [][]int{ - {0,0,0,0,0}, - {1,0,2,0,0}, - {0,0,0,0,0}, - {2,0,2,0,0}, - {1,1,1,0,1}, - }, - UserTurn: false, + Game{GameMapInterface: &gmmap.InMemoryGameMap{}, + UserTurn: false, GameFinished: false, }, - [2]int{4,0}, + [][]int{ + {0, 0, 0, 0, 0}, + {1, 0, 2, 0, 0}, + {0, 0, 0, 0, 0}, + {2, 0, 2, 0, 0}, + {1, 1, 1, 0, 1}, + }, + [2]int{4, 0}, false, }, } - funcToTestsIsWinningCombinationExistForCell(t,testGameWins) + funcToTestsIsWinningCombinationExistForCell(t, testGameWins) } func TestIsWinningCombinationExistForCellFalse(t *testing.T) { a := assert.New(t) - testGameWins :=[]userGames{ + testGameWins := []userGames{ } for _, testGameWin := range testGameWins { a.False(testGameWin.userGame.IsWinningCombinationExistForCell(testGameWin.cellCoordinate[0], testGameWin.cellCoordinate[1])) diff --git a/game/gmmap/game_map_interface.go b/game/gmmap/game_map_interface.go new file mode 100644 index 0000000..1237ab0 --- /dev/null +++ b/game/gmmap/game_map_interface.go @@ -0,0 +1,8 @@ +package gmmap + +type GameMapInterface interface { + WriteValueToCell(value int, xCoordinate int, yCoordinate int) error + GetMap() [][]int + NewMap(verticalSize int, horizontalSize int) error + LoadGameMapFromSlice(gameMap [][]int) +} diff --git a/game/gmmap/in_memory_game_map.go b/game/gmmap/in_memory_game_map.go new file mode 100644 index 0000000..55645d2 --- /dev/null +++ b/game/gmmap/in_memory_game_map.go @@ -0,0 +1,37 @@ +package gmmap + +import "errors" + +type InMemoryGameMap struct { + gameMap [][]int +} + +func (inMemoryGameMap *InMemoryGameMap) LoadGameMapFromSlice(gameMap [][]int) { + inMemoryGameMap.gameMap = gameMap +} + +func (inMemoryGameMap *InMemoryGameMap) NewMap(verticalSize int, horizontalSize int) error { + if verticalSize <= 0 || horizontalSize <= 0 { + return errors.New("size matters, the sizeNumbers should be bigger") + } + gameMap := make([][]int, 0) + + for i := 0; i < verticalSize; i++ { + //создаем пустой срез + tmp := make([]int, horizontalSize) + //допихиваем в 2d срез обычный срез + gameMap = append(gameMap, tmp) + } + inMemoryGameMap.gameMap = gameMap; + return nil +} + +func (inMemoryGameMap *InMemoryGameMap) WriteValueToCell(value int, xCoordinate int, yCoordinate int) error { + //TODO add check inArray() + inMemoryGameMap.gameMap[xCoordinate][yCoordinate] = value + return nil +} + +func (inMemoryGameMap *InMemoryGameMap) GetMap() [][]int { + return inMemoryGameMap.gameMap +}