Sleep please turn off the lights game simulation implementation

Source: Internet
Author: User

  First of all, the basic discussion about this has gone through a phase. Now is the composite version    later do Mcf java interface. Leave a   a bug is   after the computer resolved   will print a few more matrices. Improved place, 1: Added a computer solution,         2: recorded using the self-increment stack    #include "c.h" #include "c ++.h "#define   max  5#define n   10typedef int  elem_ Type;  class stack{public:  stack ()  {   top = 0;   size = n;  data = new elem_type[size]; } ~stack ()  {    top = 0;  delete []data;  data = null; }   void incsize ()  {  size = 2*size;  elem_type *p =  new elem_type[size];  memcpy (p,data,sizeof (Elem_type) *top);   delete []data ;   data = p;  } bool push (Elem_type value)  {  if ( Isfull ())   {&Nbsp;  incsize ();   }  data[top++] = value;  return true ;  }   int gettop ()//get stack top element  {  return data[--top];             }  bool isfull ()//Judging whether it is full  {   return top == size;  }    private: elem_type*  data; int top; int size;}; Int show (Int arr[][max]) {  int count=0; for (int i=0;i<max;i++)  {   for (int j=0;j<max;j++)   {                     if (arr[i][j]==1) count=1;           cout<<arr[i][j]<<  " ";  }   cout<<endl; } cout<<endl;    return 0;} Void play (int arr[][max],int x,int y) { if (x < 0 | |  y < 0 | |  x >= max | |  y >= max)  {  return ; } arr[x][y] = 1 -arr[x][ Y]; if (x-1 >= 0)  {  arr[x-1][y] = 1 - arr[x-1][y]; }  if (X+1 < max)  {  arr[x+1][y] = 1 - arr[x+1][y]; }  if (y-1 >= 0)  {  arr[x][y-1] = 1 - arr[x][y-1]; }  if (Y+1 < max)  {  arr[x][y+1] = 1 - arr[x][y+1]; }} Int cheack (int a[][max],int i) { int j = 0,state=0;;  while (J++ < max)  {  if (a[i][0] == 1) {play (a,i,0); show (a); state=1;}   if (a[i][4] == 1) {play (a,i,4); show (a); state=1;}   if (a[i][j] == 1) { break;}  }      if (state==0)  { play (A,I,J); show (a); state=1;}                return state; &NBSP;&NBSP;&NBSP;&NBSP;}INT&NBSP;CP2 (Int a[][max])  {   int state=1,i=0,j=0;       show (a);      while (state)       {   state=cheack (a,i);         for (i=0;i<MAX-1; i++)           {             for (j=0;j<max;j++)               if (a[i][j]==1) {play (a,i+1,j); show (a);}                      }        &nbsP;      state=cheack (a,i);          for (i=max-1;i>0&&state;i--)           {             for (j=max-1;j>=0;j--)               if (a[i][j]==1) {play (a,i-1,j); show (a);}          }        state= Show (a);              } cout  <<  "Over"  << endl; return 0;} &NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;CP (int v[][max],stack &s) {         int y=s.gettop ();     int x=s.gettop ();     play (v , x, y), show (v);   cout<< "Computer display complete   system exited  " &LT;&LT;endl;}  static  int  cc=1;int player () {     while (cc)        {   stack s;//Build a stack       int  v[MAX][MAX] = {0}; int x,y;        int  k=3,i=0;        int state=1;         time_t t; srand ((unsigned) time (&t));         K=rand ()%5+1;      cout<< "Sleep before lights out    Welcome to section   <<cc<<"   Off "<<endl;      cc++;while (i++<k) {        x=rand ()%5;    y=rand ()%5;     s.push (x);     s.push (y);     play (v,x,y);}  show (v);        while( state)  {  cout<< "Sleep before lights out   Enter coordinates (x, y), (0,0) exit" <<endl;   cin >>x>>y;     if (x==0&&y==0) cc=0;    x=x-1;y= Y-1;    if (x>-1&&x<5&&y>-1&&y<5)      {   i++; s.push (x);  s.push (y);       if (i++==100)        { cout<< "Computer shows up    player steps too much, system has exited  " << endl;state=0;}        play (v,x,y);   show (v);       }                      if (x+y>10&&x+y<20) {&NBSP;CP (v,s); state=0;}                  if (x+y &GT;20) Cp2 (v); &NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;    }}int main () {   cout<< "********************************* "<<endl;   cout<<"             Welcome to participate in the lighting game       <<endl<<                 provided by Wzzx "<<endl;   cout<<"      When entering x+y greater than 10 during gameplay, you can select the computer demonstration solution Step    <<endl;   cout<< ******************** "<<endl;   player ();  return 0;}

For the complexity of loops and function calls, here's a solution:

#include <iostream>
#include <time.h>
using namespace Std;

#define MAX 5
int count = 0;

void change (int arr[][max],int x,int y);

void build_game (int arr[][max])
{
 arr[0][0] = 1;
&NBSP;ARR[0][1] = 1;
 arr[1][0] = 1;
 srand (NULL);
 int i;
 int J;
 int k = rand ()%30;
 while (k--)
 {
  i = rand ()%max;
  j = rand ()%max;
  change (ARR,I,J);
 }
}
void Show (int arr[][max])
{
 for (int i=0;i<max;i++)
 {
  for (int j=0;j <max;j++)
  {
   cout<<arr[i][j]<< "";
  }
  cout<<endl;
 }
 cout<<endl;
}


void Change (int arr[][max],int x,int y)
{
 if (x < 0 | | Y < 0 | | x >= Max | | y >= max)
&nb Sp {
  return;
 }
 arr[x][y] = 1-arr[x][y];
 if (x-1 >= 0)
 {
  arr[x-1][y] = 1-arr[x-1][y];
 }
 if (X+1 < MAX)
 {
  arr[x+1][y] = 1-arr[x+1][y];
 }
 if (y-1 >= 0)
 {
  arr[x][y-1] = 1-arr[x][y-1];
 }
 if (Y+1 < MAX)
 {
  arr[x][y+1] = 1-arr[x][y+1];
 }
}
int search (int arr[][max],int n)
{
 int k = 0;
 while (K < MAX)
 {
  if (arr[n][k] = = 1)
  {
   return k ;
  }
  k++;
 }
 return-1;
}

int Click (int arr[][max])
{
if (arr[0][0] = = 1)
{
Change (arr,0,0);
}
else if (arr[0][max-1] = = 1)
{
Change (arr,0,max-1);
}
else if (arr[max-1][0] = = 1)
{
Change (arr,max-1,0);
}
else if (arr[max-1][max-1] = = 1)
{
Change (arr,max-1,max-1);
}
Else
{
int k = search (arr,0);
if (k = =-1)
{
K = Search (arr,max-1);
if (k = =-1)
{
cout<< "Customs clearance" <<endl;
return 0;
}
Change (ARR,MAX-1,K);
}
Else
{
Change (ARR,0,K);
}
}
return 1;
}

void solve (int arr[][max])
{
int i = 0;
int j = 0;
int flag = 0;
int FF = 0;
while (1)
{
if (FF! = 0)
{
if (Click (arr) = = 0)
{
Break
}
count++;
}
Show (arr);
if (flag = = 0)
{
while (I < MAX-1)
{
j = Search (arr,i);
if (j = =-1)
{
i++;
Continue
}
Change (ARR,I+1,J);
count++;
Show (arr);
}
flag = 1;
i = MAX-1;
}
Else
{
while (i > 0)
{
j = Search (arr,i);
if (j = =-1)
{
i--;
Continue
}
Change (ARR,I-1,J);
count++;
Show (arr);
}
Flag = 0;
}
FF = 1;
}
cout<< "Steps taken:" <<count<< "Step" <<endl;
}
void Menue ()
{
cout<< "------------off the Lights game------------" <<endl;
cout<< "1. Start the game" <<endl;
}

void play (int arr[][max])
{
Build_game (arr);
Show (arr);
int x, y;
while (1)
{
cin>>x>>y;
Change (arr,x,y);
Show (arr);
}


}
int main ()
{
int Arr[max][max] = {};
int x;
int y;
Build_game (arr);
Show (arr);
Solve (arr);
}

Sleep please turn off the lights game simulation implementation

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.