Upload files to ''

This commit is contained in:
Gladosse 2022-01-03 20:13:33 +01:00
parent 83f5952850
commit 6d14549463
5 changed files with 392 additions and 0 deletions

214
board.c Normal file
View File

@ -0,0 +1,214 @@
#include <stdlib.h>
#include <stdio.h>
#include <gint/rtc.h>
#include <gint/display.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include "board.h"
extern bopti_image_t img_cursor;
void set_board(struct Board *brd){
int height = brd->height;
int width = brd->width;
brd->board_state = malloc(sizeof(struct Cell*) * height);
for(int i=0; i<height; i++){
brd->board_state[i] = malloc(sizeof(struct Cell) * width);
}
for(int v=0;v<height;v++){
for(int z=0;z<width;z++){
brd->board_state[v][z].value=0; //assign all cells 0 (no mine)
brd->board_state[v][z].state=0;
brd->board_state[v][z].flagged=0;
}
}
}
int around(int x, int y, int a, int b, struct Board* brd){
int height = brd->height;
int width = brd->width;
for(int v=y-1;v<=y+1;v++){
for(int z=x-1;z<=x+1;z++){
if(z<width && v<height && z>=0 && v>=0){
if(z==a && v==b)
return 1;
}
}
}
return 0;
}
void gen_board(int a, int b, struct Board *brd){
int height = brd->height;
int width = brd->width;
for(int i=0; i<brd->nmines; i++){
int y = rand()%brd->height;
int x = rand()%brd->width;
srand(rtc_ticks());
while(brd->board_state[y][x].value==69 || around(a,b,x,y,brd)){
y = rand()%brd->height;
x = rand()%brd->width;
}
brd->board_state[y][x].value=69;
for(int v=y-1;v<=y+1;v++){
for(int z=x-1;z<=x+1;z++){
if(z<width && v<height && z>=0 && v>=0){
if(brd->board_state[v][z].value!=69)
brd->board_state[v][z].value++;
}
}
}
}
}
void draw_board(struct Board *brd){
extern bopti_image_t img_nums;
extern bopti_image_t img_bomb;
extern bopti_image_t img_desert_cell;
extern bopti_image_t img_flag;
int height = brd->height;
int width = brd->width;
for(int y=0; y<height; y++){
for(int x=0; x<width;x++){
if(brd->board_state[y][x].state){
int value = brd->board_state[y][x].value;
if(value==69){
dsubimage(x*20+7,y*20+40,&img_bomb,0,0,20,20, DIMAGE_NONE);
}else if(value==0){
dsubimage(x*20+7,y*20+40,&img_nums,160,0,180+20,20, DIMAGE_NONE);
}
else{
if(x*20<width*20)
dsubimage(x*20+7,y*20+40,&img_nums,(value-1)*20,0,value*20+20,20, DIMAGE_NONE);
}
}else{
if(brd->board_state[y][x].flagged==1){
dsubimage(x*20+7,y*20+40,&img_flag,0,0,20,20, DIMAGE_NONE);
}else{
dsubimage(x*20+7,y*20+40,&img_desert_cell,0,0,20,20,DIMAGE_NONE);
}
}
if(brd->board_state[y][x].flagged==1)
dsubimage(x*20+7,y*20+40,&img_flag,0,0,20,20, DIMAGE_NONE);
}
}
}
void draw_cursor(struct Board *brd){
dimage(brd->pos[0]*20+7,brd->pos[1]*20+40,&img_cursor);
}
int show_cell(int x,int y, struct Board *brd){
int height = brd->height;
int width = brd->width;
if (x<width && y<height && x>=0 && y>=0 && brd->board_state[y][x].flagged==0)
brd->board_state[y][x].state=1;
if (brd->board_state[y][x].value==0) { // invisible
for (int v =y-1; v<=y+1;v++) {
for (int z=x-1;z<=x+1;z++) {
if (z<width && v<height && z>=0 && v>=0 && brd->board_state[y][x].flagged==0) {
if (brd->board_state[v][z].value != 69 && brd->board_state[v][z].state == 0) { // not mine and not visible and not flagged
show_cell(z,v,brd);
}
}
}
}
}
return 0;
}
int reveal(int x, int y, struct Board *brd){
int height = brd->height;
int width = brd->width;
int flags = 0;
for(int v=y-1;v<=y+1;v++){
for(int z=x-1;z<=x+1;z++){
if(z<width && v<height && z>=0 && v>=0){
if(brd->board_state[v][z].flagged == 1)
flags++;
}
}
}
if(brd->board_state[y][x].value==flags && brd->board_state[y][x].value!=0){
for(int v=y-1;v<=y+1;v++){
for(int z=x-1;z<=x+1;z++){
if(z<width && v<height && z>=0 && v>=0){
if(brd->board_state[v][z].flagged==0){
brd->board_state[v][z].state=1;
if(brd->board_state[v][z].value==69)
return 1;
}
}
}
}
for(int v=y-1;v<=y+1;v++){
for(int z=x-1;z<=x+1;z++){
if(z<width && v<height && z>=0 && v>=0){
if(brd->board_state[v][z].state==1 && brd->board_state[v][z].value==0)
show_cell(z,v,brd);
}
}
}
}
return 0;
}
void flag_cell(int x,int y, struct Board *brd){
int height = brd->height;
int width = brd->width;
if (x<width && y<height && x>=0 && y>=0 && brd->board_state[y][x].state==0)
brd->board_state[y][x].flagged=!brd->board_state[y][x].flagged;
}
void lose(int a, int b, struct Board *brd){
extern bopti_image_t img_nums;
extern bopti_image_t img_bomb;
extern bopti_image_t img_desert_cell;
extern bopti_image_t img_flag;
extern bopti_image_t img_red_bomb;
extern bopti_image_t img_cross_bomb;
int height = brd->height;
int width = brd->width;
for(int y=0; y<height; y++){
for(int x=0; x<width;x++){
if(brd->board_state[y][x].state){
int value = brd->board_state[y][x].value;
if(value==69){
dsubimage(x*20+7,y*20+40,&img_bomb,0,0,20,20, DIMAGE_NONE);
}else if(value==0){
dsubimage(x*20+7,y*20+40,&img_nums,160,0,180+20,20, DIMAGE_NONE);
}
else{
if(x*20<width*20)
dsubimage(x*20+7,y*20+40,&img_nums,(value-1)*20,0,value*20+20,20, DIMAGE_NONE);
}
}else{
if(brd->board_state[y][x].flagged==1){
if(brd->board_state[y][x].value==69)
dsubimage(x*20+7,y*20+40,&img_flag,0,0,20,20, DIMAGE_NONE);
else
dsubimage(x*20+7,y*20+40,&img_cross_bomb,0,0,20,20, DIMAGE_NONE);
}else if(brd->board_state[y][x].value==69){
dsubimage(x*20+7,y*20+40,&img_bomb,0,0,20,20, DIMAGE_NONE);
}
else{
dsubimage(x*20+7,y*20+40,&img_desert_cell,0,0,20,20,DIMAGE_NONE);
}
}
if(brd->board_state[y][x].flagged==1)
dsubimage(x*20+7,y*20+40,&img_flag,0,0,20,20, DIMAGE_NONE);
}
}
dsubimage(a*20+7,b*20+40,&img_red_bomb,0,0,20,20, DIMAGE_NONE);
}

40
board.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef _PRINCEOFMYNES_BOARD_H
#define _PRINCEOFMYNES_BOARD_H
#define MINE 1
#define ENGINE_TICK 25
#define DIR_UP 1
#define DIR_DOWN 2
#define DIR_RIGHT 3
#define DIR_LEFT 4
#define DIR_IDLE 0
#include <gint/display.h>
#include <gint/timer.h>
#include <gint/clock.h>
struct Cell{
int value; /*69 for a mine*/
int state; /* 0-> not shown, 1-> shown*/
int flagged; /* 0-> not flagged, 1-> flagged*/
};
struct Board{
int height; /*in cells*/
int width; /*in cells*/
int nmines;
int pos[2]; /*[0]->pos x, [1]->pos y*/
struct Cell **board_state;
};
void gen_board(int, int, struct Board*);
void draw_board(struct Board*);
void draw_cursor(struct Board*);
int show_cell(int ,int, struct Board*);
void flag_cell(int ,int, struct Board*);
int reveal(int ,int, struct Board*);
void set_board(struct Board*);
void lose(int, int, struct Board*);
int around(int, int, int, int, struct Board*);
#endif

90
main.c Normal file
View File

@ -0,0 +1,90 @@
#include <gint/display.h>
#include <gint/keyboard.h>
#include "board.h"
#include "timer.h"
int main(void)
{
extern bopti_image_t img_bugdenial;
dclear(C_WHITE);
dupdate();
int key = 0;
while(key!=KEY_MENU){
key = 0;
int hasgen = 0;
int time = 0;
struct Board board;
board.height = 9;
board.width = 19;
board.nmines = 21;
board.pos[0] = 5;
board.pos[1] = 5;
struct Board *brd = &board;
set_board(brd);
dclear(C_WHITE);
draw_board(brd);
dimage(brd->width*20+7,10,&img_bugdenial);
int trollface = 0;
int trollfaced[2] = {5,5};
while(!trollface){
dclear(C_WHITE);
draw_board(brd);
dimage(brd->width*20+7,10,&img_bugdenial);
draw_cursor(brd);
draw_timer(time, brd);
dupdate();
key = getkey_opt(GETKEY_DEFAULT & ~GETKEY_MOD_SHIFT & ~GETKEY_MOD_ALPHA, NULL).key;
if(key==KEY_DOWN && brd->pos[1]<brd->height-1)
brd->pos[1]++;
else if(key==KEY_UP && brd->pos[1]>0)
brd->pos[1]--;
else if(key==KEY_RIGHT && brd->pos[0]<brd->width-1)
brd->pos[0]++;
else if(key==KEY_LEFT && brd->pos[0]>0)
brd->pos[0]--;
else if(key == 0x71)
flag_cell(brd->pos[0],brd->pos[1],brd);
else if(key == 0x81){ //shift or exe
if(!hasgen){
gen_board(brd->pos[0],brd->pos[1],brd);
hasgen = 1;
}
struct Cell cell = brd->board_state[brd->pos[1]][brd->pos[0]];
if(cell.value==69 && cell.flagged==0){
trollface = 1;
trollfaced[0] = brd->pos[0];
trollfaced[1] = brd->pos[1];
break;
}
show_cell(brd->pos[0],brd->pos[1],brd);
int rvl = reveal(brd->pos[0],brd->pos[1],brd);
if(rvl == 1){
trollface = 1;
trollfaced[0] = brd->pos[0];
trollfaced[1] = brd->pos[1];
break;
}
}
draw_cursor(brd);
}
while(trollface == 1){
dclear(C_WHITE);
lose(trollfaced[0],trollfaced[1], brd);
dimage(brd->width*20+7,10,&img_bugdenial);
dupdate();
key = getkey_opt(GETKEY_DEFAULT & ~GETKEY_MOD_SHIFT & ~GETKEY_MOD_ALPHA, NULL).key;
if(key == KEY_OPTN){
trollface = 0;
trollfaced[0] = 5;
trollfaced[1] = 5;
hasgen = 0;
}
}
}
return 1;
}

37
timer.c Normal file
View File

@ -0,0 +1,37 @@
#include <stdlib.h>
#include <stdio.h>
#include <gint/rtc.h>
#include <gint/display.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include "timer.h"
#include "board.h"
void draw_timer(int seconds, struct Board *brd){
extern bopti_image_t img_digits;
int digits[3];
int i=2;
while(i>=0){
digits[i] = seconds % 10;
seconds = seconds/10;
i--;
}
dsubimage(40, 7, &img_digits, 154, 0, 159, 27, DIMAGE_NONE);
if(digits[0]==0)
dsubimage(45, 7, &img_digits, 126, 0, 140, 27, DIMAGE_NONE);
else
dsubimage(45, 7, &img_digits, 14*(digits[0]-1), 0, 14*digits[0], 27, DIMAGE_NONE);
if(digits[1]==0)
dsubimage(45+14, 7, &img_digits, 126, 0, 140, 27, DIMAGE_NONE);
else
dsubimage(45+14, 7, &img_digits, 14*(digits[1]-1), 0, 14*digits[1], 27, DIMAGE_NONE);
if(digits[2]==0)
dsubimage(45+28, 7, &img_digits, 126, 0, 140, 27, DIMAGE_NONE);
else
dsubimage(45+28, 7, &img_digits, 14*(digits[2]-1), 0, 14*digits[2], 27, DIMAGE_NONE);
dsubimage(45+42, 7, &img_digits, 161, 0, 164, 27, DIMAGE_NONE);
}

11
timer.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef _PRINCEOFMYNES_TIMER_H
#define _PRINCEOFMYNES_TIMER_H
#include <gint/display.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include "board.h"
void draw_timer(int, struct Board*);
#endif