Skip to content

Commit 7a09b48

Browse files
author
David Kartik
committed
Fixing up some comments.
1 parent c1c2db2 commit 7a09b48

File tree

7 files changed

+96
-12
lines changed

7 files changed

+96
-12
lines changed

pieces/bishop.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ function __construct(Board $b, Colour $c, Square $s) {
1313
* Name: moveAllowed
1414
* Parameters: Square $s: The Square that the piece is attempting to move to.
1515
* Remarks:
16-
* - A delta of multiples of 9 is the bishop moving diagonally from top left to bottom right (or vice versa)
17-
* - A delta of multiples of 7 is the bishop moving diagonally from top right to bottom left (or vice versa)
16+
* - A delta of multiples of 9 is the bishop moving diagonally between top left and bottom right
17+
* - A delta of multiples of 7 is the bishop moving diagonally between top right and bottom left
1818
*/
1919
protected function moveAllowed(Square $s) {
2020
//Get the position difference between the current square and the desired square
@@ -32,14 +32,14 @@ protected function moveAllowed(Square $s) {
3232
return MoveType::ILLEGAL;
3333
}
3434

35-
// Moving from top left to bottom right
35+
// Moving diagonally between top left and bottom right
3636
if(abs($delta) % 9 == 0) {
3737
if($this->board->spacesBetweenEmpty($this->location->index, $delta / 9, 9, $direction)) {
3838
return MoveType::NORMAL;
3939
}
4040
}
4141

42-
// Moving from top right to bottom left
42+
// Moving diagonally between top right and bottom left
4343
if(abs($delta) % 7 == 0) {
4444
if($this->spacesBetweenEmpty($this->location->index, $delta / 7, 7, $direction)) {
4545
return MoveType::NORMAL;

pieces/king.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ protected function moveAllowed(Square $s) {
3131
if((abs($delta) == 1) || (abs($delta) == 7) || (abs($delta) == 9) || (abs($delta) == 8)){
3232
return MoveType::NORMAL;
3333
}
34+
35+
//If the above cases do not satisfy, move is illegal
3436
return MoveType::ILLEGAL;
3537
}
3638
}

pieces/knight.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ function __construct(Board $b, Colour $c, Square $s) {
1919
* - A delta of 17 is the Knight moving up 2 left 1 or down 2 right 1
2020
*/
2121
protected function moveAllowed(Square $s) {
22+
//Get the position difference between the current square and the desired square
2223
$delta = $s->index - $this->location->index;
24+
25+
//A piece may not be able to move to another square with a piece of the same colour
2326
if($this-board->hasSameColourPiece($s, $this->colour)) {
2427
return MoveType::ILLEGAL;
2528
}
2629

30+
//Check for a normal move
2731
if((abs($delta) == 6) || (abs($delta) == 10) || (abs($delta) == 15) || (abs($delta) == 17)){
2832
return MoveType::NORMAL;
2933
}
34+
35+
//If the above cases do not satisfy, move is illegal
3036
return MoveType::ILLEGAL;
3137
}
3238
}

pieces/pawn.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,61 @@
44
* Extends: Piece
55
*/
66
class Pawn extends Piece {
7+
78
private $direction;
89

10+
function __construct(Board $b, Colour $c, Square $s) {
11+
parent::__construct($b, $c, $s);
12+
if($c == Colour::WHITE)
13+
$this->direction = +1;
14+
else
15+
$this->direction = -1;
16+
}
17+
18+
/*
19+
* Name: moveAllowed
20+
* Parameters: Square $s: The Square that the piece is attempting to move to.
21+
* Remarks:
22+
* - A delta of 8 is the Pawn to the next row
23+
* - A delta of 16 is the Pawn opening with a doublestep (2 rows)
24+
* - A delta of 13 or 9 is the Pawn attacking diagonally
25+
*/
926
protected function moveAllowed(Square $s) {
27+
//Get the position difference between the current square and the desired square
1028
$delta = $s->index - $this->location->index;
29+
1130
//Check for a normal move
1231
if(($delta == ($this->direction * 8)) && (!$this->board->hasPiece($s))) {
1332
return MoveType::NORMAL;
1433
}
34+
1535
//Check for the double step
1636
if(($delta == ($this->direction * 16)) && (!$this->board->hasPiece($s)) && ($this->moveCount == 0)) {
1737
return MoveType::DOUBLESTEP;
1838
}
39+
1940
//Check for a normal attack
2041
if((($delta == ($this->direction * 13)) || ($delta == ($this->direction * 9))) && ($this-board->hasOpposingPiece($s, $this->colour)))
2142
{
2243
return MoveType::NORMAL;
2344
}
45+
2446
//Check for En Passant
2547
if($this->board->isEnPassant($s, $this->location))
2648
{
2749
return MoveType::ENPASSANT;
2850
}
51+
52+
//If the above cases do not satisfy, move is illegal
2953
return MoveType::ILLEGAL;
3054
}
3155

32-
function __construct(Board $b, Colour $c, Square $s) {
33-
parent::__construct($b, $c, $s);
34-
if($c == Colour::WHITE)
35-
$this->direction = +1;
36-
else
37-
$this->direction = -1;
38-
}
39-
56+
/*
57+
* Name: tryMove
58+
* Parameters: Square $s: The Square that the piece is attempting to move to.
59+
* Description: This method will attempt to move the piece on the board
60+
* Overrides method on parent class in order to consider the special En Passant move
61+
*/
4062
function tryMove(Square $s) {
4163
$mt = $this->moveAllowed($s);
4264
if($mt != MoveType::ILLEGAL) {

pieces/queen.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,72 @@
11
<?php
2+
/*
3+
* Class Name: Queen
4+
* Extends: Piece
5+
*/
26
class Queen extends Piece {
37

48
function __construct(Board $b, Colour $c, Square $s) {
59
parent::__construct($b, $c, $s);
610
}
711

12+
/*
13+
* Name: moveAllowed
14+
* Parameters: Square $s: The Square that the piece is attempting to move to.
15+
* Remarks:
16+
* - A delta of multiples of 9 is the Queen moving diagonally between top left and bottom right
17+
* - A delta of multiples of 7 is the Queen moving diagonally between top right and bottom left
18+
* - Queen can also move by staying on the same row or same column
19+
*/
820
protected function moveAllowed(Square $s) {
921
$newColumn = $s->column;
1022
$newRow = $s->row;
23+
24+
//Get the position difference between the current square and the desired square
1125
$delta = $s->index - $this->location->index;
26+
27+
//Determine which direction the piece is moving, up or down the array
1228
if($delta > 0) {
1329
$direction = 1;
1430
} else {
1531
$direction = -1;
1632
}
1733

34+
//A piece may not be able to move to another square with a piece of the same colour
1835
if($this-board->hasSameColourPiece($s, $this->colour)) {
1936
return MoveType::ILLEGAL;
2037
}
2138

39+
//Moving up and down a column
2240
if($newColumn == $this->location->column) && ($newRow != $this->location->row) {
2341
//Check to see if spaces in between are the same
2442
if($this->spacesBetweenEmpty($this->location->index, $delta / 8, 8, $direction)) {
2543
return MoveType::NORMAL;
2644
}
2745
}
2846

47+
//Moving along a row
2948
if($newColumn != $this->location->column) && ($newRow == $this->location->row) {
3049
//Check to see if spaces in between are the same
3150
if($this->spacesBetweenEmpty($this->location->index, $delta / 1, 1, $direction)) {
3251
return MoveType::NORMAL;
3352
}
3453
}
3554

55+
//Moving diagonally between top left and bottom right
3656
if(abs($delta) % 9 == 0) {
3757
if($this->board->spacesBetweenEmpty($this->location->index, $delta / 9, 9, $direction)) {
3858
return MoveType::NORMAL;
3959
}
4060
}
61+
62+
//Moving diagonally between top right and bottom left
4163
if(abs($delta) % 7 == 0) {
4264
if($this->spacesBetweenEmpty($this->location->index, $delta / 7, 7, $direction)) {
4365
return MoveType::NORMAL;
4466
}
4567
}
4668

69+
//If the above cases do not satisfy, move is illegal
4770
return MoveType::ILLEGAL;
4871
}
4972

pieces/rook.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
11
<?php
2+
/*
3+
* Class Name: Rook
4+
* Extends: Piece
5+
*/
26
class Rook extends Piece {
37

48
function __construct(Board $b, Colour $c, Square $s) {
59
parent::__construct($b, $c, $s);
610
}
711

12+
/*
13+
* Name: moveAllowed
14+
* Parameters: Square $s: The Square that the piece is attempting to move to.
15+
* Remarks:
16+
* - Rook can move by staying on the same row or same column
17+
*/
818
protected function moveAllowed(Square $s) {
919
$newColumn = $s->column;
1020
$newRow = $s->row;
21+
22+
//Get the position difference between the current square and the desired square
1123
$delta = $s->index - $this->location->index;
24+
25+
//Determine which direction the piece is moving, up or down the array
1226
if($delta > 0) {
1327
$direction = 1;
1428
} else {
1529
$direction = -1;
1630
}
1731

32+
//A piece may not be able to move to another square with a piece of the same colour
1833
if($this-board->hasSameColourPiece($s, $this->colour)) {
1934
return MoveType::ILLEGAL;
2035
}
2136

37+
//Moving up and down a column
2238
if($newColumn == $this->location->column) && ($newRow != $this->location->row) {
2339
//Check to see if spaces in between are the same
2440
if($this->spacesBetweenEmpty($this->location->index, $delta / 8, 8, $direction)) {
2541
return MoveType::NORMAL;
2642
}
2743
}
2844

45+
//Moving up and down a row
2946
if($newColumn != $this->location->column) && ($newRow == $this->location->row) {
3047
//Check to see if spaces in between are the same
3148
if($this->spacesBetweenEmpty($this->location->index, $delta / 1, 1, $direction)) {
@@ -34,9 +51,16 @@ protected function moveAllowed(Square $s) {
3451
//Check for Castle Move
3552
}
3653

54+
//If the above cases do not satisfy, move is illegal
3755
return MoveType::ILLEGAL;
3856
}
3957

58+
/*
59+
* Name: tryMove
60+
* Parameters: Square $s: The Square that the piece is attempting to move to.
61+
* Description: This method will attempt to move the piece on the board
62+
* Overrides method on parent class in order to consider the special Castle move
63+
*/
4064
function tryMove(Square $s) {
4165
$mt = $this->moveAllowed($s);
4266
if($mt != MoveType::ILLEGAL) {

square.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@ class Square {
44
public $row;
55
public $index;
66
public $piece;
7+
8+
function __construct(Piece $piece, $index) {
9+
$this->piece = $piece;
10+
$this->index = $index;
11+
$this->column = $index % 8;
12+
$this->row = floor($index / 8);
13+
}
714
}
815
?>

0 commit comments

Comments
 (0)