From e1a2d5770a9c28f60d26980ba8417084a0547ddc Mon Sep 17 00:00:00 2001 From: Kevin Washington Date: Tue, 26 Mar 2019 23:20:16 -0300 Subject: [PATCH 1/9] Adding .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a09c56d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea From abbb471ffa1041b8abd5405359be1e456809ca16 Mon Sep 17 00:00:00 2001 From: Kevin Washington Date: Tue, 26 Mar 2019 23:20:50 -0300 Subject: [PATCH 2/9] Starting refactoring --- clear/Board.php | 261 ++++++++++++++++++++++++++++++++ clear/Game.php | 101 ++++++++++++ clear/Piece.php | 43 ++++++ clear/Player.php | 63 ++++++++ README.md => clear/README.md | 0 clear/Square.php | 43 ++++++ clear/css/style.css | 102 +++++++++++++ clear/index.php | 32 ++++ smells/README.md | 12 ++ board.php => smells/board.php | 0 index.php => smells/index.php | 0 piece.php => smells/piece.php | 0 player.php => smells/player.php | 0 square.php => smells/square.php | 0 14 files changed, 657 insertions(+) create mode 100644 clear/Board.php create mode 100644 clear/Game.php create mode 100644 clear/Piece.php create mode 100644 clear/Player.php rename README.md => clear/README.md (100%) create mode 100644 clear/Square.php create mode 100644 clear/css/style.css create mode 100644 clear/index.php create mode 100644 smells/README.md rename board.php => smells/board.php (100%) rename index.php => smells/index.php (100%) rename piece.php => smells/piece.php (100%) rename player.php => smells/player.php (100%) rename square.php => smells/square.php (100%) diff --git a/clear/Board.php b/clear/Board.php new file mode 100644 index 0000000..fc17693 --- /dev/null +++ b/clear/Board.php @@ -0,0 +1,261 @@ +squares[] = new Square($i, $j, false); + }else{ + $this->squares[] = new Square($i, $j, true); + } + + } + } + + $this->playerOne = new Player('White',-1); + $this->playerTwo = new Player('Red',1); + + $this->turn = 'White'; + $this->inactivePlayer = 'Red'; + + $p1 = $this->playerOne->getPieces(); + $p2 = $this->playerTwo->getPieces(); + + foreach($this->squares as $square) { + + $x = $square->getPosX(); + $y = $square->getPosY(); + + $square->setOccupied(false); + + $this->instancePiece($p1, $x, $y, $square, 'White'); + $this->instancePiece($p2, $x, $y, $square, 'Red'); + } + } + + public function instancePiece($piece, $x, $y, $square, $color) + { + foreach($piece as $p) { + if($p != null) + { + $pX = $p->getPosX(); + $pY = $p->getPosY(); + if($pX==$x && $pY==$y){ + $square->setOccupied($color, $p->getId()); + break; + } + } + } + } + + public function setBoard() { + + reset($this->squares); + for ($i=0; $i<8; $i++) { + for ($j=0; $j<8; $j++) { + if ( ($i%2==0 && $j%2==0) || ($i%2==1 && $j%2==1) ) { + $this->squares[] = new Square($i, $j, false); + }else { + $this->squares[] = new Square($i, $j, true); + } + + } + } + + if ($this->getTurn()=='White') { + $this->playerOne->move($this->piece,$this->x,$this->y); + $pieces = $this->playerOne->getPieces(); + $otherPieces = $this->playerTwo->getPieces(); + $this->inactivePlayer = 'Red'; + }elseif ($this->getTurn()=='Red') { + $this->playerTwo->move($this->piece,$this->x,$this->y); + $pieces = $this->playerTwo->getPieces(); + $otherPieces = $this->playerOne->getPieces(); + $this->inactivePlayer = 'White'; + } + + foreach ($this->squares as $square) { + + $x = $square->getPosX(); + $y = $square->getPosY(); + + $square->setOccupied(false); + + $this->instancePiece($pieces, $x, $y, $square, $this->getTurn()); + $this->instancePiece($otherPieces, $x, $y, $square, $this->inactivePlayer); + } + + } + + public function moveIsValid() { + + if ($this->getTurn()=='White') { + $thisPiece = $this->playerOne->getPiece($this->piece); + $this->inactivePlayer='Red'; + }elseif ($this->getTurn()=='Red') { + $thisPiece = $this->playerTwo->getPiece($this->piece); + $this->inactivePlayer='White'; + }else{ + exit('Error occurred!'); + } + + if(is_object($thisPiece)){ + $moveFromX = $thisPiece->getPosX(); + $moveFromY = $thisPiece->getPosY(); + $direction = $thisPiece->getDirection(); + }else{ + exit('thisPiece is not an object!'); + } + + foreach($direction as $d) { + if($moveFromX + $d == $this->x && $moveFromY + 1 == $this->y) { + $this->validMove=true; + return 'move'; + }elseif($moveFromX + $d == $this->x && $moveFromY - 1 == $this->y) { + $this->validMove=true; + return 'move'; + } + } + + return $this->checkNearbyPieces($moveFromX, $moveFromY); + } + + public function checkNearbyPieces($moveFromX, $moveFromY) + { + if($moveFromX+2==$this->x && $moveFromY+2==$this->y) { + foreach($this->squares as $thisSquare) { + if($thisSquare->getPosX()==$moveFromX+1 && $thisSquare->getPosY()==$moveFromY+1 && $thisSquare->getOccupied()==$this->inactivePlayer) { + return $this->capturePiece($thisSquare); + } + } + return false; + }elseif($moveFromX+2==$this->x && $moveFromY-2==$this->y) { + foreach($this->squares as $thisSquare) { + if($thisSquare->getPosX()==$moveFromX+1 && $thisSquare->getPosY()==$moveFromY-1 && $thisSquare->getOccupied()==$this->inactivePlayer) { + return $this->capturePiece($thisSquare); + } + } + return false; + }elseif($moveFromX-2==$this->x && $moveFromY+2==$this->y) { + foreach($this->squares as $thisSquare) { + if($thisSquare->getPosX()==$moveFromX-1 && $thisSquare->getPosY()==$moveFromY+1 && $thisSquare->getOccupied()==$this->inactivePlayer) { + return $this->capturePiece($thisSquare); + } + } + return false; + }elseif($moveFromX-2==$this->x && $moveFromY-2==$this->y) { + foreach($this->squares as $thisSquare) { + if($thisSquare->getPosX()==$moveFromX-1 && $thisSquare->getPosY()==$moveFromY-1 && $thisSquare->getOccupied()==$this->inactivePlayer) { + return $this->capturePiece($thisSquare); + } + } + $this->validMove=false; + return false; + }else { + $this->validMove=false; + return false; + } + } + + public function capturePiece($square) + { + if($this->getTurn()=='White') { + $this->playerTwo->deletePiece($square->getPieceId()); + }elseif($this->getTurn()=='Red') { + $this->playerOne->deletePiece($square->getPieceId()); + } + $this->validMove=true; + return 'jump'; + } + + public function printBoard() { + echo "\n"; + + if ($this->playerTwo->countPieces()<=0) { + echo ''; + }elseif ($this->playerOne->countPieces()<=0) { + echo ''; + }else { + echo "\n\t"; + + for($i=0; $i<64; $i++) { + + $square = $this->squares[$i]; + + if($i%8==0 && $i>0) { + echo "\n\n\t"; + } + + if($square->getValid()) + { + $x = $square->getPosX(); + $y = $square->getPosY(); + + if($square->getOccupied()) { + $turn = $this->getTurn(); + $color = $square->getOccupied(); + $id = $square->getPieceId(); + + if($color == 'Red' && $turn == 'Red') { + echo ""; + }elseif($color == 'White' && $turn == 'White') { + echo ""; + }elseif($color == 'Red') { + echo ""; + }elseif($color == 'White') { + echo ""; + } + + }else { + echo ""; + } + + }else { + echo ""; + } + } + echo "\n"; + } + echo "\n

White Wins...

Red Wins...

 
 
 
 
  
"; + } + + public function getPlayerOne() { + return $this->playerOne; + } + public function getPlayerTwo() { + return $this->playerTwo; + } + + public function setTurn($color) { + $this->turn = $color; + } + + public function getTurn() { + return $this->turn; + } + + public function setX($num) { + $this->x = $num; + } + + public function setY($num) { + $this->y = $num; + } + + public function setPiece($id) { + $this->piece = $id; + } +} \ No newline at end of file diff --git a/clear/Game.php b/clear/Game.php new file mode 100644 index 0000000..a90c0d6 --- /dev/null +++ b/clear/Game.php @@ -0,0 +1,101 @@ +startSession(); + + $this->message = "Welcome! White's turn!"; + $this->color = 'Red'; + $this->activeColor = 'White'; + + if(array_key_exists('submit',$_GET)) { + $this->checkMove(); + } + + } + + private function startSession() + { + session_start(); + + if (!isset($_SESSION['b']) || array_key_exists('reset',$_GET)) { + $this->message = "Start New Game...White's Move"; + $this->board = new Board(); + $_SESSION['b'] = $this->board; + }else { + $this->board = $_SESSION['b']; + } + } + + public function checkMove() + { + if(array_key_exists('piece',$_GET) && array_key_exists('n',$_GET)) { + $id = $_GET['piece']; + $moveTo = explode('-',$_GET['n']); + $x = $moveTo[0]; + $y = $moveTo[1]; + $this->board->setX($x); + $this->board->setY($y); + $this->board->setPiece($id); + + $this->changeTurn(); + }else { + $this->message = "Invalid Move. Try Again."; + } + } + + public function changeTurn() + { + if($this->board->moveIsValid()=='move') { + $this->board->setBoard(); + if($this->board->getTurn()=='White') { + $this->board->setTurn('Red'); + }elseif($this->board->getTurn()=='Red') { + $this->board->setTurn('White'); + } + $this->message = $this->board->getTurn() . "'s turn..."; + }elseif($this->board->moveIsValid()=='jump') { + $this->board->setBoard(); + if($this->board->getTurn()=='White') { + $this->board->setTurn('Red'); + }elseif($this->board->getTurn()=='Red') { + $this->board->setTurn('White'); + } + $this->message = 'Jump! Still ' .$this->board->getTurn() . "'s turn..."; + }else{ + $this->message = 'Invalid Move. Still ' . $this->board->getTurn() . "'s turn..."; + } + } + + public function displayMessage() + { + echo $this->message; + } + + public function printBoard() + { + $this->board->printBoard(); + } + + public static function getInstance() + { + static $instance = null; + if (null === $instance) { + $instance = new static(); + } + + return $instance; + } +} \ No newline at end of file diff --git a/clear/Piece.php b/clear/Piece.php new file mode 100644 index 0000000..83a1c7b --- /dev/null +++ b/clear/Piece.php @@ -0,0 +1,43 @@ +moveY = array(-1,1); + $this->moveX = array($direction); + $this->posX = $x; + $this->posY = $y; + $this->pId = $id; + } + + public function getPosX() { + return $this->posX; + } + + public function getPosY() { + return $this->posY; + } + + public function getId() { + return $this->pId; + } + + public function getDirection() { + return $this->moveX; + } + + public function setPos($x,$y) { + $this->posX = $x; + $this->posY = $y; + } + + public function king() { + $this->moveX = array(-1,1); + } +} \ No newline at end of file diff --git a/clear/Player.php b/clear/Player.php new file mode 100644 index 0000000..186345a --- /dev/null +++ b/clear/Player.php @@ -0,0 +1,63 @@ +color = $color; + $this->direction = $direction; + + $this->pieces = array(); + + if($color=='White'){ + $xCoord = array(0,2,4,6,1,3,5,7,0,2,4,6); + $yCoord = array(7,7,7,7,6,6,6,6,5,5,5,5); + }elseif($color=='Red'){ + $xCoord = array(1,3,5,7,0,2,4,6,1,3,5,7); + $yCoord = array(0,0,0,0,1,1,1,1,2,2,2,2); + } + + for ($i=0; $i<12; $i++) { + $y = $xCoord[$i]; + $x = $yCoord[$i]; + $this->pieces[] = new Piece($x,$y,$direction,$i); + } + + } + + public function &getPieces() { + return $this->pieces; + } + + public function &getPiece($id) { + return $this->pieces[$id]; + } + + public function countPieces() { + $count = 0; + foreach($this->pieces as $piece) { + if(!is_null($piece)) { + $count++; + } + } + return $count; + } + + public function &deletePiece($id) { + $this->pieces[$id] = null; + return $this->pieces; + } + + public function move($piece,$x,$y) { + $pieceObj = $this->pieces[$piece]; + $pieceObj->setPos($x,$y); + if($this->color=='White' && $x==0) { + $pieceObj->king(); + }elseif($this->color=='Red' && $x==7) { + $pieceObj->king(); + } + } +} \ No newline at end of file diff --git a/README.md b/clear/README.md similarity index 100% rename from README.md rename to clear/README.md diff --git a/clear/Square.php b/clear/Square.php new file mode 100644 index 0000000..5233b3b --- /dev/null +++ b/clear/Square.php @@ -0,0 +1,43 @@ +posX = $x; + $this->posY = $y; + $this->occupied = false; + $this->pieceId = null; + $this->valid = $isValid; + } + + public function getPosX() { + return $this->posX; + } + + public function getPosY() { + return $this->posY; + } + + public function setOccupied($color=false, $id=null) { + $this->occupied = $color; + $this->pieceId = $id; + } + + public function getOccupied() { + return $this->occupied; + } + + public function getPieceId() { + return $this->pieceId; + } + + public function getValid() { + return $this->valid; + } +} \ No newline at end of file diff --git a/clear/css/style.css b/clear/css/style.css new file mode 100644 index 0000000..6fe7ac7 --- /dev/null +++ b/clear/css/style.css @@ -0,0 +1,102 @@ +h2, +p { + font-family: Helvetica, sans-serif; +} + +h2 { + margin-top: 10px; +} + +body { + background: #e5b585; +} + +table { + width: 500px; + height: 500px; + margin: 0 auto; +} + +td { + background-color: #883000; +} + +.red { + background-color: #883000; +} + +.white { + background-color: #883000; +} + +.black { + background-color: #000000; +} + +#left { + display: inline-block; + float: left; + width: 400px; +} + +#game { + text-align: center; +} + +#controls { + margin: 10px; + font-size: 15px; +} + +input[type=submit] { + padding: 12px; + margin: 0 10px; + width: 200px; + color: wheat; + background: #883000; + border: none; + border-radius: 30px; + font-weight: bold; + cursor: pointer; +} + +input[type=submit]:hover { + color: #883000; + background: white; + -webkit-transition: ease .3s; + -moz-transition: ease .3s; + -ms-transition: ease .3s; + -o-transition: ease .3s; + transition: ease .3s; +} + +input[type=submit]:focus { + outline: none; + background: #501c00; + color: white; + -webkit-transition: ease .3s; + -moz-transition: ease .3s; + -ms-transition: ease .3s; + -o-transition: ease .3s; + transition: ease .3s; +} + +input[type=checkbox] { + height: 35px; +} + +.red-piece { + height: 40px; + width: 40px; + background: red; + border-radius: 50%; + display: inline-block; +} + +.white-piece { + height: 40px; + width: 40px; + background: white; + border-radius: 50%; + display: inline-block; +} \ No newline at end of file diff --git a/clear/index.php b/clear/index.php new file mode 100644 index 0000000..05362bb --- /dev/null +++ b/clear/index.php @@ -0,0 +1,32 @@ + + + + + + + Checkers + + + + + +
+

Checkers

+

displayMessage() ?>

+ +
+ printBoard() ?> +
+ + +
+
+
+ + \ No newline at end of file diff --git a/smells/README.md b/smells/README.md new file mode 100644 index 0000000..bd14556 --- /dev/null +++ b/smells/README.md @@ -0,0 +1,12 @@ +checkers +======== + +Checkers Game + +partially functional checkers game. + +Notes: +-Kinged pieces look the same as regular pieces. +-Double jumps aren't possible. +- +- diff --git a/board.php b/smells/board.php similarity index 100% rename from board.php rename to smells/board.php diff --git a/index.php b/smells/index.php similarity index 100% rename from index.php rename to smells/index.php diff --git a/piece.php b/smells/piece.php similarity index 100% rename from piece.php rename to smells/piece.php diff --git a/player.php b/smells/player.php similarity index 100% rename from player.php rename to smells/player.php diff --git a/square.php b/smells/square.php similarity index 100% rename from square.php rename to smells/square.php From efca57e44693b9e069af7f304811fe009a097aa6 Mon Sep 17 00:00:00 2001 From: Kevin Washington Date: Tue, 26 Mar 2019 23:26:31 -0300 Subject: [PATCH 3/9] Removing old files --- .index.php.swp | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .index.php.swp diff --git a/.index.php.swp b/.index.php.swp deleted file mode 100644 index acb5ba02573b50b512ab8ba79ecf475cf7bf292d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI%J#NA<6u|M9?p0B_0JN+jp@4uJz)%T>uqVbSd?isnq)zlUy#mMK8r|9i0ZY^l z*+Bm%S(a_Rr{~{h*|X=9_r{>zl{{)9y1wV*Ziqa-i9o5tZ-s zI(AXw^`+UUKRZ>ck%s^R2q1s}0tg_000K8CkVb}l=6ye$dpG`=Z}4igi~s@%Ab Date: Tue, 26 Mar 2019 23:41:17 -0300 Subject: [PATCH 4/9] Deleting old env folder --- .idea/.name | 1 - .idea/checkers.iml | 9 - .idea/encodings.xml | 5 - .idea/misc.xml | 5 - .idea/modules.xml | 9 - .idea/scopes/scope_settings.xml | 5 - .idea/vcs.xml | 8 - .idea/workspace.xml | 496 -------------------------------- 8 files changed, 538 deletions(-) delete mode 100644 .idea/.name delete mode 100644 .idea/checkers.iml delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/scopes/scope_settings.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index ea1c4e6..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -checkers \ No newline at end of file diff --git a/.idea/checkers.iml b/.idea/checkers.iml deleted file mode 100644 index 6b8184f..0000000 --- a/.idea/checkers.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index e206d70..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 1162f43..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 9b92b39..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml deleted file mode 100644 index 922003b..0000000 --- a/.idea/scopes/scope_settings.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 2e0588c..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 063e7ec..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,496 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1370283613901 - 1370283613901 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From fda599eaa8c4599b4329c7573e71dd9b0188090e Mon Sep 17 00:00:00 2001 From: Kevin Washington Date: Wed, 27 Mar 2019 00:51:42 -0300 Subject: [PATCH 5/9] Concluding refactoring --- clear/Board.php | 51 +++++++++++++----------------------------------- clear/Game.php | 3 ++- clear/Player.php | 1 - 3 files changed, 16 insertions(+), 39 deletions(-) diff --git a/clear/Board.php b/clear/Board.php index fc17693..d68c922 100644 --- a/clear/Board.php +++ b/clear/Board.php @@ -21,7 +21,6 @@ public function __construct() { }else{ $this->squares[] = new Square($i, $j, true); } - } } @@ -71,7 +70,6 @@ public function setBoard() { }else { $this->squares[] = new Square($i, $j, true); } - } } @@ -97,7 +95,6 @@ public function setBoard() { $this->instancePiece($pieces, $x, $y, $square, $this->getTurn()); $this->instancePiece($otherPieces, $x, $y, $square, $this->inactivePlayer); } - } public function moveIsValid() { @@ -121,50 +118,29 @@ public function moveIsValid() { } foreach($direction as $d) { - if($moveFromX + $d == $this->x && $moveFromY + 1 == $this->y) { - $this->validMove=true; - return 'move'; - }elseif($moveFromX + $d == $this->x && $moveFromY - 1 == $this->y) { + if(($moveFromX + $d == $this->x && $moveFromY + 1 == $this->y) || ($moveFromX + $d == $this->x && $moveFromY - 1 == $this->y)) { $this->validMove=true; return 'move'; } } - return $this->checkNearbyPieces($moveFromX, $moveFromY); + return $this->checkNearbySquares($moveFromX, $moveFromY); } - public function checkNearbyPieces($moveFromX, $moveFromY) + public function checkNearbySquares($moveFromX, $moveFromY) { - if($moveFromX+2==$this->x && $moveFromY+2==$this->y) { - foreach($this->squares as $thisSquare) { - if($thisSquare->getPosX()==$moveFromX+1 && $thisSquare->getPosY()==$moveFromY+1 && $thisSquare->getOccupied()==$this->inactivePlayer) { - return $this->capturePiece($thisSquare); - } - } - return false; - }elseif($moveFromX+2==$this->x && $moveFromY-2==$this->y) { - foreach($this->squares as $thisSquare) { - if($thisSquare->getPosX()==$moveFromX+1 && $thisSquare->getPosY()==$moveFromY-1 && $thisSquare->getOccupied()==$this->inactivePlayer) { - return $this->capturePiece($thisSquare); - } - } - return false; - }elseif($moveFromX-2==$this->x && $moveFromY+2==$this->y) { - foreach($this->squares as $thisSquare) { - if($thisSquare->getPosX()==$moveFromX-1 && $thisSquare->getPosY()==$moveFromY+1 && $thisSquare->getOccupied()==$this->inactivePlayer) { - return $this->capturePiece($thisSquare); - } - } - return false; - }elseif($moveFromX-2==$this->x && $moveFromY-2==$this->y) { - foreach($this->squares as $thisSquare) { - if($thisSquare->getPosX()==$moveFromX-1 && $thisSquare->getPosY()==$moveFromY-1 && $thisSquare->getOccupied()==$this->inactivePlayer) { - return $this->capturePiece($thisSquare); + if($moveFromX+2==$this->x || $moveFromX-2==$this->x) + { + if($moveFromY+2==$this->y || $moveFromY-2==$this->y) + { + foreach($this->squares as $thisSquare) { + if(($thisSquare->getPosX()==$moveFromX+1 && $thisSquare->getPosY()==$moveFromY+1 && $thisSquare->getOccupied()==$this->inactivePlayer) || ($thisSquare->getPosX()==$moveFromX+1 && $thisSquare->getPosY()==$moveFromY-1 && $thisSquare->getOccupied()==$this->inactivePlayer) || ($thisSquare->getPosX()==$moveFromX-1 && $thisSquare->getPosY()==$moveFromY+1 && $thisSquare->getOccupied()==$this->inactivePlayer) || ($thisSquare->getPosX()==$moveFromX-1 && $thisSquare->getPosY()==$moveFromY-1 && $thisSquare->getOccupied()==$this->inactivePlayer)) { + return $this->capturePiece($thisSquare); + } } + return false; } - $this->validMove=false; - return false; - }else { + } else { $this->validMove=false; return false; } @@ -178,6 +154,7 @@ public function capturePiece($square) $this->playerOne->deletePiece($square->getPieceId()); } $this->validMove=true; + return 'jump'; } diff --git a/clear/Game.php b/clear/Game.php index a90c0d6..45286f8 100644 --- a/clear/Game.php +++ b/clear/Game.php @@ -23,7 +23,6 @@ protected function __construct() if(array_key_exists('submit',$_GET)) { $this->checkMove(); } - } private function startSession() @@ -98,4 +97,6 @@ public static function getInstance() return $instance; } + + private function __clone() {} } \ No newline at end of file diff --git a/clear/Player.php b/clear/Player.php index 186345a..03174cb 100644 --- a/clear/Player.php +++ b/clear/Player.php @@ -25,7 +25,6 @@ public function __construct($color, $direction) { $x = $yCoord[$i]; $this->pieces[] = new Piece($x,$y,$direction,$i); } - } public function &getPieces() { From 6efdfe672b2decd5a77ca8ab36f0ad53b97a0ebc Mon Sep 17 00:00:00 2001 From: Kevin Washington Date: Wed, 27 Mar 2019 08:44:49 -0300 Subject: [PATCH 6/9] Adding Decorator pattern --- clear/Board.php | 57 ++------------------------------ clear/BoardPrintDecorator.php | 62 +++++++++++++++++++++++++++++++++++ clear/Game.php | 4 ++- 3 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 clear/BoardPrintDecorator.php diff --git a/clear/Board.php b/clear/Board.php index d68c922..7566a1b 100644 --- a/clear/Board.php +++ b/clear/Board.php @@ -1,12 +1,12 @@ \n"; - - if ($this->playerTwo->countPieces()<=0) { - echo '

White Wins...

'; - }elseif ($this->playerOne->countPieces()<=0) { - echo '

Red Wins...

'; - }else { - echo "\n\t"; - - for($i=0; $i<64; $i++) { - - $square = $this->squares[$i]; - - if($i%8==0 && $i>0) { - echo "\n\n\t"; - } - - if($square->getValid()) - { - $x = $square->getPosX(); - $y = $square->getPosY(); - - if($square->getOccupied()) { - $turn = $this->getTurn(); - $color = $square->getOccupied(); - $id = $square->getPieceId(); - - if($color == 'Red' && $turn == 'Red') { - echo "
 "; - }elseif($color == 'White' && $turn == 'White') { - echo "
 "; - }elseif($color == 'Red') { - echo "
 "; - }elseif($color == 'White') { - echo "
 "; - } - - }else { - echo " "; - } - - }else { - echo " "; - } - } - echo "\n"; - } - echo "\n"; - } - public function getPlayerOne() { return $this->playerOne; } diff --git a/clear/BoardPrintDecorator.php b/clear/BoardPrintDecorator.php new file mode 100644 index 0000000..72dc8bc --- /dev/null +++ b/clear/BoardPrintDecorator.php @@ -0,0 +1,62 @@ +board = $board; + } + + public function printBoard() { + echo "\n"; + + if ($this->board->playerTwo->countPieces()<=0) { + echo ''; + }elseif ($this->board->playerOne->countPieces()<=0) { + echo ''; + }else { + echo "\n\t"; + + for($i=0; $i<64; $i++) { + + $square = $this->board->squares[$i]; + + if($i%8==0 && $i>0) { + echo "\n\n\t"; + } + + if($square->getValid()) + { + $x = $square->getPosX(); + $y = $square->getPosY(); + + if($square->getOccupied()) { + $turn = $this->board->getTurn(); + $color = $square->getOccupied(); + $id = $square->getPieceId(); + + if($color == 'Red' && $turn == 'Red') { + echo ""; + }elseif($color == 'White' && $turn == 'White') { + echo ""; + }elseif($color == 'Red') { + echo ""; + }elseif($color == 'White') { + echo ""; + } + + }else { + echo ""; + } + + }else { + echo ""; + } + } + echo "\n"; + } + echo "\n

White Wins...

Red Wins...

 
 
 
 
  
"; + } +} \ No newline at end of file diff --git a/clear/Game.php b/clear/Game.php index 45286f8..708aed3 100644 --- a/clear/Game.php +++ b/clear/Game.php @@ -4,6 +4,7 @@ require_once('Piece.php'); require_once('Board.php'); require_once('Square.php'); +require_once('BoardPrintDecorator.php'); class Game { @@ -85,7 +86,8 @@ public function displayMessage() public function printBoard() { - $this->board->printBoard(); + $boardPrintDecorator = new BoardPrintDecorator($this->board); + $boardPrintDecorator->printBoard(); } public static function getInstance() From 26e7ea1cffb555b16094fb122dd554c0e429c5b5 Mon Sep 17 00:00:00 2001 From: Kevin Washington Date: Thu, 28 Mar 2019 22:40:45 -0300 Subject: [PATCH 7/9] Adding README --- README.md | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..2babc90 --- /dev/null +++ b/README.md @@ -0,0 +1,60 @@ +# Refactoring project - Checkers + +This is a fork of the original project, originally developed by [ryed8118](https://github.com/ryed8118). The intent is refactor the code and apply some software design patterns. + +## Patterns Chosen +To this project, I chose to apply the three following patterns: + +- **Adapter**: the adapter pattern is a software design pattern that allows the interface of an existing class to be used from another interface. It is often used to make existing classes work with others without modifying their source code [1]. +- **Decorator**: the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern [2]. +- **Singleton**: the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton [3]. + +The last two patterns were applied in this repository, and the first was applied in [Tic Tac Toe](https://github.com/kevinwsbr/tictactoe) repository. + +## How to build + +### Environment setup +The project in this repository is a PHP application. To execute, you need to have an PHP interpreter available in your machine. The installation guide of PHP can be found [here](https://www.php.net/manual/en/install.php). +### Original project +To execute the original version (with bad smells), navigate to `smells` folder and start the PHP bult-in server: +``` +php -S localhost:8000 +``` + +Then, open that address in your browser. + +### Modified project +The process to execute the modified version is the same of the original version, but you have to navigate to `clear` folder to access the new code. After that, you can start the server: +``` +php -S localhost:8000 +``` + +Please ensure that your cache is empty by restarting your browser or opening a private tab to avoid conflicts with the old version. + +## Affected classes + +The following old classes was affected: + +- **Board**: the content of several methods was changed: large amount of duplicated code, unnecessary conditionals and absurdly large methods were reduced in smaller and simpler methods. +- **Square**: the file `square.php` was empty. So, the content of the `Square` class (originally located in `board.php`) was moved to it. + +The following classes were created: + +- **Game**: the `Game` class was developed to implement the `Singleton` pattern. All the game logic that was written in the `index.php` file was moved to this class. +- **BoardPrintDecorator**: this class offers the implementation of the board print method (originally in `Board` class) - invoked in `Game::printBoard()`. + +And finally, but no less important, the unnecessary comments in all files was removed and the page styles was moved (and updated) to `css/styles.css` stylesheet. + +## Affected functionalities + +- Game start +- Pieces movement +- Game style + +## References + +[1] [Adapter pattern](https://en.wikipedia.org/wiki/Singleton_pattern). Wikipedia + +[2] [Decorator pattern](https://en.wikipedia.org/wiki/Decorator_pattern). Wikipedia + +[3] [Singleton pattern](https://en.wikipedia.org/wiki/Adapter_pattern). Wikipedia \ No newline at end of file From 6059b94b4aff2590df7f0e689e92ebfa0b0313ad Mon Sep 17 00:00:00 2001 From: Kevin Washington Date: Thu, 28 Mar 2019 22:44:32 -0300 Subject: [PATCH 8/9] Updating README --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2babc90..0607019 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ The following old classes was affected: The following classes were created: - **Game**: the `Game` class was developed to implement the `Singleton` pattern. All the game logic that was written in the `index.php` file was moved to this class. -- **BoardPrintDecorator**: this class offers the implementation of the board print method (originally in `Board` class) - invoked in `Game::printBoard()`. +- **BoardPrintDecorator**: this class offers the implementation of the board print method (originally in `Board` class) - invoked in `Game::printBoard()` - using the Decorator pattern. And finally, but no less important, the unnecessary comments in all files was removed and the page styles was moved (and updated) to `css/styles.css` stylesheet. @@ -53,8 +53,8 @@ And finally, but no less important, the unnecessary comments in all files was re ## References -[1] [Adapter pattern](https://en.wikipedia.org/wiki/Singleton_pattern). Wikipedia +[1] [Adapter pattern](https://en.wikipedia.org/wiki/Adapter_pattern). originally in Wikipedia. -[2] [Decorator pattern](https://en.wikipedia.org/wiki/Decorator_pattern). Wikipedia +[2] [Decorator pattern](https://en.wikipedia.org/wiki/Decorator_pattern). originally in Wikipedia. -[3] [Singleton pattern](https://en.wikipedia.org/wiki/Adapter_pattern). Wikipedia \ No newline at end of file +[3] [Singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern). originally in Wikipedia. \ No newline at end of file From 01ea00b5611d0a0f5f89dfa5d307d1411e5f02c2 Mon Sep 17 00:00:00 2001 From: Kevin Washington Date: Tue, 1 Oct 2019 16:42:21 -0300 Subject: [PATCH 9/9] Updating style --- clear/Board.php => Board.php | 0 ...ntDecorator.php => BoardPrintDecorator.php | 0 clear/Game.php => Game.php | 0 clear/Piece.php => Piece.php | 0 clear/Player.php => Player.php | 0 README.md | 65 +-- clear/Square.php => Square.php | 0 clear/README.md | 12 - {clear/css => css}/style.css | 0 clear/index.php => index.php | 0 smells/README.md | 12 - smells/board.php | 371 ------------------ smells/index.php | 127 ------ smells/piece.php | 64 --- smells/player.php | 69 ---- smells/square.php | 2 - 16 files changed, 6 insertions(+), 716 deletions(-) rename clear/Board.php => Board.php (100%) rename clear/BoardPrintDecorator.php => BoardPrintDecorator.php (100%) rename clear/Game.php => Game.php (100%) rename clear/Piece.php => Piece.php (100%) rename clear/Player.php => Player.php (100%) rename clear/Square.php => Square.php (100%) delete mode 100644 clear/README.md rename {clear/css => css}/style.css (100%) rename clear/index.php => index.php (100%) delete mode 100644 smells/README.md delete mode 100644 smells/board.php delete mode 100644 smells/index.php delete mode 100644 smells/piece.php delete mode 100644 smells/player.php delete mode 100644 smells/square.php diff --git a/clear/Board.php b/Board.php similarity index 100% rename from clear/Board.php rename to Board.php diff --git a/clear/BoardPrintDecorator.php b/BoardPrintDecorator.php similarity index 100% rename from clear/BoardPrintDecorator.php rename to BoardPrintDecorator.php diff --git a/clear/Game.php b/Game.php similarity index 100% rename from clear/Game.php rename to Game.php diff --git a/clear/Piece.php b/Piece.php similarity index 100% rename from clear/Piece.php rename to Piece.php diff --git a/clear/Player.php b/Player.php similarity index 100% rename from clear/Player.php rename to Player.php diff --git a/README.md b/README.md index 0607019..a25e4b0 100644 --- a/README.md +++ b/README.md @@ -1,60 +1,7 @@ -# Refactoring project - Checkers +Checkers +======== +Partially functional checkers game -This is a fork of the original project, originally developed by [ryed8118](https://github.com/ryed8118). The intent is refactor the code and apply some software design patterns. - -## Patterns Chosen -To this project, I chose to apply the three following patterns: - -- **Adapter**: the adapter pattern is a software design pattern that allows the interface of an existing class to be used from another interface. It is often used to make existing classes work with others without modifying their source code [1]. -- **Decorator**: the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern [2]. -- **Singleton**: the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton [3]. - -The last two patterns were applied in this repository, and the first was applied in [Tic Tac Toe](https://github.com/kevinwsbr/tictactoe) repository. - -## How to build - -### Environment setup -The project in this repository is a PHP application. To execute, you need to have an PHP interpreter available in your machine. The installation guide of PHP can be found [here](https://www.php.net/manual/en/install.php). -### Original project -To execute the original version (with bad smells), navigate to `smells` folder and start the PHP bult-in server: -``` -php -S localhost:8000 -``` - -Then, open that address in your browser. - -### Modified project -The process to execute the modified version is the same of the original version, but you have to navigate to `clear` folder to access the new code. After that, you can start the server: -``` -php -S localhost:8000 -``` - -Please ensure that your cache is empty by restarting your browser or opening a private tab to avoid conflicts with the old version. - -## Affected classes - -The following old classes was affected: - -- **Board**: the content of several methods was changed: large amount of duplicated code, unnecessary conditionals and absurdly large methods were reduced in smaller and simpler methods. -- **Square**: the file `square.php` was empty. So, the content of the `Square` class (originally located in `board.php`) was moved to it. - -The following classes were created: - -- **Game**: the `Game` class was developed to implement the `Singleton` pattern. All the game logic that was written in the `index.php` file was moved to this class. -- **BoardPrintDecorator**: this class offers the implementation of the board print method (originally in `Board` class) - invoked in `Game::printBoard()` - using the Decorator pattern. - -And finally, but no less important, the unnecessary comments in all files was removed and the page styles was moved (and updated) to `css/styles.css` stylesheet. - -## Affected functionalities - -- Game start -- Pieces movement -- Game style - -## References - -[1] [Adapter pattern](https://en.wikipedia.org/wiki/Adapter_pattern). originally in Wikipedia. - -[2] [Decorator pattern](https://en.wikipedia.org/wiki/Decorator_pattern). originally in Wikipedia. - -[3] [Singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern). originally in Wikipedia. \ No newline at end of file +Notes: +- Kinged pieces look the same as regular pieces. +- Double jumps aren't possible. diff --git a/clear/Square.php b/Square.php similarity index 100% rename from clear/Square.php rename to Square.php diff --git a/clear/README.md b/clear/README.md deleted file mode 100644 index bd14556..0000000 --- a/clear/README.md +++ /dev/null @@ -1,12 +0,0 @@ -checkers -======== - -Checkers Game - -partially functional checkers game. - -Notes: --Kinged pieces look the same as regular pieces. --Double jumps aren't possible. -- -- diff --git a/clear/css/style.css b/css/style.css similarity index 100% rename from clear/css/style.css rename to css/style.css diff --git a/clear/index.php b/index.php similarity index 100% rename from clear/index.php rename to index.php diff --git a/smells/README.md b/smells/README.md deleted file mode 100644 index bd14556..0000000 --- a/smells/README.md +++ /dev/null @@ -1,12 +0,0 @@ -checkers -======== - -Checkers Game - -partially functional checkers game. - -Notes: --Kinged pieces look the same as regular pieces. --Double jumps aren't possible. -- -- diff --git a/smells/board.php b/smells/board.php deleted file mode 100644 index 0ae76aa..0000000 --- a/smells/board.php +++ /dev/null @@ -1,371 +0,0 @@ -squares[] = new Square($i, $j, false); - }else{ - $this->squares[] = new Square($i, $j, true); - } - - } - } - - - $this->playerOne = new Player('White',-1); - $this->playerTwo = new Player('Red',1); - - $this->turn = 'White'; - $this->inactivePlayer = 'Red'; - - $p1 = $this->playerOne->getPieces(); - $p2 = $this->playerTwo->getPieces(); - - foreach($this->squares as $square) { - - $x = $square->getPosX(); - $y = $square->getPosY(); - - $square->setOccupied(false); - - foreach($p1 as $p) { - $pX = $p->getPosX(); - $pY = $p->getPosY(); - if($pX==$x && $pY==$y){ - $square->setOccupied('White',$p->getId()); - break; - } - } - - - foreach($p2 as $p) { - $pX = $p->getPosX(); - $pY = $p->getPosY(); - if($pX==$x && $pY==$y){ - $square->setOccupied('Red',$p->getId()); - break; - } - } - - } - - } - - // Class Methods - public function setBoard() { - - reset($this->squares); - for ($i=0; $i<8; $i++) { - for ($j=0; $j<8; $j++) { - if ( ($i%2==0 && $j%2==0) || ($i%2==1 && $j%2==1) ) { - $this->squares[] = new Square($i, $j, false); - }else { - $this->squares[] = new Square($i, $j, true); - } - - } - } - - - if ($this->getTurn()=='White') { - $this->playerOne->move($this->piece,$this->x,$this->y); - $pieces = $this->playerOne->getPieces(); - $otherPieces = $this->playerTwo->getPieces(); - $this->inactivePlayer = 'Red'; - }elseif ($this->getTurn()=='Red') { - $this->playerTwo->move($this->piece,$this->x,$this->y); - $pieces = $this->playerTwo->getPieces(); - $otherPieces = $this->playerOne->getPieces(); - $this->inactivePlayer = 'White'; - } - - foreach ($this->squares as $square) { - - - $x = $square->getPosX(); - $y = $square->getPosY(); - - $square->setOccupied(false); - - foreach ($pieces as $p) { - if ($p != null) { - $pX = $p->getPosX(); - $pY = $p->getPosY(); - if ($pX==$x && $pY==$y){ - $square->setOccupied($this->getTurn(),$p->getId()); - break; - } - } - } - - foreach ($otherPieces as $p) { - if ($p != null) { - $pX = $p->getPosX(); - $pY = $p->getPosY(); - if ($pX==$x && $pY==$y){ - $square->setOccupied($this->inactivePlayer,$p->getId()); - break; - } - } - } - - } - - } // End method setBoard - - - public function moveIsValid() { - - if ($this->getTurn()=='White') { - $thisPiece = $this->playerOne->getPiece($this->piece); - //! - $this->inactivePlayer='Red'; - }elseif ($this->getTurn()=='Red') { - $thisPiece = $this->playerTwo->getPiece($this->piece); - //! - $this->inactivePlayer='White'; - }else{ - exit('Exception occurred!'); - } - - if(is_object($thisPiece)){ - $moveFromX = $thisPiece->getPosX(); - $moveFromY = $thisPiece->getPosY(); - $direction = $thisPiece->getDirection(); - }else{ - exit('thisPiece is not an object!'); - } - - - /* - if($moveFromX+1==$x && $moveFromY+1==$y) { - return 'move'; - }elseif($moveFromX-1==$x && $moveFromY-1==$y) { - return 'move'; - }elseif($moveFromX-1==$x && $moveFromY+1==$y) { - return 'move'; - }elseif($moveFromX+1==$x && $moveFromY-1==$y) { - return 'move'; - }*/ - //Check if move is valid. Returns true or false - foreach($direction as $d) { - if($moveFromX + $d == $this->x && $moveFromY + 1 == $this->y) { - $this->validMove=true; - return 'move'; - }elseif($moveFromX + $d == $this->x && $moveFromY - 1 == $this->y) { - $this->validMove=true; - return 'move'; - } - } - if($moveFromX+2==$this->x && $moveFromY+2==$this->y) { - foreach($this->squares as $thisSquare) { - //echo $this->turn.'-'. $this->inactivePlayer.'-' . $thisSquare->getOccupied() . '
'; - if($thisSquare->getPosX()==$moveFromX+1 && $thisSquare->getPosY()==$moveFromY+1 && $thisSquare->getOccupied()==$this->inactivePlayer) { - if($this->getTurn()=='White') { - $this->playerTwo->deletePiece($thisSquare->getPieceId()); - }elseif($this->getTurn()=='Red') { - $this->playerOne->deletePiece($thisSquare->getPieceId()); - } - $this->validMove=true; - return 'jump'; - } - } - return false; - }elseif($moveFromX+2==$this->x && $moveFromY-2==$this->y) { - foreach($this->squares as $thisSquare) { - //echo $this->turn.'-'. $this->inactivePlayer.'-' . $thisSquare->getOccupied() . '
'; - if($thisSquare->getPosX()==$moveFromX+1 && $thisSquare->getPosY()==$moveFromY-1 && $thisSquare->getOccupied()==$this->inactivePlayer) { - if($this->getTurn()=='White') { - $this->playerTwo->deletePiece($thisSquare->getPieceId()); - }elseif($this->getTurn()=='Red') { - $this->playerOne->deletePiece($thisSquare->getPieceId()); - } - $this->validMove=true; - return 'jump'; - } - } - return false; - }elseif($moveFromX-2==$this->x && $moveFromY+2==$this->y) { - foreach($this->squares as $thisSquare) { - //echo $this->turn.'-'. $this->inactivePlayer.'-' . $thisSquare->getOccupied() . '
'; - if($thisSquare->getPosX()==$moveFromX-1 && $thisSquare->getPosY()==$moveFromY+1 && $thisSquare->getOccupied()==$this->inactivePlayer) { - if($this->getTurn()=='White') { - $this->playerTwo->deletePiece($thisSquare->getPieceId()); - }elseif($this->getTurn()=='Red') { - $this->playerOne->deletePiece($thisSquare->getPieceId()); - } - $this->validMove=true; - return 'jump'; - } - } - return false; - }elseif($moveFromX-2==$this->x && $moveFromY-2==$this->y) { - - foreach($this->squares as $thisSquare) { - //echo 'Inactive Player: '.$this->inactivePlayer . 'getOccupied'.$thisSquare->getOccupied() . '
'; - if($thisSquare->getPosX()==$moveFromX-1 && $thisSquare->getPosY()==$moveFromY-1 && $thisSquare->getOccupied()==$this->inactivePlayer) { - if($this->getTurn()=='White') { - $this->playerTwo->deletePiece($thisSquare->getPieceId()); - }elseif($this->getTurn()=='Red') { - $this->playerOne->deletePiece($thisSquare->getPieceId()); - } - $this->validMove=true; - return 'jump'; - } - } - $this->validMove=false; - return false; - }else { - $this->validMove=false; - return false; - } - } - - - public function printBoard() { - echo "\n"; - - if ($this->playerTwo->countPieces()<=0) { - echo ''; - }elseif ($this->playerOne->countPieces()<=0) { - echo ''; - }else { - echo "\n\t"; - for($i=0; $i<64; $i++) { - - $square = $this->squares[$i]; - if($i%8==0 && $i>0) { - echo "\n\n\t"; - } - - if($square->getValid()) { - - - $x = $square->getPosX(); - $y = $square->getPosY(); - - if($square->getOccupied()) { - $turn = $this->getTurn(); - $color = $square->getOccupied(); - $id = $square->getPieceId(); - - if($color == 'Red' && $turn == 'Red') { - echo ""; - }elseif($color == 'White' && $turn == 'White') { - echo ""; - }elseif($color == 'Red') { - echo ""; - }elseif($color == 'White') { - echo ""; - } - - }else { - echo ""; - } - - }else { - echo ""; - } - - - } - echo "\n"; - } - echo "\n

White Wins...

Red Wins...

      
"; - } // End method printBoard() - - /* - * GETTERS & SETTERS - */ - public function getPlayerOne() { - return $this->playerOne; - } - public function getPlayerTwo() { - return $this->playerTwo; - } - - public function setTurn($color) { - $this->turn = $color; - } - - public function getTurn() { - return $this->turn; - } - - public function setX($num) { - $this->x = $num; - } - - public function setY($num) { - $this->y = $num; - } - - public function setPiece($id) { - $this->piece = $id; - } - - -} // End class definition - - -class Square { - - private $posX; - private $posY; - private $occupied; - private $pieceId; - private $valid; - - public function __construct($x,$y,$isValid) { - $this->posX = $x; - $this->posY = $y; - $this->occupied = false; - $this->pieceId = null; - $this->valid = $isValid; - } - - - public function getPosX() { - return $this->posX; - } - - public function getPosY() { - return $this->posY; - } - - public function setOccupied($color=false, $id=null) { - $this->occupied = $color; // White, Red, or false - $this->pieceId = $id; - } - - public function getOccupied() { - return $this->occupied; - } - - public function getPieceId() { - return $this->pieceId; - } - - public function getValid() { - return $this->valid; - } - - -} // End class definition \ No newline at end of file diff --git a/smells/index.php b/smells/index.php deleted file mode 100644 index dc7443e..0000000 --- a/smells/index.php +++ /dev/null @@ -1,127 +0,0 @@ -setX($x); - $b->setY($y); - $b->setPiece($id); - if($b->moveIsValid()=='move') { - $b->setBoard(); - if($b->getTurn()=='White') { - $b->setTurn('Red'); - }elseif($b->getTurn()=='Red') { - $b->setTurn('White'); - } - $message = $b->getTurn() . "'s turn..."; - }elseif($b->moveIsValid()=='jump') { - $b->setBoard(); - if($b->getTurn()=='White') { - $b->setTurn('Red'); - }elseif($b->getTurn()=='Red') { - $b->setTurn('White'); - } - $message = 'Jump! Still ' .$b->getTurn() . "'s turn..."; - }else{ - $message = 'Invalid Move. Still ' . $b->getTurn() . "'s turn..."; - } - - - }else { - $message = "Invalid Move. Try Again."; - } -} - -?> - - - - - - - - - Checkers - - - - - - - - -
- '; - var_dump($b); - echo ''; - ?> -
- -
- -

Checkers

-

- -
- - printBoard(); - - ?> - -
- - -
- -
- -
- - - - \ No newline at end of file diff --git a/smells/piece.php b/smells/piece.php deleted file mode 100644 index 0ea6d23..0000000 --- a/smells/piece.php +++ /dev/null @@ -1,64 +0,0 @@ -moveY = array(-1,1); // move options on x-axis - $this->moveX = array($direction); // move options on y-axis - $this->posX = $x; - $this->posY = $y; - $this->pId = $id; - //$this->isKing = false; - } - - public function getPosX() { - return $this->posX; - } - - public function getPosY() { - return $this->posY; - } - - public function getId() { - return $this->pId; - } - - public function getDirection() { - return $this->moveX; - } - - public function setPos($x,$y) { - $this->posX = $x; - $this->posY = $y; - } - - public function king() { - $this->moveX = array(-1,1); // piece is able to move positive & negative on y-axis - } - -/* - public function possibleMoves() { - foreach ($this->moveY as $dirY) { - foreach ($this->moveX as $dirX) { - //$this->moveX; - } - } - - if ($this->posX > 8 || $this->posX < 1 || $this->posY > 8 || $this->posY < 1) { - // Invalid Move! - } - //elseif(This Square Is Occupied) - } - - public function makeKing() { - $this->moveY = array(-1,1); - } -*/ -} // End class definition \ No newline at end of file diff --git a/smells/player.php b/smells/player.php deleted file mode 100644 index 44406a2..0000000 --- a/smells/player.php +++ /dev/null @@ -1,69 +0,0 @@ -color = $color; // White OR Red - $this->direction = $direction; // 1 or -1 - - $this->pieces = array(); - - if($color=='White'){ - $xCoord = array(0,2,4,6,1,3,5,7,0,2,4,6); - $yCoord = array(7,7,7,7,6,6,6,6,5,5,5,5); - }elseif($color=='Red'){ - $xCoord = array(1,3,5,7,0,2,4,6,1,3,5,7); - $yCoord = array(0,0,0,0,1,1,1,1,2,2,2,2); - } - - for ($i=0; $i<12; $i++) { - $y = $xCoord[$i]; - $x = $yCoord[$i]; - $this->pieces[] = new Piece($x,$y,$direction,$i); - } - - } // End constructor - - public function &getPieces() { - return $this->pieces; - } - - public function &getPiece($id) { - return $this->pieces[$id]; - } - - public function countPieces() { - $count = 0; - foreach($this->pieces as $piece) { - if(!is_null($piece)) { - $count++; - } - } - return $count; - } - - public function &deletePiece($id) { - $this->pieces[$id] = null; - return $this->pieces; - } - - public function move($piece,$x,$y) { - $pieceObj = $this->pieces[$piece]; - $pieceObj->setPos($x,$y); - if($this->color=='White' && $x==0) { - $pieceObj->king(); - }elseif($this->color=='Red' && $x==7) { - $pieceObj->king(); - } - //$this->pieces[$piece] = $pieceObj; - } - - -} // End class definition \ No newline at end of file diff --git a/smells/square.php b/smells/square.php deleted file mode 100644 index a4abe2d..0000000 --- a/smells/square.php +++ /dev/null @@ -1,2 +0,0 @@ -