From 2637a60c8357bce41ba1a83836b1b23929a87bc4 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Tue, 22 Oct 2024 17:53:12 +0200 Subject: [PATCH] killer-sudoku-helper: fix generator (#1297) * bottle-song: fix generator * resistor-color-trio: fix generator * knapsack: fix generator * killer-sudoku-helper: fix generator * state-of-tic-tac-toe: fix generator --- .../practice/bottle-song/BottleSongTests.fs | 198 ++++--- .../killer-sudoku-helper/.meta/Example.fs | 2 +- .../KillerSudokuHelper.fs | 2 +- .../KillerSudokuHelperTests.fs | 36 +- exercises/practice/knapsack/KnapsackTests.fs | 98 ++-- .../ResistorColorTrioTests.fs | 52 +- .../state-of-tic-tac-toe/.meta/Example.fs | 14 +- .../StateOfTicTacToeTests.fs | 502 ++++++++---------- generators/Generators.fs | 44 ++ 9 files changed, 441 insertions(+), 507 deletions(-) diff --git a/exercises/practice/bottle-song/BottleSongTests.fs b/exercises/practice/bottle-song/BottleSongTests.fs index 1748fb11..892049d9 100644 --- a/exercises/practice/bottle-song/BottleSongTests.fs +++ b/exercises/practice/bottle-song/BottleSongTests.fs @@ -7,130 +7,124 @@ open BottleSong [] let ``First generic verse`` () = - recite 10 1 - |> should - equal - [ "Ten green bottles hanging on the wall," - "Ten green bottles hanging on the wall," - "And if one green bottle should accidentally fall," + let expected = + [ "Ten green bottles hanging on the wall,"; + "Ten green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; "There'll be nine green bottles hanging on the wall." ] + recite 10 1 |> should equal expected [] let ``Last generic verse`` () = - recite 3 1 - |> should - equal - [ "Three green bottles hanging on the wall," - "Three green bottles hanging on the wall," - "And if one green bottle should accidentally fall," + let expected = + [ "Three green bottles hanging on the wall,"; + "Three green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; "There'll be two green bottles hanging on the wall." ] + recite 3 1 |> should equal expected [] let ``Verse with 2 bottles`` () = - recite 2 1 - |> should - equal - [ "Two green bottles hanging on the wall," - "Two green bottles hanging on the wall," - "And if one green bottle should accidentally fall," + let expected = + [ "Two green bottles hanging on the wall,"; + "Two green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; "There'll be one green bottle hanging on the wall." ] + recite 2 1 |> should equal expected [] let ``Verse with 1 bottle`` () = - recite 1 1 - |> should - equal - [ "One green bottle hanging on the wall," - "One green bottle hanging on the wall," - "And if one green bottle should accidentally fall," + let expected = + [ "One green bottle hanging on the wall,"; + "One green bottle hanging on the wall,"; + "And if one green bottle should accidentally fall,"; "There'll be no green bottles hanging on the wall." ] + recite 1 1 |> should equal expected [] let ``First two verses`` () = - recite 10 2 - |> should - equal - [ "Ten green bottles hanging on the wall," - "Ten green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be nine green bottles hanging on the wall." - "" - "Nine green bottles hanging on the wall," - "Nine green bottles hanging on the wall," - "And if one green bottle should accidentally fall," + let expected = + [ "Ten green bottles hanging on the wall,"; + "Ten green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be nine green bottles hanging on the wall."; + ""; + "Nine green bottles hanging on the wall,"; + "Nine green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; "There'll be eight green bottles hanging on the wall." ] + recite 10 2 |> should equal expected [] let ``Last three verses`` () = - recite 3 3 - |> should - equal - [ "Three green bottles hanging on the wall," - "Three green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be two green bottles hanging on the wall." - "" - "Two green bottles hanging on the wall," - "Two green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be one green bottle hanging on the wall." - "" - "One green bottle hanging on the wall," - "One green bottle hanging on the wall," - "And if one green bottle should accidentally fall," + let expected = + [ "Three green bottles hanging on the wall,"; + "Three green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be two green bottles hanging on the wall."; + ""; + "Two green bottles hanging on the wall,"; + "Two green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be one green bottle hanging on the wall."; + ""; + "One green bottle hanging on the wall,"; + "One green bottle hanging on the wall,"; + "And if one green bottle should accidentally fall,"; "There'll be no green bottles hanging on the wall." ] + recite 3 3 |> should equal expected [] let ``All verses`` () = - recite 10 10 - |> should - equal - [ "Ten green bottles hanging on the wall," - "Ten green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be nine green bottles hanging on the wall." - "" - "Nine green bottles hanging on the wall," - "Nine green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be eight green bottles hanging on the wall." - "" - "Eight green bottles hanging on the wall," - "Eight green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be seven green bottles hanging on the wall." - "" - "Seven green bottles hanging on the wall," - "Seven green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be six green bottles hanging on the wall." - "" - "Six green bottles hanging on the wall," - "Six green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be five green bottles hanging on the wall." - "" - "Five green bottles hanging on the wall," - "Five green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be four green bottles hanging on the wall." - "" - "Four green bottles hanging on the wall," - "Four green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be three green bottles hanging on the wall." - "" - "Three green bottles hanging on the wall," - "Three green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be two green bottles hanging on the wall." - "" - "Two green bottles hanging on the wall," - "Two green bottles hanging on the wall," - "And if one green bottle should accidentally fall," - "There'll be one green bottle hanging on the wall." - "" - "One green bottle hanging on the wall," - "One green bottle hanging on the wall," - "And if one green bottle should accidentally fall," + let expected = + [ "Ten green bottles hanging on the wall,"; + "Ten green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be nine green bottles hanging on the wall."; + ""; + "Nine green bottles hanging on the wall,"; + "Nine green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be eight green bottles hanging on the wall."; + ""; + "Eight green bottles hanging on the wall,"; + "Eight green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be seven green bottles hanging on the wall."; + ""; + "Seven green bottles hanging on the wall,"; + "Seven green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be six green bottles hanging on the wall."; + ""; + "Six green bottles hanging on the wall,"; + "Six green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be five green bottles hanging on the wall."; + ""; + "Five green bottles hanging on the wall,"; + "Five green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be four green bottles hanging on the wall."; + ""; + "Four green bottles hanging on the wall,"; + "Four green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be three green bottles hanging on the wall."; + ""; + "Three green bottles hanging on the wall,"; + "Three green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be two green bottles hanging on the wall."; + ""; + "Two green bottles hanging on the wall,"; + "Two green bottles hanging on the wall,"; + "And if one green bottle should accidentally fall,"; + "There'll be one green bottle hanging on the wall."; + ""; + "One green bottle hanging on the wall,"; + "One green bottle hanging on the wall,"; + "And if one green bottle should accidentally fall,"; "There'll be no green bottles hanging on the wall." ] + recite 10 10 |> should equal expected + diff --git a/exercises/practice/killer-sudoku-helper/.meta/Example.fs b/exercises/practice/killer-sudoku-helper/.meta/Example.fs index b4caf1f3..31b9f5d7 100644 --- a/exercises/practice/killer-sudoku-helper/.meta/Example.fs +++ b/exercises/practice/killer-sudoku-helper/.meta/Example.fs @@ -9,7 +9,7 @@ module List = List.map ((@) [ x ]) (combinations (k - 1) xs) @ combinations k xs -let combinations sum size exclude = +let combinations exclude size sum = [ 1..9 ] |> List.except exclude |> List.combinations size diff --git a/exercises/practice/killer-sudoku-helper/KillerSudokuHelper.fs b/exercises/practice/killer-sudoku-helper/KillerSudokuHelper.fs index da79e195..7cee9a09 100644 --- a/exercises/practice/killer-sudoku-helper/KillerSudokuHelper.fs +++ b/exercises/practice/killer-sudoku-helper/KillerSudokuHelper.fs @@ -1,4 +1,4 @@ module KillerSudokuHelper -let combinations sum size exclude = +let combinations exclude size sum = failwith "Please implement the 'combinations' function" diff --git a/exercises/practice/killer-sudoku-helper/KillerSudokuHelperTests.fs b/exercises/practice/killer-sudoku-helper/KillerSudokuHelperTests.fs index 33727c11..a59009d0 100644 --- a/exercises/practice/killer-sudoku-helper/KillerSudokuHelperTests.fs +++ b/exercises/practice/killer-sudoku-helper/KillerSudokuHelperTests.fs @@ -7,61 +7,53 @@ open KillerSudokuHelper [] let ``1`` () = - combinations 1 1 [] |> should equal [ [ 1 ] ] + combinations [] 1 1 |> should equal [[1]] [] let ``2`` () = - combinations 2 1 [] |> should equal [ [ 2 ] ] + combinations [] 1 2 |> should equal [[2]] [] let ``3`` () = - combinations 3 1 [] |> should equal [ [ 3 ] ] + combinations [] 1 3 |> should equal [[3]] [] let ``4`` () = - combinations 4 1 [] |> should equal [ [ 4 ] ] + combinations [] 1 4 |> should equal [[4]] [] let ``5`` () = - combinations 5 1 [] |> should equal [ [ 5 ] ] + combinations [] 1 5 |> should equal [[5]] [] let ``6`` () = - combinations 6 1 [] |> should equal [ [ 6 ] ] + combinations [] 1 6 |> should equal [[6]] [] let ``7`` () = - combinations 7 1 [] |> should equal [ [ 7 ] ] + combinations [] 1 7 |> should equal [[7]] [] let ``8`` () = - combinations 8 1 [] |> should equal [ [ 8 ] ] + combinations [] 1 8 |> should equal [[8]] [] let ``9`` () = - combinations 9 1 [] |> should equal [ [ 9 ] ] + combinations [] 1 9 |> should equal [[9]] [] let ``Cage with sum 45 contains all digits 1:9`` () = - combinations 45 9 [] - |> should equal [ [ 1; 2; 3; 4; 5; 6; 7; 8; 9 ] ] + combinations [] 9 45 |> should equal [[1; 2; 3; 4; 5; 6; 7; 8; 9]] [] let ``Cage with only 1 possible combination`` () = - combinations 7 3 [] - |> should equal [ [ 1; 2; 4 ] ] + combinations [] 3 7 |> should equal [[1; 2; 4]] [] let ``Cage with several combinations`` () = - combinations 10 2 [] - |> should - equal - [ [ 1; 9 ] - [ 2; 8 ] - [ 3; 7 ] - [ 4; 6 ] ] + combinations [] 2 10 |> should equal [[1; 9]; [2; 8]; [3; 7]; [4; 6]] [] let ``Cage with several combinations that is restricted`` () = - combinations 10 2 [ 1; 4 ] - |> should equal [ [ 2; 8 ]; [ 3; 7 ] ] + combinations [1; 4] 2 10 |> should equal [[2; 8]; [3; 7]] + diff --git a/exercises/practice/knapsack/KnapsackTests.fs b/exercises/practice/knapsack/KnapsackTests.fs index 1da45f62..1b0c6e6c 100644 --- a/exercises/practice/knapsack/KnapsackTests.fs +++ b/exercises/practice/knapsack/KnapsackTests.fs @@ -6,76 +6,74 @@ open Xunit open Knapsack [] -let ``No items`` () = maximumValue [] 100 |> should equal 0 +let ``No items`` () = + let items = [] + maximumValue items 100 |> should equal 0 [] let ``One item, too heavy`` () = - maximumValue [ { weight = 100; value = 1 } ] 10 - |> should equal 0 + let items = [{ weight = 100; value = 1 }] + maximumValue items 10 |> should equal 0 [] let ``Five items (cannot be greedy by weight)`` () = - maximumValue - [ { weight = 2; value = 5 } - { weight = 2; value = 5 } - { weight = 2; value = 5 } - { weight = 2; value = 5 } + let items = + [ { weight = 2; value = 5 }; + { weight = 2; value = 5 }; + { weight = 2; value = 5 }; + { weight = 2; value = 5 }; { weight = 10; value = 21 } ] - 10 - |> should equal 21 + maximumValue items 10 |> should equal 21 [] let ``Five items (cannot be greedy by value)`` () = - maximumValue - [ { weight = 2; value = 20 } - { weight = 2; value = 20 } - { weight = 2; value = 20 } - { weight = 2; value = 20 } + let items = + [ { weight = 2; value = 20 }; + { weight = 2; value = 20 }; + { weight = 2; value = 20 }; + { weight = 2; value = 20 }; { weight = 10; value = 50 } ] - 10 - |> should equal 80 + maximumValue items 10 |> should equal 80 [] let ``Example knapsack`` () = - maximumValue - [ { weight = 5; value = 10 } - { weight = 4; value = 40 } - { weight = 6; value = 30 } + let items = + [ { weight = 5; value = 10 }; + { weight = 4; value = 40 }; + { weight = 6; value = 30 }; { weight = 4; value = 50 } ] - 10 - |> should equal 90 + maximumValue items 10 |> should equal 90 [] let ``8 items`` () = - maximumValue - [ { weight = 25; value = 350 } - { weight = 35; value = 400 } - { weight = 45; value = 450 } - { weight = 5; value = 20 } - { weight = 25; value = 70 } - { weight = 3; value = 8 } - { weight = 2; value = 5 } + let items = + [ { weight = 25; value = 350 }; + { weight = 35; value = 400 }; + { weight = 45; value = 450 }; + { weight = 5; value = 20 }; + { weight = 25; value = 70 }; + { weight = 3; value = 8 }; + { weight = 2; value = 5 }; { weight = 2; value = 5 } ] - 104 - |> should equal 900 + maximumValue items 104 |> should equal 900 [] let ``15 items`` () = - maximumValue - [ { weight = 70; value = 135 } - { weight = 73; value = 139 } - { weight = 77; value = 149 } - { weight = 80; value = 150 } - { weight = 82; value = 156 } - { weight = 87; value = 163 } - { weight = 90; value = 173 } - { weight = 94; value = 184 } - { weight = 98; value = 192 } - { weight = 106; value = 201 } - { weight = 110; value = 210 } - { weight = 113; value = 214 } - { weight = 115; value = 221 } - { weight = 118; value = 229 } + let items = + [ { weight = 70; value = 135 }; + { weight = 73; value = 139 }; + { weight = 77; value = 149 }; + { weight = 80; value = 150 }; + { weight = 82; value = 156 }; + { weight = 87; value = 163 }; + { weight = 90; value = 173 }; + { weight = 94; value = 184 }; + { weight = 98; value = 192 }; + { weight = 106; value = 201 }; + { weight = 110; value = 210 }; + { weight = 113; value = 214 }; + { weight = 115; value = 221 }; + { weight = 118; value = 229 }; { weight = 120; value = 240 } ] - 750 - |> should equal 1458 + maximumValue items 750 |> should equal 1458 + diff --git a/exercises/practice/resistor-color-trio/ResistorColorTrioTests.fs b/exercises/practice/resistor-color-trio/ResistorColorTrioTests.fs index 7df6b35a..8336acc1 100644 --- a/exercises/practice/resistor-color-trio/ResistorColorTrioTests.fs +++ b/exercises/practice/resistor-color-trio/ResistorColorTrioTests.fs @@ -7,53 +7,41 @@ open ResistorColorTrio [] let ``Orange and orange and black`` () = - label [ "orange"; "orange"; "black" ] - |> should equal "33 ohms" + label ["orange"; "orange"; "black"] |> should equal "33 ohms" -[] +[] let ``Blue and grey and brown`` () = - label [ "blue"; "grey"; "brown" ] - |> should equal "680 ohms" + label ["blue"; "grey"; "brown"] |> should equal "680 ohms" -[] +[] let ``Red and black and red`` () = - label [ "red"; "black"; "red" ] - |> should equal "2 kiloohms" + label ["red"; "black"; "red"] |> should equal "2 kiloohms" -[] +[] let ``Green and brown and orange`` () = - label [ "green"; "brown"; "orange" ] - |> should equal "51 kiloohms" + label ["green"; "brown"; "orange"] |> should equal "51 kiloohms" -[] +[] let ``Yellow and violet and yellow`` () = - label [ "yellow"; "violet"; "yellow" ] - |> should equal "470 kiloohms" + label ["yellow"; "violet"; "yellow"] |> should equal "470 kiloohms" -[] +[] let ``Blue and violet and blue`` () = - label [ "blue"; "violet"; "blue" ] - |> should equal "67 megaohms" + label ["blue"; "violet"; "blue"] |> should equal "67 megaohms" -[] +[] let ``Minimum possible value`` () = - label [ "black"; "black"; "black" ] - |> should equal "0 ohms" + label ["black"; "black"; "black"] |> should equal "0 ohms" -[] +[] let ``Maximum possible value`` () = - label [ "white"; "white"; "white" ] - |> should equal "99 gigaohms" + label ["white"; "white"; "white"] |> should equal "99 gigaohms" -[] +[] let ``First two colors make an invalid octal number`` () = - label [ "black"; "grey"; "black" ] - |> should equal "8 ohms" + label ["black"; "grey"; "black"] |> should equal "8 ohms" -[] +[] let ``Ignore extra colors`` () = - label [ "blue" - "green" - "yellow" - "orange" ] - |> should equal "650 kiloohms" + label ["blue"; "green"; "yellow"; "orange"] |> should equal "650 kiloohms" + diff --git a/exercises/practice/state-of-tic-tac-toe/.meta/Example.fs b/exercises/practice/state-of-tic-tac-toe/.meta/Example.fs index 34c9bc78..bfdac564 100644 --- a/exercises/practice/state-of-tic-tac-toe/.meta/Example.fs +++ b/exercises/practice/state-of-tic-tac-toe/.meta/Example.fs @@ -16,12 +16,14 @@ type Board = Cell [,] let won (player: Cell) (board: Board) = let winning = [| player; player; player |] - Array.init 3 (fun i -> board[i, i]) = winning || - Array.init 3 (fun i -> board[i, 2 - i]) = winning - || Array.init 3 (fun i -> board[i, *]) |> Array.contains winning - || Array.init 3 (fun i -> board[*, i]) |> Array.contains winning - -let gameState (board: Board) = + Array.init 3 (fun i -> board[i, i]) = winning + || Array.init 3 (fun i -> board[i, 2 - i]) = winning + || Array.init 3 (fun i -> board[i, *]) + |> Array.contains winning + || Array.init 3 (fun i -> board[*, i]) + |> Array.contains winning + +let gamestate (board: Board) = let numCells cell = board |> Seq.cast diff --git a/exercises/practice/state-of-tic-tac-toe/StateOfTicTacToeTests.fs b/exercises/practice/state-of-tic-tac-toe/StateOfTicTacToeTests.fs index 5e0e9a4d..9f4d0e0b 100644 --- a/exercises/practice/state-of-tic-tac-toe/StateOfTicTacToeTests.fs +++ b/exercises/practice/state-of-tic-tac-toe/StateOfTicTacToeTests.fs @@ -7,328 +7,244 @@ open StateOfTicTacToe [] let ``Finished game where X won via left column victory`` () = - let board = - array2D [ - [ 'X'; 'O'; 'O' ] - [ 'X'; ' '; ' ' ] - [ 'X'; ' '; ' ' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'O'; 'O']; + ['X'; ' '; ' ']; + ['X'; ' '; ' '] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where X won via middle column victory`` () = - let board = - array2D [ - [ 'O'; 'X'; 'O' ] - [ ' '; 'X'; ' ' ] - [ ' '; 'X'; ' ' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['O'; 'X'; 'O']; + [' '; 'X'; ' ']; + [' '; 'X'; ' '] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where X won via right column victory`` () = - let board = - array2D [ - [ 'O'; 'O'; 'X' ] - [ ' '; ' '; 'X' ] - [ ' '; ' '; 'X' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['O'; 'O'; 'X']; + [' '; ' '; 'X']; + [' '; ' '; 'X'] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where O won via left column victory`` () = - let board = - array2D [ - [ 'O'; 'X'; 'X' ] - [ 'O'; 'X'; ' ' ] - [ 'O'; ' '; ' ' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['O'; 'X'; 'X']; + ['O'; 'X'; ' ']; + ['O'; ' '; ' '] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where O won via middle column victory`` () = - let board = - array2D [ - [ 'X'; 'O'; 'X' ] - [ ' '; 'O'; 'X' ] - [ ' '; 'O'; ' ' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'O'; 'X']; + [' '; 'O'; 'X']; + [' '; 'O'; ' '] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where O won via right column victory`` () = - let board = - array2D [ - [ 'X'; 'X'; 'O' ] - [ ' '; 'X'; 'O' ] - [ ' '; ' '; 'O' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'X'; 'O']; + [' '; 'X'; 'O']; + [' '; ' '; 'O'] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where X won via top row victory`` () = - let board = - array2D [ - [ 'X'; 'X'; 'X' ] - [ 'X'; 'O'; 'O' ] - [ 'O'; ' '; ' ' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'X'; 'X']; + ['X'; 'O'; 'O']; + ['O'; ' '; ' '] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where X won via middle row victory`` () = - let board = - array2D [ - [ 'O'; ' '; ' ' ] - [ 'X'; 'X'; 'X' ] - [ ' '; 'O'; ' ' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['O'; ' '; ' ']; + ['X'; 'X'; 'X']; + [' '; 'O'; ' '] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where X won via bottom row victory`` () = - let board = - array2D [ - [ ' '; 'O'; 'O' ] - [ 'O'; ' '; 'X' ] - [ 'X'; 'X'; 'X' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ [' '; 'O'; 'O']; + ['O'; ' '; 'X']; + ['X'; 'X'; 'X'] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where O won via top row victory`` () = - let board = - array2D [ - [ 'O'; 'O'; 'O' ] - [ 'X'; 'X'; 'O' ] - [ 'X'; 'X'; ' ' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['O'; 'O'; 'O']; + ['X'; 'X'; 'O']; + ['X'; 'X'; ' '] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where O won via middle row victory`` () = - let board = - array2D [ - [ 'X'; 'X'; ' ' ] - [ 'O'; 'O'; 'O' ] - [ 'X'; ' '; ' ' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'X'; ' ']; + ['O'; 'O'; 'O']; + ['X'; ' '; ' '] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where O won via bottom row victory`` () = - let board = - array2D [ - [ 'X'; 'O'; 'X' ] - [ ' '; 'X'; 'X' ] - [ 'O'; 'O'; 'O' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'O'; 'X']; + [' '; 'X'; 'X']; + ['O'; 'O'; 'O'] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where X won via falling diagonal victory`` () = - let board = - array2D [ - [ 'X'; 'O'; 'O' ] - [ ' '; 'X'; ' ' ] - [ ' '; ' '; 'X' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'O'; 'O']; + [' '; 'X'; ' ']; + [' '; ' '; 'X'] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where X won via rising diagonal victory`` () = - let board = - array2D [ - [ 'O'; ' '; 'X' ] - [ 'O'; 'X'; ' ' ] - [ 'X'; ' '; ' ' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['O'; ' '; 'X']; + ['O'; 'X'; ' ']; + ['X'; ' '; ' '] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where O won via falling diagonal victory`` () = - let board = - array2D [ - [ 'O'; 'X'; 'X' ] - [ 'O'; 'O'; 'X' ] - [ 'X'; ' '; 'O' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['O'; 'X'; 'X']; + ['O'; 'O'; 'X']; + ['X'; ' '; 'O'] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where O won via rising diagonal victory`` () = - let board = - array2D [ - [ ' '; ' '; 'O' ] - [ ' '; 'O'; 'X' ] - [ 'O'; 'X'; 'X' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ [' '; ' '; 'O']; + [' '; 'O'; 'X']; + ['O'; 'X'; 'X'] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where X won via a row and a column victory`` () = - let board = - array2D [ - [ 'X'; 'X'; 'X' ] - [ 'X'; 'O'; 'O' ] - [ 'X'; 'O'; 'O' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'X'; 'X']; + ['X'; 'O'; 'O']; + ['X'; 'O'; 'O'] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] let ``Finished game where X won via two diagonal victories`` () = - let board = - array2D [ - [ 'X'; 'O'; 'X' ] - [ 'O'; 'X'; 'O' ] - [ 'X'; 'O'; 'X' ] - ] - - let expected: Result = Ok Win - gameState board |> should equal expected - -[] -let ``A draw`` () = - let board = - array2D [ - [ 'X'; 'O'; 'X' ] - [ 'X'; 'X'; 'O' ] - [ 'O'; 'X'; 'O' ] - ] - - let expected: Result = Ok Draw - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'O'; 'X']; + ['O'; 'X'; 'O']; + ['X'; 'O'; 'X'] ] + let expected: Result = Ok EndGameState.Win + gamestate board |> should equal expected + +[] +let ``Draw`` () = + let board = + array2D [ ['X'; 'O'; 'X']; + ['X'; 'X'; 'O']; + ['O'; 'X'; 'O'] ] + let expected: Result = Ok EndGameState.Draw + gamestate board |> should equal expected + +[] let ``Another draw`` () = - let board = - array2D [ - [ 'X'; 'X'; 'O' ] - [ 'O'; 'X'; 'X' ] - [ 'X'; 'O'; 'O' ] - ] - - let expected: Result = Ok Draw - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; 'X'; 'O']; + ['O'; 'X'; 'X']; + ['X'; 'O'; 'O'] ] + let expected: Result = Ok EndGameState.Draw + gamestate board |> should equal expected + +[] let ``Ongoing game: one move in`` () = - let board = - array2D [ - [ ' '; ' '; ' ' ] - [ 'X'; ' '; ' ' ] - [ ' '; ' '; ' ' ] - ] - - let expected: Result = Ok Ongoing - gameState board |> should equal expected - -[] + let board = + array2D [ [' '; ' '; ' ']; + ['X'; ' '; ' ']; + [' '; ' '; ' '] ] + let expected: Result = Ok EndGameState.Ongoing + gamestate board |> should equal expected + +[] let ``Ongoing game: two moves in`` () = - let board = - array2D [ - [ 'O'; ' '; ' ' ] - [ ' '; 'X'; ' ' ] - [ ' '; ' '; ' ' ] - ] - - let expected: Result = Ok Ongoing - gameState board |> should equal expected - -[] + let board = + array2D [ ['O'; ' '; ' ']; + [' '; 'X'; ' ']; + [' '; ' '; ' '] ] + let expected: Result = Ok EndGameState.Ongoing + gamestate board |> should equal expected + +[] let ``Ongoing game: five moves in`` () = - let board = - array2D [ - [ 'X'; ' '; ' ' ] - [ ' '; 'X'; 'O' ] - [ 'O'; 'X'; ' ' ] - ] - - let expected: Result = Ok Ongoing - gameState board |> should equal expected - -[] + let board = + array2D [ ['X'; ' '; ' ']; + [' '; 'X'; 'O']; + ['O'; 'X'; ' '] ] + let expected: Result = Ok EndGameState.Ongoing + gamestate board |> should equal expected + +[] let ``Invalid board: X went twice`` () = - let board = - array2D [ - [ 'X'; 'X'; ' ' ] - [ ' '; ' '; ' ' ] - [ ' '; ' '; ' ' ] - ] - + let board = + array2D [ ['X'; 'X'; ' ']; + [' '; ' '; ' ']; + [' '; ' '; ' '] ] let expected: Result = Error ConsecutiveMovesBySamePlayer - gameState board - |> should equal expected + gamestate board |> should equal expected -[] +[] let ``Invalid board: O started`` () = - let board = - array2D [ - [ 'O'; 'O'; 'X' ] - [ ' '; ' '; ' ' ] - [ ' '; ' '; ' ' ] - ] - + let board = + array2D [ ['O'; 'O'; 'X']; + [' '; ' '; ' ']; + [' '; ' '; ' '] ] let expected: Result = Error WrongPlayerStarted - gameState board - |> should equal expected + gamestate board |> should equal expected -[] +[] let ``Invalid board: X won and O kept playing`` () = - let board = - array2D [ - [ 'X'; 'X'; 'X' ] - [ 'O'; 'O'; 'O' ] - [ ' '; ' '; ' ' ] - ] - + let board = + array2D [ ['X'; 'X'; 'X']; + ['O'; 'O'; 'O']; + [' '; ' '; ' '] ] let expected: Result = Error MoveMadeAfterGameWasDone - gameState board - |> should equal expected + gamestate board |> should equal expected -[] +[] let ``Invalid board: players kept playing after a win`` () = - let board = - array2D [ - [ 'X'; 'X'; 'X' ] - [ 'O'; 'O'; 'O' ] - [ 'X'; 'O'; 'X' ] - ] - + let board = + array2D [ ['X'; 'X'; 'X']; + ['O'; 'O'; 'O']; + ['X'; 'O'; 'X'] ] let expected: Result = Error MoveMadeAfterGameWasDone - gameState board - |> should equal expected + gamestate board |> should equal expected + diff --git a/generators/Generators.fs b/generators/Generators.fs index 91ba8d83..6f67ea13 100644 --- a/generators/Generators.fs +++ b/generators/Generators.fs @@ -2044,19 +2044,63 @@ type EliudsEggs() = type Knapsack() = inherit ExerciseGenerator() + + let renderItem (item: JToken) = + let weight = item["weight"].ToObject() + let value = item["value"].ToObject() + $"{{ weight = {weight}; value = {value} }}" + + override _.PropertiesWithIdentifier _ = [ "items" ] + + override _.RenderInput(testCase, key, value) = + match key with + | "items" -> List.mapRenderMultiLine renderItem value + | _ -> base.RenderInput(testCase, key, value) type BottleSong() = inherit ExerciseGenerator() + + override _.PropertiesWithIdentifier _ = [ "expected" ] + + override _.RenderExpected(_, _, value) = List.renderMultiLine value type ResistorColorTrio() = inherit ExerciseGenerator() + + override _.RenderExpected(_, _, value) = + let ohms = value["value"].ToString() + let unit = value["unit"].ToString() + $"\"{ohms} {unit}\"" type KillerSudokuHelper() = inherit ExerciseGenerator() + + override _.MapTestCase testCase = { testCase with Input = testCase.Input.["cage"].ToObject>() } type StateOfTicTacToe() = inherit ExerciseGenerator() + override _.PropertiesWithIdentifier _ = [ "board"; "expected" ] + + override _.IdentifierTypeAnnotation(_, key, _) = + if key = "expected" then Some "Result" else None + + override _.RenderInput(_, _, value) = + let rows = value |> Seq.map (fun row -> row.ToString().ToCharArray() |> List.ofArray |> List.map (fun c -> $"'{c}'") |> Obj.render) + Collection.renderMultiLine "array2D [" "]" rows + + override _.RenderExpected(_, _, value) = + match value.SelectToken "error" with + | null -> $"Ok EndGameState.{String.upperCaseFirst (value.ToString())}" + | error -> + let errorString = + match string error with + | "Impossible board: game should have ended after the game was won" -> "MoveMadeAfterGameWasDone" + | "Wrong turn order: O started" -> "WrongPlayerStarted" + | "Wrong turn order: X went twice" -> "ConsecutiveMovesBySamePlayer" + | _ -> failwith "Unknown error" + $"Error %s{errorString}" + type Satellite() = inherit ExerciseGenerator()