Ejemplo n.º 1
0
 private static Func <UserInterfaceState> invalidSelection(TextWriter writer, UserInterfaceState state)
 {
     return(() =>
     {
         writer.WriteLine("Invalid selection. Please try again.");
         return state;
     });
 }
Ejemplo n.º 2
0
 public static Option <UserInterfaceState> getTakeBack(this UserInterfaceState state)
 {
     return(state.Fold(game => Option.None <UserInterfaceState>(),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader)),
                       game => Option.Some(new UserInterfaceState(game.takeBack(), state.writer, state.reader))
                       ));
 }
Ejemplo n.º 3
0
 public static MoverOrWinner getMoverOrWinner(this UserInterfaceState state)
 {
     return(state.Fold(
                game0 => new MoverOrWinner(position => Option.Some(new UserInterfaceState(game0.move(position), state.writer, state.reader))),
                game1 => new MoverOrWinner(position => from _2 in game1.move(position) select new UserInterfaceState(_2, state.writer, state.reader)),
                game2 => new MoverOrWinner(position => from _3 in game2.move(position) select new UserInterfaceState(_3, state.writer, state.reader)),
                game3 => new MoverOrWinner(position => from _4 in game3.move(position) select new UserInterfaceState(_4, state.writer, state.reader)),
                game4 => new MoverOrWinner(position => from _5 in game4.move(position) select _5.Fold(game5 => new UserInterfaceState(game5, state.writer, state.reader), win5 => new UserInterfaceState(win5, state.writer, state.reader))),
                game5 => new MoverOrWinner(position => game5.move(position).Select(result => result.Fold(game6 => new UserInterfaceState(game6, state.writer, state.reader), win6 => new UserInterfaceState(win6, state.writer, state.reader)))),
                game6 => new MoverOrWinner(position => game6.move(position).Select(result => result.Fold(game7 => new UserInterfaceState(game7, state.writer, state.reader), win7 => new UserInterfaceState(win7, state.writer, state.reader)))),
                game7 => new MoverOrWinner(position => game7.move(position).Select(result => result.Fold(game8 => new UserInterfaceState(game8, state.writer, state.reader), win8 => new UserInterfaceState(win8, state.writer, state.reader)))),
                game8 => new MoverOrWinner(position => game8.move(position).Select(result => result.Fold(game9 => new UserInterfaceState(game9, state.writer, state.reader), win9 => new UserInterfaceState(win9, state.writer, state.reader)))),
                game9 => new MoverOrWinner(Option.None <Player>()),
                win5 => new MoverOrWinner(Option.Some(Player.Player0)),
                win6 => new MoverOrWinner(Option.Some(Player.Player1)),
                win7 => new MoverOrWinner(Option.Some(Player.Player0)),
                win8 => new MoverOrWinner(Option.Some(Player.Player1)),
                win9 => new MoverOrWinner(Option.Some(Player.Player0))
                ));
 }
Ejemplo n.º 4
0
        private static Option <KeyValuePair <UserInterfaceState, UserInterfaceState> > gameLoop(UserInterfaceState state)
        {
            state.writer.WriteLine("\n\n{0}\n", state.data.ToString(state.toChar));

            var moverOrWinner = state.getMoverOrWinner();

            moverOrWinner.Fold(printMoves(state.writer, state.data.whoseTurn()), printWinner(state.writer));

            state.writer.WriteLine("  q to Quit");
            state.writer.WriteLine("  v to view board positions");

            var takeBack = state.getTakeBack();

            if (takeBack.Any())
            {
                state.writer.WriteLine("  t to take back last move");
            }

            state.writer.Write("  > ");

            var line = state.reader.ReadLine();

            if (line == string.Empty)
            {
                state.writer.WriteLine("Please make a selection.");
                return(Option.Some(new KeyValuePair <UserInterfaceState, UserInterfaceState>(default(UserInterfaceState), state)));
            }

            var c = char.ToUpper(line[0]);

            switch (c)
            {
            case 'Q':
                state.writer.WriteLine("Bye!");
                return(Option.None <KeyValuePair <UserInterfaceState, UserInterfaceState> >());

            case 'V':
                return(Option.Some(new KeyValuePair <UserInterfaceState, UserInterfaceState>(default(UserInterfaceState), new UserInterfaceState(state, PositionExtensions.ToChar))));

            case 'T':
                return(Option.Some(new KeyValuePair <UserInterfaceState, UserInterfaceState>(default(UserInterfaceState), takeBack.Fold(invalidSelection(state.writer, state), s => s))));

            default:
                return(Option.Some(new KeyValuePair <UserInterfaceState, UserInterfaceState>(default(UserInterfaceState), (from mover in moverOrWinner.Fold(mover => Option.Some(mover), _ => Option.None <Func <Position, Option <UserInterfaceState> > >())
                                                                                                                           from position in c.ToPosition()
                                                                                                                           select mover(position)).FirstOrDefault().Fold(invalidSelection(state.writer, state), s => s))));
            }
        }
Ejemplo n.º 5
0
 public UserInterfaceState(UserInterfaceState state, Func <Position, char> toChar) : this(state.value, state.data, state.writer, state.reader, toChar)
 {
 }