HW 8

2022. 7. 3. 22:44Programming/C

Problem 1

matrix_screen

This program displays The Matrix Screen animation. (You can compile and run the code as it is.)

It represents character bars using four arrays: y, delay, length, and prevLength.

 

Mission: 

1) Read the code carefully and fully understand the techniques used in the program.

2) Replace the four arrays by a single structure array

     

- Declare a structure Bar.

- Erase the four arrays, y, delay, length, and prevLength.

- Declare a Bar type array.

- Modify the other part using the Bar array.

 

The expressions such as y[col], delay[col], length[col], prevLength[col] should be removed!

skeleton code

#include <stdio.h>
#include <time.h>

#include "Console.h"

#define MAX_WIDTH 200

char GetRandomChar();		// a function that returns a random alpha-numeric character

void clrscr(void);			// clear the screen
void gotoxy(int x, int y);	// move cursor to (x, y)


//	TO DO: declare a strucure Bar containing four integer fields: y, delay, length, prevLength


int main()
{
	int screenWidth = getWindowWidth();
	int screenHeight = getWindowHeight() - 3;

	if(screenWidth > MAX_WIDTH)			// for safety
		screenWidth = MAX_WIDTH;

	// TO DO: erase the following four arrays and declare an array of Bars instead.
	// 		Then, modify the code accordingly.

	int y[MAX_WIDTH];					// y coordinates for each screenWidth
	int delay[MAX_WIDTH];				// inverse of speed of each screenWidth
	int length[MAX_WIDTH];				// length of each screenWidth
	int prevLength[MAX_WIDTH];			// length of each screenWidth in the previous turn

	int t = 0;							// the master clock
	int col = 0;
	
	clrscr();

	printf("This program shows something interesting.\n");
	printf("If you want to finish, press ESC.\n");
	printf("Press Enter to start: ");
	getchar();

	clrscr();

	srand(time(NULL));
	
	// initialize each screenWidths
	for(col = 0; col < screenWidth; col++){
		y[col] = 1;
		delay[col] = rand() % 10 + 1;	// initialize delay and length
		length[col] = rand() % 5 + 3;
		prevLength[col] = length[col];
	}

	while(1){
		if(kbhit() && getch() == 27)	// if ESC is pressed, terminate the loop (You may ignore this line.)
				break;

		for(col = 0; col < screenWidth; col++){
			if(t % delay[col] == 0){
				// print a random character at current position
				gotoxy(col + 1, y[col]);
				putchar(GetRandomChar());

				// erase previous location
				if(y[col] - length[col] >= 1){
					gotoxy(col + 1, y[col] - length[col]);
					putchar(' ');
				}
				
				if(y[col] <= prevLength[col]){
					gotoxy(col + 1, screenHeight + y[col] - prevLength[col]);	// wrapped
					putchar(' ');
				}

				// increase current y coordinate
				y[col]++;

				// check if y reached the last screenHeight
				if(y[col] > screenHeight){
					y[col] = 1;
					delay[col] = rand() % 10 + 1;	// reset delay and length
					
					prevLength[col] = length[col];
					length[col] = rand() % 5 + 3;
				}
			}
		}
		
		Sleep(50);
		t++;				// master clock
	}

	clrscr();
	
	gotoxy(1, 1);
	printf("Good bye!\n");

	system("PAUSE");

	return 0;	
}

char GetRandomChar()
{
	const int noUpperCase = 26;
	const int noLowerCase = 26;
	const int noDigits = 10;

	int x = rand() % (noUpperCase + noLowerCase + noDigits);
	if(x < noUpperCase)
		return 'A' + x;
	else if(x < noUpperCase + noLowerCase)
		return 'a' + x - noUpperCase;
	else
		return '0' + x - (noUpperCase + noLowerCase);
}

 

revised code

#include <stdio.h>
#include <time.h>

#include "Console.h"

#define MAX_WIDTH 200

char GetRandomChar();		// a function that returns a random alpha-numeric character

void clrscr(void);			// clear the screen
void gotoxy(int x, int y);	// move cursor to (x, y)

//	TO DO: declare a strucure Bar containing four integer fields: y, delay, length, prevLength

int main()
{
	int screenWidth = getWindowWidth();
	int screenHeight = getWindowHeight() - 3;

	if(screenWidth > MAX_WIDTH)			// for safety
		screenWidth = MAX_WIDTH;

	// TO DO: erase the following four arrays and declare an array of Bars instead.
	// Then, modify the code accordingly.

	struct bar{
		int y;					// y coordinates for each screenWidth
		int delay;				// inverse of speed of each screenWidth
		int length;				// length of each screenWidth
		int prevLength;			// length of each screenWidth in the previous turn
	};

	int t = 0;							// the master clock
	int col = 0;
	
	clrscr();

	printf("This program shows something interesting.\n");
	printf("If you want to finish, press ESC.\n");
	printf("Press Enter to start: ");
	getchar();

	clrscr();

	srand(time(NULL));
	
	struct bar a[screenWidth];

	// initialize each screenWidths
	for(col = 0; col < screenWidth; col++){
		a[col].y = 1;
		a[col].delay= rand() % 10 + 1;	// initialize delay and length
		a[col].length = rand() % 5 + 3;
		a[col].prevLength = a[col].length;
	}

	while(1){
		if(kbhit() && getch() == 27)	// if ESC is pressed, terminate the loop (You may ignore this line.)
				break;

		for(col = 0; col < screenWidth; col++){
			if(t % a[col].delay == 0){
				// print a random character at current position
				gotoxy(col + 1, a[col].y);
				putchar(GetRandomChar());

				// erase previous location
				if(a[col].y - a[col].length >= 1){
					gotoxy(col + 1, a[col].y - a[col].length);
					putchar(' ');
				}
				
				if(a[col].y <= a[col].prevLength){
					gotoxy(col + 1, screenHeight + a[col].y - a[col].prevLength);	// wrapped
					putchar(' ');
				}

				// increase current y coordinate
				a[col].y++;

				// check if y reached the last screenHeight
				if(a[col].y > screenHeight){
					a[col].y = 1;
					a[col].delay = rand() % 10 + 1;	// reset delay and length
					
					a[col].prevLength = a[col].length;
					a[col].length = rand() % 5 + 3;
				}
			}
		}
		
		Sleep(50);
		t++;				// master clock
	}

	clrscr();
	
	gotoxy(1, 1);
	printf("Good bye!\n");

	system("PAUSE");

	return 0;	
}

char GetRandomChar()
{
	const int noUpperCase = 26;
	const int noLowerCase = 26;
	const int noDigits = 10;

	int x = rand() % (noUpperCase + noLowerCase + noDigits);
	if(x < noUpperCase)
		return 'A' + x;
	else if(x < noUpperCase + noLowerCase)
		return 'a' + x - noUpperCase;
	else
		return '0' + x - (noUpperCase + noLowerCase);
}

'Programming > C' 카테고리의 다른 글

Final  (0) 2022.07.03
HW 7  (0) 2022.07.03
HW 6  (0) 2022.07.03
HW 5  (0) 2022.07.03
HW 4  (0) 2022.07.03