public bool Contains(Board input) { bool contained = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (input.board[i, j] && input.board[i, j] != board[i, j]) { contained = false; break; } } if (!contained) break; } return contained; }
public static bool compareQuotients(Board b1, Board b2) { bool same = true; for (int i = 0; i < b1.quotient.Length; i++) { if (b1.quotient[i] != b2.quotient[i]) { same = false; break; } } return same; }
public static bool isKillable(Board input, ref Point[] killers) { bool killable = false; if (!isDead(input)) { Board worker; int i = 0; int j = 0; bool exit = false; do { worker = DeepCopy(input); // Make sure the original board is not modified j++; if (j > 2) { i++; j = 0; } if (!worker.board[i, j]) worker.board[i, j] = true; if (isDead(worker)) { Game.incrementArray(ref killers, new Point(i, j)); // ALL THE POINTS THAT CAN KILL killable = true; } if (i == 2 && j == 2) { exit = true; } } while (!exit); } else { killable = false; } return killable; }
public static bool isKillable(Board input) { bool killable = false; if (!isDead(input)) { Board worker; int i = 0; int j = 0; bool exit = false; do { worker = DeepCopy(input); // Make sure the original board is not modified j++; if (j > 2) { i++; j = 0; } if (!worker.board[i, j]) worker.board[i, j] = true; if (isDead(worker)) { exit = true; killable = true; } if (i == 2 && j == 2) { exit = true; } } while (!exit); } else { killable = false; } return killable; }
public static bool isDead(Board input) { bool ded = false; for (int i = 0; i < Assets.lines.Length; i++) { if (input.Contains(Assets.lines[i])) { ded = true; break; } } return ded; }
public static Board DeepCopy(Board input) { Board copy = new Board(3); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { copy.board[i, j] = input.board[i, j]; } } copy.dead = input.dead; for (int i = 0; i < 4; i++) { copy.quotient[i] = input.quotient[i]; } copy.canBeKilled = input.canBeKilled; return copy; }
private int[] sumQuotients(Board[] input, bool[] dead) { int[] fQuot = new int[4]; for (int i = 0; i < input.Length; i++) { fQuot = sumArrays(fQuot, input[i].computeQuotient(false)); } return fQuot; }
private static int count(Board[] input, bool value) { int count = 0; foreach (Board foo in input) { if (foo.dead == value) { count++; } } return count; }
private bool playThreeBoards(Board[] input) { return true; }
} // THE BOARD MAY NOT BE MODIFIED IN THE ARRAY? private bool playTwoBoards(Board[] input, bool[] deadBoards) { #region Deep Copy int killable = 0; Board[] workers = new Board[input.Length]; // MAKE SURE WE DONT MODIFY THE ORIGINAL BOARDS AND ARRAYS for (int i = 0; i < input.Length; i++) { workers[i] = input[i].DeepCopy(); killable = input[i].canBeKilled & !input[i].dead ? killable + 1 : killable; } bool[] workerDead = new bool[deadBoards.Length]; for (int i = 0; i < deadBoards.Length; i++) { workerDead[i] = deadBoards[i]; } #endregion // LOOK FOR KILLABLE BOARDS THEN CONFIRM THAT THE ANOTHER BOARD GETS THE RIGHT QUOTIENTS if (killable != 0) { for (int i = 0; i < killable; i++) { for (int j = 0; j < workers.Length; j++) { // IGNORE ALREADY LOOKED UP BOARDS } } } return true; }
} // INTERNAL BECAUSE ASSETS IS IN THE SAME ASSEMBLY AS GAME private bool playOneBoard(Board input) { if (count(input.board, true) % 2 == 0) { #region Granted Win if (Array.IndexOf(input.board, true) == -1) { play(input, new Point(2, 2)); } else { Point foo; // THE POINT THAT THE TRANSFORMATION IS APPLIED TO Point bar = new Point(); // THE POINT AFTER TRANSFORMATION bool exit = false; // LOOP CONDITION do { Point[] excluded = null; // POINTS THAT AFTER APPLYING THE KNIGHTS ALG ARE NOT ABLE TO BE PLACED, INSTEAD OF STORING THE ORDER OF THE MOVES bool toBeExcluded = true; foo = getOuterPoints(input, excluded); for (int i = 0; i < Assets.knight.Length; i++) { bar = foo; // BECAUSE Point.Offset CHANGES bar AND IT DOES NOT RETURN A NEW POINT bar.Offset(Assets.knight[i]); if (!isOutBounds(input.board, bar)) { if (play(input, bar)) { toBeExcluded = false; // DO NOT DISCARD THE ORIGINAL POINT exit = true; // EXIT THE LOOP break; } } } if (toBeExcluded) incrementArray(ref excluded, foo); } while (!exit); // PLAY A KNIGHT'S MOVE } #endregion } else { #region Probably lose Board worker = input.DeepCopy(); Point[] killers = new Point[0]; bool killable = Board.isKillable(worker, ref killers); if (!killable) // PLAY RANDOM { Random rand = new Random(); int i = 0; int j = 0; do { i = rand.Next(0, 3); j = rand.Next(0, 3); } while (i == 1 || j == 1); play(input, new Point(i, j)); } else // PLAY WHERE WE DONT LOSE { bool played = false; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (Array.IndexOf(killers, new Point(i, j)) == -1 && !input.board[i, j]) // IT IS POSSIBLE TO NOT LOSE { played = play(input, new Point(i, j)); } } } if (!played) { play(input, killers[0]); // PLAY TO LOSE } } #endregion } return true; } // THE BOARD MAY NOT BE MODIFIED IN THE ARRAY?
private bool isEnded(Board[] input) { bool ended = true; for (int i = 0; i < input.Length; i++) { if (!input[i].dead) { ended = false; break; } } return ended; }
private int[] computeTotalQuotients(Board[] input) { int[] total = new int[4]; for (int i = 0; i < input.Length; i++) { total = total.Zip(input[i].quotient, (x, y) => x + y).ToArray(); } return total; }
private bool updateGameVariables(Board[] input, out int[] quoti, out bool end) { for (int i = 0; i < input.Length; i++) { input[i].computeInternalVariables(); } quoti = computeTotalQuotients(input); end = isEnded(input); deadBoards = count(input, true); return true; }
private Point getOuterPoints(Board input, Point[] excl) { Point empty = new Point(); // The resulted point for (int i = 0; i <= input.board.GetUpperBound(0); i++) // FOR EACH ROW { for (int j = 0; j <= input.board.GetUpperBound(1); j++) // FOR EACH COLUMN { if ((i != 1 | j != 1) & input.board[i, j]) // IS THE POINT NOT THE CENTER AND IT HAVE AN X? { bool used = false; if (excl == null) // IF THE ARRAY IS NOT EMPTY { for (int k = 0; k < excl.Length; k++) { if (excl[k] == new Point(i, j)) { used = true; } } } else { used = true; } if (!used) { return new Point(i, j); } } else { empty.X = i; empty.Y = j; } } } return empty; }
public Board DeepCopy() { Board copy = new Board(3); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { copy.board[i, j] = board[i, j]; } } copy.dead = dead; for (int i = 0; i < 4; i++) { copy.quotient[i] = quotient[i]; } copy.canBeKilled = canBeKilled; return copy; }
private bool play(Board input, Point pt) { if (!input.board[pt.X, pt.Y] && !input.dead) { input.board[pt.X, pt.Y] = true; return true; } else { return false; } }