public void button_Click(object sender, EventArgs e) { Button b = (Button)sender; if (!computer_is_thinking && b.BackgroundImage == null) { Move_turn(ref b); if (radioButtonHumanComputer.Checked == true) { computer_is_thinking = true; labelStatus.Text = "Computer is thinking..."; //MessageBox.Show("calculatorul reflecta asupra situatiei..."); Position pos = new Position((short)(h), ref buttonArray); AlphaBetaStruct strct = new AlphaBetaStruct(); strct = Alpha_Beta(pos, DEPTH_MAX, -INF, +INF, true); MessageBox.Show("Calculatorul va muta la pozitia (" + strct.NextMoveToDo.x + "," + strct.NextMoveToDo.y + "), pentru ca acoro are valoarea " + strct.Evaluation); if (strct.NextMoveToDo.x < 0 || strct.NextMoveToDo.y < 0) { return; } Move_turn(ref buttonArray[strct.NextMoveToDo.x][strct.NextMoveToDo.y]); labelStatus.Text = "Yourn turn"; computer_is_thinking = false; Position pstn = new Position(h, ref buttonArray); String tabla = new String('a', 2); tabla = ""; for (short i = 0; i <= h + 1; ++i) { for (short j = 0; j <= h + 1; j++) { tabla = tabla + pstn.gameTable[i][j] + " "; } tabla += "\n"; } short evl; evl = evaluation_of_position(pstn, false); MessageBox.Show("Tabla este:\n\n\n" + tabla + "\n\n\n The evaluation is + " + evl); } } }
AlphaBetaStruct Alpha_Beta(Position pos, short depth, short alpha, short beta, bool MaximizingPlayer) { if (depth == -1) { MessageBox.Show("depth a ajuns sa fie -1"); } AlphaBetaStruct res_pair = new AlphaBetaStruct(); AlphaBetaStruct child_pair = new AlphaBetaStruct(); res_pair.Evaluation = 0; res_pair.NextMoveToDo.x = 0; res_pair.NextMoveToDo.y = 0; short v_o_d = Victory_or_Draw(ref pos, false); if (v_o_d > 0) { res_pair.NextMoveToDo.x = -1; res_pair.NextMoveToDo.y = -1; //I don't think about the next move now if (!(v_o_d == 2)) //someone won { if (MaximizingPlayer) //that's me :) { res_pair.Evaluation = INF; } else { res_pair.Evaluation = -INF;//that's the opponent :'( } } else { res_pair.Evaluation = 0; } return(res_pair); } if (depth == 0) { res_pair.NextMoveToDo.x = -1; res_pair.NextMoveToDo.y = -1;//I don't think about the next move now res_pair.Evaluation = evaluation_of_position(pos, !MaximizingPlayer); return(res_pair); } if (MaximizingPlayer) { res_pair.Evaluation = -INF - 1; res_pair.NextMoveToDo.x = -2; res_pair.NextMoveToDo.y = -2; //I will consider each child of the current position Position child_pos = new Position((short)h, ref pos); bool stop = false, little_flag = false, big_flag = false; short i = 1, j = 1; for (i = 1; i <= h && !stop; ++i) { for (j = 1; j <= h && !stop; ++j) { big_flag = true; if (pos.gameTable[i][j] == -1)//empty cell { little_flag = true; //MessageBox.Show("(" + i + "," + j + ") is an empty cell"); ///child_pos.gameTable[i][j] = my_color; //MessageBox.Show("Acum patratelele sunt " + child_pos.nr_patratele_ocupate); child_pos.Atribuie(h, i, j, my_color); //MessageBox.Show("Acum, dupa atribuire, patratelele sunt " + child_pos.nr_patratele_ocupate); if (child_pos.nr_patratele_ocupate > h * h) { MessageBox.Show("nrpatrateleocupate = " + child_pos.nr_patratele_ocupate + "i=" + i + "j=" + j); } //call the function: child_pair = Alpha_Beta(child_pos, (short)(depth - 1), alpha, beta, false); if (child_pair.Evaluation > res_pair.Evaluation) { res_pair.Evaluation = child_pair.Evaluation; res_pair.NextMoveToDo.x = i; res_pair.NextMoveToDo.y = j; } /*else * if(child_pair.Evaluation == res_pair.Evaluation) * { * Random rnd = new Random(); * short nmb = (short)rnd.Next(0, 8); * if(nmb==1) * { * res_pair.Evaluation = child_pair.Evaluation; * res_pair.NextMoveToDo.x = i; * res_pair.NextMoveToDo.y = j; * } * } */ if (child_pair.Evaluation > alpha) { alpha = child_pair.Evaluation; } if (beta <= alpha) { stop = true; } //reset the position for the next child child_pos.Atribuie(h, i, j, -1); } } } if (res_pair.NextMoveToDo.x < 0 || res_pair.NextMoveToDo.y < 0) { MessageBox.Show("Error in AlphaBeta algorithm, part1!\n x=" + res_pair.NextMoveToDo.x + ", y=" + res_pair.NextMoveToDo.y + " i=" + i + " ,j=" + j); MessageBox.Show("res_pair.Evaluation=" + res_pair.Evaluation + ", alpha=" + alpha + ", beta=" + beta + " ,child_pair.Evaluation=" + child_pair.Evaluation + " ,little_flag=" + little_flag + " ,big_flag=" + big_flag + " ,depth=" + depth + " ,nr_patratele_ocupate=" + pos.nr_patratele_ocupate); AfisareGameTable(pos); } return(res_pair); } //else --> minimizing player res_pair.Evaluation = +INF + 1; res_pair.NextMoveToDo.x = -1; res_pair.NextMoveToDo.y = -1; //I will consider each child of the current position Position child = new Position((short)(h), ref pos); bool stopp = false; for (short i = 1; i <= h && !stopp; i++) { for (short j = 1; j <= h && !stopp; ++j) { if (pos.gameTable[i][j] == -1)//empty cell { if (my_color == 1) { child.Atribuie(h, i, j, 2); } else { child.Atribuie(h, i, j, 1); } //call the function: child_pair = Alpha_Beta(child, (short)(depth - 1), alpha, beta, true); if (child_pair.Evaluation <= res_pair.Evaluation) { res_pair.Evaluation = child_pair.Evaluation; res_pair.NextMoveToDo.x = i; res_pair.NextMoveToDo.y = j; } /*else * if (child_pair.Evaluation == res_pair.Evaluation) * { * Random rnd = new Random(); * short nmb = (short)rnd.Next(0, 8); * if (nmb == 1) * { * res_pair.Evaluation = child_pair.Evaluation; * res_pair.NextMoveToDo.x = i; * res_pair.NextMoveToDo.y = j; * } * } */ if (child_pair.Evaluation <= beta) { res_pair.Evaluation = child_pair.Evaluation; res_pair.NextMoveToDo.x = i; res_pair.NextMoveToDo.y = j; beta = child_pair.Evaluation; } if (beta <= alpha) { stopp = true; } //reset the position for the next child child.Atribuie(h, i, j, -1); } } } if (res_pair.NextMoveToDo.x < 0 || res_pair.NextMoveToDo.y < 0) { MessageBox.Show("Error in AlphaBeta algorithm, part2!\n x=" + res_pair.NextMoveToDo.x + ", y=" + res_pair.NextMoveToDo.y); } return(res_pair); }