public void Next() { var rule = new RuleChecker(boardList); var allNeighbors = new List<Point>(); boardList.ToList().ForEach(p => allNeighbors.AddRange(rule.GetNeighbors(p))); boardList = new HashSet<Point>( allNeighbors.Where(p => rule.IsLive(p) || rule.IsNew(p)), new PointComparer()); }
/* * Returns a point if there is a valid move given the board history * point is determined by _AIType * Returns "pass" if there are no valid moves * Throws an exception if board history is illegal */ public string MakeAMove(string[][][] boards) { try { RuleChecker.CheckHistory(_stone, boards); } catch (RuleCheckerException) { throw new PlayerException("This history makes no sense!"); } switch (_AIType) { case "less dumb": string oppositeStone; if (_stone == "B") { oppositeStone = "W"; } else { oppositeStone = "B"; } BoardWrapper boardObject = new BoardWrapper(boards[0], boards[0].Length); List <string> pointsList = boardObject.GetPoints(oppositeStone); List <string> iterate; List <List <string> > possibleMoves = new List <List <string> >(); //Find all oppositeStones with only one adjacent liberty //Add {point, adjacentLiberty} to a possibleMoves foreach (string point in pointsList) { iterate = boardObject.GetAdjacentLiberties(point); iterate.Insert(0, point); if (iterate.Count == 2) { possibleMoves.Add(iterate); } } List <List <string> > temp = new List <List <string> >(); //Check the validity of each possibleMove //Also check that making that possibleMove will result in a capture //Failing these conditions, remove move from possibleMoves foreach (List <string> move in possibleMoves) { try { RuleChecker.CheckMove(_stone, move[1], boards); } catch (Exception e) { if (!(e is RuleCheckerException) && !(e is BoardException)) { throw; } continue; } boardObject = new BoardWrapper(boards[0], boards[0].Length); boardObject.PlaceStone(_stone, move[1]); if (!boardObject.Reachable(move[0], " ")) { temp.Add(move); } } possibleMoves = temp; //sort in lowest column lowest row order (numeric) possibleMoves.Sort(ComparePossibleMoves); //for n == 1, return the first possible Move (if it exists) if (_n == 1) { if (possibleMoves.Count > 0) { return(possibleMoves[0][1]); } } //Otherwise, n > 1, and if there is only one possibleMove, return it if (possibleMoves.Count == 1) { return(possibleMoves[0][1]); } //Otherwise, play dumb goto case "dumb"; case "dumb": for (int i = 0; i < boards[0].Length; i++) { for (int j = 0; j < boards[0].Length; j++) { try { string point = (i + 1).ToString() + "-" + (j + 1).ToString(); RuleChecker.CheckMove(_stone, point, boards); return(point); } catch (Exception e) { if (!(e is RuleCheckerException) && !(e is BoardException)) { throw; } } } } break; } return("pass"); }
public RuleCheckerTest() { _ruleChecker = new RuleChecker(); }
private static ShowCardResult ShowCard(IUser user, List <DealResult <DoudizhuUser> > results, ShowCardResult prevShowResult = null) { var pokers = results.First(x => x.User.Id == user.Id).Pokers; Console.WriteLine($"{user.Display},{string.Join(",", pokers.Select(x => $"{x.Display}({x.Key})"))}"); Console.Write($"[{(user as DoudizhuUser).UserRole.ToString()}]{user.Display}[{pokers.Count}] select pokers:"); var readString = Console.ReadLine(); if (string.IsNullOrEmpty(readString)) { //if (prevShowResult == null) //{ // Console.WriteLine("出牌不符合规则,请重新出!"); // ShowCard(user, results); //} //return new ShowCardResult //{ // RuleMatcher = new RuleMatchResult { CheckResult = true }, // Result = ShowCardResult.ShowCardResultType.NotGiven //}; readString = "P"; } if (readString.ToUpper() == "P") { if (prevShowResult != null) { prevShowResult.Result = ShowCardResult.ShowCardResultType.Pass; return(prevShowResult); } else { Console.WriteLine("头家不能PASS,请重新出!"); return(ShowCard(user, results)); } } var selectedPokerKeys = readString.Split(",").ToList(); var selectedPokers = pokers.Where(x => selectedPokerKeys.Any(y => y == x.Key)).ToList(); if (selectedPokers.Count() != selectedPokerKeys.Count) { Console.WriteLine("你恐怕出了你不存在的牌吧,请重新出!"); return(ShowCard(user, results, prevShowResult)); } if (prevShowResult == null) { var ruleMatcher = RuleChecker.Check(selectedPokers); //验证是否正确 if (!ruleMatcher.CheckResult) { Console.WriteLine("出牌不符合规则,请重新出!"); return(ShowCard(user, results)); } //移除用户手中的牌 pokers.RemoveAll(x => selectedPokerKeys.Any(y => x.Key == y)); results.First(x => x.User.Id == user.Id).PokerKeys.RemoveAll(x => selectedPokerKeys.Contains(x.PokerKey)); return(new ShowCardResult { RuleMatcher = ruleMatcher, Pokers = selectedPokers, Result = ShowCardResult.ShowCardResultType.Show, User = user }); } var currentRule = RuleHelper.BuildBoomRule(selectedPokers); if (currentRule == null) { currentRule = prevShowResult.RuleMatcher.Rule.New(selectedPokers); } if (!currentRule.Check()) { Console.WriteLine("出牌不符合规则,请重新出!"); return(ShowCard(user, results, prevShowResult)); } if (currentRule.CompareTo(prevShowResult.RuleMatcher.Rule) <= 0) { Console.WriteLine("出牌不符合规则,请重新出!"); return(ShowCard(user, results, prevShowResult)); } //移除用户手中的牌 pokers.RemoveAll(x => selectedPokerKeys.Any(y => x.Key == y)); results.First(x => x.User.Id == user.Id).PokerKeys.RemoveAll(x => selectedPokerKeys.Contains(x.PokerKey)); return(new ShowCardResult { RuleMatcher = new RuleMatchResult { Rule = currentRule, CheckResult = true }, Result = ShowCardResult.ShowCardResultType.Show, Pokers = selectedPokers, User = user }); }
static void PerformNumberCheck(int overallIdenticalNumber, int subsequentIdenticalNumber, string numberToVerify) { RuleChecker iterativeChecker = CreateIterativeRules(RuleDirection.Direct, overallIdenticalNumber, subsequentIdenticalNumber); processNumberCheck(iterativeChecker, numberToVerify); }