| #include <ctime>
#include <iostream>
#include "windows.h"
using namespace std;
class Game {
	public:
		void Move ( );
		void MoveGo ( );
		void SetConsole ( int ic_x, int ic_y );
		int Random ( int ic_numberA, int ic_numberB );
		Game ( int ic_window_width, int ic_window_heigth, int ic_speed );
		~Game ( );
	private:
		int i_window_width,
			i_window_height,
			i_window_right,
			i_window_down,
			i_random_x,
			i_random_y,
			i_formerly_x,
			i_formerly_y,
			i_direction_id,
			i_speed;
};
int main ( ) {
	system ( "mode con cols=100 lines=30" );
	srand ( time ( NULL ) );
	
	Game game ( 100, 30, 10 );
	game.MoveGo ( );
	
	return 0;
}
Game::Game ( int ic_window_width, int ic_window_height, int ic_speed ) {
	i_window_width	= ic_window_width;
	i_window_height	= ic_window_height;
	i_window_right	= i_window_width - 1;
	i_window_down	= i_window_height - 1;
	i_random_x 		= Random ( 0, i_window_right );
	i_random_y 		= Random ( 0, i_window_down );
	i_direction_id 	= Random ( 1, 4 );
	i_speed 		= ic_speed;
}
Game::~Game ( ) {
}
int Game::Random ( int ic_numberA, int ic_numberB ) {
	return ic_numberA + rand ( ) % ic_numberB;
}
void Game::SetConsole ( int ic_x, int ic_y ) {
	CONSOLE_CURSOR_INFO info = { 1, 0 };
	HANDLE hand = GetStdHandle ( STD_OUTPUT_HANDLE );
	COORD coord = { ic_x, ic_y };
	SetConsoleCursorInfo ( hand, &info );
	SetConsoleCursorPosition ( hand, coord );
}
void Game::Move ( ) {
	while ( 1 ) {
		SetConsole ( i_random_x, i_random_y );
		cout << "*";
		i_formerly_x = i_random_x;
		i_formerly_y = i_random_y;
		if ( ( i_random_x == 0 && i_random_y == 0 ) ||
			 ( i_random_x == 0 && i_random_y == i_window_down ) ||
			 ( i_random_x == i_window_right && i_random_y == 0 ) ||
			 ( i_random_x == i_window_right && i_random_y == i_window_down ) ) {
			switch ( i_direction_id ) {
				case 1: i_direction_id = 3; break;
				case 2: i_direction_id = 4; break;
				case 3: i_direction_id = 1; break;
				case 4: i_direction_id = 2; break;
				default : break;
			}
		}
		else if ( i_random_x == 0 || i_random_x == i_window_right ) {
			switch ( i_direction_id ) {
				case 1: i_direction_id = 4; break;
				case 2: i_direction_id = 3; break;
				case 3: i_direction_id = 2; break;
				case 4: i_direction_id = 1; break;
				default : break;
			}
		}
		else if ( i_random_y == 0 || i_random_y == i_window_down ) {
			switch ( i_direction_id ) {
				case 1: i_direction_id = 2; break;
				case 2: i_direction_id = 1; break;
				case 3: i_direction_id = 4; break;
				case 4: i_direction_id = 3; break;
				default : break;
			}
		}
		switch ( i_direction_id ) {
			case 1: i_random_x++; i_random_y++; break;
			case 2: i_random_x++; i_random_y--; break;
			case 3: i_random_x--; i_random_y--; break;
			case 4: i_random_x--; i_random_y++; break;
			default : break;
		}
		Sleep ( i_speed / 2 );
		SetConsole ( i_random_x, i_random_y );
		cout << "*";
		Sleep ( i_speed / 2 );
		SetConsole ( i_formerly_x, i_formerly_y );
		cout << " ";
	}
}
void Game::MoveGo ( ) {
	while ( 1 ) {
		int i = Random ( 1, 3 );
		i_formerly_x 	= i_random_x;
		i_formerly_y 	= i_random_y;
		i_direction_id 	= Random ( 1, 4 );
		SetConsole ( i_random_x, i_random_y );
		cout << "*";
		if ( i_random_x == 0 && i_random_y == 0 ) {
			i = Random ( 1, 2 );
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 2; break;
				default : break;
			}
		}
		else if ( i_random_x == 0 && i_random_y == i_window_down ) {
			i = Random ( 1, 2 );
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 4; break;
				default : break;
			}
		}
		else if ( i_random_x == i_window_right && i_random_y == 0 ) {
			i = Random ( 1, 2 );
			switch ( i ) {
				case 1: i_direction_id = 2; break;
				case 2: i_direction_id = 3; break;
				default : break;
			}
		}
		else if ( i_random_x == i_window_right && i_random_y == i_window_down ) {
			i = Random ( 1, 2 );
			switch ( i ) {
				case 1: i_direction_id = 3; break;
				case 2: i_direction_id = 4; break;
				default : break;
			}
		}
		else if ( i_random_x == 0 ) {
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 2; break;
				case 3: i_direction_id = 4; break;
				default : break;
			}
		}
		else if ( i_random_x == i_window_right ) {
			switch ( i ) {
				case 1: i_direction_id = 2; break;
				case 2: i_direction_id = 3; break;
				case 3: i_direction_id = 4; break;
				default : break;
			}
		}
		else if ( i_random_y == 0 ) {
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 2; break;
				case 3: i_direction_id = 3; break;
				default : break;
			}
		}
		else if ( i_random_y == i_window_down ) {
			switch ( i ) {
				case 1: i_direction_id = 1; break;
				case 2: i_direction_id = 3; break;
				case 3: i_direction_id = 4; break;
				default : break;
			}
		}
		switch ( i_direction_id ) {
			case 1: i_random_x++; break;
			case 2: i_random_y++; break;
			case 3: i_random_x--; break;
			case 4: i_random_y--; break;
			default : break;
		}
		Sleep ( i_speed );
		SetConsole ( i_formerly_x, i_formerly_y );
		cout << " ";
	}
}
 |