-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from ArkhamCookie/dice
Add dice lib
- Loading branch information
Showing
11 changed files
with
238 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ go 1.20 | |
use ( | ||
. | ||
./lib/crabtalk | ||
./lib/dice | ||
./lib/dice/roll | ||
./lib/formatting | ||
./lib/formatting/markdown | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module dice/roll | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters