//Placer un pion aux coordonnées indiquées public void PutPawn(caseState pawn, int col) { int rowIndex = this._environment.getTheLowestEmptyCellIndexInCol(col); if (rowIndex != -1) { _environment.setCaseNewState(col, rowIndex, pawn); this._environment.nbMovePlayed++; this._environment.turn = playerTurn.playerRed; } }
public bool allStateCaseAreEqual(playerTurn toCheck, params Case[] cases) { caseState stateToCheck = (toCheck == playerTurn.playerRed) ? caseState.red : caseState.yellow; foreach (Case c in cases) { if (c.State != stateToCheck) { return(false); } } return(true); }
// Cette fonction permet de voir tous les pions d'une certaine couleur. // entrée : Couleur (énumération) // sortie : Liste de cases des pions de cette couleur. public List <Case> GetPawns(caseState state) { List <Case> result = new List <Case>(); for (int y = 0; y < _environment.Grid.Count; y++) { for (int x = 0; x < _environment.Grid[0].Count; x++) { if (_environment.Grid[y][x].State == state) { result.Add(_environment.Grid[y][x]); } } } return(result); }
public void setCaseNewState(int x, int y, caseState newState) { this.Grid[y][x].State = newState; ui.DrawPawn(x, y); }
public bool isAWinningMove(int col, playerTurn turn) { caseState stateToCheck = (turn == playerTurn.playerRed) ? caseState.red : caseState.yellow; int rowIndexOfLastPawnInCol = this._environment.getTheLowestEmptyCellIndexInCol(col); // There is a winning move with columns ? if (rowIndexOfLastPawnInCol < 3 && this._environment.Grid[rowIndexOfLastPawnInCol + 1][col].State == stateToCheck && this._environment.Grid[rowIndexOfLastPawnInCol + 2][col].State == stateToCheck && this._environment.Grid[rowIndexOfLastPawnInCol + 3][col].State == stateToCheck) { return(true); } // There is a winning move with line ? int nbYellowAtRight = 0; int nbYellowAtLeft = 0; for (int i = 1; col + i < this._environment.Width; i++) { if (this._environment.Grid[rowIndexOfLastPawnInCol][col + i].State == stateToCheck) { nbYellowAtRight++; } else { break; } } for (int i = 1; col - i >= 0; i++) { if (this._environment.Grid[rowIndexOfLastPawnInCol][col - i].State == stateToCheck) { nbYellowAtLeft++; } else { break; } } if (nbYellowAtLeft >= 3 || nbYellowAtRight >= 3 || nbYellowAtLeft + nbYellowAtRight >= 3) { return(true); } // There is a winning move with diagonal up-right? nbYellowAtRight = 0; nbYellowAtLeft = 0; for (int i = 1; col + i < this._environment.Width && rowIndexOfLastPawnInCol - i >= 0; i++) { if (this._environment.Grid[rowIndexOfLastPawnInCol - i][col + i].State == stateToCheck) { nbYellowAtRight++; } else { break; } } for (int i = 1; col - i >= 0 && rowIndexOfLastPawnInCol + i < this._environment.Height; i++) { if (this._environment.Grid[rowIndexOfLastPawnInCol + i][col - i].State == stateToCheck) { nbYellowAtLeft++; } else { break; } } if (nbYellowAtLeft >= 3 || nbYellowAtRight >= 3 || nbYellowAtLeft + nbYellowAtRight >= 3) { return(true); } // There is a winning move with diagonal up-left? nbYellowAtRight = 0; nbYellowAtLeft = 0; for (int i = 1; col - i >= 0 && rowIndexOfLastPawnInCol - i >= 0; i++) { if (this._environment.Grid[rowIndexOfLastPawnInCol - i][col - i].State == stateToCheck) { nbYellowAtLeft++; } else { break; } } for (int i = 1; (col + i) < this._environment.Width && (rowIndexOfLastPawnInCol + i) < this._environment.Height; i++) { if (this._environment.Grid[rowIndexOfLastPawnInCol + i][col + i].State == stateToCheck) { nbYellowAtRight++; } else { break; } } if (nbYellowAtLeft >= 3 || nbYellowAtRight >= 3 || nbYellowAtLeft + nbYellowAtRight >= 3) { return(true); } // if there is no winning move return(false); }
public Case(int x, int y) { _x = x; _y = y; _state = caseState.empty; }
//implémentation de negaMax, une amélioration de min max avec de l'élagage aplha Beta public int negaMax(playerTurn turn, int alpha, int beta, ref int bestColToPlay, int depth) { int bestScore = -int.MaxValue; if (_beliefs.environment.nbMovePlayed == _beliefs.environment.Height * _beliefs.environment.Width) { return(0); } if (depth <= 0) { bestScore = eval(turn); return(bestScore); } else { List <int> columns = new List <int>() { 0, 1, 2, 3, 4, 5, 6 }; //shuffle col numbers columns.shuffle(); foreach (int x in columns) // compute the score of all possible next move and keep the best one { if (this.captor.canPlay(x)) { if (this._beliefs.environment.Grid[this._beliefs.environment.getTheLowestEmptyCellIndexInCol(x)][x].State == caseState.empty) { caseState state = (turn == playerTurn.playerRed) ? caseState.red : caseState.yellow; int row = this._beliefs.environment.getTheLowestEmptyCellIndexInCol(x); this._beliefs.environment.Grid[row][x].State = state; this._beliefs.environment.nbMovePlayed++; int nextBestColToPlay = 3; //depth--; int score = -negaMax(playerTurn.playerRed, -beta, -alpha, ref nextBestColToPlay, depth--); this._beliefs.environment.Grid[row][x].State = caseState.empty; this._beliefs.environment.nbMovePlayed--; if (score > bestScore) { bestScore = score; bestColToPlay = x; } if (score > alpha) { alpha = bestScore; bestColToPlay = x; if (alpha > beta) { break; } } } } } } return(bestScore); }
//fonction d'évaluation public int eval(playerTurn player) { caseState statePlayer = (player == playerTurn.playerRed) ? caseState.red : caseState.yellow; playerTurn otherPlayer = (player == playerTurn.playerRed) ? playerTurn.playerYellow : playerTurn.playerRed; caseState stateOtherPlayer = (otherPlayer == playerTurn.playerRed) ? caseState.red : caseState.yellow; //score eval int score = 1, cpt = 0; if (this._beliefs.environment.getWinner(otherPlayer) == otherPlayer) { return(-10000 + this._beliefs.environment.nbMovePlayed); } else if (this._beliefs.environment.getWinner(player) == player) { return(10000 - this._beliefs.environment.nbMovePlayed); } for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (this._beliefs.environment.Grid[i][j].State == caseState.empty) { continue; } //Colonnes if (i > 3) { if (this._beliefs.environment.Grid[i][j].State == statePlayer) { ++cpt; while (this._beliefs.environment.Grid[i - cpt][j].State == statePlayer) { ++cpt; } if (this._beliefs.environment.Grid[i - cpt - 1][j].State != stateOtherPlayer) { if (cpt == 3) { score += 1000; } else if (cpt == 2) { score += 100; } else { score += 10; } } cpt = 0; } else if (this._beliefs.environment.Grid[i][j].State == stateOtherPlayer) { ++cpt; while (this._beliefs.environment.Grid[i - cpt][j].State == stateOtherPlayer) { ++cpt; } if (this._beliefs.environment.Grid[i - cpt - 1][j].State != statePlayer) { if (cpt == 3) { score -= 1000; } else if (cpt == 2) { score -= 100; } else { score -= 10; } } cpt = 0; } } // Amelioration à apporter : ne scanne pas les 3 cases à gauche //Lignes if (j < 3) { if (this._beliefs.environment.Grid[i][j].State == statePlayer) { ++cpt; while (this._beliefs.environment.Grid[i][j + cpt].State == statePlayer) { ++cpt; } if (i != 0) { if (this._beliefs.environment.Grid[i][j + cpt + 1].State != stateOtherPlayer) { if (cpt == 3) { score += 1000; } else if (cpt == 2) { score += 100; } else { score += 10; } } } else { if (j != 0) { if (this._beliefs.environment.Grid[i][j + cpt + 1].State != stateOtherPlayer || this._beliefs.environment.Grid[i][j - 1].State != stateOtherPlayer) { if (cpt == 3) { score += 1000; } else if (cpt == 2) { score += 100; } else { score += 10; } } else { if (this._beliefs.environment.Grid[i][j + cpt + 1].State != stateOtherPlayer) { if (cpt == 3) { score += 1000; } else if (cpt == 2) { score += 100; } else { score += 10; } } } } } cpt = 0; } else if (this._beliefs.environment.Grid[i][j].State == stateOtherPlayer) { ++cpt; while (this._beliefs.environment.Grid[i][j + cpt].State == stateOtherPlayer) { ++cpt; } if (i != 0) { if (j + cpt + 1 < 7 && this._beliefs.environment.Grid[i][j + cpt + 1].State != statePlayer) { if (cpt == 3) { score -= 1000; } else if (cpt == 2) { score -= 100; } else { score -= 10; } } } else { if (this._beliefs.environment.Grid[i][j + cpt + 1].State != statePlayer || this._beliefs.environment.Grid[i][j - 1].State != statePlayer) { if (cpt == 3) { score -= 1000; } else if (cpt == 2) { score -= 100; } else { score -= 10; } } } cpt = 0; } } //verif diagonale vers haut droite if (i > 2 && j < 3) { if (this._beliefs.environment.Grid[i][j].State == statePlayer) { ++cpt; while (this._beliefs.environment.Grid[i - cpt][j + cpt].State == statePlayer) { ++cpt; } if (i != 5 && j != 0) { if (this._beliefs.environment.Grid[i - cpt][j + cpt].State != stateOtherPlayer || this._beliefs.environment.Grid[i + 1][j - 1].State != stateOtherPlayer) { if (cpt == 3) { score += 1000; } else if (cpt == 2) { score += 100; } else { score += 10; } } } else { if (j + cpt + 1 < 7 && this._beliefs.environment.Grid[i - cpt][j + cpt].State != stateOtherPlayer) { if (cpt == 3) { score += 1000; } else if (cpt == 2) { score += 100; } else { score += 10; } } } cpt = 0; } else if (this._beliefs.environment.Grid[i][j].State == stateOtherPlayer) { ++cpt; while (this._beliefs.environment.Grid[i - cpt][j + cpt].State == stateOtherPlayer) { ++cpt; } if (i != 5 && j != 0) { if (this._beliefs.environment.Grid[i - cpt][j + cpt].State != statePlayer || this._beliefs.environment.Grid[i + 1][j - 1].State != statePlayer) { if (cpt == 3) { score -= 1000; } else if (cpt == 2) { score -= 100; } else { score -= 10; } } } else { if (this._beliefs.environment.Grid[i - cpt][j + cpt].State != statePlayer) { if (cpt == 3) { score -= 1000; } else if (cpt == 2) { score -= 100; } else { score -= 10; } } } cpt = 0; } } //verif diagonale vers haut gauche if (i > 2 && j > 3) { if (this._beliefs.environment.Grid[i][j].State == statePlayer) { ++cpt; while (this._beliefs.environment.Grid[i - cpt][j - cpt].State == statePlayer) { ++cpt; } if (j != 6 && i != 5) { if (this._beliefs.environment.Grid[i - cpt][j - cpt].State != stateOtherPlayer || this._beliefs.environment.Grid[i + 1][j + 1].State != stateOtherPlayer) { if (cpt == 3) { score += 1000; } else if (cpt == 2) { score += 100; } else { score += 10; } } } else { if (this._beliefs.environment.Grid[i - cpt][j - cpt].State != stateOtherPlayer) { if (cpt == 3) { score += 1000; } else if (cpt == 2) { score += 100; } else { score += 10; } } } cpt = 0; } else if (this._beliefs.environment.Grid[i][j].State == stateOtherPlayer) { ++cpt; while (this._beliefs.environment.Grid[i - cpt][j - cpt].State == stateOtherPlayer) { ++cpt; } if (i != 5 && j != 6) { if (this._beliefs.environment.Grid[i - cpt][j - cpt].State != statePlayer || this._beliefs.environment.Grid[i + 1][j + 1].State != statePlayer) { if (cpt == 3) { score -= 1000; } else if (cpt == 2) { score -= 100; } else { score -= 10; } } } else { if (this._beliefs.environment.Grid[i - cpt][j - cpt].State != statePlayer) { if (cpt == 3) { score -= 1000; } else if (cpt == 2) { score -= 100; } else { score -= 10; } } } cpt = 0; } } } } return(score); }