簡単なヘビ食いゲーム実現


ヘビを貪る機能を実現する:
(1)食いしん坊蛇ゲームの碁盤図の定義
(2)碁盤の初期化
(3)ボードの位置情報を出力する
(3)ゲームの難易度の選択
(4)ランダムに食べ物を作る
(5)ゲーム動態の更新
(6)ゲーム対応の操作を設定する
(7)ゲーム結果の印刷
コード実装:
#include   
#include   
#include   
#include   
#include   
#include   
#include    
using namespace std;
const int N = 32;	//         

//           
class SnakePosition
{
public:
	int x, y;      //x   ,y     

	void Initialize(int & j)//     
	{
		x = 1;
		y = j;
	}
};

//          , (N-2)*(N-2)     
SnakePosition position[(N - 2)*(N - 2) + 1]; 

class SnakeMap//         
{
public:
	SnakeMap(int h = 4, int t = 1, int l = 4, char d = 77, int s = 0)	//  
		: length(l)
		, direction(d)
		, head(h)
		, tail(t)
		, score(s)
	{}

	void Initialize()   //       ,             
	{
		int i, j;
		for (i = 1; i <= 3; i++)
			s[1][i] = '*';
		s[1][4] = '#';
		for (i = 1; i <= N - 2; i++)
		for (j = 1; j <= N - 2; j++)
			s[i][j] = ' '; //                 
		for (i = 0; i <= N - 1; i++)
			s[0][i] = s[N - 1][i] = '-'; //              
		for (i = 1; i <= N - 2; i++)
			s[i][0] = s[i][N - 1] = '|'; //              
	}

	void ShowGame()		//          
	{
		system("cls"); //     
		int i, j;
		cout << endl;
		for (i = 0; i= 8 && gameauto)
			{
				length -= 8;
				grade++;
				if (gamespeed >= 200)
					gamespeed -= 200; //            
				else
					gamespeed = 100;
			}
			s[x][y] = '#';  //      
			s[position[head].x][position[head].y] = '*'; //              
			head = (head + 1) % ((N - 2)*(N - 2));   //       
			position[head].x = x;
			position[head].y = y;
			ShowGame();
			gameover = 1;
			score += grade * 20;  //    
			SetPoint();   //     
		}
		else
		{
			//      
			s[position[tail].x][position[tail].y] = ' ';//       
			tail = (tail + 1) % ((N - 2) * (N - 2));//        
			s[position[head].x][position[head].y] = '*';  //         
			head = (head + 1) % ((N - 2) * (N - 2));
			position[head].x = x;
			position[head].y = y;
			s[position[head].x][position[head].y] = '#'; //      
			gameover = 1;
		}
		return gameover;
	}

	void SetPoint()//      
	{
		P p;
		srand((unsigned int)time(0));	
		//  rand()      。       seed, rand()            
		do
		{
			p.x1 = rand() % (N - 2) + 1;
			p.y1 = rand() % (N - 2) + 1;
		} while (s[p.x1][p.y1] != ' ');

		s[p.x1][p.y1] = '*';
	}

	void GetGrade()//       
	{
		cin >> grade;
		while (grade>7 || grade<1)
		{
			cout << "     1-7    ,        " << endl;
			cin >> grade;
		}
		switch (grade)
		{
		case 1:
			gamespeed = 1000;
			gameauto = 0;
			break;
		case 2:
			gamespeed = 800;
			gameauto = 0;
			break;
		case 3:
			gamespeed = 600;
			gameauto = 0;
			break;
		case 4:
			gamespeed = 400;
			gameauto = 0;
			break;
		case 5:
			gamespeed = 200;
			gameauto = 0;
			break;
		case 6:
			gamespeed = 100;
			gameauto = 0;
			break;
		case 7:
			grade = 1;
			gamespeed = 1000;
			gameauto = 1;
			break;
		}
	}

	void Display()//    ,          
	{
		cout << "
\t\t\t\t :" << grade; cout << "


\t\t\t\t :" << gamespeed; cout << "


\t\t\t\t :" << score << " "; } private: char s[N][N];// , 。 int grade, length; int gamespeed; // char direction; // , int head, tail; int score; bool gameauto; struct P { int x1; int y1; // }; int x, y; }; int main() { char ctn = 'y'; int nodead = 1; cout << "




\t\t\t !" << endl;// ; cout << "


\t\t\t 。。。" << endl;// ;; _getch(); while (ctn == 'y') { system("cls"); // SnakeMap snake; snake.Initialize(); cout << "

:" << endl; cout << "


\t\t\t1. : 1000

\t\t\t2. : 800\

\t\t\t3. : 600

\t\t\t4. : 400\

\t\t\t5. : 200

\t\t\t6. : 100\

\t\t\t7. " << endl; snake.GetGrade();// for (int i = 1; i <= 4; i++) { position[i].Initialize(i);// } snake.SetPoint(); // do { snake.ShowGame(); nodead = snake.UpdataGame(); } while (nodead); system("cls"); // cout << "


\t\t\t\tGameover!

" << endl; snake.Display();// / cout << "


\t\t ? y ,n " << endl; cin >> ctn; } return 0; }