Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
114
Task/15-puzzle-game/Processing/15-puzzle-game.processing
Normal file
114
Task/15-puzzle-game/Processing/15-puzzle-game.processing
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
int number_of_grid_cells = 16; // Set the number of cells of the board here 9, 16, 25 etc
|
||||
color piece_color = color(255, 175, 0);
|
||||
color background_color = color(235, 231, 178);
|
||||
color piece_shadow_dark = color(206, 141, 0);
|
||||
color piece_shadow_light = color(255, 214, 126);
|
||||
int z, t, p, piece_number, row_length, piece_side_length;
|
||||
|
||||
PuzzlePiece[] piece = new PuzzlePiece[number_of_grid_cells]; // Number of puzzle pieces objects array
|
||||
|
||||
void setup() {
|
||||
size(400, 400); // Window size width and height must be egual
|
||||
background(200, 50, 0);
|
||||
row_length = int(sqrt(number_of_grid_cells));
|
||||
piece_side_length = width/row_length;
|
||||
textSize(piece_side_length/2.7);
|
||||
textAlign(CENTER);
|
||||
|
||||
PVector[] xy_values = new PVector[number_of_grid_cells]; // Setting the x and y values for each cell on grid
|
||||
for (int i = 0; i < number_of_grid_cells; i += row_length) { // Values are the top left pixel of the cell
|
||||
for (int j = 0; j < row_length; j++) {
|
||||
xy_values[z] = new PVector();
|
||||
xy_values[z].x = j*piece_side_length;
|
||||
xy_values[z].y = t*piece_side_length;
|
||||
z++;
|
||||
}
|
||||
t++;
|
||||
}
|
||||
|
||||
int[] place = new int[number_of_grid_cells]; // This array is to help placing the pieces randomly and store values in piece objects array
|
||||
for (int i = 0; i < number_of_grid_cells; i++) place[i] = 0;
|
||||
piece_number = 0;
|
||||
|
||||
while (piece_number < number_of_grid_cells) { // Placing pieces randomly in grid
|
||||
p = int(random(0, number_of_grid_cells));
|
||||
if (place[p] == 0) { // Once placed will be set to 1 to avoid designing again at this location
|
||||
piece[piece_number] = new PuzzlePiece(piece_number, xy_values[p].x, xy_values[p].y); // Creating the piece objects array
|
||||
place[p] = 1;
|
||||
piece[piece_number].design(); // Design newly create piece object
|
||||
piece_number++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
for (int i = 0; i < number_of_grid_cells; i++) { // Search all piece object indexes and verify which one is mouse pressed in this loop
|
||||
if (mousePressed == true && mouseX >= piece[i].xPosition() && mouseX <= piece[i].xPosition()+piece_side_length && mouseY >= piece[i].yPosition() && mouseY <= piece[i].yPosition()+piece_side_length && piece[i].pieceNumber() != 15) {
|
||||
if (pieceMove(piece[number_of_grid_cells-1].xPosition(), piece[number_of_grid_cells-1].yPosition(), piece[i].xPosition(), piece[i].yPosition())) {
|
||||
float temp_x = piece[number_of_grid_cells-1].xPosition(); // Remember x and y value of final piece index (white piece)
|
||||
float temp_y = piece[number_of_grid_cells-1].yPosition();
|
||||
piece[number_of_grid_cells-1].storePos(piece[i].xPosition(), piece[i].yPosition()); // Store clicked x and y value in final index of piece array
|
||||
piece[i].storePos(temp_x, temp_y); // Store temp x and y value (the last/previous final index values) in current clicked piece index
|
||||
piece[number_of_grid_cells-1].design(); // draw the final index piece index (only final piece index is painted white)
|
||||
piece[i].design(); // Draw a numbered piece of current index
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean pieceMove(float final_index_piece_x, float final_index_piece_y, float current_index_x, float current_index_y) {
|
||||
// If both x values from clicked and white piece have same value meaning in same horizontal column
|
||||
// AND current clicked y value is equal to white piece y value - piece side lenght OR current clicked y value + piece side lenght is egual to white piece y
|
||||
if (current_index_x == final_index_piece_x && (current_index_y == final_index_piece_y-piece_side_length || (current_index_y == final_index_piece_y+piece_side_length))) return true;
|
||||
// If both y values from clicked and white piece have same value meaning in same vertical column
|
||||
// AND current clicked x value is equal to white piece x value - piece side lenght OR current clicked x value + piece side lenght is egual to white piece x
|
||||
else if (current_index_y == final_index_piece_y && (current_index_x == final_index_piece_x-piece_side_length || (current_index_x == final_index_piece_x+piece_side_length))) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
class PuzzlePiece {
|
||||
int piece_number;
|
||||
float x_pos, y_pos;
|
||||
|
||||
PuzzlePiece(int _piece_nr, float _xp, float _yp) {
|
||||
piece_number = _piece_nr;
|
||||
x_pos = _xp;
|
||||
y_pos = _yp;
|
||||
}
|
||||
|
||||
void storePos(float _xp, float _yp) {
|
||||
x_pos = _xp;
|
||||
y_pos = _yp;
|
||||
}
|
||||
|
||||
int pieceNumber() {
|
||||
return piece_number;
|
||||
}
|
||||
|
||||
float xPosition() {
|
||||
return x_pos;
|
||||
}
|
||||
|
||||
float yPosition() {
|
||||
return y_pos;
|
||||
}
|
||||
|
||||
void design() {
|
||||
noStroke();
|
||||
fill(piece_color);
|
||||
if (piece_number == number_of_grid_cells-1) fill(background_color);
|
||||
rect(x_pos+1, y_pos+1, piece_side_length-1, piece_side_length-1);
|
||||
if (piece_number != number_of_grid_cells-1) {
|
||||
fill(0); // Black text shadow
|
||||
text(piece_number+1, x_pos+piece_side_length/2+1, y_pos+piece_side_length/2+textAscent()/2);
|
||||
fill(255);
|
||||
text(piece_number+1, x_pos+piece_side_length/2, y_pos+piece_side_length/2+textAscent()/2);
|
||||
stroke(piece_shadow_dark);
|
||||
line(x_pos+piece_side_length-1, y_pos+1, x_pos+piece_side_length-1, y_pos+piece_side_length-1); // Right side shadow
|
||||
line(x_pos+2, y_pos+piece_side_length, x_pos+piece_side_length-1, y_pos+piece_side_length); // Bottom side shadow
|
||||
stroke(piece_shadow_light);
|
||||
line(x_pos+2, y_pos-1, x_pos+2, y_pos+piece_side_length); // Left bright
|
||||
line(x_pos+2, y_pos+1, x_pos+piece_side_length-1, y_pos+1); // Upper bright
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue