Difference between revisions of "FP Homework 2"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 180: Line 180:
 
sampleInput = [Cube { start = Point 1 1 10, size = 4, color = 'X'},  
 
sampleInput = [Cube { start = Point 1 1 10, size = 4, color = 'X'},  
 
               Cube { start = Point 1 5 10, size = 3, color = 'O'},
 
               Cube { start = Point 1 5 10, size = 3, color = 'O'},
               Cube { start = Point 10 10 0, size = 2, color = '#'},
+
               Cube { start = Point 10 8 0, size = 2, color = '#'},
 
               Cube { start = Point 0 0 0, size = 10, color = '*'}]
 
               Cube { start = Point 0 0 0, size = 10, color = '*'}]
  
Line 188: Line 188:
 
<syntaxhighlight lang="Haskell" class="myDark" >
 
<syntaxhighlight lang="Haskell" class="myDark" >
 
*Main> pp (view sampleInput )
 
*Main> pp (view sampleInput )
 
+
**********##
          ##
+
**********##
          ##
 
**********
 
**********
 
 
*OOO******
 
*OOO******
 
*OOO******
 
*OOO******
 
*OOO******
 
*OOO******
*XXXX*****  
+
*XXXX*****
 
*XXXX*****
 
*XXXX*****
 
*XXXX*****
 
*XXXX*****

Revision as of 10:20, 19 November 2020

Basic notes

In all exercises you are required to write something to standard output. You can use the same strategy as in Laboratory 7.

Lets define a type for the result:

type Result = [String]

Now, if you want to print this result nicely on the screen, you can use:

pp :: Result -> IO ()
pp x = putStr (concat (map (++"\n") x))

1 - Painting

Lets define a new data types representing a circle and a rectangle.

data Point = Point Int Int
data Shape = Circle Point Int
           | Rectangle {topLeft:: Point, bottomRight::Point}

Using these types write a function view that creates a view of defined shapes. The first parameter is a tuple (columns, rows) defining the size of the resulting view. Left top corner has a coordinate (0,0). Second argument is a list of shapes (either circles or rectangles).

view :: (Int,Int) -> [Shape] -> Result

The result may differ based on rounding. In following example '.' was used for the free spot, '#' for the filled spot.

Prelude>pp(view (40,15) [Circle (Point 8 4) 5, Box {topLeft = (Point 15 5), bottomRight = (Point 35 12) }, Circle (Point 30 12) 8] )
....###...###...........................
....#.......#...........................
...##.......##..........................
...#.........#..........................
...#.........#.............#######......
...#.........#.#####################....
...##.......##.#........##.........##...
....#.......#..#.......##..........###..
....###...###..#.......#...........#.#..
......#####....#......##...........#.##.
...............#......#............#..#.
...............#......#............#..#.
...............#####################..#.
......................#...............#.
......................#...............#.

2 - Drawing

Lets define a new data types representing a point and a line.

data Point = Point Int Int
data Line = Line Point Point

Using these types write a function drawLines that creates a view of defined lines. The first parameter is a tuple (columns, rows) defining the size of the resulting view. Left top corner has a coordinate (0,0). Second argument is a list of lines.

drawLines :: (Int,Int) -> [Line] -> Result

The result may differ based on rounding. In following example '.' was used for the free spot, '#' for the filled spot.

Prelude>pp(drawLines (31,15) [Line (Point x y) (Point 15 7)|(x,y)<-concat [[(x,y)|y<-[0,7,14]]|x<-[0,15,30]]])
##.............#.............##
..##...........#...........##..
....##.........#.........##....
.....###.......#.......###.....
........##.....#.....##........
..........###..#..###..........
.............#####.............
###############################
.............#####.............
..........###..#..###..........
........##.....#.....##........
.....###.......#.......###.....
....##.........#.........##....
..##...........#...........##..
##.............#.............##

3 - Filling

Lets define a picture that is composed from '.' which is used for a free spot and '#' is used for the filled spot.

sampleInput = 
   ["....................",
    "....................",
    "....#####...........",
    "...##...##..........",
    "..##.....##.........",
    "..#.......#.........",
    "..#...############..",
    "..#...#...#......#..",
    "..##..#..##......#..",
    "...##.#.##.......#..",
    "....#####........#..",
    "......#..........#..",
    "......############..",
    "....................",
    "...................."]

Write a function fill that takes a defined picture and a starting position (tuple (column, row)) and it fills the continuous area of free cells starting from the defined starting position with character '*'. You can use the algorithm flood fill.

fill :: Result -> (Int,Int) -> Result
Prelude> pp(fill sampleInput (0,0))
********************
********************
****#####***********
***##...##**********
**##.....##*********
**#.......#*********
**#...############**
**#...#...#......#**
**##..#..##......#**
***##.#.##.......#**
****#####........#**
******#..........#**
******############**
********************
********************

4 - Poker

Lets define a data types representing a deck of Poker cards. In Poker, a player gets 5 cards into the hand. Dealt hands are classified into several categories. These categories are important to define who is the winner. Rules for each category cen be found Here

data Suit = Hearts | Clubs | Diamonds | Spades deriving (Eq, Show)
data Rank = Numeric Int | Jack | Queen | King | Ace deriving (Eq, Show)
data Card = Card Rank Suit deriving (Eq, Show)
type Hand = [Card]
data Category = RoyalFlush
              | StraightFlush
              | Four
              | FullHouse
              | Flush
              | Straight
              | Three
              | TwoPair
              | Pair
              | HighCard deriving (Eq, Show)

Write a function decide that takes 5 dealt cards - Hand and returns a poker category in which it fits.

decide:: Hand -> Category
Prelude> decide [Card (Numeric 2) Hearts,Card (Numeric 2) Clubs,Card Ace Hearts,Card Ace Clubs,Card King Spades]
TwoPair
Prelude> decide [Card (Numeric 2) Hearts,Card (Numeric 2) Clubs,Card Ace Hearts,Card Ace Clubs,Card Ace Spades]
FullHouse
Prelude> decide [Card Ace Hearts,Card (Numeric 2) Hearts,Card (Numeric 5) Hearts,Card (Numeric 3) Hearts,Card (Numeric 4) Clubs]
Straight
Prelude> decide [Card (Numeric 2) Hearts,Card (Numeric 5) Clubs,Card Ace Hearts,Card King Clubs,Card Jack Spades]
HighCard

5 - Cubes

Lets assume, that we have a 3D model composed from cubes in standard Euclidean space. In our model, we are using only positive integers. Each of these cubes is defined by its corner with the smallest x, y and z coordinates and by its size. Also each of these boxes have a color. In our case, while we are using only a text mode, it will be a character. You can assume, that the cubes in our model do not overlap, but there can be cubes placed above other cubes. In the code, it will be represented by following type Cube

-- Point x y z
data Point = Point Int Int Int
data Cube = Cube {start::Point, size::Int, color::Char }

Write a function view that takes a list of cubes ([Cube]) and returns a floor plan (a view from the top). The size of this floor plan will be computed based on the size of the boxes in the input. The result will be a list of strings. Use following function pp to print it on the screen. For the output format, see the sample output bellow. The bottom left corner has the coordinates (0,0), y-axis corresponds to rows and x-axis to columns. Visible parts of the cubes are visualized by its colors. Empty space is represented by character ' '.

type Result = [String]

pp :: Result -> IO ()
pp x = putStr (concat (map (++"\n") x))

sampleInput :: [Cube]
sampleInput = [Cube { start = Point 1 1 10, size = 4, color = 'X'}, 
               Cube { start = Point 1 5 10, size = 3, color = 'O'},
               Cube { start = Point 10 8 0, size = 2, color = '#'},
               Cube { start = Point 0 0 0, size = 10, color = '*'}]

view :: [Cube] -> Result
*Main> pp (view sampleInput )
**********##
**********##
*OOO******
*OOO******
*OOO******
*XXXX*****
*XXXX*****
*XXXX*****
*XXXX*****
**********