1
0
mirror of https://github.com/MADTeacher/go_basics.git synced 2025-11-23 21:34:47 +02:00
Files
go_basics/part_8/tic_tac_toe_v7/client/utils.go

72 lines
1.8 KiB
Go
Raw Normal View History

2025-06-21 13:28:09 +03:00
package client
import (
"fmt"
b "tic-tac-toe/board"
2025-06-23 17:12:57 +03:00
g "tic-tac-toe/game"
2025-06-21 13:28:09 +03:00
)
2025-06-23 17:12:57 +03:00
// Выводит информацию о ходе игрока
2025-06-21 13:28:09 +03:00
func (c *Client) printTurnInfo() {
if c.board == nil {
return
}
2025-06-23 17:12:57 +03:00
if c.currentPlayer == c.mySymbol { // Если ход игрока
2025-06-21 13:28:09 +03:00
fmt.Println("It's your turn.")
2025-06-23 17:12:57 +03:00
} else if c.currentPlayer != b.Empty { // Если ход оппонента
2025-06-21 13:28:09 +03:00
fmt.Printf("It's player %s's turn.\n", c.currentPlayer)
} else {
2025-06-23 17:12:57 +03:00
// Игра может быть завершена или находиться
// в промежуточном состоянии
2025-06-21 13:28:09 +03:00
}
fmt.Print("> ")
}
2025-06-23 17:12:57 +03:00
// Проверяем валидность хода игрока
2025-06-21 13:28:09 +03:00
func (c *Client) validateMove(row, col int) bool {
2025-06-23 17:12:57 +03:00
if c.board == nil { // Если игра не начата
2025-06-21 13:28:09 +03:00
fmt.Println("Game has not started yet.")
return false
}
2025-06-23 17:12:57 +03:00
// Если ход вне поля
2025-06-21 13:28:09 +03:00
if row < 1 || row > c.board.Size || col < 1 || col > c.board.Size {
2025-06-23 17:12:57 +03:00
fmt.Printf(
"Invalid move. Row and column must be between 1 and %d.\n",
c.board.Size,
)
2025-06-21 13:28:09 +03:00
return false
}
2025-06-23 17:12:57 +03:00
// Преобразуем в 0-индексированный для доступа к полю
2025-06-21 13:28:09 +03:00
if c.board.Board[row-1][col-1] != b.Empty {
fmt.Println("Invalid move. Cell is already occupied.")
return false
}
return true
}
2025-06-23 17:12:57 +03:00
// Конвертируем экземпляр типа GameMode в строку
func gameModeToString(mode g.GameMode) string {
switch mode {
case g.PvP:
return "PvP"
case g.PvC:
return "PvC"
default:
return "Unknown"
}
}
// Конвертируем экземпляр типа Difficulty в строку
func difficultyToString(difficulty g.Difficulty) string {
switch difficulty {
case g.Easy:
return "Easy"
case g.Medium:
return "Medium"
case g.Hard:
return "Hard"
default:
return ""
}
}