/**
Primaries prototype 01.
Controls
To control the selection box of a grid, the grid must be active. A grid is active when its border is brighter than
the others. Only one grid may be active at a time. Press r to make the red grid active, g to make the green
grid active, and b to make the blue grid active.
When a grid is active you may move the selection box around by using the arrow keys. The selection will wrap to the
other side of the grid when you reach the edge.
At any time you may press s to swap the selected tiles clockwise or a to swap the selected tiles counter-clockwise.
Color Matching
Points are scored by matching three or more tiles of the same color either horizontally or vertically. However, you can only match red tiles in
the grid with the red border, blue tiles in the grid with the blue border, and green tiles in the grid with the green border. When tiles
are successfully matched, they clear and the tiles above them drop down. New tiles appear at the top of the grid, but only tiles that are
*not* the same color as that grid. In other words, when you match three red tiles in the red grid, you might get two blues and a green at
the top. You will never get a red tile.
Color Mixing
If you swap the selected tiles and it results in a match in two or more grids, it results in a mixing of the colors and some tiles
of the mixed color will appear at the top of the grid. For example, if you swap clockwise and it results in a match in the red grid and
the blue grid, it will cause magenta tiles to appear at the top of the red and blue grids, along with the other primary colors that
could appear. If you matched three blue and three red, you might get a magenta tile, a red tile, and a green tile at the top of the blue grid
and two magenta tiles, and a green tile at the top of the red grid. These mixed colors are called Secondaries and they may be matched in
either of the grids that produced them. In other words, magenta tiles can be matched in the red grid and the blue grid. Mixing
three Primaries (RGB) or two Secondaries (CMY) creates a White tile. White tiles can be matched in any of the grids.
Scoring
Primaries are worth 5 points, Secondaries 10 points, and Whites 20. You get a x2 score multiplier when you create a Secondary and a x3 multiplier
when you create a White.
*/
ColorGridRenderer redCGR;
BoardManipulator redBM;
int[] redMatches = new int[] { ColorGrid.RED, ColorGrid.MAGENTA, ColorGrid.YELLOW, ColorGrid.WHITE };
ColorGridRenderer blueCGR;
BoardManipulator blueBM;
int[] blueMatches = new int[] { ColorGrid.BLUE, ColorGrid.MAGENTA, ColorGrid.CYAN, ColorGrid.WHITE };
ColorGridRenderer greenCGR;
BoardManipulator greenBM;
int[] greenMatches = new int[] { ColorGrid.GREEN, ColorGrid.CYAN, ColorGrid.YELLOW, ColorGrid.WHITE };
BoardManipulator activeBoard;
// for now, some timers because we don't have transitions
int matchTimer;
int dropTimer;
int repopTimer;
int totalScore;
String justScored = "";
void setup()
{
size(200, 210);
smooth();
rectMode(CORNER);
ColorGrid cg = new ColorGrid(8, 8);
cg.populate( new color[] { ColorGrid.GREEN, ColorGrid.BLUE } );
redCGR = new CGRDrawn(cg , 110, 25, 10, ColorGrid.RED);
redBM = new BoardManipulator(redCGR);
cg = new ColorGrid(8, 8);
cg.populate( new color[] { ColorGrid.RED, ColorGrid.GREEN } );
blueCGR = new CGRDrawn(cg, 10, 25, 10, ColorGrid.BLUE);
blueBM = new BoardManipulator(blueCGR);
cg = new ColorGrid(8, 8);
cg.populate( new color[] { ColorGrid.RED, ColorGrid.BLUE } );
greenCGR = new CGRDrawn(cg, 65, 120, 10, ColorGrid.GREEN);
greenBM = new BoardManipulator(greenCGR);
activeBoard = redBM;
matchTimer = millis() + 2000;
dropTimer = repopTimer = Integer.MAX_VALUE;
totalScore = 0;
textFont( loadFont("CourierNewPS-BoldMT-12.vlw") );
}
void draw()
{
// *********************************
// ** Updating
// *********************************
activeBoard.focus();
if ( millis() - matchTimer > 0 )
{
//println("Testing for matches.");
clearMatches();
}
if ( millis() - dropTimer > 0 )
{
//println("Dropping tiles.");
redBM.dropTiles();
blueBM.dropTiles();
greenBM.dropTiles();
// clear the drop
dropTimer = Integer.MAX_VALUE;
// queue a fill
repopTimer = millis() + 1000;
}
if ( millis() - repopTimer > 0 )
{
//println("Repoping tiles.");
redBM.repop();
greenBM.repop();
blueBM.repop();
// clear the repop
repopTimer = Integer.MAX_VALUE;
justScored = "";
}
// **********************************
// ** Drawing
// **********************************
background(0);
fill(255);
text(justScored, 10, 15);
text("SCORE: " + totalScore, 100, 15);
redCGR.draw();
greenCGR.draw();
blueCGR.draw();
}
void clearMatches()
{
boolean redMatched = false;
boolean blueMatched = false;
boolean greenMatched = false;
int matched;
int mult = 1;
int score = 0;
for (int i = 0; i < 4; i++)
{
// match and score the red board
matched = redBM.findMatches(redMatches[i]);
if ( matched > 0 )
{
score += points(redMatches[i]) * matched;
redMatched = true;
}
// match and score the blue board
matched = blueBM.findMatches(blueMatches[i]);
if ( matched > 0 )
{
score += points(blueMatches[i]) * matched;
blueMatched = true;
}
// match and score the green board
matched = greenBM.findMatches(greenMatches[i]);
if ( matched > 0 )
{
score += points(greenMatches[i]) * matched;
greenMatched = true;
}
}
int redEmpties = redBM.emptyTiles();
int blueEmpties = blueBM.emptyTiles();
int greenEmpties = greenBM.emptyTiles();
if ( redMatched && blueMatched && greenMatched )
{
score *= 3;
mult = 3;
// all three matched so award a white tile
int which = int(random(0, 3));
if ( which == 0 )
{
redBM.queueTile(ColorGrid.WHITE);
redEmpties--;
}
else if ( which == 1 )
{
blueBM.queueTile(ColorGrid.WHITE);
blueEmpties--;
}
else if ( which == 2 )
{
greenBM.queueTile(ColorGrid.WHITE);
greenEmpties--;
}
}
else if ( redMatched && blueMatched )
{
score *= 2;
mult = 2;
// red and blue matched, so award a magenta tile
int which = int(random(0,2));
if ( which == 0 )
{
redBM.queueTile(ColorGrid.MAGENTA);
redEmpties--;
}
else
{
blueBM.queueTile(ColorGrid.MAGENTA);
blueEmpties--;
}
}
else if ( redMatched && greenMatched )
{
score *= 2;
mult = 2;
// red and green matched to award a yellow tile
int which = int(random(0,2));
if ( which == 0 )
{
redBM.queueTile(ColorGrid.YELLOW);
redEmpties--;
}
else
{
greenBM.queueTile(ColorGrid.YELLOW);
greenEmpties--;
}
}
else if ( blueMatched && greenMatched )
{
score *= 2;
mult = 2;
// blue and green matched, so award a cyan tile
int which = int(random(0,2));
if ( which == 0 )
{
blueBM.queueTile(ColorGrid.CYAN);
blueEmpties--;
}
else
{
greenBM.queueTile(ColorGrid.CYAN);
greenEmpties--;
}
}
//println(redEmpties + " empty red tiles.");
//println(greenEmpties + " empty green tiles.");
//println(blueEmpties + " empty blue tiles.");
// generate some primary tiles for the remaining empties
int[] newRed = new int[redEmpties];
for(int i = 0; i < newRed.length; i++)
{
newRed[i] = blueOrGreen();
}
redBM.queueTiles(newRed);
int[] newBlue = new int[blueEmpties];
for(int i = 0; i < newBlue.length; i++)
{
newBlue[i] = redOrGreen();
}
blueBM.queueTiles(newBlue);
int[] newGreen = new int[greenEmpties];
for(int i = 0; i < newGreen.length; i++)
{
newGreen[i] = blueOrRed();
}
greenBM.queueTiles(newGreen);
totalScore += score;
if ( score > 0 )
{
justScored = ((score/mult) + " x" + mult + " = " + score);
dropTimer = millis() + 500;
}
// queue a test for six seconds in the future
matchTimer = millis() + 6000;
}
color blueOrGreen()
{
return random(1) > 0.5 ? ColorGrid.BLUE : ColorGrid.GREEN;
}
color blueOrRed()
{
return random(1) > 0.5 ? ColorGrid.BLUE : ColorGrid.RED;
}
color redOrGreen()
{
return random(1) > 0.5 ? ColorGrid.RED : ColorGrid.GREEN;
}
void keyPressed()
{
if ( key == 'p' )
{
saveFrame("primaries_board.jpg");
}
if ( key == 'r' )
{
activeBoard.unfocus();
activeBoard = redBM;
}
if ( key == 'g' )
{
activeBoard.unfocus();
activeBoard = greenBM;
}
if ( key == 'b' )
{
activeBoard.unfocus();
activeBoard = blueBM;
}
// swap
if ( key == 's' || key == 'a' )
{
color r = redBM.getSelectionColor();
color b = blueBM.getSelectionColor();
color g = greenBM.getSelectionColor();
// to the right
if ( key == 's' )
{
blueBM.setSelectionColor(g);
redBM.setSelectionColor(b);
greenBM.setSelectionColor(r);
}
// to the left
else if ( key == 'a' )
{
blueBM.setSelectionColor(r);
redBM.setSelectionColor(g);
greenBM.setSelectionColor(b);
}
clearMatches();
}
if ( key == 'w' )
{
redBM.wrapSelection(true);
greenBM.wrapSelection(true);
blueBM.wrapSelection(true);
}
if ( key == 'q' )
{
redBM.wrapSelection(false);
greenBM.wrapSelection(false);
blueBM.wrapSelection(false);
}
if ( key == CODED )
{
if ( keyCode == LEFT )
{
activeBoard.moveSelectionLeft();
}
else if ( keyCode == RIGHT )
{
activeBoard.moveSelectionRight();
}
else if ( keyCode == DOWN )
{
activeBoard.moveSelectionDown();
}
else if ( keyCode == UP )
{
activeBoard.moveSelectionUp();
}
}
}