public SearchResult simpleSearch(Panel[,] board, Player player, int depth) { if (depth <= 0 || isEndState(board)) { return new SearchResult(null, player.Evaluate(board)); } else { List<Point> possible_moves = Explore(board, player.Color); SearchResult best = new SearchResult(null, Int16.MinValue); if (possible_moves.Capacity == 0) { possible_moves = Explore(board, player.OpponentColor); if (possible_moves.Capacity == 0) { switch (Math.Sign(PlayersDiffCount(board, player))) { case -1: best = new SearchResult(null, Int16.MinValue); break; case 0: best = new SearchResult(null, 0); break; case 1: best = new SearchResult(null, Int16.MaxValue); break; } } else { best = simpleSearch(board, player.Opponent, depth - 1).negate(); } } else { foreach (Point p in possible_moves) { //przepisanie Panel zeby nie zmieniac wartosci na oryginale subBoard = new Panel[tileCount, tileCount]; for (int i = 0; i < tileCount; i++) for (int j = 0; j < tileCount; j++) { subBoard[i, j] = new Panel(); subBoard[i, j].Bounds = board[i, j].Bounds; subBoard[i, j].Tag = board[i, j].Tag; } makeMove(ref subBoard, p, player.Color); int score = simpleSearch(subBoard, player.Opponent, depth - 1).negate().Score; if (best.Score < score) { best = new SearchResult(p, score); } } } return best; } }
public SearchResult Search(Panel[,] board, Player player, int alpha, int beta, int depth) { if (depth <= 0 || isEndState(board)) { return new SearchResult(null, player.Evaluate(board)); } else /* wiecej do sprawdzenia*/ { List<Point> possible_moves = Explore(board, player.Color); SearchResult best = new SearchResult(null, alpha); if (possible_moves.Capacity == 0) { /* turn is lost - chceck next player*/ possible_moves = Explore(board, player.OpponentColor); if (possible_moves.Capacity == 0) { //koniec gry - psrawdzenie czy jest zwyciezca switch (Math.Sign(PlayersDiffCount(board, player))) { case -1: best = new SearchResult(null, Int16.MinValue); break; case 0: best = new SearchResult(null, 0); break; case 1: best = new SearchResult(null, Int16.MaxValue); break; } } else { /* gra trwa nadal - brak ruchow do sprawdzenia*/ best = Search(board, player.Opponent, -beta, -alpha, depth - 1).negate(); } } else {//spardzenie wyniku dla kazdego ruchu na liscie foreach (Point p in possible_moves) { //przepisanie Panel zeby nie zmieniac wartosci na oryginale subBoard = new Panel[tileCount, tileCount]; for(int i=0;i<tileCount;i++) for (int j = 0; j < tileCount; j++) { subBoard[i, j] = new Panel(); subBoard[i, j].Bounds = board[i, j].Bounds; subBoard[i, j].Tag = board[i, j].Tag; } makeMove(ref subBoard, p, player.Color); int score = Search( subBoard, player.Opponent, -beta, -alpha, depth - 1).negate().Score; if (alpha < score) { alpha = score; best = new SearchResult(p, score); } /**alpha beta prunning**/ if (alpha >= beta) { return best; } } } return best; } }