diff --git a/pkg/core/pcplayer/pc_player_engine.go b/pkg/core/pcplayer/pc_player_engine.go index d877a96..909b75b 100644 --- a/pkg/core/pcplayer/pc_player_engine.go +++ b/pkg/core/pcplayer/pc_player_engine.go @@ -1,4 +1,4 @@ -// Package pcplayer provides megods for simple-AI logic +// Package pcplayer provides methods for simple-AI logic // used in Tic-Tac-Go for calculating PC-player's move. package pcplayer @@ -68,7 +68,7 @@ O-player lost. func canWinTwoMoves(gameBoard *board.Board, player letter.Letter) (result []int) { // nolint:gomnd // look a scheme above - in the second one, the chain is by 2 less than max minimalChainLen := gameBoard.ChainLength() - 2 - if minimalChainLen <= 0 { // nolint:gomnd // processing this values doesn't make sense with chain smaller than 3 + if minimalChainLen <= 0 { return nil } @@ -92,13 +92,13 @@ validatingChains: potentiallyAvailableChains := gameBoard.GetWinBoard(gameBoard.ChainLength() + 1) for _, potentialPlace := range potentiallyAvailableChains { for _, chain := range availableWinningChains { - //if potentialPlace[1] == chain[0] && potentialPlace[2] == chain[1] { if reflect.DeepEqual(potentialPlace[1:len(chain)+1], chain) { result = append(result, potentialPlace[len(potentialPlace)-2]) + break - //} else if potentialPlace[2] == chain[0] && potentialPlace[3] == chain[1] { } else if reflect.DeepEqual(potentialPlace[2:len(chain)+2], chain) { result = append(result, potentialPlace[1]) + break } } diff --git a/scripts/draw-board/main.go b/scripts/draw-board/main.go index 21ca8d7..912a612 100644 --- a/scripts/draw-board/main.go +++ b/scripts/draw-board/main.go @@ -1,11 +1,13 @@ package main import ( + "errors" "flag" "fmt" - "github.com/gucio321/tic-tac-go/pkg/core/board/letter" "strconv" + "github.com/gucio321/tic-tac-go/pkg/core/board/letter" + "github.com/gucio321/tic-tac-go/pkg/core/board" ) @@ -14,6 +16,8 @@ const ( defaultChainLen = 3 ) +var errIncorrectLetter = errors.New("incorrect number") + var _ flag.Value = &lettersValue{} type lettersValue []int @@ -25,13 +29,13 @@ func (l *lettersValue) String() string { func (l *lettersValue) Set(value string) (err error) { for _, letter := range value { if letter < '0' || letter > '9' { - return fmt.Errorf("invalid letter: %v", letter) + return fmt.Errorf("%w %v", errIncorrectLetter, letter) } } x, err := strconv.Atoi(value) if err != nil { - return err + return fmt.Errorf("converting string to int: %w", err) } *l = append(*l, x)