From 66d5c35b60086221d10d151cea356f5c2e747684 Mon Sep 17 00:00:00 2001 From: peet Date: Wed, 6 Mar 2024 15:01:45 +0100 Subject: [PATCH] keine ahnung --- src/main/scala/ScalaGo/REPL.scala | 12 +++- src/main/scala/ScalaGo/ScalaGo.scala | 86 +++++++++++++++++++++++----- 2 files changed, 81 insertions(+), 17 deletions(-) diff --git a/src/main/scala/ScalaGo/REPL.scala b/src/main/scala/ScalaGo/REPL.scala index a5e746c..a890cdc 100644 --- a/src/main/scala/ScalaGo/REPL.scala +++ b/src/main/scala/ScalaGo/REPL.scala @@ -28,7 +28,12 @@ object REPL: * @return String depicting the FieldContent */ def getFieldContentString(c: FieldContent): String = - ??? + c match + case Stone(White) => "○" + case Stone(Black) => "●" + case Empty(White) => "◌" + case Empty(Black) => "◍" + case _ => " " /** * Return a String displaying the current state of the board. If the previous board is supplied it is used to @@ -58,7 +63,7 @@ object REPL: * Print which player's turn it is to standard output */ def printCurrentPlayer(): Unit = - ??? + println(gs.player) /** * Print the players' score to standard output @@ -69,4 +74,5 @@ object REPL: * @param fieldsBlack number of fields enclosed by black stones */ def printFinalScore(stonesWhite: Int, fieldsWhite: Int, stonesBlack: Int, fieldsBlack: Int): Unit = - ??? + println(s"Schwarze Steine: $stonesBlack , Weiße Steine: $stonesWhite") + println(s"Schwarze Felder: $fieldsBlack , Weiße Felder: $fieldsWhite") diff --git a/src/main/scala/ScalaGo/ScalaGo.scala b/src/main/scala/ScalaGo/ScalaGo.scala index dcf5ccf..204aea7 100644 --- a/src/main/scala/ScalaGo/ScalaGo.scala +++ b/src/main/scala/ScalaGo/ScalaGo.scala @@ -85,12 +85,11 @@ object ScalaGo: def main(args: Array[String]): Unit = - ??? - //val (start, end) = buildStartToEndStates() + val (start, end) = buildStartToEndStates() - //gs = start + gs = start - //REPL.repl(end) + REPL.repl(end) /** @@ -99,13 +98,35 @@ object ScalaGo: * @return a tuple of the start and the end states */ def buildStartToEndStates(): (GameState, GameState) = - ??? + var startState = GameState(Nobody,new Array[(String => Boolean, GameState)](1), ()=> println("Start game!")) + var blackPlayState = GameState(Black, new Array[(String => Boolean, GameState)](2), outputBoardAndPlayer()) + var whitePlayState = GameState(White, new Array[(String => Boolean, GameState)](2), outputBoardAndPlayer()) + var blackPlayWhitePassedState = GameState(Black, new Array[(String => Boolean, GameState)](2), outputBoardAndPlayer()) + var whitePlayBlackPassedState = GameState(White, new Array[(String => Boolean, GameState)](2), outputBoardAndPlayer()) + var calculateScoreState = GameState(Nobody, new Array[(String => Boolean, GameState)](1), calculateScore()) + var endState = GameState(Nobody, Array.empty, {}) + + startState.transitions(0) = (buildBoardTransition(), blackPlayState) + blackPlayState.transitions(0) = (placeStoneTransition(), whitePlayState) + blackPlayState.transitions(1) = (passTransition(), whitePlayState) + whitePlayState.transitions(0) = (placeStoneTransition(), blackPlayState) + whitePlayState.transitions(1) = (passTransition(), blackPlayState) + blackPlayWhitePassedState(0) = (placeStoneTransition(), whitePlayState) + blackPlayWhitePassedState(1) = (passTransition(), calculateScoreState) + whitePlayBlackPassedState(0) = (placeStoneTransition(), blackPlayState) + whitePlayBlackPassedState(1) = (passTransition(), calculateScoreState) + calculateScoreState.transitions(0) = ((_) => true, endState) + + return (startState, endState) + /** * Output the current board an player and afterwards remove old RemovedStone markers */ def outputBoardAndPlayer(): Unit = - ??? + REPL.printBoard(board, prevBoard) + REPL.printCurrentPlayer() + removeOldRemovedStones(board) /** * Remove all RemovedStone markers from the board @@ -113,7 +134,9 @@ object ScalaGo: * @param b board to remove from */ def removeOldRemovedStones(b: Goboard): Unit = - ??? + for i <- b.indices; j <- b.indices do { + if b(i)(j) == Empty then Empty(Nobody) + } /** * Remove stones of each chain's positions where the chain has no liberties @@ -123,7 +146,17 @@ object ScalaGo: * @param b board to remove the stones from */ def removeStonesOfZeroChains(chains: SimpleList[Chain], c: Color, b: Goboard): Unit = - ??? + var chain = chains + while chain != null do { + if (chain.entry.isDefined && length(chain.entry.get.liberties) == 0 && chain.entry.get.color == c) { + var positions = chain.entry.get.position + while positions != null do { + setPosition(b, positions.entry.get, Empty(c)) + positions = positions.next + } + } + chain = chain.next + } /** * Remove all stones of chains with zero liberties starting with the opposing players color and then remove remaining @@ -132,7 +165,11 @@ object ScalaGo: * @param b board to remove the stones from */ def killChains(b: Goboard): Unit = - ??? + var chain = findChains(b) + updateLiberties(chain, b) + removeStonesOfZeroChains(chain, if gs.player == Black then White else Black, b) + updateLiberties(chain, b) + removeStonesOfZeroChains(chain, gs.player, b) /** * Tests if two boards are identical with regard to only Empty and Stones. @@ -142,7 +179,12 @@ object ScalaGo: * @return true if both boards represent equal positions, false otherwise */ def equalBoardPositions(b1: Goboard, b2: Goboard): Boolean = - ??? + (b1(i)(j), b(i)(j)) match + case (Stone(x), Stone(y)) if (x != y) => false + case (Empty(_), Stone(_)) => false + case (Stone(_), Empty(_)) => false + case _ => + /** * Create a real copy of the board @@ -151,7 +193,7 @@ object ScalaGo: * @return an identical board at a different memory location */ def copyBoard(b: Goboard): Goboard = - ??? + b.clone() /** * Set a position within a board. Thows an OutOfBoardException if the position is not within the board. @@ -161,7 +203,8 @@ object ScalaGo: * @param fc the new content of the field at position */ def setPosition(b: Goboard, pos: Position, fc: FieldContent): Unit = - ??? + if pos._1 < 0 || pos._1 >= b.length || pos._2 < 0 || pos._2 >= b.length then throw new OutOfBoardException("Nicht innerhalb des Spielfeldes") + b(pos._1)(pos._2) = fc /** * Test if a field is only surrounded by stones of one color. This search may span multiple Empty fields. @@ -179,7 +222,21 @@ object ScalaGo: * */ def calculateScore(): Unit = - ??? + removeOldRemovedStones(board) + var stonesWhite = 0 + var stonesBlack = 0 + var fieldsWhite = 0 + var fieldsBlack = 0 + for (i <- board.indices; j <- board.indices) do { + getFieldContent(i, j , board) match + case Stone(White) => stonesWhite = stonesWhite + 1 + case Stone(Black) => stonesBlack = stonesBlack + 1 + case Empty(Nobody) => + if (isSurroundedByOnly((i,j),Black,board)) then fieldsBlack = fieldsBlack + 1 + else if (isSurroundedByOnly((i,j), White, board)) then fieldsWhite = fieldsWhite + 1 + case _ => throw new Exception("Du hast verkackt beim programieren") + } + REPL.printFinalScore(stonesWhite, fieldsWhite, stonesBlack, fieldsBlack) /** * Generate a board of size*size dimensions. @@ -188,7 +245,8 @@ object ScalaGo: * @return true if a valid board could be printed, false otherwise */ def buildBoardTransition(size: String): Boolean = - ??? + if () then true + else false /** * Parse a player's input to detect a stone placement command and place a stone