Difference between revisions of "FP Homework 1 extension"

From Marek Běhálek Wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
== 1 - Chess position ==
 
== 1 - Chess position ==
  
Implement the function <code>move</code> which has 2 arguments of the type <code>Result</code> (position A and position B). Function result type is  <code>Bool</code>, It is <code>True<code> if it is possible with one chess piece move get from position A to B, <code>false</code> otherwise.
+
Implement the function <code>move</code> which has 2 arguments of the type <code>Result</code> (position A and position B). Function result type is  <code>Bool</code>, It is <code>True</code> if it is possible with one chess piece move get from position A to B, <code>false</code> otherwise.
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 
move :: Result-> Result -> Bool
 
move :: Result-> Result -> Bool
Line 11: Line 11:
 
== 2 - Ticktacktoe ==
 
== 2 - Ticktacktoe ==
 
   
 
   
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.
+
Implement the function <code>winner</code> which has 2 arguments, the same description of a play-filed as before. It will return <code>Bool</code> indicating that one of the players have more then 4 pieces in a row, column or diagonally.
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
ticktack::(Int,Int) -> [(Int,Int)] -> Result
+
winner:: (Int,Int) -> [(Int,Int)] -> Bool
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark" >
 
<syntaxhighlight lang="Haskell" class="myDark" >
Prelude>pp( ticktack (8,8) [(1,1),(8,8),(2,2,)(3,3)(4,2)(3,2)])
 
----------
 
|      o|
 
|        |
 
|        |
 
|        |
 
|        |
 
|  o    |
 
| xox    |
 
|x      |
 
----------
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== 3 - Maze ==
 
== 3 - 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.
+
Implement the function <code>length</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 a position in the maze. Find the length of a shortest path from start to this position.
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
maze :: Result -> String -> Result
+
length :: Result -> (Int,Int) -> Int
  
 
sampleInput = ["*********",
 
sampleInput = ["*********",
Line 45: Line 34:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark" >
 
<syntaxhighlight lang="Haskell" class="myDark" >
Prelude>pp(maze sampleInput "dddrruuurrdddrrddllllll")
 
*********
 
*s*...* *
 
*.*.*.* *
 
*.*.*.* *
 
*...*...*
 
*******.*
 
.......*
 
*********
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== 4 - Minesweeper ==
 
== 4 - Minesweeper ==
  
Implement the function <code>minesweeper</code> 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).
+
Implement the function <code>makeMove</code> which has 2 argument. First is <code>Result</code>. It will be a result from the function <code>minesweeper</code>.  
 +
Second argument will be pair <code>(row,column)</code>, it will represent a position where player 'clicked'. The result will be a playground where just a part of the play-ground will be visible. The character <code>.</code> detonates a hidden field (initially all fields are hidden). If player clicked in a mine, you can output the input. If he clicked on a number different then zero, just the number will be visible. If it is a zero, then the whole consecutive area of zeroes along with bordering numbers will be visible.
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 
minesweeper :: Result -> Result
 
minesweeper :: Result -> Result
sampleInput = ["       ",
+
sampleInput = ["1110000",
               " *     ",
+
               "1*11110",
               "   * ",
+
               "1122*10",
               "   *   ",
+
               "001*221",
               "     *",
+
               "233211*",
               "***   ",
+
               "***2011",
               "* *   ",
+
               "*8*3000",
               "***   "]
+
               "***2000"]
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark" >
 
<syntaxhighlight lang="Haskell" class="myDark" >
Prelude>pp(minesweeper sampleInput)
+
Prelude>pp(makeMove sampleInput (0,7))
1110000
+
..10000
1*11110
+
..11110
1122*10
+
.....10
001*221
+
.....21
233211*
+
.......
***2011
+
.......
*8*3000
+
.......
***2000
+
.......
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== 5 - Ships ==
 
== 5 - Ships ==
  
Implement the function <code>ships</code> 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 every 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, ' ' empty square not attacked yet. You can consider that the size of play-field is 10x10.
+
Implement the function <code>checkOverlap</code> which has 2 arguments. First is a pair <code>(columns, rows)</code> which defines size of the playground. Second argument is a list of list containing coordinates of squares taken by ships. Each of the inner lists represents one ship. Ship is defined as a set of coordinates <code>(column, row)</code>, where column is given using small English alphabet. You can consider that the size of play-field will be smaller then 25x25. Output <code>Bool</code> indicating that the game description is valid. It is valid when no ships overlap or touch each other.
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
ships :: Result -> [(Char, Int)] -> Result
+
checkOverlap:: -> (Int, Int) -> [[(Char, Int)]] -> Bool
sampleInput = ["  o    o  ",
 
              "      ooo ",
 
              "  oo    ",
 
              "          ",
 
              "          ",
 
              "    o    ",
 
              "    o    ",
 
              "    o    ",
 
              "          ",
 
              "          ",
 
              "  oooo    "]
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark" >
 
<syntaxhighlight lang="Haskell" class="myDark" >
Prelude>pp(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
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 07:32, 30 October 2019

1 - Chess position

Implement the function move which has 2 arguments of the type Result (position A and position B). Function result type is Bool, It is True if it is possible with one chess piece move get from position A to B, false otherwise.

move :: Result-> Result -> Bool

2 - Ticktacktoe

Implement the function winner which has 2 arguments, the same description of a play-filed as before. It will return Bool indicating that one of the players have more then 4 pieces in a row, column or diagonally.

winner:: (Int,Int) -> [(Int,Int)] -> Bool

3 - Maze

Implement the function length 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 a position in the maze. Find the length of a shortest path from start to this position.

length :: Result -> (Int,Int) -> Int 

sampleInput = ["*********",
               "*s*   * *",
               "* * * * *",
               "* * * * *",
               "*   *   *",
               "******* *",
               "        *",
               "*********"]

4 - Minesweeper

Implement the function makeMove which has 2 argument. First is Result. It will be a result from the function minesweeper. Second argument will be pair (row,column), it will represent a position where player 'clicked'. The result will be a playground where just a part of the play-ground will be visible. The character . detonates a hidden field (initially all fields are hidden). If player clicked in a mine, you can output the input. If he clicked on a number different then zero, just the number will be visible. If it is a zero, then the whole consecutive area of zeroes along with bordering numbers will be visible.

minesweeper :: Result -> Result
sampleInput = ["1110000",
               "1*11110",
               "1122*10",
               "001*221",
               "233211*",
               "***2011",
               "*8*3000",
               "***2000"]
Prelude>pp(makeMove sampleInput (0,7))
..10000
..11110
.....10
.....21
.......
.......
.......
.......

5 - Ships

Implement the function checkOverlap which has 2 arguments. First is a pair (columns, rows) which defines size of the playground. Second argument is a list of list containing coordinates of squares taken by ships. Each of the inner lists represents one ship. Ship is defined as a set of coordinates (column, row), where column is given using small English alphabet. You can consider that the size of play-field will be smaller then 25x25. Output Bool indicating that the game description is valid. It is valid when no ships overlap or touch each other.

checkOverlap:: -> (Int, Int) -> [[(Char, Int)]] -> Bool