Update column.c
This commit is contained in:
+46
-20
@@ -15,14 +15,18 @@
|
|||||||
* orange = 5 *
|
* orange = 5 *
|
||||||
* cyan = 6 *
|
* cyan = 6 *
|
||||||
* ******************/
|
* ******************/
|
||||||
int tile_current[3] = {1, 2, 3};
|
|
||||||
int tile_next[3] = {5, 5, 5};
|
int tile_current[3];
|
||||||
int colcnt[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // number of empty spaces in each column
|
int tile_next[3];
|
||||||
|
int tile_initial[3];
|
||||||
|
int colcnt[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // number of empty spaces in each column
|
||||||
|
|
||||||
//////////////////////////////// GAME BOARD ARRAY //////////////////////////
|
//////////////////////////////// GAME BOARD ARRAY //////////////////////////
|
||||||
|
// central to game
|
||||||
|
|
||||||
int grid [2][18][8] = {
|
int grid [2][18][8] = {
|
||||||
{
|
{
|
||||||
{0, 0, 0, 0, 0, 0, 0, 0}, // colours
|
{0, 0, 0, 0, 0, 0, 0, 0}, // colours
|
||||||
{0, 0, 0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
{0, 0, 0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
{0, 0, 0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
@@ -42,7 +46,7 @@ int grid [2][18][8] = {
|
|||||||
{0, 0, 0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
{0, 0, 0, 0, 0, 0, 0, 0}, // matches
|
{0, 0, 0, 0, 0, 0, 0, 0}, // matches
|
||||||
{0, 0, 0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
{0, 0, 0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
{0, 0, 0, 0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
@@ -64,6 +68,8 @@ int grid [2][18][8] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////////// COUNT COLUMNS ////////////////////////////////
|
///////////////////////////// COUNT COLUMNS ////////////////////////////////
|
||||||
|
// count available rows in each column
|
||||||
|
|
||||||
int count_col(void) {
|
int count_col(void) {
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
colcnt[i] = 0;
|
colcnt[i] = 0;
|
||||||
@@ -71,18 +77,19 @@ colcnt[i] = 0;
|
|||||||
|
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
for (int j = 0; j < 18; j++) {
|
for (int j = 0; j < 18; j++) {
|
||||||
if (grid[0][j][i] == 0) { colcnt[i]++;} // count empty spaces in each column
|
if (grid[0][j][i] == 0) { colcnt[i]++;} // count empty rows in each column
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////// NEW COLUMN //////////////////////////
|
//////////////////////////////// NEW COLUMN //////////////////////////
|
||||||
// randomly choose from remaining available columns to drop next brick into
|
// randomly choose from remaining available columns to drop next brick into
|
||||||
|
|
||||||
int new_column(void) {
|
int new_column(void) {
|
||||||
int avl; // general counter
|
int avl; // general counter
|
||||||
int actual_col = 0; // actual absolute column in game panel
|
int actual_col = -1; // actual absolute column in game panel
|
||||||
int totcol_avail = 0; // keep track of available columns
|
int totcol_avail = 0; // keep track of available columns
|
||||||
int randc; // random column number
|
int randc; // random column number
|
||||||
|
|
||||||
//totcol_avail = 0;
|
//totcol_avail = 0;
|
||||||
count_col();
|
count_col();
|
||||||
@@ -90,16 +97,15 @@ int new_column(void) {
|
|||||||
if (colcnt[i] > 3) {totcol_avail++;} // count total available columns
|
if (colcnt[i] > 3) {totcol_avail++;} // count total available columns
|
||||||
}
|
}
|
||||||
if (totcol_avail == 0) {
|
if (totcol_avail == 0) {
|
||||||
printf("game over\n");
|
game_end = 2; // game over
|
||||||
restart = true;
|
} else { // no more columns available
|
||||||
} else { // no more columns available
|
|
||||||
randc = randombytes_uniform(totcol_avail) + 0; // choose random number from available columns
|
randc = randombytes_uniform(totcol_avail) + 0; // choose random number from available columns
|
||||||
avl = -1;
|
avl = -1;
|
||||||
for (int i = 0; randc != avl; i++) { // translate random num from avail column to actual column
|
for (int i = 0; randc != avl; i++) { // translate random num from avail column to actual column
|
||||||
if (colcnt[i] > 3) {
|
if (colcnt[i] > 3) {
|
||||||
avl++;
|
avl++;
|
||||||
}
|
}
|
||||||
actual_col = i; // might need to move this down to next brace
|
actual_col = i; // might need to move this down to next brace
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return actual_col; // return the random column
|
return actual_col; // return the random column
|
||||||
@@ -107,6 +113,7 @@ int new_column(void) {
|
|||||||
|
|
||||||
//////////////////////////////// DISPLAY COLUMN //////////////////////////
|
//////////////////////////////// DISPLAY COLUMN //////////////////////////
|
||||||
// display brick in relevant row/column of board array
|
// display brick in relevant row/column of board array
|
||||||
|
|
||||||
int disp_column(int row, int col) {
|
int disp_column(int row, int col) {
|
||||||
for (int i = -1; i < 3; i++) {
|
for (int i = -1; i < 3; i++) {
|
||||||
grid[0][row + i][col] = tile_current[i];
|
grid[0][row + i][col] = tile_current[i];
|
||||||
@@ -115,18 +122,34 @@ int new_column(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////// NEW BRICK //////////////////////////
|
//////////////////////////////// NEW BRICK //////////////////////////
|
||||||
// generate random 3 tile bricks for current and preview
|
// generate random 3 tile bricks for initial, current and preview
|
||||||
int new_brick(void) {
|
|
||||||
|
int next_brick(void) {
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
tile_current[i] = randombytes_uniform(6) + 1; // brick in play
|
tile_next[i] = randombytes_uniform(6) + 1; // next brick preview
|
||||||
tile_next[i] = randombytes_uniform(6) + 1; // next brick preview
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int current_brick(void) {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
tile_current[i] = tile_next[i]; // brick in play (current brick)
|
||||||
|
}
|
||||||
|
next_brick();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int initial_brick(void) {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
tile_current[i] = randombytes_uniform(6) + 1; // initial brick
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////// ROTATE COLUMN //////////////////////////
|
//////////////////////////////// ROTATE COLUMN //////////////////////////
|
||||||
// rotate (cycle) tiles in brick by 1
|
// rotate (cycle) tiles in brick by 1
|
||||||
int rotate_col(void) {
|
|
||||||
|
int rotate_col(void) {
|
||||||
int temp = tile_current[0];
|
int temp = tile_current[0];
|
||||||
tile_current[0] = tile_current[1];
|
tile_current[0] = tile_current[1];
|
||||||
tile_current[1] = tile_current[2];
|
tile_current[1] = tile_current[2];
|
||||||
@@ -136,12 +159,14 @@ int rotate_col(void) {
|
|||||||
|
|
||||||
//////////////////////////////// MOVE HORIZONTALLY //////////////////////////
|
//////////////////////////////// MOVE HORIZONTALLY //////////////////////////
|
||||||
// move brick left & right using cursor keys
|
// move brick left & right using cursor keys
|
||||||
|
|
||||||
int move_horiz(void) {
|
int move_horiz(void) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////// MOVE VERTICALLY //////////////////////////
|
//////////////////////////////// MOVE VERTICALLY //////////////////////////
|
||||||
// move brick down one place at a time
|
// move brick down one place at a time
|
||||||
|
|
||||||
int move_down(void) {
|
int move_down(void) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -162,6 +187,7 @@ int reset(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reset row/column
|
// reset row/column
|
||||||
row = col = 0;
|
row = -1;
|
||||||
|
col = 0; // col = new_column();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user