Difference between revisions of "FP Homework 1 extension"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 39: Line 39:
  
 
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>.  
 
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).  
+
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
Line 54: Line 54:
 
Prelude>pp( makeMove (minesweeper sampleInput) (0,7))
 
Prelude>pp( makeMove (minesweeper sampleInput) (0,7))
 
..10000
 
..10000
1*11110
+
..11110
1122*10
+
.....10
001*221
+
.....21
233211*
+
.......
***2011
+
.......
*8*3000
+
.......
***2000
+
.......
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 07:19, 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 = ["       ",
               " *     ",
               "    *  ",
               "   *   ",
               "      *",
               "***    ",
               "* *    ",
               "***    "]
Prelude>pp( makeMove (minesweeper sampleInput) (0,7))
..10000
..11110
.....10
.....21
.......
.......
.......
.......

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

ships :: Result -> [(Char, Int)] -> Result
sampleInput = ["  o    o  ",
               "      ooo ",
               "   oo     ",
               "          ",
               "          ",
               "     o    ",
               "     o    ",
               "     o    ",
               "          ",
               "          ",
               "  oooo    "]
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