Skip to content

Commit 2ae4ba1

Browse files
authored
Merge pull request #114 from MichaelMrt/dev
Refactoring and optimization of methods
2 parents c1e8542 + fc6c4e5 commit 2ae4ba1

File tree

14 files changed

+272
-283
lines changed

14 files changed

+272
-283
lines changed

src/logic/bot.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ public function alpha_beta_pruning(Chessboard $chessboard_obj, mixed $chessboard
4545
$best_move = null;
4646

4747
for($i = 0; $i < count($legal_moves); $i++){ // Loop through all legal moves
48-
$current_x = $legal_moves[$i][0];
49-
$current_y = $legal_moves[$i][1];
50-
$move_to_x = $legal_moves[$i][2];
51-
$move_to_y = $legal_moves[$i][3];
52-
$move = $current_x.$current_y.$move_to_x.$move_to_y;
53-
$new_board = $chessboard_obj->test_move($chessboard, $current_x, $current_y, $move_to_x, $move_to_y);
48+
49+
$current_move = $legal_moves[$i];
50+
51+
$new_board = $chessboard_obj->test_move($chessboard, $current_move); // test move
5452
$new_score = $this->evaluate_board($new_board);
5553
$node_data = $this->alpha_beta_pruning($chessboard_obj, $new_board, $depth-1, $new_score,$alpha,$beta, !$isBotMove); // evaluate node
5654
$node_bestscore = $node_data[1];
@@ -59,15 +57,15 @@ public function alpha_beta_pruning(Chessboard $chessboard_obj, mixed $chessboard
5957
if($isBotMove==true){ // Maximize for bot
6058
if($node_bestscore > $max_value){
6159
$max_value = $node_bestscore;
62-
$best_move = $move;
60+
$best_move = $current_move;
6361
}
6462
if($node_bestscore > $alpha){
6563
$alpha = $node_bestscore;
6664
}
6765
}else{ // Minimize for bot
6866
if($node_bestscore < $min_value){
6967
$min_value = $node_bestscore;
70-
$best_move = $move;
68+
$best_move = $current_move;
7169
}
7270
if($node_bestscore < $beta){
7371
$beta = $node_bestscore;

src/logic/chessboard.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,32 +131,32 @@ private function render_square(mixed $piece, string $background_color, string $s
131131

132132
}
133133

134-
public function move(mixed $chessboard, int $current_x, int $current_y, int $move_to_x, int $move_to_y):mixed
134+
public function move(Move $move):mixed
135135
{
136-
$piece = $this->chessboard[$current_x][$current_y];
137-
$piece->update_position($move_to_x,$move_to_y);
136+
$piece = $this->chessboard[$move->from_x][$move->from_y];
137+
$piece->update_position($move->to_x,$move->to_y);
138138
# Copy the piece to the new position
139-
$this->chessboard[$move_to_x][$move_to_y] = $chessboard[$current_x][$current_y];
139+
$this->chessboard[$move->to_x][$move->to_y] = $piece;
140140

141141
# Delete old piece position
142-
$this->chessboard[$current_x][$current_y] = "";
142+
$this->chessboard[$move->from_x][$move->from_y] = "";
143143

144144
return $this->chessboard;
145145
}
146146

147-
public function test_move(mixed $chessboard, int $current_x, int $current_y, int $move_to_x, int $move_to_y):mixed
147+
public function test_move(mixed $chessboard, Move $move):mixed
148148
{
149149
# Copy the piece to the new position
150-
$chessboard[$move_to_x][$move_to_y] = $chessboard[$current_x][$current_y];
150+
$chessboard[$move->to_x][$move->to_y] = $chessboard[$move->from_x][$move->from_y];
151151

152-
$piece = $chessboard[$move_to_x][$move_to_y];
153-
if($this->can_promote($piece, $move_to_y)){
154-
$chessboard[$move_to_x][$move_to_y] = new Queen($piece->get_color(), $move_to_x, $move_to_y);
152+
$piece = $chessboard[$move->to_x][$move->to_y];
153+
if($this->can_promote($piece, $move->to_y)){
154+
$chessboard[$move->to_x][$move->to_y] = new Queen($piece->get_color(), $move->to_x, $move->to_y);
155155
}
156156

157157
# Delete old piece position
158-
$chessboard[$current_x][$current_y] = "";
159-
$chessboard[$current_x][$current_y] = "";
158+
$chessboard[$move->from_x][$move->from_y] = "";
159+
$chessboard[$move->from_x][$move->from_y] = "";
160160
return $chessboard;
161161
}
162162

@@ -184,8 +184,10 @@ public function can_promote(mixed $piece, int $y):bool
184184
return false;
185185
}
186186

187-
public function promote(int $x, int $y, string $color):mixed
187+
public function promote(Move $move, string $color):mixed
188188
{
189+
$x = $move->to_x;
190+
$y = $move->to_y;
189191
$this->chessboard[$x][$y] = new Queen($color, $x, $y);
190192
return $this->chessboard;
191193
}

src/logic/chesspieces/bishop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function __construct(String $color, int $x, int $y)
1919
}
2020
}
2121

22-
function check_move_legal(mixed $chessboard,int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool
22+
function check_move_legal(mixed $chessboard, Move $move):bool
2323
{
24-
if($this->check_legal_bishopmove($chessboard, $current_x, $current_y, $move_to_x, $move_to_y)){
24+
if($this->check_legal_bishopmove($chessboard, $move)){
2525
return true;
2626
}
2727

src/logic/chesspieces/chess_piece.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,18 @@ public function get_has_moved_status():bool
6969
return $this->has_moved;
7070
}
7171

72-
protected function check_target_square(mixed $chessboard,int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool
72+
protected function check_target_square(mixed $chessboard, Move $move):bool
7373
{
74-
if($chessboard[$move_to_x][$move_to_y]==""){
74+
if($chessboard[$move->to_x][$move->to_y]==""){
7575
return true;
7676
}
77-
if($chessboard[$current_x][$current_y]->get_color()!=$chessboard[$move_to_x][$move_to_y]->get_color()){
77+
if($chessboard[$move->from_x][$move->from_y]->get_color()!=$chessboard[$move->to_x][$move->to_y]->get_color()){
7878
return true;
7979
}else{
8080
return false;
8181
}
8282
}
8383
# ---abstract methods---
84-
abstract function check_move_legal(mixed $chessboard, int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool;
84+
abstract function check_move_legal(mixed $chessboard, Move $move):bool;
8585

8686
}

src/logic/chesspieces/king.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ function __construct(String $color, int $x, int $y)
1414
}
1515
}
1616

17-
function check_move_legal(mixed $chessboard, int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool
17+
function check_move_legal(mixed $chessboard, Move $move):bool
1818
{
19-
$distance_x = sqrt(pow(($move_to_x-$current_x),2));
20-
$distance_y = sqrt(pow(($move_to_y-$current_y),2));
19+
$distance_x = sqrt(pow(($move->to_x-$move->from_x),2));
20+
$distance_y = sqrt(pow(($move->to_y-$move->from_y),2));
2121

2222
if($distance_x <= 1 && $distance_y <= 1){
2323
# check if there is a piece on the move to square and if it is opposite color
24-
if($chessboard[$move_to_x][$move_to_y] instanceof ChessPiece && $chessboard[$current_x][$current_y]->get_color()!=$chessboard[$move_to_x][$move_to_y]->get_color()){
24+
if($chessboard[$move->to_x][$move->to_y] instanceof ChessPiece && $chessboard[$move->from_x][$move->from_y]->get_color()!=$chessboard[$move->to_x][$move->to_y]->get_color()){
2525
return true;
26-
}elseif(!$chessboard[$move_to_x][$move_to_y] instanceof ChessPiece){
26+
}elseif(!$chessboard[$move->to_x][$move->to_y] instanceof ChessPiece){
2727
return true;
2828
}
2929
}
3030

3131
if($this->has_moved==false){
3232
# castling short as white
33-
if($this->color=='white' && $move_to_x==7 && $move_to_y==1){
33+
if($this->color=='white' && $move->to_x==7 && $move->to_y==1){
3434
if(!$chessboard[6][1] instanceof ChessPiece && !$chessboard[7][1] instanceof ChessPiece){
3535
if($chessboard[8][1] instanceof Rook && $chessboard[8][1]->has_moved==false){
3636
return true;
@@ -39,7 +39,7 @@ function check_move_legal(mixed $chessboard, int $current_x, int $current_y, int
3939
}
4040

4141
# castling long as white
42-
if($this->color=='white' && $move_to_x==3 && $move_to_y==1){
42+
if($this->color=='white' && $move->to_x==3 && $move->to_y==1){
4343
if(!$chessboard[2][1] instanceof ChessPiece && !$chessboard[3][1] instanceof ChessPiece && !$chessboard[4][1] instanceof ChessPiece){
4444
if($chessboard[1][1] instanceof Rook && $chessboard[1][1]->has_moved==false){
4545
return true;
@@ -48,7 +48,7 @@ function check_move_legal(mixed $chessboard, int $current_x, int $current_y, int
4848
}
4949

5050
# castling short as black
51-
if($this->color=='black' && $move_to_x==7 && $move_to_y==8){
51+
if($this->color=='black' && $move->to_x==7 && $move->to_y==8){
5252
if(!$chessboard[6][8] instanceof ChessPiece && !$chessboard[7][8] instanceof ChessPiece){
5353
if($chessboard[8][8] instanceof Rook && $chessboard[8][8]->has_moved==false){
5454
return true;
@@ -57,7 +57,7 @@ function check_move_legal(mixed $chessboard, int $current_x, int $current_y, int
5757
}
5858

5959
# castling long as black
60-
if($this->color=='black' && $move_to_x==3 && $move_to_y==8){
60+
if($this->color=='black' && $move->to_x==3 && $move->to_y==8){
6161
if(!$chessboard[2][8] instanceof ChessPiece && !$chessboard[3][8] instanceof ChessPiece && !$chessboard[4][8] instanceof ChessPiece){
6262
if($chessboard[8][8] instanceof Rook && $chessboard[8][8]->has_moved==false){
6363
return true;

src/logic/chesspieces/knight.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function __construct(String $color, int $x, int $y)
1414
}
1515
}
1616

17-
function check_move_legal(mixed $chessboard, int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool
17+
function check_move_legal(mixed $chessboard, Move $move):bool
1818
{
1919
# 1-up and 2-right jump
2020
# 1-up and 2-left jump
@@ -26,16 +26,16 @@ function check_move_legal(mixed $chessboard, int $current_x, int $current_y, int
2626
# 2-down and 1-left jump
2727

2828
# check if moving in legal pattern, then check if its moving to a field with a piece and if its opposite colors
29-
if(($move_to_y==$current_y+1 && $move_to_x==$current_x+2)||
30-
$move_to_y==$current_y+1 && $move_to_x==$current_x-2||
31-
$move_to_y==$current_y-1 && $move_to_x==$current_x+2||
32-
$move_to_y==$current_y-1 && $move_to_x==$current_x-2||
33-
$move_to_y==$current_y+2 && $move_to_x==$current_x+1||
34-
$move_to_y==$current_y+2 && $move_to_x==$current_x-1||
35-
$move_to_y==$current_y-2 && $move_to_x==$current_x+1||
36-
$move_to_y==$current_y-2 && $move_to_x==$current_x-1){
37-
if($chessboard[$move_to_x][$move_to_y] instanceof ChessPiece){
38-
if($chessboard[$current_x][$current_y]->get_color()!=$chessboard[$move_to_x][$move_to_y]->get_color()){
29+
if(($move->to_y==$move->from_y+1 && $move->to_x==$move->from_x+2)||
30+
$move->to_y==$move->from_y+1 && $move->to_x==$move->from_x-2||
31+
$move->to_y==$move->from_y-1 && $move->to_x==$move->from_x+2||
32+
$move->to_y==$move->from_y-1 && $move->to_x==$move->from_x-2||
33+
$move->to_y==$move->from_y+2 && $move->to_x==$move->from_x+1||
34+
$move->to_y==$move->from_y+2 && $move->to_x==$move->from_x-1||
35+
$move->to_y==$move->from_y-2 && $move->to_x==$move->from_x+1||
36+
$move->to_y==$move->from_y-2 && $move->to_x==$move->from_x-1){
37+
if($chessboard[$move->to_x][$move->to_y] instanceof ChessPiece){
38+
if($chessboard[$move->from_x][$move->from_y]->get_color()!=$chessboard[$move->to_x][$move->to_y]->get_color()){
3939
return true;
4040
}
4141
}else{

src/logic/chesspieces/pawn.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,43 @@ function __construct(String $color, int $x, int $y)
2121
}
2222

2323

24-
function check_move_legal(mixed $chessboard, int $current_x, int $current_y, int $move_to_x, int $move_to_y): bool
24+
function check_move_legal(mixed $chessboard, Move $move): bool
2525
{
2626

27-
return ($this->check_moving_onesquare_forwards($chessboard, $current_x, $current_y, $move_to_x, $move_to_y) or
28-
$this->check_moving_twosquares_forwards($chessboard, $current_x, $current_y, $move_to_x, $move_to_y) or
29-
$this->check_diagonal_move($chessboard, $current_x, $current_y, $move_to_x, $move_to_y) or
30-
$this->check_enpassant($move_to_x, $move_to_y)) and
31-
$this->check_target_square($chessboard, $current_x, $current_y, $move_to_x, $move_to_y);
27+
return ($this->check_moving_onesquare_forwards($chessboard, $move) or
28+
$this->check_moving_twosquares_forwards($chessboard, $move) or
29+
$this->check_diagonal_move($chessboard, $move) or
30+
$this->check_enpassant($move)) and
31+
$this->check_target_square($chessboard, $move);
3232
}
3333

3434

35-
function check_moving_onesquare_forwards(mixed $chessboard, int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool
35+
function check_moving_onesquare_forwards(mixed $chessboard, Move $move):bool
3636
{
37-
$is_white_move = $this->color == "white" && $current_y + 1 == $move_to_y;
38-
$is_black_move = $this->color == "black" && $current_y - 1 == $move_to_y;
39-
$same_file = $current_x == $move_to_x;
40-
$target_square_empty = $this->target_square_empty($chessboard, $move_to_x, $move_to_y);
37+
$is_white_move = $this->color == "white" && $move->from_y + 1 == $move->to_y;
38+
$is_black_move = $this->color == "black" && $move->from_y - 1 == $move->to_y;
39+
$same_file = $move->from_x == $move->to_x;
40+
$target_square_empty = $this->target_square_empty($chessboard, $move);
4141
return ($is_white_move || $is_black_move) && $same_file && $target_square_empty;
4242
}
4343

4444

45-
function check_moving_twosquares_forwards(mixed $chessboard, int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool
45+
function check_moving_twosquares_forwards(mixed $chessboard, Move $move):bool
4646
{
47-
$is_white_move = $this->color == "white" && $current_y == 2 && $move_to_y == 4 && $chessboard[$current_x][$current_y + 1] == "";
48-
$is_black_move = $this->color == "black" && $current_y == 7 && $move_to_y == 5 && $chessboard[$current_x][$current_y - 1] == "";
49-
$same_file = $current_x == $move_to_x;
50-
$target_square_empty = $this->target_square_empty($chessboard, $move_to_x, $move_to_y);
47+
$is_white_move = $this->color == "white" && $move->from_y == 2 && $move->to_y == 4 && $chessboard[$move->from_x][$move->from_y + 1] == "";
48+
$is_black_move = $this->color == "black" && $move->from_y == 7 && $move->to_y == 5 && $chessboard[$move->from_x][$move->from_y - 1] == "";
49+
$same_file = $move->from_x == $move->to_x;
50+
$target_square_empty = $this->target_square_empty($chessboard, $move);
5151
return ($is_white_move || $is_black_move) && $same_file && $target_square_empty;
5252
}
5353

5454

55-
function check_diagonal_move(mixed $chessboard, int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool
55+
function check_diagonal_move(mixed $chessboard, Move $move):bool
5656
{
57-
$is_takes_move = $chessboard[$move_to_x][$move_to_y] instanceof ChessPiece;
58-
$is_black_move = $this->color == "black" && $current_y - 1 == $move_to_y;
59-
$is_white_move = $this->color == "white" && $current_y + 1 == $move_to_y;
60-
$is_diagonal_move = abs($current_x - $move_to_x) == 1;
57+
$is_takes_move = $chessboard[$move->to_x][$move->to_y] instanceof ChessPiece;
58+
$is_black_move = $this->color == "black" && $move->from_y - 1 == $move->to_y;
59+
$is_white_move = $this->color == "white" && $move->from_y + 1 == $move->to_y;
60+
$is_diagonal_move = abs($move->from_x - $move->to_x) == 1;
6161
return $is_takes_move && $is_diagonal_move && ($is_black_move || $is_white_move);
6262
}
6363

@@ -89,27 +89,27 @@ function get_enpassant_right_possible():bool
8989
return $this->enpassant_right_possible;
9090
}
9191

92-
function check_enpassant(int $move_to_x, int $move_to_y):bool
92+
function check_enpassant(Move $move):bool
9393
{
9494
if($this->color == "white"){
9595
if($this->enpassant_left_possible){
96-
if($move_to_x == $this->x-1 && $move_to_y == $this->y+1){
96+
if($move->to_x== $this->x-1 && $move->to_y == $this->y+1){
9797
return true;
9898
}
9999
}
100100
if($this->enpassant_right_possible){
101-
if($move_to_x == $this->x+1 && $move_to_y == $this->y+1){
101+
if($move->to_x== $this->x+1 && $move->to_y == $this->y+1){
102102
return true;
103103
}
104104
}
105105
}else{
106106
if($this->enpassant_left_possible){
107-
if($move_to_x == $this->x-1 && $move_to_y == $this->y-1){
107+
if($move->to_x== $this->x-1 && $move->to_y == $this->y-1){
108108
return true;
109109
}
110110
}
111111
if($this->enpassant_right_possible){
112-
if($move_to_x == $this->x+1 && $move_to_y == $this->y-1){
112+
if($move->to_x== $this->x+1 && $move->to_y == $this->y-1){
113113
return true;
114114
}
115115
}
@@ -118,9 +118,9 @@ function check_enpassant(int $move_to_x, int $move_to_y):bool
118118
}
119119

120120

121-
function target_square_empty(mixed $chessboard, int $move_to_x, int $move_to_y): bool
121+
function target_square_empty(mixed $chessboard, Move $move): bool
122122
{
123-
if ($chessboard[$move_to_x][$move_to_y] == "") {
123+
if ($chessboard[$move->to_x][$move->to_y] == "") {
124124
return true;
125125
}else{
126126
return false;

src/logic/chesspieces/queen.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function __construct(String $color, int $x, int $y)
1919
}
2020
}
2121

22-
function check_move_legal(mixed $chessboard,int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool
22+
function check_move_legal(mixed $chessboard, Move $move):bool
2323
{
24-
if($this->check_legal_rookmove($chessboard, $current_x, $current_y, $move_to_x, $move_to_y) || $this->check_legal_bishopmove($chessboard, $current_x, $current_y, $move_to_x, $move_to_y)){
24+
if($this->check_legal_rookmove($chessboard, $move) || $this->check_legal_bishopmove($chessboard, $move)){
2525
return true;
2626
}
2727
$_SESSION['error'] = "<p class='error'>queens can't move like that</p>";

src/logic/chesspieces/rook.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ function __construct(String $color, int $x, int $y)
1717
}
1818
}
1919

20-
function check_move_legal(mixed $chessboard,int $current_x, int $current_y, int $move_to_x, int $move_to_y):bool
20+
function check_move_legal(mixed $chessboard, Move $move):bool
2121
{
22-
if($this->check_legal_rookmove($chessboard, $current_x,$current_y,$move_to_x,$move_to_y)){
22+
if($this->check_legal_rookmove($chessboard, $move)){
2323
return true;
2424
}
2525

0 commit comments

Comments
 (0)