コード例 #1
0
ファイル: Bowling.cs プロジェクト: johnspray74/GameScoring
        public Bowling()
        {
            // standard rules game
            // This section of code is generated manually from the diagram
            // Go change the diagram first where you can reason about the logic, then come here and make it match the diagram
            // Note following code uses the fluent pattern - every method returns the this reference of the object it is called on.
            scorerEngine = new Frame("game")
                           .setIsFrameCompleteLambda((gameNumber, frames, score) => frames == 10)
                           .WireTo(new Bonuses("bonus")
                                   .setIsBonusesCompleteLambda((plays, score) => score < 10 || plays == 3)
                                   .WireTo(new Frame("frame")
                                           .setIsFrameCompleteLambda((frameNumber, balls, pins) => frameNumber < 9 && (balls == 2 || pins[0] == 10) || (balls == 2 && pins[0] < 10 || balls == 3))
                                           .WireTo(new SinglePlay("SinglePlay")
                                                   )));



            consolerunner = new ConsoleGameRunner("Enter number pins:", (pins, scorer) => scorer.Ball(0, pins))
                            .WireTo(scorerEngine)
                            .WireTo(new Scorecard(
                                        "-------------------------------------------------------------------------------------\n" +
                                        "|F00|F01|F10|F11|F20|F21|F30|F31|F40|F41|F50|F51|F60|F61|F70|F71|F80|F81|F90|F91|F92|\n" +
                                        "|    ---+    ---+    ---+    ---+    ---+    ---+    ---+    ---+    ---+    ---+----\n" +
                                        "|  T0-  |  T1-  |  T2-  |  T3-  |  T4-  |  T5-  |  T6-  |  T7-  |  T8-  |    T9-    |\n" +
                                        "-------------------------------------------------------------------------------------\n")
                                    .WireTo(new ScoreBinding <List <List <string> > >("F",
                                                                                      () => TranslateFrameScores(
                                                                                          scorerEngine.GetSubFrames().Select(f => f.GetSubFrames().Select(b => b.GetScore()[0]).ToList()).ToList())))
                                    .WireTo(new ScoreBinding <List <int> >("T",
                                                                           () => scorerEngine.GetSubFrames().Select(sf => sf.GetScore()[0]).Accumulate().ToList()))
                                    );
        }
コード例 #2
0
ファイル: Tennis.cs プロジェクト: johnspray74/GameScoring
        public Tennis()
        {
            // This code is hand written from the diagram. Go change the diagram first where you can reason about the logic, then come here and make it match the diagram
            // Note following code uses the fluent pattern - every method returns the this reference of the object it is called on.
            match = new Frame("match")                                                         // (note the string is just used to identify instances during debugging, but also helps reading this code to know what they are for)
                    .setIsFrameCompleteLambda((matchNumber, nSets, score) => score.Max() == 3) // best of 5 sets is first to win 3 sets
                    .WireTo(new WinnerTakesPoint("winnerOfSet")
                            .WireTo(new Switch("switch")
                                    .setSwitchLambda((setNumber, nGames, score) => (setNumber < 4 && score[0] == 6 && score[1] == 6))
                                    .WireTo(new Frame("set")
                                            .setIsFrameCompleteLambda((setNumber, nGames, score) => score.Max() >= 6 && Math.Abs(score[0] - score[1]) >= 2)
                                            .WireTo(new WinnerTakesPoint("winnerOfGame")
                                                    .WireTo(new Frame("game")
                                                            .setIsFrameCompleteLambda((gameNumber, nBalls, score) => score.Max() >= 4 && Math.Abs(score[0] - score[1]) >= 2)
                                                            .WireTo(new SinglePlay("singlePlayGame"))
                                                            )
                                                    )
                                            )
                                    .WireTo(new WinnerTakesPoint("winnerOfTieBreak")
                                            .WireTo(new Frame("tiebreak")
                                                    .setIsFrameCompleteLambda((setNumber, nBalls, score) => score.Max() == 7)
                                                    .WireTo(new SinglePlay("singlePlayTiebreak"))
                                                    )
                                            )
                                    )
                            );



            consolerunner = new ConsoleGameRunner("Enter winner 0 or 1", (winner, scorer) => scorer.Ball(winner, 1))
                            .WireTo(match)
                            .WireTo(new Scorecard(
                                        "--------------------------------------------\n" +
                                        "| M0  |S00|S10|S20|S30|S40|S50|S60|  G0--- |\n" +
                                        "| M1  |S01|S11|S21|S31|S41|S51|S61|  G1--- |\n" +
                                        "--------------------------------------------\n")
                                    .WireTo(new ScoreBinding <int[]>("M", () => match.GetScore()))
                                    .WireTo(new ScoreBinding <List <int[]> >("S", () => GetSetScores(match)))
                                    .WireTo(new ScoreBinding <string[]>("G", () => GetLastGameScore(match)))
                                    );
        }