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);
|
||||
|
||||
typedef enum screens {
|
||||
DEFAULT_SCREEN,
|
||||
GAME_LOOP,
|
||||
GAME_OVER,
|
||||
HELP,
|
||||
@@ -54,7 +55,7 @@ typedef enum screens {
|
||||
|
||||
Screen game_loop(Minefield* mf);
|
||||
/* Screen game_over(Minefield* mf); */
|
||||
Screen help_screen(Minefield* mf);
|
||||
Screen help_loop(Minefield *mf);
|
||||
|
||||
Screen (*screen_fn(Screen scr))(Minefield *);
|
||||
Screen last_screen;
|
||||
@@ -64,12 +65,14 @@ const int screenHeight = 450;
|
||||
float cell_size;
|
||||
int mark_count;
|
||||
bool dead;
|
||||
bool quit;
|
||||
|
||||
int main(){
|
||||
srand(time(NULL));
|
||||
Minefield mf = make_minefield(10, 20, 30);
|
||||
/* print_minefield(&mf, stdout); */
|
||||
dead = false;
|
||||
quit = false;
|
||||
mark_count = 0;
|
||||
{
|
||||
float width = (float) screenWidth / mf.col;
|
||||
@@ -81,10 +84,13 @@ int main(){
|
||||
SetTargetFPS(60);
|
||||
|
||||
Screen screen = GAME_LOOP;
|
||||
Screen temp_screen;
|
||||
Screen (*loop)(Minefield*);
|
||||
while(!WindowShouldClose()){
|
||||
while(!WindowShouldClose() && !quit){
|
||||
loop = screen_fn(screen);
|
||||
screen = (*loop)(&mf);
|
||||
temp_screen = (*loop)(&mf);
|
||||
last_screen = screen;
|
||||
screen = temp_screen;
|
||||
}
|
||||
|
||||
free_minefield(&mf);
|
||||
@@ -93,10 +99,10 @@ int main(){
|
||||
}
|
||||
|
||||
Screen game_loop(Minefield* mf){
|
||||
while(!WindowShouldClose()){
|
||||
while(!WindowShouldClose() && !quit){
|
||||
|
||||
if (IsKeyPressed(KEY_H)){
|
||||
break;
|
||||
return HELP;
|
||||
}
|
||||
|
||||
if (!dead){
|
||||
@@ -108,11 +114,10 @@ Screen game_loop(Minefield* mf){
|
||||
draw_minefield(mf);
|
||||
EndDrawing();
|
||||
}
|
||||
last_screen = GAME_LOOP;
|
||||
return HELP;
|
||||
return DEFAULT_SCREEN;
|
||||
}
|
||||
|
||||
Screen help_screen(Minefield* mf){
|
||||
Screen help_loop(Minefield *mf) {
|
||||
while (!WindowShouldClose() && IsKeyDown(KEY_H)){
|
||||
|
||||
BeginDrawing();
|
||||
@@ -131,7 +136,7 @@ Screen (*screen_fn(Screen scr))(Minefield *) {
|
||||
/* case GAME_OVER: */
|
||||
/* return game_loop; */
|
||||
case HELP:
|
||||
return help_screen;
|
||||
return help_loop;
|
||||
default:
|
||||
return game_loop;
|
||||
}
|
||||
@@ -288,6 +293,21 @@ void handle_click(Minefield* mf){
|
||||
}
|
||||
mark_count += mf->cells[r * mf->col + c] & MARKED ? -1 : 1;
|
||||
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