睡覺請關燈   遊戲模擬實現

 首先,關於這個的基本討論已過一階段了。現在就是合成版  以後做mcf java等的界面。
留了 一個bug就是 電腦解決完後 會多打印幾個矩陣。
改進的地方,1:增加了一種電腦解法,
        2:記錄用了自增棧  
#include"c.h"
#include"c++.h"
#define  MAX  5
#define N   10
typedef 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())
  {
   IncSize();
  }
  data[top++] = value;
  return true;
 }
  
 int gettop()//得到棧頂元素
 {
  return data[--top];           
 }
 
 bool isfull()//判斷是否已滿
 {
  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;
    
}
int 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);}
           
         }      
         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;
}
  
  
void cp(int v[][MAX],Stack &s)
{    
    int y=s.gettop();
    int x=s.gettop();
    play(v,x,y);show(v); 
  cout<<"電腦顯示完畢 系統已退出 "<<endl;
}
 static  int  cc=1;

int player()
{    
 while(cc)  
    {
   Stack s;//建立一個棧     
 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<<"熄燈前睡覺  歡迎來到第 "<<cc<<" 關"<<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<<"熄燈前睡覺 輸入座標(x,y),(0,0)退出"<<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<<"電腦顯示完畢  玩家步數太多,系統已退出 "<<endl;state=0;}
       play(v,x,y);  show(v);
      }    
                 if(x+y>10&&x+y<20){ cp(v,s);state=0;}
                 if(x+y>20)cp2(v);
 }
     
    }
}
int main()
{
   cout<<"***************************************"<<endl;
   cout<<"          歡迎參與點燈遊戲     "<<endl<<"              由wzzx提供"<<endl;
   cout<<"   遊戲過程中輸入x+y大於10時可以選擇電腦演示求解步驟  "<<endl;
   cout<<"***************************************"<<endl;
   player();
 return 0;
}

 關於循環與函數調用的複雜度,以下給一個解:

#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;
 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)
 {
  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<<"已通關"<<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<<"所走步數:"<<count<<"步"<<endl;
}
void Menue()
{
 cout<<"------------關燈遊戲------------"<<endl;
 cout<<"          1.開始遊戲            "<<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);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章