Skip to content

Commit

Permalink
Init.
Browse files Browse the repository at this point in the history
  • Loading branch information
kraasch committed Jan 10, 2025
0 parents commit 42626c7
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Goreleaser

on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+' # Trigger on version tags, aka. semver (eg v1.2.0).

permissions:
contents: write
# packages: write
# issues: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: stable

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser # Either 'goreleaser' (default) or 'goreleaser-pro'
version: "~> v1" # Version is either 'latest', 'nightly', 'vX.Y.Z'.
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
6 changes: 6 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# docs: https://goreleaser.com/customization/builds/go/

builds:
- main: ./cmd/

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Alex Kraasch

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

run:
go run ./cmd/stuff.go

test:
go test ./...

.PHONY: build
build:
rm -rf ./build/
mkdir -p ./build/
go build -o ./build/wumpus ./cmd/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# notes

- code from: https://github.com/charmbracelet/bubbletea/discussions/818
302 changes: 302 additions & 0 deletions cmd/stuff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
package main

import (
"fmt"
"math/rand"
"time"
"os"
_ "strconv" // for debug prints.
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

var (
top_msg = "HUNT THE WUMPUS\n Quit (q), move (hjkl), shoot (HJKL)."
bot_msg = "..."
you_died = false
game_over = false
arrow_count = 0
outerBox = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("56"))
cursorStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#7D56F4"))
fogStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FFFFFF")).
Background(lipgloss.Color("#FFFFFF"))
noFogStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#000000")).
Background(lipgloss.Color("#000000"))
)

type model struct {
width int
height int
arr [5][5]string
cursor_x int
cursor_y int
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func (m model) Init() tea.Cmd { return nil }

func update_positional_messages(m *model) {
bot_msg = ""
////////////////////////////////////////////////
c := m.arr[m.cursor_y][m.cursor_x]
if c == "a" { // Stepped on an arrow.
// 2. a = arrow
m.arr[m.cursor_y][m.cursor_x] = "N" // Remove the arrow.
arrow_count += 1
// bot_msg += "You found an arrow! (" + strconv.Itoa(arrow_count) + ") " // NOTE: debug print.
bot_msg += "You found an arrow! " // TODO: uncomment.
} else if c == "X" { // Stepped into fog.
m.arr[m.cursor_y][m.cursor_x] = "N" // Remove the fog.
} else if c == "b" || c == "o" || c == "w" {
// 3. b = bat, 4. o = hole, 5. w = wumpus
bot_msg += "You Died! "
switch c {
case "b":
bot_msg += "The bat eats you. "
case "o":
bot_msg += "You fall into a hole. "
case "w":
bot_msg += "The wumpus got you. "
}
you_died = true
return
} else {
// 1. h = hunter
{} // golang's no-op.
}
////////////////////////////////////////////////
// Loop over the 3x3 subarray around hunter's position at (cursor_x, cursor_y).
// Make sure that (I, J) stays within bounds of the 5x5 array.
for I := max(0, m.cursor_y-1); I < m.cursor_y+2 && I < 5 && I >= 0; I++ {
for J := max(0, m.cursor_x-1); J < m.cursor_x+2 && J < 5 && J >= 0; J++ {
switch m.arr[I][J] {
case "b":
bot_msg += "You hear flapping. "
case "o":
bot_msg += "You feel a draft. "
case "w":
bot_msg += "You smell wumpus. "
}
}
}
////////////////////////////////////////////////
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
case tea.KeyMsg:
switch msg.String() {
case "H", "a": // shoot left.
if you_died || game_over {
return m, tea.Quit
}
if arrow_count > 0 {
bot_msg = "You shoot left! "
arrow_count--
left := max(m.cursor_x - 1, 0)
if m.arr[m.cursor_y][left] == "w" {
bot_msg += "You shoot the wumpus!"
game_over = true
}
}
case "L", "d": // shoot right.
if you_died || game_over {
return m, tea.Quit
}
if arrow_count > 0 {
bot_msg = "You shoot right! "
arrow_count--
right := min(m.cursor_x + 1, 4)
if m.arr[m.cursor_y][right] == "w" {
bot_msg += "You shoot the wumpus!"
game_over = true
}
}
case "K", "w": // shoot up.
if you_died || game_over {
return m, tea.Quit
}
if arrow_count > 0 {
bot_msg = "You shoot up! "
arrow_count--
up := max(m.cursor_y - 1, 0)
if m.arr[up][m.cursor_x] == "w" {
bot_msg += "You shoot the wumpus!"
game_over = true
}
}
case "J", "s": // shoot down.
if you_died || game_over {
return m, tea.Quit
}
if arrow_count > 0 {
bot_msg = "You shoot down! "
arrow_count--
down := min(m.cursor_y + 1, 4)
if m.arr[down][m.cursor_x] == "w" {
bot_msg += "You shoot the wumpus!"
game_over = true
}
}
case "h", "left": // move left.
if you_died || game_over {
return m, tea.Quit
}
if m.cursor_x > 0 {
m.cursor_x--
update_positional_messages(&m)
}
case "l", "right": // move right.
if you_died || game_over {
return m, tea.Quit
}
if m.cursor_x < 4 {
m.cursor_x++
update_positional_messages(&m)
}
case "k", "up": // move up.
if you_died || game_over {
return m, tea.Quit
}
if m.cursor_y > 0 {
m.cursor_y--
update_positional_messages(&m)
}
case "j", "down": // move down.
if you_died || game_over {
return m, tea.Quit
}
if m.cursor_y < 4 {
m.cursor_y++
update_positional_messages(&m)
}
case "q":
return m, tea.Quit
}
}
return m, cmd
}

func (m model) View() string {
if m.width == 0 {
return ""
}
r := top_msg + "\n"
r += outerBox.Render(pack(m.arr, m))
r += "\n" + bot_msg + "\n"
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, r)
}

func pack(in [5][5]string, m model) string {
s := ""
for i := 0; i < len(in); i++ {
s += " "
for j := 0; j < len(in[i]); j++ {
// character := in[i][j] // NOTE: for debugging.
if m.cursor_x == j && m.cursor_y == i {
// Render the hunter.
// cursor := cursorStyle.Render(character) // NOTE: for debugging.
cursor := cursorStyle.Render(" ")
s += fmt.Sprintf("%s ", cursor)
} else if in[i][j] == "N" {
// Render empty tile.
// cursor := noFogStyle.Render(character) // NOTE: for debugging.
cursor := noFogStyle.Render(" ")
s += fmt.Sprintf("%s ", cursor)
} else if in[i][j] == "X" {
// Render foggy tile.
// cursor := fogStyle.Render(character) // NOTE: for debugging.
cursor := fogStyle.Render(" ")
s += fmt.Sprintf("%s ", cursor)
} else {
// Render object.
// s += fmt.Sprintf("%s ", character) // NOTE: for debugging.
{} // golang's no-op.
cursor := fogStyle.Render(" ")
s += fmt.Sprintf("%s ", cursor)
}
}
if i != len(in) -1 { // All but last line append newline.
s += fmt.Sprintln()
}
}
return s
}

func main() {
//////////////////////////////////////////////////////////
rand.Seed(time.Now().UnixNano()) // Seed the random number generator to get different results each time.
selectedIndices := make(map[int]bool) // Create a slice to hold the selected indices.
for len(selectedIndices) < 5 { // Loop until we have 5 unique indices.
index := rand.Intn(25) // Generate a random index between 0 and 24.
selectedIndices[index] = true // Add the index to the map (map keys are unique, so no duplicates).
}
var indices []int // Convert the map keys to a slice for easy access.
for index := range selectedIndices {
indices = append(indices, index)
}
// fmt.Printf("Random indices: %d\n", indices) // NOTE: debug print.
//////////////////////////////////////////////////////////
// 1. h = hunter
// 2. a = arrow
// 3. b = bat
// 4. o = hole
// 5. w = wumpus
letters := []rune{'h', 'a', 'b', 'o', 'w'} // The slice to shuffle.
rand.Shuffle(len(letters), func(i, j int) { // Shuffle the slice in place.
letters[i], letters[j] = letters[j], letters[i]
})
// fmt.Println("Shuffled letters:", string(letters)) // NOTE: debug print.
//////////////////////////////////////////////////////////
var newArr [5][5]string
m := model{0, 0, newArr, 0, 0}
for i := 0; i < len(m.arr); i++ { // Filling the array with values.
for j := 0; j < len(m.arr[i]); j++ {
// m.arr[i][j] = fmt.Sprintf("%c", 'A' + i*5 + j) // NOTE: for debug.
m.arr[i][j] = "X"
}
}
//////////////////////////////////////////////////////////
// MERGE.
for i, I := range indices {
m.arr[I%5][I/5] = string(letters[i])
// set inital hunter position.
if letters[i] == 'h' {
m.cursor_x = I/5
m.cursor_y = I%5
m.arr[I%5][I/5] = "N" // Uncover the start location.
}
}
//////////////////////////////////////////////////////////

if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
10 changes: 10 additions & 0 deletions cmd/stuff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"fmt"
"testing"
)

func TestPass(t *testing.T) {
fmt.Println("Tests go here")
}
Loading

0 comments on commit 42626c7

Please sign in to comment.