This commit is contained in:
Neil Hanlon 2015-10-30 12:59:54 -04:00
parent 3425e46d65
commit be6f939ef6
11 changed files with 84 additions and 0 deletions

BIN
computerchoice.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

BIN
iconPaper.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

BIN
iconRock.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

BIN
iconScissors.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

BIN
logic.exe Normal file

Binary file not shown.

84
logic.hs Normal file
View File

@ -0,0 +1,84 @@
import qualified Graphics.Gloss.Interface.Pure.Game as Gloss
import qualified Graphics.Gloss.Data.Bitmap as Bitmap
import qualified Data.Map as Map
import Data.List
import Data.Maybe
import System.Random (randomRIO)
data Move = Paper | Rock | Scissors | Tie deriving (Eq, Ord, Show)
--data Result = Computer | Human | Tie deriving (Eq, Ord, Show)
data Game = Game {
human :: Maybe Move,
computer :: Move,
humanScore :: Int,
computerScore :: Int
}
beats :: Move -> Move
beats move = case move of
Paper -> Scissors
Rock -> Paper
Scissors -> Rock
score :: Move -> Move -> Move
score this_move other_move
| this_move == beats other_move = this_move
| this_move == other_move = Tie
| otherwise = other_move
score' (Game h@(Just this_move) other_move hs cs)
| this_move == beats other_move = this_move
| this_move == other_move = Tie
| otherwise = other_move
updateScore (Game h@(Just this_move) other_move hs cs)
| this_move == beats other_move = Game h other_move (hs+1) cs
| this_move == other_move = Game h other_move hs cs
| otherwise = Game h other_move hs (cs+1)
--Define a random choice
pick :: [a] -> IO a
pick xs = randomRIO (0, length xs -1 ) >>= return . (xs !!)
whatdoescomputersay = pick [Rock, Paper, Scissors]
handleEvent (Gloss.EventKey (Gloss.Char keypress) Gloss.Down _ (_, _)) game@(Game h c hs cs)
| keypress == 'p' = updateScore (Game (Just Paper) c hs cs)
| keypress == 'r' = updateScore (Game (Just Rock) c hs cs)
| keypress == 's' = updateScore (Game (Just Scissors) c hs cs)
| otherwise = Game (Nothing) c hs cs
handleEvent _ game = game
main = do
let possibleMoves = [Paper, Rock, Scissors]
computer <- pick possibleMoves
rockwin <- Bitmap.loadBMP "rockwins.bmp"
paperwin <- Bitmap.loadBMP "paperwins.bmp"
scissorswin <- Bitmap.loadBMP "scissorswin.bmp"
tie <- Bitmap.loadBMP "tie.bmp"
rockPicture <- Bitmap.loadBMP "iconRock.bmp"
scissorsPicture <- Bitmap.loadBMP "iconScissors.bmp"
paperPicture <- Bitmap.loadBMP "iconPaper.bmp"
yourchoice <- Bitmap.loadBMP "yourchoice.bmp"
computerchoice <- Bitmap.loadBMP "computerchoice.bmp"
let userUI = Map.fromList [(Paper, paperPicture), (Scissors, scissorsPicture), (Rock, rockPicture)]
let results = Map.fromList [(Rock, rockwin), (Paper, paperwin), (Scissors, scissorswin), (Tie, tie)]
let all_pictures = [(Gloss.translate (-275) 0 rockPicture), (Gloss.translate 0 0 paperPicture), (Gloss.Translate 275 0 scissorsPicture)]
let display game@(Game human computer humanScore computerScore) = if human == Nothing then Gloss.Pictures ([Gloss.Translate (-550) (300) (Gloss.Color Gloss.white (Gloss.Text "Press p, r, or s!"))]++all_pictures) else Gloss.Pictures [results Map.! (score' game), Gloss.Translate (-500) 0 (userUI Map.! fromJust (human)), Gloss.Translate 500 0 (userUI Map.! computer), Gloss.Translate (-500) 250 yourchoice, Gloss.Translate 500 250 computerchoice]
let initialGame = Game (Nothing) computer 0 0
Gloss.play
(Gloss.InWindow "Rock Paper Scissors" (1920,1080) (0,0))
Gloss.black --background color
10
initialGame
display
handleEvent
(\f g -> g)

BIN
paperwins.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 MiB

BIN
rockwins.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 MiB

BIN
scissorswin.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 MiB

BIN
tie.bmp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 KiB

After

Width:  |  Height:  |  Size: 2.2 MiB

BIN
yourchoice.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB