#include <iostream>using namespace std; #define ROW 12#define COL 10class star{public:enum stat_star{willremoved =- 2,hasremoved, normal}; Star () {for (unsigned i = 0; i < ROW, i++) {for (unsigned j = 0; J < COL; J + +) {m_arr[i][j].state = Normal;m_arr[i][j]. visited = false;}}} ~star () {}void print () {cout << "=====================" << endl;for (unsigned i = 0; i < ROW; i++) {for (Unsig Ned j = 0; J < COL; J + +) {cout << m_arr[i][j].data << ("<< m_arr[i][j].state <<") "<<" \ T ";} cout << Endl;}} void resetvisited () {for (unsigned i = 0; i < ROW, i++) {for (unsigned j = 0; J < COL; J + +) {if (m_arr[i][j].state! = Ha sremoved) {m_arr[i][j].visited = false;}}}} Detects if the game is over bool Checkgameover () {for (unsigned i = 0; i < ROW; i++) {for (unsigned j = 0; J < COL; J + +) {if (Findwillremo ve (i, J) > 1) {return false;}}} return true;} Detects whether to move the entire column of void Checkcolmove () {int dt = 0;//column increment for (int i = 0; i < Col-dt; i++) {bool allremoved = true;//detectionWhether or not a column is listed as empty for (int j = 0; J < ROW; J + +) {if (m_arr[j][i].state! = hasremoved) {allremoved = False;break;}} Start Move column if (allremoved) {for (int x = i; x < col, + +) {for (int y = 0; y < ROW; y++) {if (x + 1 < COL) {Star_data TempS tar = m_arr[y][x]; M_ARR[Y][X] = m_arr[y][x + 1];}}} The last column is empty for (int y = 0; y < ROW; y++) {m_arr[y][col-1].state = hasremoved;} i--;d t++;}}} After finding the same ball, delete the same ball void Removesamestar () {//column to find for (int j = 0; J < COL; J + +) {for (int i = ROW-1; I >= 0; i--) {if (M_ar R[i][j].state = = willremoved) {Star_data Tempstar = m_arr[i][j];//Turn the stars down and place the empty stars on top for (int x = i; x >= 0; x--) {if (x -1 >= 0) {M_arr[x][j] = M_arr[x-1][j];}} M_ARR[0][J] = Tempstar;m_arr[0][j].state = hasremoved;i++;}}} Click on a star. Find the star that will be removed int findwillremove (int i, int j) {int findcount = 0;findwillremove (i, J, Findcount); resetvisited (); return Findcount;} find void Findwillremove (int i, int j, int &findcount) to be removed {if (i >= ROW | | I < 0 | | J >= COL | | J < 0 | | M_arr[i][j].state = = hasremoved | | m_arr[i][j].visited) {return;} m_arr[i][j].visited = true;findcount++;//Left if (i-1 >= 0 && m_arr[i-1][j].state! = hasremoved && M_arr [I-1] [J].data = = m_arr[i][j].data) {m_arr[i][j].state = Willremoved;m_arr[i-1][j].state = Willremoved;findwillremove (I-1, J , Findcount);} Right if (i + 1 < ROW && M_arr[i + 1][j].state! = hasremoved && m_arr[i + 1][j].data = m_arr[i][j].data) {m _arr[i][j].state = willremoved;m_arr[i + 1][j].state = willremoved;findwillremove (i + 1, J, Findcount);} On if (j + 1 < COL && M_arr[i][j + 1].state! = hasremoved && m_arr[i][j + 1].data = m_arr[i][j].data) {m _arr[i][j].state = willremoved;m_arr[i][j + 1].state = Willremoved;findwillremove (i, j + 1, findcount);} Next if (j-1 >= 0 && m_arr[i][j-1].state! = hasremoved && m_arr[i][j-1].data = m_arr[i][j].data) {m_ Arr[i][j].state = Willremoved;m_arr[i][j-1].state = Willremoved;findwillremove (i, j-1, Findcount);}} struct Star_data{int data;//data Stat_star state;//state bool visited;//Access}; Star_data m_arr[row][col];}; int main () {Star star;int-arr[row][col] = {2, 1, 3, 2, 1, 2, 1, 3, 2, 1, 2, 2, 5, 1, 2, 2, 2, 5, 1, 2, 3, 2, 1, 3, 3, 3, 2, 1 , 3, 3, 2, 5, 4, 1, 4, 2, 5, 4, 1, 4, 2, 2, 3, 2, 5, 2, 2, 3, 2, 5, 2, 1, 3, 2, 1, 2, 1, 3, 2, 1, 2, 2, 5, 1, 2, 2, 2, 5, 1, 2, 3, 2, 1, 3, 3, 3, 2, 1, 3, 3, 2, 5, 4, 1, 4, 2, 5, 4, 1, 4, 2, 2, 3, 2, 5, 2, 2, 3, 2, 5, 2, 5, 4, 1, 4, 2, 5, 4, 1, 4 , 2, 5,2, 2, 3, 2, 5};for (unsigned i = 0; i < ROW; i++) {for (unsigned j = 0; J < COL; J + +) {Star.m_arr[i][j].data = ARR[I][J];}} Star.print (); star.findwillremove (0, 0); Star.print (); Star.removesamestar (); Star.print (); Star.checkcolmove (); Star.print (); Star.checkgameover ()? cout << "Over": cout << "no over"; System ("pause"); return 0;}
An array of efficient algorithms for destroying stars (C + + code, console program)