Skip to content

Commit

Permalink
Merge pull request #30 from ArkhamCookie/dice
Browse files Browse the repository at this point in the history
Add dice lib
  • Loading branch information
ArkhamCookie authored Jan 7, 2024
2 parents 50f1378 + f8afaaf commit 2bfdf46
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ Whether you need a basic Discord bot in a *pinch* or want to spice up your serve
### Util

- [formatting/markdown](/lib/formatting/markdown)
\- Formatting for [Discord's markdown syntax](https://www.markdownguide.org/tools/discord/).
Formatting for [Discord's markdown syntax](https://www.markdownguide.org/tools/discord/).

### Fun

- [crabtalk](/lib/crabtalk/)
\- Communicate in crab!
— Communicate in crab!
- [dice](/lib/dice/)
<!-- — *description* -->
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
crabot/crabtalk v0.0.0
formatting/markdown v0.0.0
crabot/formatting/markdown v0.0.0
internal/env v0.0.0
)

Expand All @@ -20,7 +20,9 @@ require (

replace (
crabot/crabtalk => ./lib/crabtalk
crabot/dice => ./lib/dice
crabot/dice/roll => ./lib/dice/roll
crabot/formatting => ./lib/formatting
formatting/markdown => ./lib/formatting/markdown
crabot/formatting/markdown => ./lib/formatting/markdown
internal/env => ./internal/env
)
2 changes: 2 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ go 1.20
use (
.
./lib/crabtalk
./lib/dice
./lib/dice/roll
./lib/formatting
./lib/formatting/markdown

Expand Down
41 changes: 41 additions & 0 deletions lib/dice/coin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dice

import (
"errors"

"crabot/dice/roll"
)

// CoinFlip flips a coin and get heads or tails.
func CoinFlip() (string, error) {
// Flip the coin (roll d2).
value := roll.Roll(2)

// Set side depending on roll.
switch value {
case 1:
return "heads", nil
case 2:
return "tails", nil
}
return "", errors.New("issue flipping coin")
}

// CoinCall allows you to 'call' the result of CoinFlip 'in the air'
// by selecting heads or tails.
// If you 'call' it correctly, CoinCall returns true.
func CoinCall(call string) (string, bool, error) {
// Flip a coin and get the result
side, err := CoinFlip()
if err != nil {
return "", false, err
}

// If called correct, then return true.
if side == call {
return side, false, nil
}

// If called wrong, then return false.
return side, false, nil
}
28 changes: 28 additions & 0 deletions lib/dice/coin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dice_test

import (
"log"
"testing"

"crabot/dice"
)

func TestCoinFlip(t *testing.T) {
// Flip the coin
result, err := dice.CoinFlip()
// Confirm no error occurs
if err != nil {
log.Fatalln("Error:", err)
}

// Confirm it is heads or tails,
// if it isn't throw an error.
switch result {
case "heads":
return
case "tails":
return
default:
t.Fatalf(`dice.CoinFlip() = wanted "heads" or "tails" got %s`, result)
}
}
100 changes: 100 additions & 0 deletions lib/dice/diceTypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package dice

import (
"crabot/dice/roll"
)

var (
result int
)

// D2 rolls a D2 (1-2)
func D2(count int) int {
for count > 0 {
result += roll.Roll(2)
count--
}
return result
}

// D4 rolls a D4 (1-4)
func D4(count int) int {
for count > 0 {
result += roll.Roll(4)
count--
}
return result
}

// D6 rolls a D6 (1-6)
func D6(count int) int {
for count > 0 {
result += roll.Roll(6)
count--
}
return result
}

// D8 rolls a D8 (1-8)
func D8(count int) int {
for count > 0 {
result += roll.Roll(8)
count--
}
return result
}

// D10 rolls a D10 (1-10)
func D10(count int) int {
for count > 0 {
result += roll.Roll(10)
count--
}
return result
}

// D12 rolls a D12 (1-12)
func D12(count int) int {
for count > 0 {
result += roll.Roll(12)
count--
}
return result
}

// D20 rolls a D20 (1-20)
func D20(count int) int {
for count > 0 {
result += roll.Roll(20)
count--
}
return result
}

// Can only roll one at a time for the moment
// See branch d00 for the dev version
func D00(count int) int {
for count > 0 {
// Roll a D10
a := roll.Roll(10)

// Change the D10 into a D00
if a == 10 {
a = 0
} else {
a = a * 10
}

// Roll a D10
b := roll.Roll(10)

if b == 1 && a == 0 {
return 100
}

// Add the results together
result = a + b
count--
}
return result
}
39 changes: 39 additions & 0 deletions lib/dice/diceTypes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dice_test

import (
"testing"

"crabot/dice"
)

var (
result int
)

func TestD2(t *testing.T) {
result = dice.D2(1)

// TODO: Test that it is a full number

if result < 0 {
t.Fatal("dice.D2 was negitive")
} else if result == 0 {
t.Fatal("dice.D2 was 0")
}

if result > 2 {
t.Fatal("dice.D2 was greater than 2")
}

result = dice.D2(2)

if result < 0 {
t.Fatal("dice.D2 was negitive")
} else if result == 0 {
t.Fatal("dice.D2 was 0")
}

if result > 2 {
t.Fatal("dice.D2 was greater than 2")
}
}
7 changes: 7 additions & 0 deletions lib/dice/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module crabot/dice

go 1.20

require crabot/dice/roll v0.0.0

replace crabot/dice/roll => ./roll
3 changes: 3 additions & 0 deletions lib/dice/roll/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module dice/roll

go 1.20
9 changes: 9 additions & 0 deletions lib/dice/roll/roll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package roll

import "math/rand"

// Roll *dice* and return the roll.
func Roll(dice int) int {
// Add 1 so it 1-int rather than 0-int
return rand.Intn(dice) + 1
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os/signal"

"crabot/crabtalk"
"formatting/markdown"
"crabot/formatting/markdown"
"internal/env"

"github.com/bwmarrin/discordgo"
Expand Down

0 comments on commit 2bfdf46

Please sign in to comment.