Difference between revisions of "FP Homework 1"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 41: Line 41:
 
== 2 - Ticktacktoe ==
 
== 2 - Ticktacktoe ==
 
   
 
   
Implement the function ticktack which has 2 arguments. First argument is a tuple of natural numbers and defines the number of rows and columns of a play field. Second list contains a record of a match of ticktacktoe game given by coordinates on which played in turns player 'x' and player 'o'. Print actual state of the game in the way where play-field will be bordered by characters '-' and '|', empty squares ' ' and characters 'x' and 'o' will be on squares where the players have played.
+
Implement the function <code>ticktack</code> which has 2 arguments. First argument is a tuple of natural numbers and defines the number of rows and columns of a play field. Second list contains a record of a match of ticktacktoe game given by coordinates on which played in turns player 'x' and player 'o'. Print actual state of the game in the way where play-field will be bordered by characters '-' and '|', empty squares ' ' and characters 'x' and 'o' will be on squares where the players have played.
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 
ticktack::(Int,Int) -> [(Int,Int)] -> Result
 
ticktack::(Int,Int) -> [(Int,Int)] -> Result
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark" >
 
<syntaxhighlight lang="Haskell" class="myDark" >
Prelude>ticktack (8,8) [(1,1),(8,8),(2,2,)(3,3)(4,2)(3,2)]
+
Prelude>pp( ticktack (8,8) [(1,1),(8,8),(2,2,)(3,3)(4,2)(3,2)])
 
----------
 
----------
 
|      o|
 
|      o|
Line 60: Line 60:
 
== 3 - Maze ==
 
== 3 - Maze ==
  
Implement the function maze which has 2 arguments. First argument is a list of strings representing a maze row by row from top to bottom ('*' - wall, ' ' - empty square, 's' - starting position, 'd' - destination). At the begining we are at position 's'. Second argument is list of directions ('d' - down, 'u' - up, 'l' - left, 'r' - right). Each letter means move by one square in the given direction and on this new square character '.' is placed. Print actual state of a maze.
+
Implement the function <code>maze</code> which has 2 arguments. First argument is a list of strings representing a maze row by row from top to bottom ('*' - wall, ' ' - empty square, 's' - starting position). At the beginning we are at position 's'. Second argument is list of directions ('d' - down, 'u' - up, 'l' - left, 'r' - right). Each letter means move by one square in the given direction and on this new square character '.' is placed. Print actual state of a maze.
 +
<syntaxhighlight lang="Haskell">
 +
maze :: Result -> String -> Result
  
 
sampleInput = ["*********",
 
sampleInput = ["*********",
Line 70: Line 72:
 
               "d      *",
 
               "d      *",
 
               "*********"]
 
               "*********"]
               
+
</syntaxhighlight>
maze sampleInput "dddrruuurrdddrrddllllll"
+
<syntaxhighlight lang="Haskell" class="myDark" >
 
+
Prelude>pp(maze sampleInput "dddrruuurrdddrrddllllll")
 
*********
 
*********
 
*s*...* *
 
*s*...* *
Line 79: Line 81:
 
*...*...*
 
*...*...*
 
*******.*
 
*******.*
c.......*
+
.......*
 
*********
 
*********
 +
</syntaxhighlight>
  
 
== 4 - Minesweeper ==
 
== 4 - Minesweeper ==

Revision as of 12:14, 21 October 2019

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 - Chess position

Implement the function chess which has 2 arguments of the type [String]. Each of these strings in both lists contain 3 characters:

  • first stands for chess piece ('K' - king, 'Q' - queen, 'R' - rook, 'B' - bishop, 'N' - knight, 'P' - pawn)
  • second stands for column ('a'-'h')
  • third is row ('1'-'8')

First list contains actual positions of white pieces and second list positions of black pieces. Print actual state of a chessboard in the way where '.' stands for empty square, capital letters mean white pieces and small letters mean black pieces. Each row and column should be labeled by its number or letter.

chess :: String -> String -> Result
Prelude> pp( chess["Ke1","Ra1","Rh1","Pa2","Be5"] ["Ke8","Ra8","Rh8","Pa7","Qd8","Bc8","Nb8"])
8rnbqk..r
7p.......
6........
5....B...
4........
3........
2P.......
1R...K..R
 abcdefgh

2 - Ticktacktoe

Implement the function ticktack which has 2 arguments. First argument is a tuple of natural numbers and defines the number of rows and columns of a play field. Second list contains a record of a match of ticktacktoe game given by coordinates on which played in turns player 'x' and player 'o'. Print actual state of the game in the way where play-field will be bordered by characters '-' and '|', empty squares ' ' and characters 'x' and 'o' will be on squares where the players have played.

ticktack::(Int,Int) -> [(Int,Int)] -> Result
Prelude>pp( ticktack (8,8) [(1,1),(8,8),(2,2,)(3,3)(4,2)(3,2)])
----------
|       o|
|        |
|        |
|        |
|        |
|  o     |
| xox    |
|x       |
----------

3 - Maze

Implement the function maze which has 2 arguments. First argument is a list of strings representing a maze row by row from top to bottom ('*' - wall, ' ' - empty square, 's' - starting position). At the beginning we are at position 's'. Second argument is list of directions ('d' - down, 'u' - up, 'l' - left, 'r' - right). Each letter means move by one square in the given direction and on this new square character '.' is placed. Print actual state of a maze.

maze :: Result -> String -> Result 

sampleInput = ["*********",
               "*s*   * *",
               "* * * * *",
               "* * * * *",
               "*   *   *",
               "******* *",
               "d       *",
               "*********"]
Prelude>pp(maze sampleInput "dddrruuurrdddrrddllllll")
*********
*s*...* *
*.*.*.* *
*.*.*.* *
*...*...*
*******.*
 .......*
*********

4 - Minesweeper

Implement the function minesweeper which has 1 argument of the type list of strings. The strings represent play field row by row from top to bottom ('*' - mine, ' ' - empty square). Print play field in a way where mines will be represented by '*' and on each square not containing a mine will be a number - count of all mines directly adjacent to this square (it can be adjacent vertically, horizontally or diagonally).

sampleInput = [" ",

              " *     ",
              "    *  ",
              "   *   ",
              "      *",
              "***    ",
              "* *    ",
              "***    "]

minesweeper sampleInput

1110000 1*11110 1122*10 001*221 233211*

      • 2011
  • 8*3000
      • 2000

5 - Ships

Implement the function ships which has 2 arguments. First argument is a list of strings representing play field of one player row by row from top to bottom ('o' - square containing a ship, ' ' - empty square). Second list contains coordinates of squares attacked by second player. Print actual state of a play in the way where avery row and column will be labelled by its number or letter, 'o' will be square with ship not attacked yet, 'x' square with ship already attacked, '.' already attacked empty square, ' ' empy square not attacked yet. You can consider the size of play field 10x10.

sampleInput = [" o o ",

              "      ooo ",
              "   oo     ",
              "          ",
              "          ",
              "     o    ",
              "     o    ",
              "     o    ",
              "          ",
              "          ",
              "  oooo    "]

ships sampleInput [('a',1),('d',1),('d',2),('c',1),('b',1),('e',1),('f',1),('g',1),('c',7),('c',10)]

10 x o

9      ooo 
8   oo     
7  .       
6          
5     o    
4     o    
4     o    
3          
2   .      
1..xxxx.   
 abcdefghij