Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGhostOfInky committed Feb 6, 2022
0 parents commit 8272d56
Show file tree
Hide file tree
Showing 6 changed files with 770 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist-newstyle
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for diceroll

## 0.1.0.0 -- 2020-02-06

* First version. Released on an unsuspecting world.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Diceroll
Diceroll is a Haskell script that generates all possible unique combinations for a given list of values whose sum equals an inputted value.

### Usage
Using cabal:
```
cabal run diceroll <number>
```
Using the compiled exe:
```
diceroll.exe <number>
```

\* If no number is provided it will default to 20
55 changes: 55 additions & 0 deletions app/diceroll.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import System.Environment (getArgs)
import Data.List (sort)
computelist :: (Int, [Int],[[Int]]) -> IO () -- Creates list out of list of positions
computelist (x,l,m) = do
let list = [1,2,3,4,5,6] -- List of possible combinations
let newa = [list!!(l!!0),list!!(l!!1),list!!(l!!2),list!!(l!!3),list!!(l!!4),list!!(l!!5),list!!(l!!6),list!!(l!!7)] -- Creates list from list combinations and positions
if sum newa == x -- Checks if sum of elements of list matches x
then print newa -- Prints output if it does
else return () -- Returns if it doesn't
if sum l == 8*(length list -1) -- Checks if list has reached the end of the possibilities
then return() -- Returns if it does
else do
let newlist = nextList(l,m,length list -1,7) -- Creates a new list if possibilities are available
computelist(x,newlist,addM(newlist,m)) -- Recurses function with new list and adds the list of the list of lists

lastN :: Int -> [a] -> [a] -- Returns last n elements of list
lastN n xs = drop (length xs - n) xs

incrementX :: ([Int],Int) -> [Int] -- Increments i element of list by 1
incrementX (l,i) = take i l ++ [l!!i + 1] ++ lastN (7-i) l

resetM :: ([Int],Int) -> [Int] -- Sets the i element of list to 0
resetM (l,i) = take i l ++ [0] ++ lastN (7-i) l

addM :: ([Int], [[Int]]) -> [[Int]] -- Adds list of integers to list of lists of integers
addM (l,m) = m ++ [l]

checkRep :: ([Int],[[Int]]) -> Bool -- Checks if list is in the list of lists
checkRep (l,m) = do
if elem l m
then False
else True

incrementM :: ([Int], Int, Int) -> [Int] -- Returns an incremented matrix
incrementM (l,i,len) = do
if l!!i < len -- Checks if element i can be incremented
then incrementX(l,i) -- Increments if it can
else if i > 0 --checks if i is grester than zero
then incrementM(resetM(l,i),i-1,len) -- Recurses to increment matrix on the i value above if it is
else [len,len,len,len,len,len,len,len] -- Else returns the list of the final values


nextList :: ([Int], [[Int]], Int, Int) -> [Int] --Returns a new unique matrix
nextList (l,m,len,n) = do
let new = incrementM(l,n,len) -- Creates a new list that has an incremented value
if checkRep(sort(new),m) -- Checks if list is already in list of lists
then new -- Returns the matrix if it is unique
else nextList(new,m,len,n) -- Recurses fucntion to generate new unique list

main :: IO ()
main = do
let basem = [0,0,0,0,0,0,0,0] -- Sets base list as all 0's
args <- getArgs -- Fetches command line arguments
let len = if length args> 0 then read(args !! 0) :: Int else 20 -- Sets desired lenght to the first argument or defaults to 20
computelist(len, basem,[basem]) -- Computes list with the input parameters
21 changes: 21 additions & 0 deletions diceroll.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cabal-version: 2.4
name: diceroll
version: 0.1.0.0
synopsis: A basic diceroll combinations calculator
description: Diceroll is a Haskell script that generates all possible unique combinations for a given list of values whose sum equals an inputted value.
bug-reports: https://github.com/TheGhostOfInky/diceroll/issues
license: GPL-3.0-or-later
author: TheGhostOfInky
maintainer: theghostofinky@gmail.com
extra-source-files: CHANGELOG.md

executable diceroll
ghc-options:
-threaded
-funfolding-use-threshold=16
-O2
-optc-O3
main-is: diceroll.hs
build-depends: base ^>=4.16.0.0
hs-source-dirs: app
default-language: Haskell2010

0 comments on commit 8272d56

Please sign in to comment.