Paint fill function

Implement a paint fill function.  After a particular point on screen is picked, all pixels that are of old color should be changed to new color as long as they are adjacent (left, right, top or bottom) to the pixel picked.

/*

 * PaintFillImplementation.cpp

 *

 *  Created on: May 14, 2012

 *      Author: Kiran Hegde

 */

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

/**

 *

 */

enum Color {

    Red, White, Blue, Green

};

/**

 * Convert color to a char

 */

char getColor(Color c) {

    char ret = '0';

    switch(c) {

    case Red:

        ret = 'R';

        break;

    case White:

        ret = 'W';

        break;

    case Blue:

        ret = 'B';

        break;

    case Green:

        ret = 'G';

        break;

    default:

        break;

    }

    return ret;

}

/**

 * create a screen with colors - Red, White and Blue

 */

void createScreen(Color **screen, int width, int height) {

    /*

     * generate random numbers between 0 and 2.  If value is 0

     * color is Red, if 1 color is White, if 2 color is Blue.

     */

    srand(time(NULL));

    for (int y=0; y<height; y++) {

        for (int x=0; x<width; x++) {

            int colorInt = rand() % 3;

            switch(colorInt) {

            case 0:

                screen[y][x] =  Red;

                break;

            case 1:

                screen[y][x] =  White;

                break;

            case 2:

                screen[y][x] =  Blue;

                break;

            default:

                break;

            }

        }

    }

}

/**

 * rows of matrix represent height (y direction) and

 * columns of matrix represent width (x direction)

 */

void printScreen(Color **screen, int width, int height) {

    cout << "Bottom Left = (0, 0) Top Right = (X, Y)" << endl << endl;

    for (int y=height-1; y>=0; y--) {

        for (int x=0; x<width; x++) {

            cout << getColor(screen[y][x]) << " ";

        }

        cout << endl;

    }

}

/**

 * Paint Fill.  All adjacent cells (top, bottom, left or right) are colored

 * with newColor if they are of oldColor

 */

bool paintFill(Color **screen, int width, int height, int x, int y,

        Color oldColor, Color newColor) {

    if ((x < 0) || (x >= width) || (y < 0) || (y >= height)) {

        return false;

    }

    if (screen[y][x] == oldColor) {

        screen[y][x] = newColor;

        paintFill(screen, width, height, x-1, y, oldColor, newColor);

        paintFill(screen, width, height, x+1, y, oldColor, newColor);

        paintFill(screen, width, height, x, y-1, oldColor, newColor);

        paintFill(screen, width, height, x, y+1, oldColor, newColor);

    }

    return true;

}

/**

 *

 */

void paintFill(Color **screen, int width, int height, int x, int y, Color newColor) {

    Color oldColor = screen[y][x];

    paintFill(screen, width, height, x, y, oldColor, newColor);

}

/**

 * Test the code

 */

int main() {

    int width = 10;

    int height = 10;

    Color **screen = new Color* [height];

    for (int i=0; i<10; i++) {

        screen[i] = new Color[width];

    }

    createScreen(screen, width, height);

    printScreen(screen, width, height);

    int x = 4, y = 4;

    cout << endl << "Paint Fill with Green (G) at " << x << ", " << y << endl;

    paintFill(screen, width, height, x, y, Green);

    cout << endl << "After paint fill: " << endl;

    printScreen (screen, width, height);

}