public State(State Parents) { AvailablePosition[0] = new List<int>(); AvailablePosition[1] = new List<int>(); this.Parent = Parents; ChildGenerated = -1; Child = new List<State>(); for (int i = 0; i < 8; i++) { this.Board[i] = new int[8]; for (int j = 0; j < 8; j++) { this.Board[i][j] = Parents.Board[i][j]; } } EstValue = -5800; this.NextPlayer = 7 - Parents.NextPlayer; }
private void EnemyMatch(int x,int y) { bool WrongMove = false; if (Current.AvailablePosition[0].Count == 0) { //MessageBox.Show("You do not have move!"); Current = Current.Child[0];//No chess to go; return; } for (int i = 0; i < Current.AvailablePosition[0].Count; i++) { if (Current.AvailablePosition[0][i] == x && Current.AvailablePosition[1][i] == y) { Current = Current.Child[i + 1];//The current is updated to the enemy's finished state return; } } if (WrongMove == false)//The enemy make wrong move; { MessageBox.Show("Your input is invalid, my victory!"); if (Current.AvailablePosition[0].Count == 0) { Current = Current.Child[0];//No chess to go; return; } else { Current = Current.Child[1];//The current is updated to the enemy's finished state return; } } return; }
public int Search(State aState, int depth, int pass) { int best_value = -5000; int best_value2 = 5000; if (depth == 0) { aState.EstValue = (int)evaluation(aState); return aState.EstValue; } for (int i = 1; i < aState.AvailablePosition[0].Count+1; i++) { int value=Search(aState.Child[i],depth-1,0);//Search the child if (aState.NextPlayer == Enemy) { best_value = GetLarger(best_value, value); aState.EstValue = best_value; } else { best_value2 = GetLess(best_value2, value); aState.EstValue = best_value2; } } if (aState.AvailablePosition[0].Count == 0)//No step to move next; { best_value = Search(aState.Child[0], depth - 1, 0); aState.EstValue = best_value; } return aState.EstValue; }
public int ProcessNext(ref State Current, State aState, int total) { int x = total / 8; int y = total % 8; //int y = Int32.Parse(textBox2.Text); int AssumeChild = 0; int nextMove = 0; aState.GenerateToAllChild(); Current=EnemyMatch(Current,aState,x,y);//global "Current" is the enemy's state now, update enemy state's board; //AddTwoLevelChild(Current,0);//Initialzie the states which self may put, the available position is also calculated AssumeChild = NextStep(Current); if (AssumeChild != 0) { nextMove = Current.AvailablePosition[0][AssumeChild - 1] * 8 + Current.AvailablePosition[1][AssumeChild - 1]; } else { MessageBox.Show("I have no way to go"); nextMove = 64; } Current.DisposeAllButOne(AssumeChild); Current = Current.Child[AssumeChild];//update the current child; return nextMove; }
public int NextStep(State aState) { int alpha =99600; int beta = -99600; int TopVal = alpha_beta(aState, ref alpha, ref beta, SearchLeve, 0); //int TopVal = Search(aState, SearchLeve, 0); for (int i = 1; i < aState.AvailablePosition[0].Count + 1; i++) { if (aState.Child[i].EstValue == TopVal) { return i; } } return 0; }
public static int NextStep(State CurrentState) { if (CurrentState.Child.Count == 1) return 0;//no next step to go else { int CurrentMaxScore = 5000; int BestChild = -1; for (int i = 1; i < CurrentState.Child.Count; i++) { int MaxScoreGet = GetMaxScore(CurrentState.Child[i]); if (MaxScoreGet < CurrentMaxScore)//better because enemy get smaller score { BestChild = i; } else if (MaxScoreGet == CurrentMaxScore)//Two steps will result in enemy get same max score { if (Alg_1.GetScore(CurrentState.NextPlayer, CurrentState.Child[i].Board) > Alg_1.GetScore(CurrentState.NextPlayer, CurrentState.Child[BestChild].Board))//Better if it self get higher score { BestChild = i; } } } return BestChild; } }
private void Reset2() { counter = 0; Steps = 4; Start = new State(); MyAlg_2 = new Alg_2(); Start.Board[3][3] = 0; Start.Board[4][3] = 7; Start.Board[4][4] = 0; Start.Board[3][4] = 7; if (CurrentPlayer == PlayerRole.White) Start.NextPlayer = 7; else Start.NextPlayer = 7; Rules.getAvailableList(Start.NextPlayer, Start.Board, Start.AvailablePosition); Current = Start; TempCurrent = Start; UpdateChessBoard(Current); }
private void ProcessNext() { int total = Int32.Parse(textBox1.Text); int x = total / 8; int y = total % 8; //int y = Int32.Parse(textBox2.Text); Steps+=2; int AssumeChild = 0; int nextMove = 0; if (Steps != 4) { EnemyMatch(x, y);//global "Current" is the enemy's state now, update enemy state's board; } Current.GenerateChild();//Initialzie the states which self may put, the available position is also calculated if (Current.AvailablePosition[0].Count != 0) { for (int i = 1; i < Current.AvailablePosition[0].Count + 1; i++) { Current.Child[i].GenerateChild();//Initialize all the enemy's state after each self state; } AssumeChild = Alg_1.NextStep(Current); nextMove = Current.AvailablePosition[0][AssumeChild - 1] * 8 + Current.AvailablePosition[1][AssumeChild - 1]; } else { Current.Child[0].GenerateChild(); AssumeChild = 0; nextMove = 64; MessageBox.Show("I have no way to go"); //Do not need to update the state; } Current = Current.Child[AssumeChild]; NextStateGet = false; UpdateChessBoard(Current); }
public double evaluation(State aState) { int step = Rules.countStep(aState.Board); double positionScore = (double)GetPositionScore(aState); double score = 0.0; double stableEnemy = (double)(stableCheck.stableCount(aState.Board, Enemy)); double stableMe = (double)(stableCheck.stableCount(aState.Board, 7 - Enemy)); double stable = stableEnemy - stableMe; double mobility = -(double)(aState.AvailablePosition[0].Count); double number = (double)(Rules.countMyself(aState.Board, Enemy)); if (step <= 8) { score = 1.0 * mobility + 2.0 * stable - 0.5 * number + 1.0 * positionScore; } else if (step >= 8 && step <= 48) { score = 2.0 * mobility + 8.0 * stable - 0.5 * number + 4.0 * positionScore; } else if (step >= 48 && step <= 64) { score = 1.0 * mobility + 3.0 * stable + 0.8 * number + 1.0 * positionScore; } return score; /* int[][] Corner = new int[8][]; for (int i = 0; i < 8; i++) { Corner[i] = new int[8]; for (int j = 0; j < 8; j++) { Corner[i][j] = 0; } } Corner[0][0] = 8; Corner[0][1] = -4; Corner[1][0] = -4; Corner[1][1] = -6; Corner[0][7] = 8; Corner[1][7] = -4; Corner[0][6] = -4; Corner[1][6] = -6; Corner[7][0] = 8; Corner[6][0] = -4; Corner[7][1] = -4; Corner[6][1] = -6; Corner[7][7] = 8; Corner[7][6] = -4; Corner[6][7] = -4; Corner[6][6] = -6; */ }
public State EnemyMatch(State Current, State aState, int x, int y) { State Current0; bool WrongMove = false; if (aState.AvailablePosition[0].Count == 0) { MessageBox.Show("You do not have move!"); Current0 = aState.Child[0];//No chess to go; return Current0; } for (int i = 0; i < aState.AvailablePosition[0].Count; i++) { if (aState.AvailablePosition[0][i] == x && aState.AvailablePosition[1][i] == y) { Current0 = aState.Child[i + 1];//The current is updated to the enemy's finished state aState.DisposeAllButOne(i + 1); return Current0; } } if (WrongMove == false)//The enemy make wrong move; { MessageBox.Show("Wrong Move! Go next Available!"); if (aState.AvailablePosition[0].Count == 0) { Current0 = aState.Child[0];//No chess to go; return Current0; } else { aState.DisposeAllButOne(1); Current0 = aState.Child[1];//The current is updated to the enemy's finished state return Current0; } } Current0 = aState; return Current0; }
public int alpha_beta(State aState, ref int alpha, ref int beta, int depth, int passed) { int alpha_img=alpha; int beta_img=beta; int best_value1 = -50000; int best_value2 = 50000; if (depth == 0) { aState.EstValue = (int)evaluation(aState); return aState.EstValue; } for (int i = 1; i < aState.AvailablePosition[0].Count + 1; i++) { aState.GenerateSingle(i);//if this child has been generated, the function won't generate it more int value = alpha_beta(aState.Child[i], ref alpha_img, ref beta_img, depth - 1, 0); if (aState.NextPlayer != Enemy)//get min { if (value < best_value2)//update best_value2 { best_value2 = value; } if (best_value2 < beta )//pruning { aState.EstValue = best_value2; return best_value2; } } else//get max { if (value > best_value1) { best_value1 = value; } if (best_value1 > beta)//pruning { aState.EstValue = best_value1; return best_value1; } } } if (aState.AvailablePosition[0].Count == 0)//No step to move next; { aState.GenerateSingle(0); int value = alpha_beta(aState.Child[0], ref beta_img, ref alpha_img, depth - 1, 0); if (aState.NextPlayer != Enemy) { best_value2 = value; aState.EstValue = best_value2; } else { best_value1 = value; aState.EstValue=best_value1; } } //Not pruning, change alpha and beta; if (aState.NextPlayer != Enemy)//get min { beta = best_value2; aState.EstValue = beta; return beta; } else { beta = best_value1; aState.EstValue = beta; return beta; } }
public void AddTwoLevelChild(State aState, int CurrentLevel) { if (CurrentLevel == SearchLeve - 2) { aState.GenerateChild(); Numberr1++; if (aState.AvailablePosition[0].Count != 0) { for (int i = 1; i < aState.AvailablePosition[0].Count + 1; i++) { aState.Child[i].GenerateChild(); Numberr2++; Numberr3 += aState.Child[i].AvailablePosition[0].Count; } } else//No step to go; { aState.Child[0].GenerateChild(); Numberr2++; } return; } else { if (aState.AvailablePosition[0].Count != 0) { for (int i = 1; i < aState.AvailablePosition[0].Count + 1; i++) { AddTwoLevelChild(aState.Child[i], CurrentLevel + 1); } } else//No step to go; { AddTwoLevelChild(aState.Child[0], CurrentLevel + 1); } } }
public void Disposing(State aState) { if (aState.Child.Count != 0) { for (int i = 0; i < aState.Child.Count; i++) { Disposing(aState.Child[i]); aState.Child[i] = null; } } }
private int Play(int numReceived) { NextStateGet = true; int AssumeChild = 0 ; int nextMove = 0; int total = numReceived; int x = total / 8; int y = total % 8; //int y = Int32.Parse(textBox2.Text); Steps+=2; if (Steps != 4) { EnemyMatch(x, y);//global "Current" is the enemy's state now, update enemy state's board; } Current.GenerateChild();//Initialzie the states which self may put, the available position is also calculated if (Current.AvailablePosition[0].Count != 0) { for (int i = 1; i < Current.AvailablePosition[0].Count + 1; i++) { Current.Child[i].GenerateChild();//Initialize all the enemy's state after each self state; } AssumeChild = Alg_1.NextStep(Current); nextMove = Current.AvailablePosition[0][AssumeChild - 1] * 8 + Current.AvailablePosition[1][AssumeChild - 1]; } else { Current.Child[0].GenerateChild(); AssumeChild = 0; nextMove = 64; } Current = Current.Child[AssumeChild]; NextStateGet = false; UpdateChessBoard(Current); return nextMove; }
public void generateLevelChild(State aState,int currentLevel ) { if (currentLevel == SearchLeve-1) return; aState.GenerateChild(); if (aState.AvailablePosition[0].Count != 0) { for (int i = 1; i < aState.AvailablePosition[0].Count + 1; i++) { generateLevelChild(aState.Child[i], currentLevel + 1); } } else//No step to go; { generateLevelChild(aState.Child[0], currentLevel + 1); } }
private int Play2(int totalNum) { int nextMove = 0; nextMove = MyAlg_2.ProcessNext(ref TempCurrent, Current, totalNum);//Go next 2 steps; Current = TempCurrent;//To avoid dynamic updating of "Current" UpdateChessBoard(Current); return nextMove; }
public int GetPositionScore(State aState) { aState.PositionScore7 = 0; aState.PositionScore0 = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (aState.Board[i][j] == Enemy) { aState.PositionScore7 += scoreList[i][j]; } } } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (aState.Board[i][j] == 7 - Enemy) { aState.PositionScore0 += scoreList[i][j]; } } } return aState.PositionScore7 - aState.PositionScore0; }
private int ProcessNext2() { int nextMove = 0; int totalNum = Int32.Parse(textBox1.Text); nextMove = MyAlg_2.ProcessNext(ref TempCurrent, Current, totalNum);//Go next 2 steps; Current = TempCurrent;//To avoid dynamic updating of "Current" UpdateChessBoard(Current); return nextMove; }
public void InitTree(State Current, State aState) { Current = aState; generateLevelChild(Current, 0); }
private void UpdateChessBoard(State ShowState) { BlackNumb = 0; WhiteNumb = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (ShowState.Board[i][j] == -1) { listC[i * 8 + j].Visible = false; } else if (ShowState.Board[i][j] == 0) { listC[i * 8 + j].Visible = true; listC[i * 8 + j].ChessColor = ChessItem.cColor.white; listC[i * 8 + j].Debugmode = false; WhiteNumb++; } else { listC[i * 8 + j].Visible = true; listC[i * 8 + j].ChessColor = ChessItem.cColor.black; listC[i * 8 + j].Debugmode = false; BlackNumb++; } } } for (int i = 0; i < ShowState.AvailablePosition[0].Count; i++) { if (ShowState.NextPlayer == 0) { listC[(ShowState.AvailablePosition[0][i]) * 8 + ShowState.AvailablePosition[1][i]].Visible = true; listC[(ShowState.AvailablePosition[0][i]) * 8 + ShowState.AvailablePosition[1][i]].ChessColor = ChessItem.cColor.white; listC[(ShowState.AvailablePosition[0][i]) * 8 + ShowState.AvailablePosition[1][i]].Debugmode = true; } else { listC[(ShowState.AvailablePosition[0][i]) * 8 + ShowState.AvailablePosition[1][i]].Visible = true; listC[(ShowState.AvailablePosition[0][i]) * 8 + ShowState.AvailablePosition[1][i]].ChessColor = ChessItem.cColor.black; listC[(ShowState.AvailablePosition[0][i]) * 8 + ShowState.AvailablePosition[1][i]].Debugmode = true; } } BlackNumberLabel.Text = BlackNumb.ToString(); WhiteNumberLabel.Text = WhiteNumb.ToString(); BlackNumberLabel_Copy.Text = BlackNumb.ToString(); WhiteNumberLabel_Copy.Text = WhiteNumb.ToString(); System.Windows.Media.Animation.Storyboard sdb = (System.Windows.Media.Animation.Storyboard)FindResource("ScoreUpdate"); sdb.Begin(); if (BlackNumb > WhiteNumb) { string packUri = "pack://application:,,,/CS541%20Final%20Project;component/Images/Smile.jpg"; image1.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource; } else if (BlackNumb == WhiteNumb) { string packUri = "pack://application:,,,/CS541%20Final%20Project;component/Images/curious.jpg"; image1.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource; } else { string packUri = "pack://application:,,,/CS541%20Final%20Project;component/Images/Worry.jpg"; image1.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource; } AllowUIToUpdate(); }
public static int GetMaxScore(State PossibleState) { if (PossibleState.Child.Count == 1) return -20;//The enemy does not have any step to go else { int MaxScore = -50; for (int i = 1; i < PossibleState.Child.Count; i++) { int ScoreGet=GetScore(PossibleState.NextPlayer,PossibleState.Board); if (ScoreGet > MaxScore) { MaxScore = ScoreGet; } } return MaxScore; } }