Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a lightweight web service in PHP for playing Omok games. Omok (a.k.a. Gomoku and Gobang), literally meaning five pieces, is a two-player strategy game

Write a lightweight web service in PHP for playing Omok games. Omok (a.k.a. Gomoku and Gobang), literally meaning five pieces, is a two-player strategy game typically played with go pieces --- black and white stones --- on a go board with 15x15 intersections (or places) [Wikipedia]. It is also possible to play the game with paper and pencils because stones, once placed, can't be moved or removed from the board. The two players alternate and place a stone of their color on an empty intersection. The game objective is to put one's stones in a row of five consecutive places vertically, horizontally, or diagonally. The winner is the first player to create an unbroken row of five stones. As a Web service, your PHP code provides a few functions implementing the logic, data, and process for playing Omok games, not a complete Web application. Do not include any GUI components in your web service. The service APIs are predefined and accessed by specifying URLs as described below. Below are the minimum requirements that your implementation has to satisfy. R1: The web service shall work with a provided Java client available and downloadable from the course website (omokClient.jar). R2: The web service shall support multiple clients at the same time, each client playing against the server (see R5 below). R3: A game board shall consist of 15*15 intersections (or places) on which black/white stones can be placed. R4: An individual place of a board shall be identified by a pair (x,y) of 0-based column/row indices, e.g., (0,2) for the intersection at the first column and the third row. R5. The Web service shall provide at least two different move strategies for the computer, e.g., Random and Smart. The Smart strategy shall be "smart" to allow for a realistic play. Minimally, it should detect a winning/losing row, e.g., three consecutive discs with an open end. Hint: Use the Strategy design patterns to support different computer move strategies [Chapter 5 of 2]. R6. The Web service shall determine the outcome of a game: win, loss, or draw. The web service APIs are implemented as URLs as specified below. All communications between the web service and a client shall be done using the HTTP query string and the JavaScript Object Notation (JSON), a lightweight data-interchange format (see www.json.org) [3]. All inputs to the web service shall be obtained from HTTP query strings, and all outputs to the client shall be written in JSON. Following the REST principles, the web service shall be stateless and provide the following three URLs: 
1. https:///info (short for .../info/ or .../info/index.php), where  is the address of your omok service typically consisting of a hostname and a pathname. Provide game information, including the board size and available computer move strategies. Below is a sample JSON output. {"size": 15, "strategies": ["Smart", "Random"]} Use json_encode() function to create a JSON (string) representation of a PHP value or object. 2. https:///new?strategy=s Create a new game to play against the specified computer strategy. A normal response will be a JSON string like: {"response": true, "pid": "55ed3eb95f5a3"} where pid is a unique play identifier generated by the web service. It will be used to play the newly created game (see 3 below). As shown below, an error shall be notified to the client by providing an an appropriate error response. {"response": false, "reason": "Strategy not specified"} {"response": false, "reason": "Unknown strategy"} Use the uniqid() function to generate a unique identifier based on the current time in microseconds. Use the Strategy design pattern to define different strategy classes, e.g., RandomStrategy and SmartStrategy [Chapter 5 of 2]. 3. https:///play?pid=p&x=x&y=y Make a move by placing a stone on the specified place (x,y) to play the specified game (p). Example: .../play/?pid=57cdc4815e1e5&x=4&y=5. A normal response will be a JSON string like:
{"response": true, "ack_move": { "x": 4, "y": 5, "isWin": false, // winning move? "isDraw": false, // draw? "row": []}, // winning row if isWin is true "move": { "x": 4, "y": 6, "isWin": false, "isDraw": false, "row": []}} where "ack_move" is the acknowledgment and the outcome of the requested move of the player, and "move" is the computer move made right after the player's; there will be no computer move if the player's move is a game-ending (win or draw) move. For a winning move, the value of "row" is an array of numbers denoting the indices of the winning row [x1,y1,x2,y2,...,xn,yn], where x's and y's are 0-based column and row indices of places, e.g., "row":[6,7,7,7,8,7,9,7,10,7] If there is an error, it should be reported to the client by providing an appropriate error message like: {"response": false, "reason": "Pid not specified"} {"response": false, "reason": "Unknown pid"} {"response": false, "reason": "x not specified"} {"response": false, "reason": "y not specified"} {"response": false, "reason": "Invalid x coordinate, 20"} {"response": false, "reason": "Invalid y coordinate, 30"}
 Define several classes to model the omok game, say, Play, Board, etc. The states of some of these classes need to be stored externally, e.g., in a file, because the web service sessions are stateless and the game state should be preserved between sessions. You may use JSON to encode and persist game states. You may use the player identifier (pid) as a file name to store the game state, and the game state may be stored as or restored from a JSON string; use json_encode() and json_decode() functions. Use json_decode() function to convert a JSON encoded string to a PHP value or object.
With the above three web service URLs, an omok game can be played as follows: Step 1: visit https:///info to find game info Step 2: visit https:///new?strategy=s to create a new game Step 3: repeatedly visit https:///play?pid=p&move=x,y to place a stone, e.g. in pseudo code: while (true) { visit https:///play?pid=p&move=x,y if (ack_move.isWin) { break; // player won } else if (ack_move.isDraw) { break; // draw } else if (move.isWin) { break; // computer won } else if (move.isDraw) { break; // draw } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Discuss the steps in the development planning process. page 381

Answered: 1 week ago

Question

What is conservative approach ?

Answered: 1 week ago

Question

6. The cost of the training creates a need to show that it works.

Answered: 1 week ago