From b18bbfdbeadf3ca26940fda1e56a03b0b7d399ba Mon Sep 17 00:00:00 2001 From: Jan-Eric Nitschke <47750513+JanEricNitschke@users.noreply.github.com> Date: Tue, 17 Sep 2024 07:08:09 +0200 Subject: [PATCH] Make F# version functional --- tictactoe_fsharp/TicTacToe/Program.fs | 61 +++++++++++++++++++-------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/tictactoe_fsharp/TicTacToe/Program.fs b/tictactoe_fsharp/TicTacToe/Program.fs index 97642e3..9ab23d9 100644 --- a/tictactoe_fsharp/TicTacToe/Program.fs +++ b/tictactoe_fsharp/TicTacToe/Program.fs @@ -1,4 +1,4 @@ -namespace Tictactoe.Lib +open System type AIStrength = | HUMAN = 0 @@ -123,36 +123,63 @@ type Game(xStrength: AIStrength, oStrength: AIStrength) = board.[move] <- player System.Threading.Thread.Sleep(1000) + let humanTurn player = + // Handle player input for the move + let mutable validMove = false + + while not validMove do + printfn "Player %c, enter your move (0-8): " player + showBoard () + let input = System.Console.ReadLine() + + match System.Int32.TryParse(input) with + | (true, move) when move >= 0 && move <= 8 && board.[move] <> 'X' && board.[move] <> 'O' -> + board.[move] <- player + validMove <- true + | _ -> printfn "Invalid move. Try again." + member this.PlayGame() = let mutable currentPlayer = 'X' + let mutable Break = false - while true do + while not Break do if currentPlayer = 'X' && xStrength <> AIStrength.HUMAN then aiTurn currentPlayer xStrength elif currentPlayer = 'O' && oStrength <> AIStrength.HUMAN then aiTurn currentPlayer oStrength else - // Handle player input for the move - let mutable validMove = false - - while not validMove do - printfn "Player %c, enter your move (0-8): " currentPlayer - showBoard () - let input = System.Console.ReadLine() - - match System.Int32.TryParse(input) with - | (true, move) when move >= 0 && move <= 8 && board.[move] <> 'X' && board.[move] <> 'O' -> - board.[move] <- currentPlayer - validMove <- true - | _ -> printfn "Invalid move. Try again." + humanTurn currentPlayer if isWinner currentPlayer then printfn "Player %c wins!" currentPlayer - break + Break <- true elif isBoardFull () then printfn "It's a tie!" - break + Break <- true currentPlayer <- swapPlayer currentPlayer showBoard () + + + +[] +let main (args) = + printfn "args: %A" args + let mutable X = AIStrength.HUMAN + let mutable O = AIStrength.HUMAN + + for i in 0 .. args.Length - 1 do + if args.[i].StartsWith("-") then + if args.[i] = "-X" && i + 1 < args.Length then + match Enum.TryParse(args.[i + 1]) with + | (true, value) -> X <- value + | _ -> () + elif args.[i] = "-O" && i + 1 < args.Length then + match Enum.TryParse(args.[i + 1]) with + | (true, value) -> O <- value + | _ -> () + + let game = Game(X, O) + game.PlayGame() + 0