packages feed

Hricket (empty) → 0.1

raw patch · 5 files changed

+145/−0 lines, 5 filesdep +basedep +containerssetup-changed

Dependencies added: base, containers

Files

+ Hricket.cabal view
@@ -0,0 +1,20 @@+Name:           Hricket+Version:        0.1+Synopsis:       A Cricket scoring application.+Description:    An application to help with scoring Cricket dart games.+Cabal-Version:  >= 1.2+License:        BSD3+License-file:	LICENSE+Author:         Anthony Simpson+Maintainer:     DiscipleRayne@gmail.com+Homepage:       http://github.com/Raynes/Hricket+Copyright:	(c) 2010 Anthony Simpson+Build-Type:     Simple+Tested-With:    GHC == 6.10+Category:	Game+Extra-source-files:	README,LICENSE++Executable hricket+  Build-Depends:  base >= 3 && <= 4, containers >= 0.2+  Main-Is:        Main.hs+  Hs-Source-Dirs: src
+ LICENSE view
@@ -0,0 +1,24 @@+Copyright (c) 2010, Anthony Simpson+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:+    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.+    * Redistributions in binary form must reproduce the above copyright+      notice, this list of conditions and the following disclaimer in the+      documentation and/or other materials provided with the distribution.+    * Neither the name of the <organization> nor the+      names of its contributors may be used to endorse or promote products+      derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README view
@@ -0,0 +1,11 @@+This is not a cricket game. Hricket is a scoring application that will save you from having to draw up a Cricket scorecard if you don't have a chalkboard, or just don't feel like getting chalk on your hands. ;)++It supports two players, and will likely never support more. The bullseye is represented as the number '21'. This was done to avoid unnecessary complexity, and might change in future versions, but for now, the bullseye is 21. When you hit a bullseye, you enter 21, and not bullseye or b. If you try to enter those, it will tell it that it's invalid and have you try again.++The recommended way to use this application, is to begin by opening it up and entering the names of both players. You'll then want player 1 to throw 3 darts. Enter each dart into the application one at a time. You'll type the number you hit on the board, a single space, and the number of markings. For instance, if you hit the number 16 in the treble, you would enter '16 3'. If you hit 20 in the single you would enter '20 1'. For numbers lower than 15, which are not used in standard Cricket games, you can either enter 0, or go ahead and enter the number you hit and the number of markings, but that will take longer. When all numbers are closed by one of the players, that player's score will be ran against the other players score, and if it's higher, that player will be declared the winner. Otherwise, the other player wins.++You are expected to know how to play Cricket before using this application. This is not a Cricket tutorial, and the application will not teach you to play Cricket.++++I made this application our of necessity and boredom. Enjoy.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Main.hs view
@@ -0,0 +1,88 @@+module Main where++import GameCard+import Player+import Game+import Card+import System.IO+import Data.Char (isDigit, isSpace)+import Data.IORef (newIORef, readIORef, writeIORef, IORef)+import Control.Monad (liftM2)++data GState = GState {gamecard :: GameCard, cplayer :: Int+                     ,ctime :: Int}+              deriving (Show)++newGame :: String -> String -> GState+newGame p1n p2n = GState (createNew p1n p2n) 1 0++increment :: Int -> Int+increment n = if n == 2 then 1 else 2++incrementT n = if n < 2 then n+1 else 0++mainLoop :: IO (IORef GState) -> IO ()+mainLoop dp = do+  res <- dp+  gstate <- readIORef res+  if isGameOver (getPlayer 1 (gamecard gstate)) (getPlayer 2 (gamecard gstate))+   then do +     print (gamecard gstate)+     ref <- readIORef res+     let p1 = getScore (card (getPlayer (cplayer ref) (gamecard ref)))+         p2 = getScore (card (getPlayer (increment (cplayer ref)) (gamecard ref)))+         winner = if p1 > p2 then cplayer ref else increment (cplayer ref)+     putStrLn $ (++ " wins!") $ show $ name (getPlayer winner (gamecard ref))+    else mainLoop $ dartPrompt res++main :: IO ()+main = do+  prompted <- prompt+  mainLoop $ dartPrompt prompted++prompt :: IO (IORef GState)+prompt = do+  putStrLn "Welcome to Hricket. The Haskell cricket scoring application.\n\n\+         \Please enter player 1's name: "+  p1 <- getLine+  putStrLn "\nPlease enter player 2's name: "+  p2 <- getLine+  putStr "\n"+  newIORef $ newGame p1 p2++dartPrompt gst = do+  gsraw' <- readIORef gst+  let gsraw = gsraw' {ctime = (incrementT (ctime gsraw'))}+      gstate = gamecard gsraw+      pn = cplayer gsraw+  print gstate+  putStrLn "\nEnter the dart you hit, a single space, and the number of markings.\+            \\nFor example: 15 3 ,15 2, 15 1 or 0 for nothing.\n"+  ds <- getValidInput+  let marked = mark (getPlayer pn gstate) (getPlayer (increment pn) gstate) ds+  writeIORef gst gsraw {gamecard = (setPlayer pn gstate marked)+                       ,cplayer = (if ctime gsraw == 0 then increment pn else pn)}+  return gst++getValidInput :: IO String+getValidInput = helper 0 ""+    where helper 1 s = return s+          helper 0 s = do +            x <- getLine+            case checkInput x of+              Right y -> helper 1 y+              Left  y -> putStrLn y >> helper 0 ""++checkInput x+  | not $ noLetters x = Left "Invalid input. Please try again."+  | any isSpace x =+    let (sub, end) = break isSpace x+        y = read sub+        l = read end+    in+      if y > 20 && y < 25 || y < 1 || l > 3 || l < 1 +      then Left "Invalid input. Please try again." +      else Right x+  | read x == 0 = Right x+  | otherwise = Left "Invalid input. Please try again."+  where noLetters = all (liftM2 (||) isDigit isSpace)