public static bool isWinOrBlockAvailable(Flower[] board, Flower.FlowerType searchFor, ref int move) { bool canWin = false; int positives; int neutrals; int neutralsMove = -1; for (int y = 0; y < CHECKS_HEIGHT; y++) { positives = 0; neutrals = 0; for (int x = 0; x < CHECKS_WIDTH; x++) { if (board[BOARD_LINE_CHECKS[y, x]].Type == searchFor) { positives++; } else if (board[BOARD_LINE_CHECKS[y, x]].Type == Flower.FlowerType.None) { neutralsMove = BOARD_LINE_CHECKS[y, x]; neutrals++; } } if (positives == 2 && neutrals == 1) { move = neutralsMove; canWin = true; break; } } return(canWin); }
public static Flower.FlowerType[] cloneFlowerTypes(Flower.FlowerType[] oldTypes) { Flower.FlowerType[] newTypes = new Flower.FlowerType[oldTypes.Length]; for (int i = 0; i < oldTypes.Length; i++) { newTypes[i] = oldTypes[i]; } return(newTypes); }
public static Flower.FlowerType[] getFlowerTypes(Flower[] board) { Flower.FlowerType[] types = new Flower.FlowerType[board.Length]; for (int i = 0; i < board.Length; i++) { types[i] = board[i].Type; } return(types); }
public static Flower.FlowerType translateTurnToFlowerType(StateManager.TurnType turn) { Flower.FlowerType type = Flower.FlowerType.None; if (turn == StateManager.TurnType.Computers) { type = COMPUTERS_TYPE; } else if (turn == StateManager.TurnType.Players) { type = PLAYERS_TYPE; } return(type); }
public Player(ContentManager content, SpriteFont font, string name, Vector2 scorePosition, Vector2 turnSpritePosition, string aliveTexture, string dyingTexture, Flower.FlowerType flowerType) { this.name = name; this.AliveTexture = LoadingUtils.load <Texture2D>(content, aliveTexture); this.DyingTexture = LoadingUtils.load <Texture2D>(content, dyingTexture); this.FlowerType = flowerType; this.Score = 0; Text2DParams textParams = new Text2DParams(); textParams.Font = font; textParams.LightColour = ResourceManager.getInstance().TextColour; textParams.Position = scorePosition; this.Text = new Text2D(textParams); this.myTurnSprite = FlowerBuilder.getFlowerSprite(content, turnSpritePosition, this.AliveTexture, AnimationState.PlayForwardOnce); this.notMyTurnSprite = FlowerBuilder.getFlowerSprite(content, turnSpritePosition, this.DyingTexture, AnimationState.PlayForwardOnce); }
private static bool checkBoardsState(Flower.FlowerType[] board, Flower.FlowerType flowerType, out int[] winningIndexes) { bool foundMatch = false; winningIndexes = null; for (int i = 0; i < CHECKS_HEIGHT; i++) { if (board[BOARD_LINE_CHECKS[i, 0]] == flowerType && board[BOARD_LINE_CHECKS[i, 1]] == flowerType && board[BOARD_LINE_CHECKS[i, 2]] == flowerType) { foundMatch = true; winningIndexes = new int[CHECKS_WIDTH]; for (int j = 0; j < CHECKS_WIDTH; j++) { winningIndexes[j] = BOARD_LINE_CHECKS[i, j]; } break; } } return(foundMatch); }
//Alpha: a score the computer knows with certanty it can achieve //Beta: a score the human knows with certanty it can achieve //If beta becomes <= alpha, further investigation is useless private int miniMax(Flower.FlowerType[] types, Flower.FlowerType turn, int alpha, int beta) { int bestMove; int move; Winner winner; if (LogicUtils.isGameOver(types, out winner)) { if (winner.winningType == LogicUtils.COMPUTERS_TYPE) { return(1); } else if (winner.winningType == LogicUtils.PLAYERS_TYPE) { return(-1); } else { return(0); } } else { if (LogicUtils.COMPUTERS_TYPE.Equals(turn)) { bestMove = alpha; } else { bestMove = beta; } Flower.FlowerType[] cloned = null; Flower.FlowerType inversedTurn = EnumUtils.inverseValue <Flower.FlowerType>(turn); for (int i = 0; i < types.Length; i++) { if (types[i] == Flower.FlowerType.None) // get valid moves { cloned = LogicUtils.cloneFlowerTypes(types); // get our result cloned[i] = turn; move = miniMax(cloned, inversedTurn, alpha, beta); // interpret our result if (LogicUtils.COMPUTERS_TYPE == turn) { if (move > alpha) { alpha = move; } if (alpha >= beta) { return(alpha); } } else { if (move < beta) { beta = move; } if (alpha >= beta) { return(beta); } } } } } if (turn == LogicUtils.COMPUTERS_TYPE) { return(alpha); } else { return(beta); } }