-
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.
Need to finish learn you a haskell for good first.
- Loading branch information
Xa
authored and
Xa
committed
Apr 3, 2016
1 parent
4417d2d
commit 7a01bd9
Showing
2 changed files
with
153 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{-# OPTIONS_GHC -Wall #-} | ||
module HW02 where | ||
-- Mastermind ----------------------------------------- | ||
|
||
-- A peg can be one of six colors | ||
data Peg = Red | Green | Blue | Yellow | Orange | Purple | ||
deriving (Show, Eq, Ord) | ||
|
||
-- A code is defined to simply be a list of Pegs | ||
type Code = [Peg] | ||
|
||
-- A move is constructed using a Code and two integers; the number of | ||
-- exact matches and the number of regular matches | ||
data Move = Move Code Int Int | ||
deriving (Show, Eq) | ||
|
||
-- List containing all of the different Pegs | ||
colors :: [Peg] | ||
colors = [Red, Green, Blue, Yellow, Orange, Purple] | ||
|
||
-- Exercise 1 ----------------------------------------- | ||
|
||
-- Get the number of exact matches between the actual code and the guess | ||
exactMatches :: Code -> Code -> Int | ||
exactMatches a b = sum [1 | (x,y) <-xs, x==y] | ||
where xs = zip a b | ||
|
||
-- Exercise 2 ----------------------------------------- | ||
|
||
-- For each peg in xs, count how many times is occurs in ys | ||
findn :: Peg -> Code -> Int | ||
findn a xs = sum [1 | x <-xs , x ==a] | ||
|
||
countColors :: Code -> [Int] | ||
countColors xs = [findn a xs | a <- colors] | ||
|
||
-- Count number of matches between the actual code and the guess | ||
matches :: Code -> Code -> Int | ||
matches test code = sum [if x<y then x else y | (x,y) <-xs] | ||
where xs = zip (countColors test) (countColors code) | ||
|
||
-- Exercise 3 ----------------------------------------- | ||
|
||
-- Construct a Move from a guess given the actual code | ||
getMove :: Code -> Code -> Move | ||
getMove = undefined | ||
|
||
-- Exercise 4 ----------------------------------------- | ||
|
||
isConsistent :: Move -> Code -> Bool | ||
isConsistent = undefined | ||
|
||
-- Exercise 5 ----------------------------------------- | ||
|
||
filterCodes :: Move -> [Code] -> [Code] | ||
filterCodes = undefined | ||
|
||
-- Exercise 6 ----------------------------------------- | ||
|
||
allCodes :: Int -> [Code] | ||
allCodes = undefined | ||
|
||
-- Exercise 7 ----------------------------------------- | ||
|
||
solve :: Code -> [Move] | ||
solve = undefined | ||
|
||
-- Bonus ---------------------------------------------- | ||
|
||
fiveGuess :: Code -> [Move] | ||
fiveGuess = undefined |
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,82 @@ | ||
-- CIS 194, Spring 2015 | ||
-- | ||
-- Test cases for HW 02 | ||
|
||
module HW02Tests where | ||
|
||
import HW02 | ||
import Testing | ||
|
||
|
||
-- Exercise 1 ----------------------------------------- | ||
|
||
ex1Tests :: [Test] | ||
ex1Tests = [ testF2 "exactMatches test" exactMatches | ||
[ ([Red, Blue, Green, Yellow], [Blue, Green, Yellow, Red], 0) | ||
, ([Red, Blue, Green, Yellow], [Red, Purple, Green, Orange], 2) | ||
] | ||
] | ||
|
||
-- Exercise 2 ----------------------------------------- | ||
|
||
ex2Tests :: [Test] | ||
ex2Tests = [ testF1 "countColors test" countColors | ||
[ ([Red, Blue, Yellow, Purple], [1, 0, 1, 1, 0, 1]) | ||
, ([Green, Blue, Green, Orange], [0, 2, 1, 0, 1, 0]) | ||
] | ||
, testF2 "matches test" matches | ||
[ ([Red, Blue, Yellow, Orange], [Red, Orange, Orange, Blue], 3) ] | ||
] | ||
|
||
-- Exercise 3 ----------------------------------------- | ||
|
||
ex3Tests :: [Test] | ||
ex3Tests = [ testF2 "getMove test" getMove | ||
[ ([Red, Blue, Yellow, Orange], [Red, Orange, Orange, Blue], | ||
Move [Red, Orange, Orange, Blue] 1 2) | ||
] | ||
] | ||
|
||
-- Exercise 4 ----------------------------------------- | ||
|
||
ex4Tests :: [Test] | ||
ex4Tests = [ testF2 "isConsistent test" isConsistent | ||
[ (Move [Red, Red, Blue, Green] 1 1, [Red, Blue, Yellow, Purple], | ||
True) | ||
, (Move [Red, Red, Blue, Green] 1 1, [Red, Blue, Red, Purple], | ||
False) | ||
] | ||
] | ||
|
||
-- Exercise 5 ----------------------------------------- | ||
|
||
ex5Tests :: [Test] | ||
ex5Tests = [] | ||
|
||
-- Exercise 6 ----------------------------------------- | ||
|
||
ex6Tests :: [Test] | ||
ex6Tests = [] | ||
|
||
-- Exercise 7 ----------------------------------------- | ||
|
||
ex7Tests :: [Test] | ||
ex7Tests = [] | ||
|
||
-- Bonus ---------------------------------------------- | ||
|
||
bonusTests :: [Test] | ||
bonusTests = [] | ||
|
||
-- All Tests ------------------------------------------ | ||
|
||
allTests :: [Test] | ||
allTests = concat [ ex1Tests | ||
, ex2Tests | ||
, ex3Tests | ||
, ex4Tests | ||
, ex5Tests | ||
, ex6Tests | ||
, ex7Tests | ||
, bonusTests | ||
] |