Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
fp2024:hw3 [2024/05/05 11:36] pdmatei |
fp2024:hw3 [2024/05/08 16:56] (current) pdmatei [Submission rules] |
||
---|---|---|---|
Line 36: | Line 36: | ||
==== Coding tasks ==== | ==== Coding tasks ==== | ||
- | **1.** Write a function which converts a string into a ''Board''. As a helper, you can use ''_.split(c)'' where c is a separator string, and ''_.toList''. The best solution is to use a combination of ''map'' calls with the above mentioned functions. A string is encoded exactly as in the examples shown above: | + | **1.** Boards will be represented using the class ''Board'' (see the project template), which contains a board matrix, as well as the player whose turn it is to play. You may consider that the first player starts the game. (For testing purposes, you may also create other apply methods). In order to create a board, write a function which converts a string into a ''Board''. Implement the apply method in the companion object ''Board''. As a helper, you can use ''_.split( c )'' where c is a separator string, and ''_.toList''. The best solution is to use a combination of ''map'' calls with the above mentioned functions. A string is encoded exactly as in the examples shown above: |
* there are no whitespaces - empty positions are marked by the character '.' | * there are no whitespaces - empty positions are marked by the character '.' | ||
* lines are delimited by '\n' (the last line does not have a trailing '\n'). | * lines are delimited by '\n' (the last line does not have a trailing '\n'). | ||
<code scala> | <code scala> | ||
- | def makeBoard(s: String): Board = { | + | def apply(s: String): Board = { |
def toPos(c: Char): Player = | def toPos(c: Char): Player = | ||
c match { | c match { | ||
Line 51: | Line 51: | ||
</code> | </code> | ||
- | **2.** Write a function which checks if a position ''(x,y)'' on the board is free. Recall that list indexing can be done using ''l(_)''. Positions are numbered from 0. | + | **2.** Implement the member function ''isFree'' which checks if a position ''(x,y)'' on the board is free. Recall that list indexing can be done using ''l(_)''. Positions are numbered from 0. |
<code scala> | <code scala> | ||
- | def isFree(x:Int, y:Int, b:Board):Boolean = ??? | + | def isFree(x:Int, y:Int):Boolean = ??? |
</code> | </code> | ||
- | **3.** Write a function which returns the //opponent// of a player: | + | **3.** Write a function which returns the //opponent// of a player. The function is defined in the trait ''Player'', which is allowed in Scala: |
<code scala> | <code scala> | ||
- | def complement(p: Player): Player = | + | def complement: Player = ??? |
</code> | </code> | ||
- | **4.** Write a function which converts a board to a string, following the same strategy. **Important:** this function will be used throughout the tests. Make sure the string doesn't end with '\n'. Hint: instead of ''foldRight'', you can use ''reduce'' which works quite similarly, but without requiring an accumulator. | + | **4.** Implement the ''toString'' functions which converts a board to a string, following the same strategy. **Important:** this function will be used throughout the tests. Make sure the string doesn't end with '\n'. Hint: instead of ''foldRight'', you can use ''reduce'' which works quite similarly, but without requiring an accumulator. |
<code scala> | <code scala> | ||
- | def show(b: Board): String = ??? | + | override def toString: String = ??? |
</code> | </code> | ||
**5.** Write a function which returns the //columns// of a board: | **5.** Write a function which returns the //columns// of a board: | ||
<code scala> | <code scala> | ||
- | def getColumns(b:Board): Board = ??? | + | def getColumns: Board = ??? |
</code> | </code> | ||
**6.** Implement the following two functions for extracting the first and second diagonal, as lines, from a board. Hint: use for comprehensions. | **6.** Implement the following two functions for extracting the first and second diagonal, as lines, from a board. Hint: use for comprehensions. | ||
<code scala> | <code scala> | ||
- | def getFstDiag(b:Board): Line = ??? | + | def getFstDiag: Line = ??? |
- | def getSndDiag(b:Board): Line = ??? | + | def getSndDiag: Line = ??? |
</code> | </code> | ||
**7.** Implement the following functions for extracting diagonals above/below the first/second diagonal, as lines. It's not really necessary to make sure that at least 5 positions are available, for now. Hint: if one function must be implemented with element-by-element iteration, the three other can be implemented using each-other, as single-line calls. | **7.** Implement the following functions for extracting diagonals above/below the first/second diagonal, as lines. It's not really necessary to make sure that at least 5 positions are available, for now. Hint: if one function must be implemented with element-by-element iteration, the three other can be implemented using each-other, as single-line calls. | ||
<code scala> | <code scala> | ||
- | def getAboveFstDiag(b: Board): List[Line] = ??? | + | def getAboveFstDiag: List[Line] = ??? |
- | def getBelowFstDiag(b: Board): List[Line] = ??? | + | def getBelowFstDiag: List[Line] = ??? |
- | def getAboveSndDiag(b: Board): List[Line] = ??? | + | def getAboveSndDiag: List[Line] = ??? |
- | def getBelowSndDiag(b: Board): List[Line] = ??? | + | def getBelowSndDiag: List[Line] = ??? |
</code> | </code> | ||
- | **8.** Write a function which checks if a player is the winner. Hint: functions ''l.forall(_)'' and ''l.exists(_)'' may be very helpful, together with patterns. | + | **8.** Write a function which checks if the current player is the winner. Hint: functions ''l.forall(_)'' and ''l.exists(_)'' may be very helpful, together with patterns. |
<code scala> | <code scala> | ||
- | def winner(p: Player)(b: Board): Boolean = | + | def winner: Boolean = ??? |
</code> | </code> | ||
**9.** Write a function which updates a position from the board, with a given player. The position need not be empty and you are not required to check this. Hint: re-use an inner aux-function together with ''take'' and ''drop''. | **9.** Write a function which updates a position from the board, with a given player. The position need not be empty and you are not required to check this. Hint: re-use an inner aux-function together with ''take'' and ''drop''. | ||
<code scala> | <code scala> | ||
- | def update(p: Player)(ln: Int, col: Int, b: Board) : Board = ??? | + | def update(ln: Int, col: Int) : Board = ??? |
</code> | </code> | ||
**10.** Write a function which generates all possible next-moves for any of the two players. A next-move consists in a new board, where the player-at-hand played his move. The order in which you generate next-moves is not important. | **10.** Write a function which generates all possible next-moves for any of the two players. A next-move consists in a new board, where the player-at-hand played his move. The order in which you generate next-moves is not important. | ||
<code scala> | <code scala> | ||
- | def next(p: Player)(b: Board): List[Board] = ??? | + | def next: List[Board] = ??? |
</code> | </code> | ||
Line 110: | Line 110: | ||
<code scala> | <code scala> | ||
- | def sequences(p: Player)(b: Board): Map[Int,Int] = ??? | + | def sequences: Map[Int,Int] = ??? |
</code> | </code> | ||
Line 116: | Line 116: | ||
===== Submission rules ===== | ===== Submission rules ===== | ||
- | * Please follow the [[fp2023:submission-guidelines| Submission guidelines]] which are the same for all homework. | + | * Please follow the [[fp2024:submission-guidelines| Submission guidelines]] which are the same for all homework. |
- | * To solve your homework, download the {{:fp2023:h3-5-in-a-row.zip|Project template}}, import it in IntellIJ, and you are all set. Do not rename the project manually, as this may cause problems with IntellIJ. | + | * To solve your homework, download the {{:fp2024:hw03-five-in-a-row.zip|Project template}}, import it in IntellIJ, and you are all set. Do not rename the project manually, as this may cause problems with IntellIJ. |