Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A simple API to play the Connect Four game. The API consists of three URLs: info

ID: 3755296 • Letter: A

Question

A simple API to play the Connect Four game. The API consists of three URLs: info, new and play.

Info

Provide information about the service, including the dimension of the grid and the computer move strategies.

Request URL
http://www.cs.utep.edu/cheon/cs3360/project/c4/info/

Response
Service information encoded in JSON

JSON fields in response body:

Example: see Java client code.

Java client code:

New

Create a new game to play against the specified computer strategy.

Request URL
http://www.cs.utep.edu/cheon/cs3360/project/c4/new/?strategy=s

Required request parameters:

Response
Unique Id of the newly created game, or an error message.

JSON fields in response body:

Examples: see Java client code.

Java client code:

Play

Make a move by dropping a disc in a slot.

Request URL
http://www.cs.utep.edu/cheon/cs3360/project/c4/play/?pid=p&move=x

Required request parameters:

Response
Acknowledgement of the player's move along with the computer's move, or an error message.

JSON fields in response body:

Both the ack_move and move have the following fields:

Examples: see Java client code.

Java client code:

Test with this code:

Field Type Description width Integer Number of columns of the grid height Integer Number of rows of the grid strategies Array Supported computer move strategies

Explanation / Answer

Answer:

<?php

class ConnectFour {

                protected $_rows = 6;

                protected $_columns = 6;

                protected $_board_array = array();

                protected $_current_player = 0;

                protected $_moves = 0;

                function __construct( $rows = 6, $cols = 6){

                                $this->_setDimensions( $rows, $cols );

                                $this->_initGame();

                }

               

               

                protected function _initializeGameBoard(){

                                //resets the board array

                                $_board_array = array();

                                for($i = 0; $i < $this->getRows() ; $i ++ ){

                                                $_board_array[$i] = array();

                                                for($j = 0; $j < $this->getColumns() ; $j ++ ){

                                                                //-1 means this slot is unoccupied.

                                                                $_board_array[$i][$j] = -1;

                                                }

                                }

                                $this->_setCurrentBoard($_board_array);

                }

               

                protected function _initGame(){

                                //Setup our game board

                                $this->_initializeGameBoard();

                                //Set a random player to start first

                                $this->_setCurrentPlayer(rand(1,2));

                               

                                //start dropping pieces

                                $this->_dropPiece();

                }

               

                protected function _dropPiece(){

                                //Check if total moves reached. (Recursive baseline)

                                if( $this->_moves >= ( $this->getRows() * $this->getColumns() )) {

                                                //No winner then =(

                                                $this->_showNoWinnerMessage();

                                                return false;

                                }

                               

                                //Random column chosen for placing chips

                                $_target_col = rand(0, $this->getColumns()-1);

                                $_current_board = $this->_getCurrentBoard();

                                for( $row = $this->getRows()-1; $row>=0; $row-- ){

                                                //If slot is currently empty

                                                if( $_current_board[$row][$_target_col] === -1 ){

                                                                //Set slot to current player

                                                                $_current_board[$row][$_target_col] = $this->_getCurrentPlayer();

                                                                //Update the no. of moves, might wana setter/getter this

                                                                $this->_moves++;

                                                                //Update the board

                                                                $this->_setCurrentBoard($_current_board);

                                                                //Print current board

                                                                $this->_printBoard();                                    

                //Check for winner

                                                                if( $this->_checkForWinner( $row, $_target_col ) ){

                                                                               

                                                                                //If winner is found

                                                                                $this->_showWinnerMessage();

                                                                               

                                                                                return false;                                                      

}

else{

                                                                               

                                                                                //Else continue the game

                                                                                //Change player

                                                                                $this->_togglePlayer();

                                                                                //Drop the piece

                                                                                $this->_dropPiece();

                                                                }

                                                               

                                                                //exit once a piece is dropped for this move

                                                                return false;

                                                }

                                }

                               

                                //If it comes to here, it means no slots are empty (column is full). Redo move again

                                $this->_dropPiece();

                               

                }

               

                protected function _printBoard(){

                               

                                print '<p>Player '. $this->_getCurrentPlayer() .': Move No. ' . $this->_moves . '</p>';

                                print '<table>';

                                $_board_array = $this->_getCurrentBoard();     

                                for($i = 0; $i < $this->getRows() ; $i ++ ){

                                                print '<tr>';

                                                for($j = 0; $j < $this->getColumns() ; $j ++ ){

                                                                //decoration

                                                                $_class = "";

                                                                if( $_board_array[$i][$j] === 1 ){

                                                                                //player 1 color

                                                                                $_class = "player-1";

                                                                }else if( $_board_array[$i][$j] === 2 ){

                                                                                //player 2 color

                                                                                $_class = "player-2";

                                                                }

                                                                print '<td class="'.$_class.'" >' . $_board_array[$i][$j] . '</td>';                  

                                                }

                                                print '</tr>';

                                }

                                print '</table>';

                }

                protected function _showWinnerMessage(){

                               

                                print '<p class="message">Player ' . $this->_getCurrentPlayer() .' wins the game!</p>';

                               

                }

               

               

                protected function _showNoWinnerMessage(){

                                print '<p class="message">No winner for this round.</p>';

                }

                protected function _togglePlayer(){

                               

                                $this->_setCurrentPlayer($this->_getCurrentPlayer()===1?2:1);

                }

               

                protected function _getCurrentPlayer(){

                                return $this->_current_player;                

                }

               

                protected function _setCurrentPlayer( $player_no ){

                                $this->_current_player = $player_no;   

                }

               

               

                protected function _getCurrentBoard(){

                                return $this->_board_array;      

                }

               

                protected function _setCurrentBoard( $board_array ){

                                $this->_board_array = $board_array;    

                }

               

               

                protected function _checkForWinner( $row, $col ){

                                if($this->_horizontalCheck($row, $col)

                                                                || $this->_verticalCheck($row, $col)

                                ){

                                                return true;

                                }

                                return false;

                }

               

               

                private function _horizontalCheck( $row, $col ){

                               

                                $_board_array = $this->_getCurrentBoard();

                                $_player = $_board_array[$row][$col];

                                $_count = 0;

                               

                                //count towards the left of current piece

                                for ( $i = $col; $i>=0; $i-- )

                                {

                                                if( $_board_array[$row][$i] !== $_player ){

                                                                break;

                                                }

                                                $_count++;

                                }

                               

                                //count towards the right of current piece

                                for ( $i = $col + 1; $i<$this->getColumns(); $i++ )

                                {             

                                                if( $_board_array[$row][$i] !== $_player ){

                                                                break;

                                                }

                                                               

                                                $_count++;        

                                }

                                return $_count>=4 ? true : false;

                }

               

               

                private function _verticalCheck( $row, $col ){

               

                                //if current piece is less than 4 pieces from bottom, skip check

                                if ( $row >= $this->getRows()-3 ) {

                                               

                                                return false;

                                }

                               

                                $_board_array = $this->_getCurrentBoard();

                                $_player = $_board_array[$row][$col];

                               

                                for ( $i = $row + 1; $i <= $row + 3; $i++ ){

                                                if($_board_array[$i][$col] !== $_player){

                                                                return false;      

                                                }

                                }

                               

                                return true;

                               

                }

               

                protected function _setDimensions($rows = 6, $cols = 6){

                                if(!isset($rows)) return;

                                $this->setRows($rows);

                                $this->setColumns($cols===null?$rows:$cols);

                }

               

               

                public function setRows($rows = 6){

                                $this->_rows = $rows;

               

                }

                public function getRows(){         

                                return $this->_rows;     

                }

               

                public function setColumns($col = 6){

                                $this->_columns = $col;

                }

               

               

                public function getColumns(){

                                return $this->_columns;

                }

}

?>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote