/// <summary> /// Method to check for a win. /// Calls CheckForLength to find a line of 4 or more. /// </summary> /// <param Array of cells on board ="gameBoard"></param> /// <param Gamestate to compare for win. 1 = player. 2 = ai. ="gameState"></param> /// <param x index of last piece placed on gameboard ="xPos"></param> /// <param y index of last piece placed on gameboard ="yPos"></param> /// <returns>Integer. 0 = no win. 1 = player 1 win. 2 = player 2 win. True if CheckForLength returns a length of 4</returns> public int CheckForWin(int[,] board, int player, int xPos, int yPos) { int win = 0; List <Threat> threats = threatManager.FilterBoardThreats(board, player); //List<Threat> threats = threatManager.CollectThreats(board, player, xPos, yPos); //Debug.Log("Checking for threats"); for (int i = 0; i < threats.Count; i++) { Threat currentThreat = threats[i]; Debug.Log("Threat of length " + currentThreat.length + " and orientation " + currentThreat.orientation + " with " + currentThreat.open + " open space(s), for player " + currentThreat.player); if (currentThreat.length >= 4) { win = player; Debug.Log("Winning Threat"); Debug.Log("Winner: " + win); //Debug.Log("Threat of length " + threats[i].length + " and orientation " + threats[i].orientation + " for player " + threats[i].player); break; } } //Find the longest length threat to see if the player has got 4 in a row. /*if(threats.Max(x => x.length) >= 4) * { * win = player; * }*/ return(win); }
//return score based on length of players connected lines public int Evaluate(int[,] board, int maximisingPlayer, int depth) { int opponent = 0; opponent = 3 - maximisingPlayer; //Populate a list of each player's threats List <Threat> playerLines = threatManager.FilterBoardThreats(board, maximisingPlayer); List <Threat> opponentLines = threatManager.FilterBoardThreats(board, opponent); //Count the number of different length threats int fours = CheckFours(playerLines); int opponentFours = CheckFours(opponentLines); int threes = GetThreeScore(playerLines); int opponentThrees = GetThreeScore(opponentLines); int score = (threes) - (opponentThrees) - (opponentFours); //If player has one or more four in a row's, return a winning score if (fours > 0) { winner = maximisingPlayer; return((100000 + depth) * fours); } //If opponent has one or more four in a row's, return a losing score if (opponentFours > 0) { winner = opponent; return((-100000 - depth) * opponentFours); } //Otherwise a score should be returned based on the available threats else { winner = 0; return(score); } }