Difference between revisions of "FP Homework 1"

From Marek Běhálek Wiki
Jump to navigation Jump to search
(Created page with "== 1 - Šachová pozice == Napište funkci sachy, která má 2 argumenty typu seznam řetězců. Řetězce v seznamech obsahují 3 znaky: - první určuje šachovou figuru ('...")
 
Line 1: Line 1:
== 1 - Šachová pozice ==
+
== Basic notes ==
  
Napište funkci sachy, která má 2 argumenty typu seznam řetězců. Řetězce v seznamech obsahují 3 znaky:
+
in all exercises you are required to write something to standard output. You can use the same strategy as in  [[FP_Laboratory_7 | Laboratory 7]].  
- první určuje šachovou figuru ('K' - král, 'D' - dáma, 'V' - věž, 'S' - střelec, 'J' - jezdec, 'P' - pěšec)
 
- druhý znak určuje sloupec ('a'-'h')
 
- třetí je číslo řádku ('1'-'8')
 
První seznam reprezentuje aktuální rozmístění bílých figur a druhý černých. Vypište aktuální pozici tak, že volná políčka budou reprezentována znakem '.', bílé figury svým písmenem velkým a černé figury svým písmenem malým. Řádky i sloupce budou označeny čísly resp. písmeny.
 
  
sachy ["Ke1","Va1","Vh1","Pa2","Se5"] ["Ke8","Va8","Vh8","Pa7","Dd8","Sc8","Jb8"]
+
Lets define a type for the result:
  
8vjsdk..v
+
<syntaxhighlight lang="Haskell">type Result = [String]</syntaxhighlight>
 +
 
 +
Now, if you want to print this result you can use:
 +
 
 +
<syntaxhighlight lang="Haskell">
 +
pp :: Result -> IO ()
 +
pp x = putStr (concat (map (++"\n") x))
 +
</syntaxhighlight>
 +
 
 +
== 1 - Chess position ==
 +
 
 +
Implement the function <code>chess</code> which has 2 arguments of the type <code>[String]</code>. 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.
 +
 
 +
<syntaxhighlight lang="Haskell">
 +
chess :: String -> String -> Result
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="Haskell" class="myDark" >
 +
Prelude> chess["Ke1","Ra1","Rh1","Pa2","Be5"] ["Ke8","Ra8","Rh8","Pa7","Qd8","Bc8","Nb8"]
 +
8rnbqk..r
 
7p.......
 
7p.......
 
6........
 
6........
5....S...
+
5....B...
 
4........
 
4........
 
3........
 
3........
 
2P.......
 
2P.......
1V...K..V
+
1R...K..R
 
  abcdefgh
 
  abcdefgh
 +
</syntaxhighlight>
  
== 2 - Piškvorky ==
+
== 2 - Ticktacktoe ==
 
   
 
   
Napište funkci sachPozice, která má 2 argumenty. První je dvojice přirozených čísel určující počet řádků a sloupců hrací plochy. Druhý seznam reprezentuje průběh hry piškvorky, kde jsou souřadnice políček, na které střídavě hrál hráč 'x' a hráč 'o'. Vypište aktuální stav hry tak, že hrací pole bude ohraničeno znaky '-' a '|', volné pozice ' ' a znaky 'x' a 'o' budou na pozicích, kam zahráli příslušní hráči.
+
Implement the function ticktack which has 2 arguments. First argument is a tuple of natural numbers and means number of rows and columns of a play field. Second list contains a play of ticktacktoe given by coordinates on which played in turns player 'x' and player 'o'. Print actual state of a play in the way where playfield will be bordered by characters '-' and '|', empty squares ' ' and characters 'x' and 'o' will be on squeres where the players have played.
  
piskvorky (8,8) [(1,1),(8,8),(2,2,)(3,3)(4,2)(3,2)]
+
ticktack (8,8) [(1,1),(8,8),(2,2,)(3,3)(4,2)(3,2)]
  
 
----------
 
----------
Line 36: Line 56:
 
----------
 
----------
  
== 3 - Bludiště ==
+
== 3 - Maze ==
  
Napište funkci bludiste, která má 2 argumenty. Prvním argumentem je seznam řetězců, které reprezentují bludiště po řádcích postupně shora dolů ('*' - stěna, ' ' - průchozí pole, 's' - startovní pozice, 'c' - cílová pozice). Na začátku se nacházíme na pozici 's'. Druhým argumentem je seznam směrů pohybu ('d' - down, 'u' - up, 'l' - left, 'r' - right). Každé písmeno znamená, že se posuneme o 1 buňku daným směrem a na novou pozici umístíme znak '.' Vypište aktuální situaci po provedení všech kroků z druhého seznamu.
+
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.
  
ukazkovyVstup = ["*********",
+
sampleInput = ["*********",
                "*s*  * *",
+
              "*s*  * *",
                "* * * * *",
+
              "* * * * *",
                "* * * * *",
+
              "* * * * *",
                "*  *  *",
+
              "*  *  *",
                "******* *",
+
              "******* *",
                "c       *",
+
              "d       *",
                "*********"]
+
              "*********"]
 
                  
 
                  
bludiste ukazkovy
+
maze sampleInput "dddrruuurrdddrrddllllll"
Vstup "dddrruuurrdddrrddllllll"
 
  
 
*********
 
*********
Line 61: Line 80:
 
*********
 
*********
  
== 4 - Miny ==
+
== 4 - Minesweeper ==
  
Napište funkci miny, která má argument typu seznam řetězců. Řetězce reprezentují hrací plochu po řádcích postupně shora dolů ('*' - mina, ' ' - prázdné pole). Vypište hrací pole tak, že miny budou stále reprezentovány '*', ale na každém políčku bez miny bude číslo znamenající celkový počet min, se kterými toto prázdné pole přímo sousedí (sousedit může vodorovně, svisle i šikmo).
+
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).
  
ukazkovyVstup = ["      ",
+
sampleInput = ["      ",
                " *    ",
+
              " *    ",
                "    *  ",
+
              "    *  ",
                "  *  ",
+
              "  *  ",
                "      *",
+
              "      *",
                "***    ",
+
              "***    ",
                "* *    ",
+
              "* *    ",
                "***    "]
+
              "***    "]
  
miny ukazkovyVstup
+
minesweeper sampleInput
  
 
1110000
 
1110000
Line 85: Line 104:
 
***2000
 
***2000
  
== 5 - Lode ==
+
== 5 - Ships ==
  
Napište funkci lode, která má dva argumenty. První je seznam řetězců, které reprezentují hrací plochu jednoho hráče po řádcích postupně shora dolů ('o' - políčko obsazené lodí, ' ' - prázdné pole). Druhým argumentem je seznam dvojic souřadnic políček, na které druhý hráč zkoušel střílet. Vykreslete aktuální stav hry tak, že řádky a sloupce budou označeny svým číslem resp. písmenem, 'o' bude dosud nezasažené políčko s lodí, 'x' zasažené políčko s lodí, '.' místo, kam se střílelo, ale nic nezasáhlo, ' ' prázdná a dosud nezasažená políčka. Můžete předpokládat hrací plochu velikosti 10x10.
+
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.
  
ukazkovyVstup = ["  o    o  ",
+
sampleInput = ["  o    o  ",
                "      ooo ",
+
              "      ooo ",
                "  oo    ",
+
              "  oo    ",
                "          ",
+
              "          ",
                "          ",
+
              "          ",
                "    o    ",
+
              "    o    ",
                "    o    ",
+
              "    o    ",
                "    o    ",
+
              "    o    ",
                "          ",
+
              "          ",
                "          ",
+
              "          ",
                "  oooo    "]
+
              "  oooo    "]
  
lode ukazkovyVstup [('a',1),('d',1),('d',2),('c',1),('b',1),('e',1),('f',1),('g',1),('c',7),('c',10)]
+
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   
 
10  x    o   

Revision as of 12:00, 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 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> 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 means number of rows and columns of a play field. Second list contains a play of ticktacktoe given by coordinates on which played in turns player 'x' and player 'o'. Print actual state of a play in the way where playfield will be bordered by characters '-' and '|', empty squares ' ' and characters 'x' and 'o' will be on squeres where the players have played.

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, '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.

sampleInput = ["*********",

              "*s*   * *",
              "* * * * *",
              "* * * * *",
              "*   *   *",
              "******* *",
              "d       *",
              "*********"]
                

maze sampleInput "dddrruuurrdddrrddllllll"

  • s*...* *
  • .*.*.* *
  • .*.*.* *
  • ...*...*
              • .*

c.......*

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