public bool ComplexClick(int x, int y, bool hint_mode = false)//analyze in 3*3+,simplified and comfirmed { List <SolveUnit> units = new List <SolveUnit> { }; SolveUnit center = new SolveUnit(mines[x, y].mine_count); center.Simplified_blocks = GetBlockSet(x, y, true); center.comfirmed_mine_count = mines[x, y].mine_count - GetInfo(x, y).flag_count; for (int i = x - 1; i < x + 2; i++) { for (int j = y - 1; j < y + 2; j++) { if (inBorder(i, j) && !mines[i, j].is_cover && mines[i, j].mine_count != 0) { SolveUnit edge = new SolveUnit(mines[i, j].mine_count); edge.Simplified_blocks = GetBlockSet(i, j, true); edge.comfirmed_mine_count = mines[i, j].mine_count - GetInfo(i, j).flag_count; units.Add(edge); } } } for (int i = 0; i < units.Count; i++) { if (units[i].Simplified_blocks.IsProperSubsetOf(center.Simplified_blocks) && units[i].comfirmed_mine_count == center.comfirmed_mine_count) { center.Simplified_blocks.ExceptWith(units[i].Simplified_blocks); foreach (Position p in center.Simplified_blocks) { if (hint_mode) { ShowAnimation(p.x, p.y); } else { if (mines[p.x, p.y].mine_count != 0) { if (openBlock(p.x, p.y)) { return(true);//win or lose } } else { openEmpty(p.x, p.y); } } } return(true); } } return(false); }
public bool ComplexFlag(int x, int y, bool hint_mode = false)//analyze in 3*3 { List <SolveUnit> units = new List <SolveUnit> { }; SolveUnit center = new SolveUnit(mines[x, y].mine_count); center.Simplified_blocks = GetBlockSet(x, y, true); center.comfirmed_mine_count = mines[x, y].mine_count - GetInfo(x, y).flag_count; for (int i = x - 1; i < x + 2; i++) { for (int j = y - 1; j < y + 2; j++) { if (inBorder(i, j) && !mines[i, j].is_cover && mines[i, j].mine_count != 0) { SolveUnit edge = new SolveUnit(mines[i, j].mine_count); edge.Simplified_blocks = GetBlockSet(i, j, true); edge.comfirmed_mine_count = mines[i, j].mine_count - GetInfo(i, j).flag_count; units.Add(edge); } } } for (int i = 0; i < units.Count; i++) { if (units[i].Simplified_blocks.IsProperSubsetOf(center.Simplified_blocks) && units[i].comfirmed_mine_count < center.comfirmed_mine_count) { int mine_diff = center.comfirmed_mine_count - units[i].comfirmed_mine_count; var new_set = new HashSet <Position>(center.Simplified_blocks); new_set.ExceptWith(units[i].Simplified_blocks); int block_diff = new_set.Count; if (mine_diff == block_diff) { foreach (Position p in new_set) { if (hint_mode) { ShowAnimation(p.x, p.y); } else { flagBlock(p.x, p.y); } } return(true); } } } return(false); }
public bool CompleteAnalyze(int x, int y, bool hint_mode = false)//analyze in 5*5+,simplified and comfirmed,test needed { List <SolveUnit> units = new List <SolveUnit> { }; SolveUnit center = new SolveUnit(mines[x, y].mine_count); center.Simplified_blocks = GetBlockSet(x, y, true); center.comfirmed_mine_count = mines[x, y].mine_count - GetInfo(x, y).flag_count; for (int i = x - 2; i < x + 3; i++) { for (int j = y - 2; j < y + 3; j++) { if (inBorder(i, j) && !mines[i, j].is_cover && // in border and is a uncovered number mines[i, j].mine_count != 0 && (x != i || y != j) //number is not 0 and is not center(x,y) ) { SolveUnit edge = new SolveUnit(mines[i, j].mine_count); edge.Simplified_blocks = GetBlockSet(i, j, true); edge.comfirmed_mine_count = mines[i, j].mine_count - GetInfo(i, j).flag_count; if (edge.comfirmed_mine_count != 0) //not a empty condition { units.Add(edge); } } } } if (units.Count == 0) { return(false); } var combinations = GetAllCombination(units);//get all combinatons foreach (List <SolveUnit> set in combinations) { HashSet <Position> solve = new HashSet <Position> { }; int before = 0; int after = 0; int real_mine_count = 0; foreach (SolveUnit su in set) { before = solve.Count; solve.UnionWith(su.Simplified_blocks); //union after = solve.Count; if (after - before != su.Simplified_blocks.Count) //intersect not empty { goto end; } real_mine_count += su.comfirmed_mine_count; } //CheckSet(solve); if (solve.IsProperSubsetOf(center.Simplified_blocks))//properset { int block_diff = center.Simplified_blocks.Count - solve.Count; int mine_diff = center.comfirmed_mine_count - real_mine_count; //Console.WriteLine("{0} {1}", block_diff, mine_diff); if (mine_diff == 0) { center.Simplified_blocks.ExceptWith(solve); foreach (Position p in center.Simplified_blocks) { if (hint_mode) { ShowAnimation(p.x, p.y); } else { openBlock(p.x, p.y); if (getGameState() == GameState.Lose || getGameState() == GameState.Win) { return(true);//win or lose } } } return(true); } if (mine_diff > 0 && mine_diff == block_diff) { center.Simplified_blocks.ExceptWith(solve); //CheckSet(center.Simplified_blocks); foreach (Position p in center.Simplified_blocks) { if (hint_mode) { ShowAnimation(p.x, p.y); } else { flagBlock(p.x, p.y); } } return(true); } } else if (center.Simplified_blocks.IsProperSubsetOf(solve))//superset { int block_diff = solve.Count - center.Simplified_blocks.Count; int mine_diff = real_mine_count - center.comfirmed_mine_count; //Console.WriteLine("{0} {1}", block_diff, mine_diff); if (mine_diff == 0) { CheckSet(solve); CheckSet(center.Simplified_blocks); solve.ExceptWith(center.Simplified_blocks); foreach (Position p in solve) { if (hint_mode) { ShowAnimation(p.x, p.y); } else { openBlock(p.x, p.y); if (getGameState() == GameState.Lose || getGameState() == GameState.Win) { return(true);//win or lose } } } return(true); } if (mine_diff > 0 && mine_diff == block_diff) { solve.ExceptWith(center.Simplified_blocks); //CheckSet(center.Simplified_blocks); foreach (Position p in solve) { if (hint_mode) { ShowAnimation(p.x, p.y); } else { flagBlock(p.x, p.y); } } return(true); } } else if (!solve.SetEquals(center.Simplified_blocks))//not properset, not equal { int mine_diff = real_mine_count - center.comfirmed_mine_count; if (mine_diff > 0) { var new_set = new HashSet <Position>(solve); new_set.ExceptWith(center.Simplified_blocks); if (new_set.Count != 0) { int block_diff = new_set.Count; if (block_diff == mine_diff) { foreach (Position p in new_set) { if (hint_mode) { ShowAnimation(p.x, p.y); } else { flagBlock(p.x, p.y); } } return(true); } } } else if (mine_diff < 0) { //CheckSet(solve); //CheckSet(center.Simplified_blocks); var new_set = new HashSet <Position>(center.Simplified_blocks); new_set.ExceptWith(solve); int block_diff = new_set.Count; if (block_diff == -mine_diff) { foreach (Position p in new_set) { if (hint_mode) { ShowAnimation(p.x, p.y); } else { flagBlock(p.x, p.y); } } return(true); } } } end :; } return(false); }
public bool UncertainComplexFlag(int x, int y, bool hint_mode = false)//analyze in 3*3, opt needed { List <SolveUnit> units = new List <SolveUnit> { }; SolveUnit center = new SolveUnit(mines[x, y].mine_count); for (int i = x - 1; i < x + 2; i++) { for (int j = y - 1; j < y + 2; j++) { if (inBorder(i, j) && !mines[i, j].is_cover && //in border and a uncovered number mines[i, j].mine_count != 0 && (x != i || y != j)) //number not 0 and block not center { SolveUnit edge = new SolveUnit(mines[i, j].mine_count); for (int m = i - 1; m < i + 2; m++) { for (int n = j - 1; n < j + 2; n++) { if (inBorder(m, n) && InBorderNineByNine(m, n, x - 1, y - 1) && mines[m, n].is_cover) { edge.AddPosition(m, n); } } } units.Add(edge); } if (inBorder(i, j) && mines[i, j].is_cover) { center.AddPosition(i, j); } } } for (int i = 0; i < units.Count; i++) { int diff = center.mine_count - units[i].mine_count;//at least diff mines int diff_block = center.Blocks.Count - units[i].Blocks.Count; if (diff == diff_block && diff > 0) { bool test = false; center.Blocks.ExceptWith(units[i].Blocks); //CheckSet(center.Blocks); foreach (Position p in center.Blocks) { if (!mines[p.x, p.y].is_flag) { if (hint_mode) { ShowAnimation(p.x, p.y); } else { flagBlock(p.x, p.y); } test = true; } } return(test); } } return(false); }