Skip to content

Commit

Permalink
Add title view
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben9922 committed Apr 9, 2023
1 parent 5148753 commit 693306f
Showing 1 changed file with 73 additions and 48 deletions.
121 changes: 73 additions & 48 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type view int
const (
PointSelection view = iota
PointConfirmation
TitleView
)

type model struct {
Expand Down Expand Up @@ -75,7 +76,7 @@ func initialModel() model {
return model{
grid: *newGrid(),
selectedPoint: vector2d{3, 3},
view: PointSelection,
view: TitleView,
currentPlayer: DarkPlayer,
disksFlipped: make([]vector2d, 0),
}
Expand Down Expand Up @@ -129,6 +130,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} else if m.currentPlayer == LightPlayer {
m.currentPlayer = DarkPlayer
}
case TitleView:
switch msg.(type) {
case tea.KeyMsg:
m.view = PointSelection
}
}

return m, nil
Expand Down Expand Up @@ -365,60 +371,79 @@ func (m model) View() string {
}
}

infoText := make([]string, 0, 10)
infoText = append(infoText, lipgloss.NewStyle().
Foreground(lipgloss.Color("63")).
Bold(true).
Render(fmt.Sprintf("%s (%s)'s turn", m.currentPlayer.String(), m.currentPlayer.toSymbol())))

var scoreStringBuilder strings.Builder
if scores[LightPlayer] == scores[DarkPlayer] {
scoreStringBuilder.WriteString("Draw")
} else if scores[DarkPlayer] > scores[LightPlayer] {
scoreStringBuilder.WriteString(fmt.Sprintf("%s winning!", DarkPlayer))
} else if scores[LightPlayer] > scores[DarkPlayer] {
scoreStringBuilder.WriteString(fmt.Sprintf("%s winning!", LightPlayer))
}
scoreStringBuilder.WriteString(" - ")
scoreStringBuilder.WriteString(fmt.Sprintf("%s: %d; %s: %d", DarkPlayer.String(), scores[DarkPlayer], LightPlayer.String(),
scores[LightPlayer]))
infoText = append(infoText, scoreStringBuilder.String())
gridString := lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("63")).
MarginRight(6).
Render(gridStringBuilder.String())

switch m.view {
case PointSelection:
infoText = append(infoText, "", "Choose where to place your disk")
if slices.Contains(availablePoints, m.selectedPoint) {
infoText = append(infoText, lipgloss.NewStyle().
Foreground(lipgloss.Color("#00cc00")).
Render("Can place disk here"))
infoText = append(infoText, "", lipgloss.NewStyle().
var infoText []string
if m.view == TitleView {
const titleText = ` ____ _
| _ \ _____ _____ _ __ ___(_)
| |_) / _ \ \ / / _ \ '__/ __| |
| _ < __/\ V / __/ | \__ \ |
|_| \_\___| \_/ \___|_| |___/_|`

infoText = []string{
titleText,
"",
"Press any key to start...",
"",
lipgloss.NewStyle().
Foreground(lipgloss.Color("241")).
Render("arrow keys: move • enter: place tile • q: exit"))
} else {
infoText = append(infoText, lipgloss.NewStyle().
Foreground(lipgloss.Color("#cc0000")).
Render("Cannot place disk here"))
Render("any key: continue"),
}
} else {
infoText = make([]string, 0, 10)
infoText = append(infoText, lipgloss.NewStyle().
Foreground(lipgloss.Color("63")).
Bold(true).
Render(fmt.Sprintf("%s (%s)'s turn", m.currentPlayer.String(), m.currentPlayer.toSymbol())))

var scoreStringBuilder strings.Builder
if scores[LightPlayer] == scores[DarkPlayer] {
scoreStringBuilder.WriteString("Draw")
} else if scores[DarkPlayer] > scores[LightPlayer] {
scoreStringBuilder.WriteString(fmt.Sprintf("%s winning!", DarkPlayer))
} else if scores[LightPlayer] > scores[DarkPlayer] {
scoreStringBuilder.WriteString(fmt.Sprintf("%s winning!", LightPlayer))
}
scoreStringBuilder.WriteString(" - ")
scoreStringBuilder.WriteString(fmt.Sprintf("%s: %d; %s: %d", DarkPlayer.String(), scores[DarkPlayer], LightPlayer.String(),
scores[LightPlayer]))
infoText = append(infoText, scoreStringBuilder.String())

switch m.view {
case PointSelection:
infoText = append(infoText, "", "Choose where to place your disk")
if slices.Contains(availablePoints, m.selectedPoint) {
infoText = append(infoText, lipgloss.NewStyle().
Foreground(lipgloss.Color("#00cc00")).
Render("Can place disk here"))
infoText = append(infoText, "", lipgloss.NewStyle().
Foreground(lipgloss.Color("241")).
Render("arrow keys: move • enter: place tile • q: exit"))
} else {
infoText = append(infoText, lipgloss.NewStyle().
Foreground(lipgloss.Color("#cc0000")).
Render("Cannot place disk here"))
infoText = append(infoText, "", lipgloss.NewStyle().
Foreground(lipgloss.Color("241")).
Render("arrow keys: move • q: exit"))
}
case PointConfirmation:
if len(m.disksFlipped) == 0 {
infoText = append(infoText, "", "No disks flipped this time")
} else {
infoText = append(infoText, "", fmt.Sprintf("%s flipped %s!", m.currentPlayer, english.Plural(len(m.disksFlipped), "disk", "")))
}
infoText = append(infoText, "", lipgloss.NewStyle().
Foreground(lipgloss.Color("241")).
Render("arrow keys: move • q: exit"))
}
case PointConfirmation:
if len(m.disksFlipped) == 0 {
infoText = append(infoText, "", "No disks flipped this time")
} else {
infoText = append(infoText, "", fmt.Sprintf("%s flipped %s!", m.currentPlayer, english.Plural(len(m.disksFlipped), "disk", "")))
Render("any key: continue"))
}
infoText = append(infoText, "", lipgloss.NewStyle().
Foreground(lipgloss.Color("241")).
Render("any key: continue"))
}

gridString := lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("63")).
MarginRight(6).
Render(gridStringBuilder.String())

infoTextString := lipgloss.JoinVertical(lipgloss.Left, infoText...)

return lipgloss.NewStyle().
Expand Down

0 comments on commit 693306f

Please sign in to comment.