Example of an algorithm that uses PHP to implement happy message consumption. Example of a php Algorithm

Source: Internet
Author: User

Example of an algorithm that uses PHP to implement happy message consumption. Example of a php Algorithm

Preface

This article mainly introduces how to implement the happy and happy algorithms that we all know about PHP. We will share these algorithms for your reference and learning. I will not talk about them here. Let's take a look at the detailed introduction.

I. Requirement Description:

1. Random color blocks of 5 colors appear in an 8*8 matrix square.

2. When three or more color blocks are connected horizontally or vertically, these color blocks are eliminated.

3. After the color block is eliminated, the color block on the top is moved down and the random Color Block filling matrix is left blank.

4. Repeat steps 2 and 3.

5. Eliminate 3 Identical color blocks plus 10 points, 4 plus 15 points, 5 plus 20 points, 6 plus 30 points, and 7 plus 40 points, 8 + 70 points, 9 + 100 points, 10 + 150 points, then each additional one will be 50 points more than the previous one.

Ii. Code above

<? Php // initialization data of all images. The key represents the location, and the value represents the color $ xxl = array ('','','','','','', '',''), array ('',''), array ('', '',''), array ('','','','','', '','', ''), array ('','','','','','','',''), array ('',''), array ('','','', '',''), array ('','','','','','','', ''),); $ point = play ($ xxl, $ point); // start game echo" \ n Total points: {$ point} ";/* start to remove * $ xxl array all graph sets * $ point int points acquisition */$ bu = 0; function play ($ xxl, $ point) {global $ bu; $ bu ++; echo '============================================== start '. $ bu. 'Step ========================================= '; $ color = array (1 => 'red', 2 => 'green', 3 => 'yellow', 4 => 'blue ', 5 => 'black'); // represents 5 colors $ samCol = array (); // a set of color blocks connected to columns $ nowCol = array (); // The connected color block pointer $ samArr = array (); // The total set of connected color blocks $ group = 1; // The group pointer/ /Randomly fill in the color and obtain the connected color block start foreach ($ xxl as $ k1 => $ v1) {$ sam = array (); // a set of connected color blocks on the row $ now = 1; // The color block pointer foreach ($ v1 as $ k2 = >$ v2) {if (empty ($ v2) | $ v2 = '') {$ v2 = $ xxl [$ k1] [$ k2] = array_rand ($ color); // random fill color} if (! Isset ($ nowCol [$ k2]) {$ nowCol [$ k2] = 1;} if ($ k1 = 0) {$ samCol [$ k2] [$ nowCol [$ k2] [$ k1. '-'. $ k2] = array ($ k1, $ k2, $ v2, $ k1. '-'. $ k2. '-'. $ v2);} else {if ($ v2! = $ Xxl [$ k1-1] [$ k2]) {// the same column is different from the previous one in color $ nowCol [$ k2] ++ ;} $ samCol [$ k2] [$ nowCol [$ k2] [$ k1. '-'. $ k2] = array ($ k1, $ k2, $ v2, $ k1. '-'. $ k2. '-'. $ v2);} if ($ k2 = 0) {$ sam [$ now] [$ k1. '-'. $ k2] = array ($ k1, $ k2, $ v2, $ k1. '-'. $ k2. '-'. $ v2);} else {if ($ v2! = $ Xxl [$ k1] [$ k2-1]) {// the same row is different from the previous $ now ++;} $ sam [$ now] [$ k1. '-'. $ k2] = array ($ k1, $ k2, $ v2, $ k1. '-'. $ k2. '-'. $ v2) ;}// obtain the connected color block on the row start foreach ($ sam as $ x =>$ y) {if (count ($ y)> 2) {$ key = 'r -'. $ group; foreach ($ y as $ x2 => $ y2) {$ y [$ x2] ['group'] ['R'] = $ key ;} $ samArr + = $ y; $ group ++ ;}// obtain the end Color Block connected to the row} // random fill color, and get the end Color Block connected to the row. // get the connected color block start $ group = 1; foreach ($ samCol as $ k => $ v) {foreach ($ V as $ x => $ y) {if (count ($ y)> 2) {$ key = 'l -'. $ group; foreach ($ y as $ x2 => $ y2) {$ y [$ x2] ['group'] ['l'] = $ key; if (isset ($ samArr [$ x2] ['group'] ['R']) {// determine whether the current vertex has exists in the horizontal group $ samArr [$ x2] ['group'] ['l'] = $ key ;}}$ samArr + = $ y; $ group ++ ;}}// obtain the end Color Block of the connected columns // search for the connected color block start $ res = array (); // The connected color block set $ hasRes = array (); foreach ($ samArr as $ k =>v v) {if (isset ($ hasRes [$ k]) {continue ;}$ arr = array (); seek ($ sam Arr, $ v, $ arr); $ res [] = array_keys ($ arr); $ hasRes + = $ arr;} // find the end show ($ xxl) of connected color blocks ); // print the image before elimination if (empty ($ res )) {// exit the recursive echo '============================== ===== completed! ======================================= '; Return $ point;} $ thisPoint = countPoint ($ res); // calculate the points for this elimination. $ point + = $ thisPoint; // accumulate to total points // remove the connected color block start $ next = $ xxl; foreach ($ res as $ k => $ v) {foreach ($ v as $ k2 = >$ v2) {$ y = $ samArr [$ v2] [0]; $ x = $ samArr [$ v2] [1]; $ xxl [$ y] [$ x] = '*'; unset ($ next [$ y] [$ x]);} // eliminate the end show ($ xxl) of the connected color block; // print the image at the time of elimination $ next = step ($ next); show ($ next ); // print the image echo after elimination. "Number of points obtained for this elimination: {$ thisPoint} \ n"; return play ($ next, $ point );} /* calculate the number of points x $ xxl array connected color block set */function countPoint ($ xxl) {// initialize the point configuration start $ config = array (3 => 10, 4 => 15, 5 => 20, 6 => 30, 7 => 40, 8 => 70, 9 => 100); for ($ I = 10; $ I <= 64; $ I ++) {$ config [$ I] = 100 + ($ I-9) * 50 ;} // initialize the point configuration end $ point = 0; foreach ($ xxl as $ v) {$ key = count ($ v ); $ point + = $ config [$ key];} return $ point;}/* remove and move left * $ xxl array all graph sets */function step ($ xxl) {foreach ($ xxl as $ k => $ v) {$ temp = array_merge ($ v); $ count = count ($ temp); if ($ count = 8) {continue;} for ($ I = $ count; $ I <= 7; $ I ++) {$ temp [$ I] = '';} $ xxl [$ k] = $ temp;} return $ xxl ;} /* Find an adjacent vertex * $ xxl array Connected Graph Set * $ one array a vertex * $ adjacent vertex in the arr array Graph Set */function seek ($ xxl, $ one, & $ arr) {// global $ I; $ near = array (); $ near ['up'] = ($ one [0]-1 ). '-'. $ one [1]; // the above point $ near ['drop'] = ($ one [0] + 1 ). '-'. $ one [1]; // The following vertex $ near ['left'] = $ one [0]. '-'. ($ one [1]-1); // The left vertex $ near ['right'] = $ one [0]. '-'. ($ one [1] + 1); // The right vertex foreach ($ near as $ v) {if (isset ($ xxl [$ v]) & $ xxl [$ v] [2] ==$ one [2]) {// locate the adjacent vertex $ xj = array_intersect ($ one ['group'], $ xxl [$ v] ['group']); if (empty ($ xj) {// if the adjacent vertex is not in this group, skip continue ;} if (isset ($ arr [$ v]) {// if this point has been traversed, skip continue;} $ arr [$ v] = $ xxl [$ v]; seek ($ xxl, $ xxl [$ v], $ arr ); // continue to find the adjacent vertex }}/ * print the graph * $ xxl array all the graph sets */function show ($ xxl) {// clockwise rotation matrix start $ arr = array (); foreach ($ xxl as $ k = >$ v) {foreach ($ v as $ k2 => $ v2) {$ arr [7-$ k2] [$ k] = $ v2 ;}} ksort ($ arr); // clockwise rotation matrix end $ str = ''; foreach ($ arr as $ v) {foreach ($ v as $ v2) {$ str. = ''. $ v2;} $ str. = "\ n";} echo "\ n ". $ str ;}

The running result is as follows:

12345 represents five colors.

====================================== Start Step 1 = ============================== 3 3 3 2 2 1 1 1 4 4 3 4 3 4 1 3 3 4 1 1 1 1 4 1 2 2 3 3 3 1 2 4 4 4 4 2 2 2 2 4 3 3 2 2 2 3 2 3 3 1 1 5 2 1 3 2 1 4 5 4 5 1 3 3 3 3 3 3 2 2 2*4 4 3*3 1*1 4*2 2 3*3 1 2 4 4 4 2 **** 1 4 3 3 3 2 1*3 1 1 1 1 5 2 1 3*1 4 3 4 5 1 3 3 3 3 3 4 4 3 2 3 3 3 1 1 2 2 3 4 4 4 4 2 3 4 2 2 4 3 3 3 2 1 3 1 1 1 5 2 1 3 1 1 4 5 3 4 5 1 3 3 3 3 the number of points earned by this elimination: 55 ==================================== start Step 1 = ============================== 3 3 3 2 2 3 2 4 4 3 3 2 1 3 3 3 3 3 3 3 4 1 4 2 2 3 5 1 4 4 4 4 2 3 4 2 4 4 3 3 2 1 1 5 2 1 3 1 1 4 4 4 5 1 3 3 3 3 3 3 2 3 3 3 3 3 4 4 3 3 2 1 *** 3 1 3 3 3 4 1 4 2 2 3 5 1 2*4 2 3 4 2 1 4 3 3 2 1 1 3 1 1 1 1 1 2 2 3 1 1 4 5 3 4 5 1 3 3 3 3 3 2 2 3 4 3 3 1 3 3 3 3 3 3 2 4 2 3 5 1 2 4 2 2 2 3 4 4 3 3 3 3 2 1 1 1 1 1 5 2 1 1 1 4 5 3 4 4 5 1 3 2 3 3. The number of points obtained after this elimination: 20 ==================================== start Step 1 = ============================== 3 3 3 2 2 3 4 1 3 4 3 3 2 1 4 2 5 3 3 3 3 3 2 2 2 3 5 1 2 2 4 2 2 2 3 4 4 3 3 2 1 1 1 1 1 1 5 2 1 3 1 1 4 5 4 5 1 3 3 3 3 ============================== ======== completed! ================================================ Total number of points received: 75

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.