Skip to content

Commit

Permalink
realize work with gameMap throw interface
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoryKrasnochub committed Mar 22, 2020
1 parent f8a6847 commit b5a7a5d
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 187 deletions.
10 changes: 5 additions & 5 deletions game/console/console_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand Down Expand Up @@ -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] == "" {
Expand Down
101 changes: 54 additions & 47 deletions game/game.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
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
}

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

This comment has been minimized.

Copy link
@pymq

pymq Mar 22, 2020

мб вкусовщина, но я бы поменял сигнатуру функции на MakeTurn(x int, y int) error - у тебя ведь bool нигде не используется и true равнозначно наличию ошибки

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)

This comment has been minimized.

Copy link
@pymq

pymq Mar 22, 2020

а это не баг? у тебя тут всегда x == y
я бы предложил сохранять размеры поля в Game структуру, добавить публичный метод для их получения и не высчитывать их каждый раз - видел в PrintMap они заново высчитываются


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)

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -171,28 +176,28 @@ 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

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
}

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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit b5a7a5d

Please sign in to comment.