public static void Interactive() { Reset(); string cmd = string.Empty, cmdData = null, cmdData2 = null; while (!UserCommand.Quit.Equals(cmd)) { if (Mode == AIMode.Manual || State == GameState.NoGame || PlayerWithTheDice.Value.Type == PlayerType.Human) { Write("<farkler{0}> $ ", PlayerWithTheDice == null ? "" : " : " + PlayerWithTheDice.Value.Name); var cmdtext = Console.ReadLine().Replace(" ", " ").Trim().Split(' '); cmd = cmdtext[0]; cmdData = cmdtext.Length > 1 ? cmdtext[1] : null; cmdData2 = cmdtext.Length > 2 ? cmdtext[2] : null; } else { RandomRoll(); WriteLine(Roll); cmd = string.Empty; //Thread.Sleep(2000); } switch (cmd) { case UserCommand.Reset: Reset(); break; case UserCommand.AddPlayer: if (State == GameState.InGame) { WriteLine("ERR - cannot add a player while a game is in progress."); } else if (cmdData == null) { WriteLine("ERR - (p) Usage: p-PlayerName"); } else if (Players.Any(x => x.Name.Equals(cmdData))) { WriteLine("ERR - player with that name already exists."); } else { Players.AddFirst(new FarklePlayer(cmdData)); WriteLine("Added new player {0}, now there are {1} players.", cmdData, Players.Count); } break; case UserCommand.AddPlayerAI: if (State == GameState.InGame) { WriteLine("ERR - cannot add a player while a game is in progress."); } else if (cmdData == null) { WriteLine("ERR - (p) Usage: p-PlayerName"); } else if (Players.Any(x => x.Name.Equals(cmdData))) { WriteLine("ERR - player with that name already exists."); } else { Players.AddFirst(new FarklePlayer(cmdData, PlayerType.AI)); WriteLine("Added new AI player {0}, now there are {1} players.", cmdData, Players.Count); } break; case UserCommand.ModeAutomatic: Mode = AIMode.Automatic; WriteLine("Mode set to Automatic"); break; case UserCommand.ModeManual: Mode = AIMode.Manual; WriteLine("Mode set to Manual"); break; case UserCommand.RandomRoll: if (Roll != null) { WriteLine("ERR - cannot Roll until current roll is acted upon."); } else { RandomRoll(); WriteLine(Roll); } break; case UserCommand.Roll: if (Roll != null) { WriteLine("ERR - cannot Roll until current roll is acted upon."); } else if (cmdData == null || !Regex.IsMatch(cmdData, @"^\d{" + DiceToRoll + "}$")) { WriteLine("ERR - (r) Usage: r-122245"); } else { Roll = new Roll(cmdData); ActionsPossible = Farkle.GenerateActions(Roll); WriteLine(Roll); } break; case UserCommand.Action: if (PlayerWithTheDice.Value.Type != PlayerType.Human) { WriteLine("ERR - cannot perform Action, player is not human."); } else if (Roll == null) { WriteLine("ERR - cannot perform Action, there is no roll on the table."); } else if (cmdData == null) { WriteLine("ERR - (a) Usage: a-400-3 (a-points-dicetoroll)"); } else { int points; if (!int.TryParse(cmdData, out points)) { WriteLine("ERR - (a) Usage: a-400-3 (a-points-dicetoroll)"); break; } if (!ActionsPossible.Any(x => x.ScoreToAdd == points)) { WriteLine("ERR - no matching possible action."); break; } if (cmdData2 == null) { if (TurnScore + points < 300) { WriteLine("ERR - cannot bank less than 300 points."); break; } TurnScore += points; PlayerWithTheDice.Value.BankedScore += TurnScore; WriteLine("{0} BANKS {1}", CurrentPlayerInfo(), TurnScore); EndTurn(); } else { int dice; if (!int.TryParse(cmdData2, out dice)) { WriteLine("ERR - (a) Usage: a-400-3 (a-points-dicetoroll)"); break; } if (!ActionsPossible.Any(x => x.ScoreToAdd == points && x.DiceToRoll == dice)) { WriteLine("ERR - no matching possible action."); break; } TurnScore += points; DiceToRoll = dice; WriteLine("{0} STASHES {1} and WILL ROLL {2}", CurrentPlayerInfo(), points, dice); Roll = null; } } break; case UserCommand.NewGame: NewGame(); break; case UserCommand.Quit: State = GameState.NoGame; break; default: break; } if (GameState.NoGame.Equals(State)) { continue; } if (Roll != null) { bool turnIsOver = false; switch (PlayerWithTheDice.Value.Type) { case PlayerType.AI: turnIsOver = ChooseAndPerformAction(); Roll = null; ActionsPossible.Clear(); break; case PlayerType.Human: turnIsOver = false; if (!ActionsPossible.Any()) { TurnScore = 0; WriteLine("{0} BUSTED!", CurrentPlayerInfo()); turnIsOver = true; } break; } if (turnIsOver) { EndTurn(); } } FarklePlayer winner = Players.FirstOrDefault(x => x.BankedScore >= ExpectedValueCalc.WinScore); if (winner != null) { WriteLine("\n\n---------- {0} WINS! ----------", winner.Name); WriteLine("{0} Turns for an average of {1} points per turn.", winner.TurnsTaken, (double)winner.BankedScore / winner.TurnsTaken); NewGame(); State = GameState.NoGame; } } //while }
static void RandomRoll() { Roll = Dice.RandomRoll(DiceToRoll); ActionsPossible = Farkle.GenerateActions(Roll); }
public bool Equals(Roll other) { return(this.OrderBy(x => x).SequenceEqual(other.OrderBy(x => x))); }
public Roll(Roll roll) : base(roll) { }
public static List <Roll> RollPermutations(int dice, Roll roll) { switch (dice) { case 1: return ((from a in roll select new Roll { a }).ToList()); case 2: return ((from a in roll from b in roll select new Roll { a, b }).ToList()); case 3: return ((from a in roll from b in roll from c in roll select new Roll { a, b, c }).ToList()); case 4: return ((from a in roll from b in roll from c in roll from d in roll select new Roll { a, b, c, d }).ToList()); case 5: return ((from a in roll from b in roll from c in roll from d in roll from e in roll select new Roll { a, b, c, d, e }).ToList()); case 6: return ((from a in roll from b in roll from c in roll from d in roll from e in roll from f in roll select new Roll { a, b, c, d, e, f }).ToList()); default: return(null); } }
public static List <Roll> RollCombinations(int dice, Roll roll) { Tuple <int, Roll> key = new Tuple <int, Roll>(dice, roll); List <Roll> combos; if (RollComboCache.TryGetValue(key, out combos)) { return(combos); } if (dice == roll.Count()) { combos = new List <Roll> { new Roll(roll.OrderBy(x => x)) }; RollComboCache.Add(key, combos); return(combos); } combos = new List <Roll>(); for (int a = 0; a < roll.Count; a++) { if (dice == 1) { combos.Add(new Roll { roll[a] }); continue; } for (int b = a + 1; b < roll.Count; b++) { if (dice == 2) { combos.Add(new Roll { roll[a], roll[b] }); continue; } for (int c = b + 1; c < roll.Count; c++) { if (dice == 3) { combos.Add(new Roll { roll[a], roll[b], roll[c] }); continue; } for (int d = c + 1; d < roll.Count; d++) { if (dice == 4) { combos.Add(new Roll { roll[a], roll[b], roll[c], roll[d] }); continue; } for (int e = d + 1; e < roll.Count; e++) { if (dice == 5) { combos.Add(new Roll { roll[a], roll[b], roll[c], roll[d], roll[e] }); continue; } for (int f = e + 1; f < roll.Count; f++) { combos.Add(new Roll { roll[a], roll[b], roll[c], roll[d], roll[e], roll[f] }); } } } } } } combos = combos.Distinct().ToList(); RollComboCache.Add(key, combos); return(combos); }