finally added win condition (a bit scuffed, insta quit on win)
This commit is contained in:
@@ -47,6 +47,7 @@ void expose_cell(Minefield* mf, int r, int c);
|
|||||||
void handle_click(Minefield* mf);
|
void handle_click(Minefield* mf);
|
||||||
|
|
||||||
typedef enum screens {
|
typedef enum screens {
|
||||||
|
DEFAULT_SCREEN,
|
||||||
GAME_LOOP,
|
GAME_LOOP,
|
||||||
GAME_OVER,
|
GAME_OVER,
|
||||||
HELP,
|
HELP,
|
||||||
@@ -54,7 +55,7 @@ typedef enum screens {
|
|||||||
|
|
||||||
Screen game_loop(Minefield* mf);
|
Screen game_loop(Minefield* mf);
|
||||||
/* Screen game_over(Minefield* mf); */
|
/* Screen game_over(Minefield* mf); */
|
||||||
Screen help_screen(Minefield* mf);
|
Screen help_loop(Minefield *mf);
|
||||||
|
|
||||||
Screen (*screen_fn(Screen scr))(Minefield *);
|
Screen (*screen_fn(Screen scr))(Minefield *);
|
||||||
Screen last_screen;
|
Screen last_screen;
|
||||||
@@ -64,12 +65,14 @@ const int screenHeight = 450;
|
|||||||
float cell_size;
|
float cell_size;
|
||||||
int mark_count;
|
int mark_count;
|
||||||
bool dead;
|
bool dead;
|
||||||
|
bool quit;
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
Minefield mf = make_minefield(10, 20, 30);
|
Minefield mf = make_minefield(10, 20, 30);
|
||||||
/* print_minefield(&mf, stdout); */
|
/* print_minefield(&mf, stdout); */
|
||||||
dead = false;
|
dead = false;
|
||||||
|
quit = false;
|
||||||
mark_count = 0;
|
mark_count = 0;
|
||||||
{
|
{
|
||||||
float width = (float) screenWidth / mf.col;
|
float width = (float) screenWidth / mf.col;
|
||||||
@@ -81,10 +84,13 @@ int main(){
|
|||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
Screen screen = GAME_LOOP;
|
Screen screen = GAME_LOOP;
|
||||||
|
Screen temp_screen;
|
||||||
Screen (*loop)(Minefield*);
|
Screen (*loop)(Minefield*);
|
||||||
while(!WindowShouldClose()){
|
while(!WindowShouldClose() && !quit){
|
||||||
loop = screen_fn(screen);
|
loop = screen_fn(screen);
|
||||||
screen = (*loop)(&mf);
|
temp_screen = (*loop)(&mf);
|
||||||
|
last_screen = screen;
|
||||||
|
screen = temp_screen;
|
||||||
}
|
}
|
||||||
|
|
||||||
free_minefield(&mf);
|
free_minefield(&mf);
|
||||||
@@ -93,10 +99,10 @@ int main(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
Screen game_loop(Minefield* mf){
|
Screen game_loop(Minefield* mf){
|
||||||
while(!WindowShouldClose()){
|
while(!WindowShouldClose() && !quit){
|
||||||
|
|
||||||
if (IsKeyPressed(KEY_H)){
|
if (IsKeyPressed(KEY_H)){
|
||||||
break;
|
return HELP;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dead){
|
if (!dead){
|
||||||
@@ -108,11 +114,10 @@ Screen game_loop(Minefield* mf){
|
|||||||
draw_minefield(mf);
|
draw_minefield(mf);
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
last_screen = GAME_LOOP;
|
return DEFAULT_SCREEN;
|
||||||
return HELP;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Screen help_screen(Minefield* mf){
|
Screen help_loop(Minefield *mf) {
|
||||||
while (!WindowShouldClose() && IsKeyDown(KEY_H)){
|
while (!WindowShouldClose() && IsKeyDown(KEY_H)){
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
@@ -131,7 +136,7 @@ Screen (*screen_fn(Screen scr))(Minefield *) {
|
|||||||
/* case GAME_OVER: */
|
/* case GAME_OVER: */
|
||||||
/* return game_loop; */
|
/* return game_loop; */
|
||||||
case HELP:
|
case HELP:
|
||||||
return help_screen;
|
return help_loop;
|
||||||
default:
|
default:
|
||||||
return game_loop;
|
return game_loop;
|
||||||
}
|
}
|
||||||
@@ -288,6 +293,21 @@ void handle_click(Minefield* mf){
|
|||||||
}
|
}
|
||||||
mark_count += mf->cells[r * mf->col + c] & MARKED ? -1 : 1;
|
mark_count += mf->cells[r * mf->col + c] & MARKED ? -1 : 1;
|
||||||
mf->cells[r * mf->col + c] ^= MARKED;
|
mf->cells[r * mf->col + c] ^= MARKED;
|
||||||
|
|
||||||
|
bool sol = true;
|
||||||
|
if (mark_count == mf->mine_count){
|
||||||
|
for (int i = 0; i < mf->row * mf->col; i++){
|
||||||
|
cell cel = mf->cells[i] & (MARKED | MINE);
|
||||||
|
if (cel & MARKED && !(cel & MINE)){
|
||||||
|
sol = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sol){
|
||||||
|
printf("Solved\n");
|
||||||
|
quit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user