public KingTree(TreeTake value) { Value = value; UpLeft = null; UpRight = null; DownLeft = null; DownRight = null; }
public static List <List <TreeTake> > KingTreeToArray(KingTree mainTree) { List <List <TreeTake> > result = new List <List <TreeTake> >(); iter(mainTree, new List <TreeTake>()); void iter(KingTree tree, List <TreeTake> list) { if (tree.DownLeft != null && tree.DownRight != null && tree.UpLeft != null && tree.UpRight != null) { list.Add(tree.Value); iter(tree.DownLeft, new List <TreeTake>(list)); iter(tree.DownRight, new List <TreeTake>(list)); iter(tree.UpLeft, new List <TreeTake>(list)); iter(tree.UpRight, new List <TreeTake>(list)); } else if (tree.DownLeft != null && tree.DownRight != null && tree.UpLeft != null) { list.Add(tree.Value); iter(tree.DownLeft, new List <TreeTake>(list)); iter(tree.DownRight, new List <TreeTake>(list)); iter(tree.UpLeft, new List <TreeTake>(list)); } else if (tree.DownLeft != null && tree.DownRight != null && tree.UpRight != null) { list.Add(tree.Value); iter(tree.DownLeft, new List <TreeTake>(list)); iter(tree.DownRight, new List <TreeTake>(list)); iter(tree.UpRight, new List <TreeTake>(list)); } else if (tree.DownLeft != null && tree.UpLeft != null && tree.UpRight != null) { list.Add(tree.Value); iter(tree.DownLeft, new List <TreeTake>(list)); iter(tree.UpLeft, new List <TreeTake>(list)); iter(tree.UpRight, new List <TreeTake>(list)); } else if (tree.DownRight != null && tree.UpLeft != null && tree.UpRight != null) { list.Add(tree.Value); iter(tree.DownRight, new List <TreeTake>(list)); iter(tree.UpLeft, new List <TreeTake>(list)); iter(tree.UpRight, new List <TreeTake>(list)); } else if (tree.DownLeft != null && tree.DownRight != null) { list.Add(tree.Value); iter(tree.DownLeft, new List <TreeTake>(list)); iter(tree.DownRight, new List <TreeTake>(list)); } else if (tree.DownLeft != null && tree.UpLeft != null) { list.Add(tree.Value); iter(tree.DownLeft, new List <TreeTake>(list)); iter(tree.UpLeft, new List <TreeTake>(list)); } else if (tree.DownLeft != null && tree.UpRight != null) { list.Add(tree.Value); iter(tree.DownLeft, new List <TreeTake>(list)); iter(tree.UpRight, new List <TreeTake>(list)); } else if (tree.DownRight != null && tree.UpLeft != null) { list.Add(tree.Value); iter(tree.DownRight, new List <TreeTake>(list)); iter(tree.UpLeft, new List <TreeTake>(list)); } else if (tree.DownLeft != null && tree.UpRight != null) { list.Add(tree.Value); iter(tree.DownLeft, new List <TreeTake>(list)); iter(tree.UpRight, new List <TreeTake>(list)); } else if (tree.UpLeft != null && tree.UpRight != null) { list.Add(tree.Value); iter(tree.UpLeft, new List <TreeTake>(list)); iter(tree.UpRight, new List <TreeTake>(list)); } else if (tree.DownLeft != null) { list.Add(tree.Value); iter(tree.DownLeft, new List <TreeTake>(list)); } else if (tree.DownRight != null) { list.Add(tree.Value); iter(tree.DownRight, new List <TreeTake>(list)); } else if (tree.UpLeft != null) { list.Add(tree.Value); iter(tree.UpLeft, new List <TreeTake>(list)); } else if (tree.UpRight != null) { list.Add(tree.Value); iter(tree.UpRight, new List <TreeTake>(list)); } else { list.Add(tree.Value); result.Add(list); } } return(result); }
public static KingTree checkKingTake(int[,] board, int preHeight, int preWidth, int height, int width, int[] playerToTake, KingTree tree) { try { //Down left if (Array.IndexOf(playerToTake, board[height + 1, width - 1]) > -1) { if (board[height + 2, width - 2] == 5 || board[height + 2, width - 2] == 6) { if (preHeight != height + 2 || preWidth != width - 2) { tree.DownLeft = new KingTree(new TreeTake { TakeHeight = height + 1, TakeWidth = width - 1, NextHeight = height + 2, NextWidth = width - 2 }); checkKingTake(board, height, width, height + 2, width - 2, playerToTake, tree.DownLeft); } } } } catch { } try { //Down Right if (Array.IndexOf(playerToTake, board[height + 1, width + 1]) > -1) { if (board[height + 2, width + 2] == 5 || board[height + 2, width + 2] == 6) { if (preHeight != height + 2 || preWidth != width + 2) { tree.DownRight = new KingTree(new TreeTake { TakeHeight = height + 1, TakeWidth = width + 1, NextHeight = height + 2, NextWidth = width + 2 }); checkKingTake(board, height, width, height + 2, width + 2, playerToTake, tree.DownRight); } } } } catch { } try { //Up left if (Array.IndexOf(playerToTake, board[height - 1, width - 1]) > -1) { if (board[height - 2, width - 2] == 5 || board[height - 2, width - 2] == 6) { if (preHeight != height - 2 || preWidth != width - 2) { tree.UpLeft = new KingTree(new TreeTake { TakeHeight = height - 1, TakeWidth = width - 1, NextHeight = height - 2, NextWidth = width - 2 }); checkKingTake(board, height, width, height - 2, width - 2, playerToTake, tree.UpLeft); } } } } catch { } try { //Up Right if (Array.IndexOf(playerToTake, board[height - 1, width + 1]) > -1) { if (board[height - 2, width + 2] == 5 || board[height - 2, width + 2] == 6) { if (preHeight != height - 2 || preWidth != width + 2) { tree.UpRight = new KingTree(new TreeTake { TakeHeight = height - 1, TakeWidth = width + 1, NextHeight = height - 2, NextWidth = width + 2 }); checkKingTake(board, height, width, height - 2, width + 2, playerToTake, tree.UpRight); } } } } catch { } return(tree); }
public static List <NextMove> FindAvailableMoves(int[,] board) { List <NextMove> results = new List <NextMove>(); for (int i = 0; i < board.GetLength(0); i++) { // Skip over pieces that cant be player for (int j = 1 - (i % 2); j < board.GetLength(1); j += 2) { int piece = board[i, j]; if (piece == 1) { if (CheckMove.checkMoveUpLeft(board, i, j)) { results.Add(new NextMove { CurrentHeight = i, CurrentWidth = j, NextHeight = i - 1, NextWidth = j - 1, Piece = 1 }); } if (CheckMove.checkMoveUpRight(board, i, j)) { results.Add(new NextMove { CurrentHeight = i, CurrentWidth = j, NextHeight = i - 1, NextWidth = j + 1, Piece = 1 }); } Tree resultTree = CheckMove.checkTakeUp(board, i, j, new int[] { 2 }, new Tree(new TreeTake { CurrentHeight = i, CurrentWidth = j, Piece = 1 })); if (resultTree.Left != null || resultTree.Right != null) { List <List <TreeTake> > treeArray = Tree.TreeToArray(resultTree); results.AddRange(ProcessTreeArray(treeArray)); } } else if (piece == 2) { if (CheckMove.checkMoveDownLeft(board, i, j)) { results.Add(new NextMove { CurrentHeight = i, CurrentWidth = j, NextHeight = i + 1, NextWidth = j - 1, Piece = 2 }); } if (CheckMove.checkMoveDownRight(board, i, j)) { results.Add(new NextMove { CurrentHeight = i, CurrentWidth = j, NextHeight = i + 1, NextWidth = j + 1, Piece = 2 }); } Tree resultTree = CheckMove.checkTakeDown(board, i, j, new int[] { 1 }, new Tree(new TreeTake { CurrentHeight = i, CurrentWidth = j, Piece = 2 })); if (resultTree.Left != null || resultTree.Right != null) { List <List <TreeTake> > treeArray = Tree.TreeToArray(resultTree); results.AddRange(ProcessTreeArray(treeArray)); } } else if ((piece == 3) || (piece == 4)) { if (CheckMove.checkMoveUpLeft(board, i, j)) { results.Add(new NextMove { CurrentHeight = i, CurrentWidth = j, NextHeight = i - 1, NextWidth = j - 1, Piece = piece == 3 ? 1 : 2 }); } if (CheckMove.checkMoveUpRight(board, i, j)) { results.Add(new NextMove { CurrentHeight = i, CurrentWidth = j, NextHeight = i - 1, NextWidth = j + 1, Piece = piece == 3 ? 1 : 2 }); } if (CheckMove.checkMoveDownLeft(board, i, j)) { results.Add(new NextMove { CurrentHeight = i, CurrentWidth = j, NextHeight = i + 1, NextWidth = j - 1, Piece = piece == 3 ? 1 : 2 }); } if (CheckMove.checkMoveDownRight(board, i, j)) { results.Add(new NextMove { CurrentHeight = i, CurrentWidth = j, NextHeight = i + 1, NextWidth = j + 1, Piece = piece == 3 ? 1 : 2 }); } int[] countersToTake = piece == 3 ? new int[] { 2, 4 } : new int[] { 1, 3 }; KingTree resultTree = CheckMove.checkKingTake(board, i, j, i, j, countersToTake, new KingTree(new TreeTake { CurrentHeight = i, CurrentWidth = j, Piece = piece == 3 ? 1 : 2 })); if (resultTree.DownLeft != null || resultTree.DownRight != null || resultTree.UpLeft != null || resultTree.UpRight != null) { List <List <TreeTake> > treeArray = KingTree.KingTreeToArray(resultTree); results.AddRange(ProcessTreeArray(treeArray)); } } } } return(results); }