- Joined
- Mar 19, 2023
- Messages
- 1
- Reaction score
- 0
I am writing a code to output a user selected pattern with a user selected 'foreground' character over a 30x10 grid. Unfortunately I am having trouble with 'case Patterns::X' and 'case Patterns::UPPER_TRI' as they keep printing the wrong pattern. For Patterns X I am trying to have an x pattern printed across a 30x10 grid, so essentially just an x over a 10x10 grid stretched 3 wide. For Patterns UPPER_TRI I am trying to have foreground characters filled the top right half of the 30x10 grid and leave the bottom left half to be background characters.
Here is my code:
#include <iostream>
#include <cstring>
//The purpose of this program is to print a user selected pattern on a 10x10 grid
//using a user selected character
//defining width and height for 10x10 pattern field
const int WIDTH = 30;
const int HEIGHT = 10;
enum class Colors { FOREGROUND, BACKGROUND};
enum class Patterns { FILLED, STRIPES, CHECKERBOARD, SQUARE, X, UPPER_TRI};
struct Pixel {Colors color;};
//pattern output function
void create_shape(Pixel p[], int array_size, Patterns pattern)
{
// Set the colors in the Pixel array
for (int i = 0; i < array_size; i++)
{
switch (pattern)
{
case Patterns::FILLED:
p.color = Colors::FOREGROUND;
break;
case Patterns::STRIPES:
p.color = ((i / WIDTH) % 2 == 0) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
case Patterns::CHECKERBOARD:
p.color = (((i / WIDTH) + (i % WIDTH)) % 2 == 0) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
case Patterns::SQUARE:
p.color = ((i % WIDTH <= 2 || i % WIDTH >= 27) || (i / WIDTH <= 0 || i / WIDTH >= 9)) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
case Patterns::X:
p.color = ((i % (WIDTH + 1) == i / (WIDTH + 1)) || ((i % (WIDTH - 1) == WIDTH - (i / WIDTH) - 1) && (i % WIDTH != 0) && (i % WIDTH != WIDTH - 1))) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
case Patterns::UPPER_TRI:
p.color = ((i % WIDTH >= i / WIDTH) && (i % WIDTH > WIDTH - i / WIDTH)) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
}
}
}
//function depicting between FOREGROUND and BACKGROUND outputs
void draw ( Pixel p[], int array_size, char foreground){
for (int i = 0; i < array_size; i++){
//if pixel color set to foreground, output foreground character
if (p.color == Colors::FOREGROUND){
std::cout << foreground;
}
else if (p.color == Colors::BACKGROUND)
{
//if not foreground, then background, output blank space
std::cout << " ";
}
if ((i+1)% WIDTH == 0){
// if i reaches boarder make new line
std::cout << std::endl;
}
}
}
int main(){
Pixel pixel_buffer [WIDTH * HEIGHT];
char foreground;
std::string input_pattern = "abc";
do{
std::string input_pattern = "abc";
std::cout << "Welcome to my pattern program \n";
std::cout << "Please select an ASCII character you would like to use to fill your pattern (e.g. 0-9, a-z, A-Z, &*!@, etc.), Then press enter\n";
std::cin >> foreground;
std::cout << "Now type which pattern you would like to generate (case sensitive): FILLED, STRIPES, CHECKERBOARD, SQUARE, X, UPPER_TRI or press q to exit\n";
std::cin >> input_pattern;
if (input_pattern == "FILLED")
{
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::FILLED);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "STRIPES"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::STRIPES);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "CHECKERBOARD"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::CHECKERBOARD);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "SQUARE"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::SQUARE);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "X"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::X);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "UPPER_TRI"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::UPPER_TRI);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "q"){
break;
}
}
while(input_pattern != "q" && input_pattern != "Q" );
}
Here is my code:
#include <iostream>
#include <cstring>
//The purpose of this program is to print a user selected pattern on a 10x10 grid
//using a user selected character
//defining width and height for 10x10 pattern field
const int WIDTH = 30;
const int HEIGHT = 10;
enum class Colors { FOREGROUND, BACKGROUND};
enum class Patterns { FILLED, STRIPES, CHECKERBOARD, SQUARE, X, UPPER_TRI};
struct Pixel {Colors color;};
//pattern output function
void create_shape(Pixel p[], int array_size, Patterns pattern)
{
// Set the colors in the Pixel array
for (int i = 0; i < array_size; i++)
{
switch (pattern)
{
case Patterns::FILLED:
p.color = Colors::FOREGROUND;
break;
case Patterns::STRIPES:
p.color = ((i / WIDTH) % 2 == 0) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
case Patterns::CHECKERBOARD:
p.color = (((i / WIDTH) + (i % WIDTH)) % 2 == 0) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
case Patterns::SQUARE:
p.color = ((i % WIDTH <= 2 || i % WIDTH >= 27) || (i / WIDTH <= 0 || i / WIDTH >= 9)) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
case Patterns::X:
p.color = ((i % (WIDTH + 1) == i / (WIDTH + 1)) || ((i % (WIDTH - 1) == WIDTH - (i / WIDTH) - 1) && (i % WIDTH != 0) && (i % WIDTH != WIDTH - 1))) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
case Patterns::UPPER_TRI:
p.color = ((i % WIDTH >= i / WIDTH) && (i % WIDTH > WIDTH - i / WIDTH)) ? Colors::FOREGROUND : Colors::BACKGROUND;
break;
}
}
}
//function depicting between FOREGROUND and BACKGROUND outputs
void draw ( Pixel p[], int array_size, char foreground){
for (int i = 0; i < array_size; i++){
//if pixel color set to foreground, output foreground character
if (p.color == Colors::FOREGROUND){
std::cout << foreground;
}
else if (p.color == Colors::BACKGROUND)
{
//if not foreground, then background, output blank space
std::cout << " ";
}
if ((i+1)% WIDTH == 0){
// if i reaches boarder make new line
std::cout << std::endl;
}
}
}
int main(){
Pixel pixel_buffer [WIDTH * HEIGHT];
char foreground;
std::string input_pattern = "abc";
do{
std::string input_pattern = "abc";
std::cout << "Welcome to my pattern program \n";
std::cout << "Please select an ASCII character you would like to use to fill your pattern (e.g. 0-9, a-z, A-Z, &*!@, etc.), Then press enter\n";
std::cin >> foreground;
std::cout << "Now type which pattern you would like to generate (case sensitive): FILLED, STRIPES, CHECKERBOARD, SQUARE, X, UPPER_TRI or press q to exit\n";
std::cin >> input_pattern;
if (input_pattern == "FILLED")
{
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::FILLED);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "STRIPES"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::STRIPES);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "CHECKERBOARD"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::CHECKERBOARD);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "SQUARE"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::SQUARE);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "X"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::X);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "UPPER_TRI"){
create_shape(pixel_buffer, WIDTH * HEIGHT, Patterns::UPPER_TRI);
draw(pixel_buffer, WIDTH * HEIGHT, foreground);
}
else if (input_pattern == "q"){
break;
}
}
while(input_pattern != "q" && input_pattern != "Q" );
}