1
0
mirror of https://github.com/abakh/nbsdgames.git synced 2024-12-04 14:46:22 -05:00

improved handling of the scorefiles

This commit is contained in:
Your Name 2021-04-24 13:11:43 +04:30
parent 0035eb5238
commit 375d551487
8 changed files with 159 additions and 390 deletions

76
common.h Normal file
View File

@ -0,0 +1,76 @@
/*
Authored by abakh <abakh@tuta.io>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
#include "config.h"
#define FOPEN_FAIL -10
typedef signed char byte;
FILE* score_file;
byte score_write(const char* path, long wscore, byte save_to_num){// only saves the top 10, returns the place in the chart
score_file=fopen(path,"r");
if(!score_file){
score_file=fopen(path,"w");
if(!score_file){
return FOPEN_FAIL;
}
}
char name_buff[save_to_num][60];
long score_buff[save_to_num];
memset(name_buff,0,save_to_num*60*sizeof(char) );
memset(score_buff,0,save_to_num*sizeof(long) );
long scanned_score =0;
char scanned_name[60]={0};
byte location=0;
while( fscanf(score_file,"%59s : %ld\n",scanned_name,&scanned_score) == 2 && location<save_to_num){
strcpy(name_buff[location],scanned_name);
score_buff[location] = scanned_score;
++location;//so it doesn't save more scores than intented
memset(scanned_name,0,60);
scanned_score=0;
}
score_file = fopen(path,"w+") ;//will get rid of the previous text
byte scores_count=location;//if 5 scores were scanned, it is 5. the number of scores it reached
byte ret = -1;
bool wrote_it=0;
for(byte i=0;i<=scores_count && i<save_to_num-wrote_it;++i){
if(!wrote_it && (i>=scores_count || wscore>=score_buff[i]) ){
fprintf(score_file,"%s : %ld\n",getenv("USER"),wscore);
ret=i;
wrote_it=1;
}
if(i<save_to_num-wrote_it && i<scores_count){
fprintf(score_file,"%s : %ld\n",name_buff[i],score_buff[i]);
}
}
fflush(score_file);
return ret;
}
byte fallback_to_home(const char* name,long wscore,byte save_to_num){// only saves the top 10, returns the place in the chart
byte ret;
char full_path[1000]={0};
snprintf(full_path,1000,"%s/%s",SCORES_DIR,name);
ret=score_write(full_path,wscore,save_to_num);
if(ret==FOPEN_FAIL){
snprintf(full_path,1000,"%s/.%s",getenv("HOME"),name);
ret=score_write(full_path,wscore,save_to_num);
}
return ret;
}

View File

@ -2,19 +2,15 @@
//the default scorefiles
#ifdef Plan9
#define PP_SCORES "/sys/lib/games/pp_scores"
#define JW_SCORES "/sys/lib/games/jw_scores"
#define FSH_SCORES "/sys/lib/games/fsh_scores"
#define MNCH_SCORES "/sys/lib/games/mnch_scores"
#define MT_SCORES "/sys/lib/games/mt_scores"
#define DRT_SCORES "/sys/lib/games/drt_scores"
#define SCORES_DIR "/sys/lib/games/"
#else
#define PP_SCORES "/usr/games/pp_scores"
#define SCORES_DIR "/var/games/"
/* #define PP_SCORES "/usr/games/pp_scores"
#define JW_SCORES "/usr/games/jw_scores"
#define FSH_SCORES "/usr/games/fsh_scores"
#define MNCH_SCORES "/usr/games/mnch_scores"
#define MT_SCORES "/usr/games/mt_scores"
#define DRT_SCORES "/usr/games/drt_scores"
#define DRT_SCORES "/usr/games/drt_scores"*/
#endif
#ifdef Plan9

76
darrt.c
View File

@ -21,6 +21,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include <curses.h>
#include <unistd.h>
#include "config.h"
#include "common.h"
#define SAVE_TO_NUM 11
#define LEN 24
#define HLEN LEN/2
@ -46,7 +47,6 @@ int usleep(long usec) {
chtype colors[3]={0};
long score=0;
char error [150]={0};
FILE* scorefile;
chtype background[LEN][WID];
@ -288,68 +288,18 @@ void end_scene(){
refresh();
}
byte scorewrite(void){// only saves the top 10, returns the place in the chart
bool deforno;
if( !getenv("DRT_SCORES") && (scorefile= fopen(DRT_SCORES,"r")) ){
deforno=1;
}
else{
deforno=0;
if( !(scorefile = fopen(getenv("DRT_SCORES"),"r")) ){
sprintf(error,"No accessible score files found. You can make an empty text file in %s or set DRT_SCORES to such a file to solve this.",DRT_SCORES);
return -1;
}
}
byte save_score(void){
return fallback_to_home("darrt_scores",score,SAVE_TO_NUM);
char namebuff[SAVE_TO_NUM][60];
long scorebuff[SAVE_TO_NUM];
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
long fuckingscore =0;
char fuckingname[60]={0};
byte location=0;
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
strcpy(namebuff[location],fuckingname);
scorebuff[location] = fuckingscore;
++location;
memset(fuckingname,0,60);
fuckingscore=0;
}
if(deforno)
scorefile = fopen(DRT_SCORES,"w+");//get rid of the previous text first
else
scorefile = fopen(getenv("DRT_SCORES"), "w+") ;
if(!scorefile){
strcpy(error, "The file cannot be opened in w+. ");
return -1;
}
byte itreached=location;
byte ret = -1;
bool wroteit=0;
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;++location){
if(!wroteit && score>=scorebuff[location] ){
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
ret=location;
wroteit=1;
}
if(location<SAVE_TO_NUM-wroteit && location<itreached)
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
}
fflush(scorefile);
return ret;
}
void showscores(byte playerrank){
void show_scores(byte playerrank){
byte y,x;
attron(colors[3]);
filled_rect(0,0,LEN,WID);
red_border();
if(*error){
mvaddstr(1,0,error);
if(playerrank==FOPEN_FAIL){
mvaddstr(1,0,"Could not open score file.");
mvprintw(2,0,"However, your score is %ld.",score);
refresh();
return;
@ -357,9 +307,9 @@ void showscores(byte playerrank){
if(playerrank == 0){
char formername[60]={0};
long formerscore=0;
rewind(scorefile);
fscanf(scorefile,"%*s : %*d");
if ( fscanf(scorefile,"%s : %ld",formername,&formerscore)==2 && formerscore>0){
rewind(score_file);
fscanf(score_file,"%*s : %*d");
if ( fscanf(score_file,"%s : %ld",formername,&formerscore)==2 && formerscore>0){
byte a = (LEN-9)/2;
star_line(1);
star_line(LEN-2);
@ -387,10 +337,10 @@ void showscores(byte playerrank){
char pname[60] = {0};
long pscore=0;
byte rank=0;
rewind(scorefile);
rewind(score_file);
mvaddstr(1,WID/2-4,"HIGH SCORES");
attron(colors[3]);
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
while( rank<SAVE_TO_NUM && fscanf(score_file,"%s : %ld\n",pname,&pscore) == 2){
star_line(2+2*rank);
move(2+2*rank,1);
if(rank == playerrank)
@ -485,7 +435,7 @@ int main(void){
cbreak();
curs_set(1);
end_scene();
showscores(scorewrite());
show_scores(save_score());
attron(colors[0]|A_STANDOUT);
mvprintw(LEN-1,HWID-11,"Wanna play again? (y/n)");
attroff(colors[0]|A_STANDOUT);

View File

@ -20,6 +20,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include <curses.h>
#include <unistd.h>
#include "config.h"
#include "common.h"
#define SAVE_TO_NUM 11
#define HOOKS 10
#define LEN 24
@ -49,9 +50,7 @@ unsigned int count[10]={0};
unsigned long score=0;
const char sym[]="~:=!@+><;&";
byte hook=0, hooknum=0;
char error [150]={0};
byte clb,clbtime=0;
FILE* scorefile;
int input;
byte digit_count(int num){
@ -185,68 +184,19 @@ void draw(void){
attroff(colors[1]);
}
byte scorewrite(void){// only saves the top 10, returns the place in the chart
bool deforno;
if( !getenv("FSH_SCORES") && (scorefile= fopen(FSH_SCORES,"r")) ){
deforno=1;
}
else{
deforno=0;
if( !(scorefile = fopen(getenv("FSH_SCORES"),"r")) ){
sprintf(error,"No accessible score files found. You can make an empty text file in %s or set FSH_SCORES to such a file to solve this.",FSH_SCORES);
return -1;
}
}
byte save_score(void){
return fallback_to_home("fisher_scores",score,SAVE_TO_NUM);
char namebuff[SAVE_TO_NUM][60];
long scorebuff[SAVE_TO_NUM];
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
long fuckingscore =0;
char fuckingname[60]={0};
byte location=0;
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
strcpy(namebuff[location],fuckingname);
scorebuff[location] = fuckingscore;
++location;
memset(fuckingname,0,60);
fuckingscore=0;
}
if(deforno)
scorefile = fopen(FSH_SCORES,"w+");//get rid of the previous text first
else
scorefile = fopen(getenv("FSH_SCORES"), "w+") ;
if(!scorefile){
strcpy(error, "The file cannot be opened in w+. ");
return -1;
}
byte itreached=location;
byte ret = -1;
bool wroteit=0;
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;++location){
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
ret=location;
wroteit=1;
}
if(location<SAVE_TO_NUM-wroteit && location<itreached)
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
}
fflush(scorefile);
return ret;
}
void showscores(byte playerrank){
void show_scores(byte playerrank){
byte y,x;
attron(colors[3]);
filled_rect(0,0,LEN,WID);
green_border();
if(*error){
mvaddstr(1,0,error);
if(playerrank==FOPEN_FAIL){
mvaddstr(1,0,"Could not open score file");
mvprintw(2,0,"However, your score is %ld.",score);
refresh();
return;
@ -254,9 +204,9 @@ void showscores(byte playerrank){
if(playerrank == 0){
char formername[60]={0};
long formerscore=0;
rewind(scorefile);
fscanf(scorefile,"%*s : %*d");
if ( fscanf(scorefile,"%s : %ld",formername,&formerscore)==2 && formerscore>0){
rewind(score_file);
fscanf(score_file,"%*s : %*d");
if ( fscanf(score_file,"%s : %ld",formername,&formerscore)==2 && formerscore>0){
byte a = (LEN-9)/2;
star_line(1);
star_line(LEN-2);
@ -284,10 +234,10 @@ void showscores(byte playerrank){
char pname[60] = {0};
long pscore=0;
byte rank=0;
rewind(scorefile);
rewind(score_file);
mvaddstr(1,WID/2-4,"HIGH SCORES");
attron(colors[3]);
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
while( rank<SAVE_TO_NUM && fscanf(score_file,"%s : %ld\n",pname,&pscore) == 2){
star_line(2+2*rank);
move(2+2*rank,1);
if(rank == playerrank)
@ -424,7 +374,7 @@ int main(void){
nocbreak();
cbreak();
curs_set(1);
showscores(scorewrite());
show_scores(save_score());
attron(colors[2]);
mvprintw(LEN-1,HWID-11,"Wanna play again? (y/n)");
attroff(colors[2]);

View File

@ -19,6 +19,7 @@ TODO make it like puyo puyo instead of the remake of what i poorly remembered*/
#include <string.h>
#include <stdlib.h>
#include "config.h"
#include "common.h"
#define LEN 17
#define WID 19
#define DELAY 2
@ -32,70 +33,22 @@ byte jx,jy; //first jewel's position
byte kx,ky;//second jewel's position in relation to that of j
long score=0;
char* controls = "j,l-Move k-Rotate p-Pause q-Quit";
FILE* scorefile;
byte scorewrite(long score){// only saves the top 10
bool deforno;
if( !getenv("JW_SCORES") && (scorefile= fopen(JW_SCORES,"r")) ){
deforno=1;
}
else{
deforno=0;
if( !(scorefile = fopen(getenv("JW_SCORES"),"r")) ){
fprintf(stderr,"\nNo accessible score files found. You can make an empty text file in %s or set JW_SCORES to such a file to solve this. \n",JW_SCORES);
exit(EXIT_SUCCESS);
}
}
char namebuff[SAVE_TO_NUM][60];
long scorebuff[SAVE_TO_NUM];
byte save_score(void){
return fallback_to_home("jewels_scores",score,SAVE_TO_NUM);
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
long fuckingscore =0;
char fuckingname[60]={0};
byte location=0;
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
strcpy(namebuff[location],fuckingname);
scorebuff[location] = fuckingscore;
++location;
memset(fuckingname,0,60);
fuckingscore=0;
}
if(deforno)
scorefile = fopen(JW_SCORES,"w+");//get rid of the text
else
scorefile = fopen(getenv("JW_SCORES"), "w+") ;
if(!scorefile){
printf("\nThe file cannot be opened in w+.\n");
exit(EXIT_SUCCESS);
}
byte itreached=location;
byte ret = -1;
bool wroteit=0;
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;++location){
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
ret=location;
wroteit=1;
}
if(location<SAVE_TO_NUM-wroteit && location<itreached)
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
}
fflush(scorefile);
return ret;
}
void showscores(byte playerrank){
void show_scores(byte playerrank){
if(playerrank==FOPEN_FAIL){
printf("Could not open scorefile.");
abort();
}
if(playerrank == 0){
char formername[60]={0};
long formerscore=0;
rewind(scorefile);
fscanf(scorefile,"%*s : %*d\n");
if ( fscanf(scorefile,"%s : %ld\n",formername,&formerscore)==2){
rewind(score_file);
fscanf(score_file,"%*s : %*d\n");
if ( fscanf(score_file,"%s : %ld\n",formername,&formerscore)==2){
printf("\n*****CONGRATULATIONS!****\n");
printf(" _____ You bet the\n");
printf(" .' | previous\n");
@ -114,15 +67,16 @@ void showscores(byte playerrank){
char pname[60] = {0};
long pscore=0;
byte rank=0;
rewind(scorefile);
rewind(score_file);
printf("\n>*>*>Top %d<*<*<\n",SAVE_TO_NUM);
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
while( rank<SAVE_TO_NUM && fscanf(score_file,"%s : %ld\n",pname,&pscore) == 2){
if(rank == playerrank)
printf(">>>");
printf("%d) %s : %ld\n",rank+1,pname,pscore);
++rank;
}
putchar('\n');
fclose(score_file);
}
//apply gravity
bool fall(void){
@ -386,6 +340,6 @@ int main(void){
endwin();
printf("%s _Jewels_ %s\n",jwstr,jwstr);
printf("You have scored %ld points.\n",score);
showscores(scorewrite(score));
show_scores(save_score());
return EXIT_SUCCESS;
}

View File

@ -18,6 +18,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include <unistd.h>
#include <stdbool.h>
#include "config.h"
#include "common.h"
#define SAVE_TO_NUM 10
#define MINLEN 10
#define MAXLEN 24
@ -57,7 +58,6 @@ byte pse_msg=100;
//no need to a epilepsy variable like in muncher as zeroing the colors suffices
FILE *scorefile;
char error[150]={0};
void move_tron(void){
switch(direction){
@ -87,68 +87,16 @@ void logo(void){
mvaddstr(1,0,"|\\/|");
mvaddstr(2,0,"| |IKETRON");
}
byte scorewrite(void){// only saves the top 10, returns the place in the chart
bool deforno;
if( !getenv("MT_SCORES") && (scorefile= fopen(MT_SCORES,"r")) ){
deforno=1;
}
else{
deforno=0;
if( !(scorefile = fopen(getenv("MT_SCORES"),"r")) ){
sprintf(error,"No accessible score files found. You can make an empty text file in %s or set MT_SCORES to such a file to solve this.",MT_SCORES);
return -1;
}
}
byte save_score(void){
return fallback_to_home("miketron_scores",score,SAVE_TO_NUM);
char namebuff[SAVE_TO_NUM][60];
long scorebuff[SAVE_TO_NUM];
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
long fuckingscore=0;
char fuckingname[60]={0};
byte location=0;
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
strcpy(namebuff[location],fuckingname);
scorebuff[location] = fuckingscore;
++location;
memset(fuckingname,0,60);
fuckingscore=0;
}
if(deforno)
scorefile = fopen(MT_SCORES,"w+");//get rid of the previous text first
else
scorefile = fopen(getenv("MT_SCORES"), "w+") ;
if(!scorefile){
strcpy(error, "The file cannot be opened in w+. ");
return -1;
}
byte itreached=location;
byte ret = -1;
bool wroteit=0;
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;++location){
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
ret=location;
wroteit=1;
}
if(location<SAVE_TO_NUM-wroteit && location<itreached)
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
}
fflush(scorefile);
return ret;
}
void showscores(byte playerrank){
void show_scores(byte playerrank){
erase();
logo();
if(*error){
mvaddstr(3,0,error);
if(playerrank==FOPEN_FAIL){
mvaddstr(3,0,"Couldn't open scorefile.");
printw("\nHowever, your score is %ld.",score);
refresh();
return;
@ -156,11 +104,11 @@ void showscores(byte playerrank){
if(playerrank == 0){
char formername[60]={0};
long formerscore=0;
rewind(scorefile);
fscanf(scorefile,"%*s : %*d\n");
rewind(score_file);
fscanf(score_file,"%*s : %*d\n");
move(3,0);
byte b=0;
if ( fscanf(scorefile,"%s : %ld\n",formername,&formerscore)==2){
if ( fscanf(score_file,"%s : %ld\n",formername,&formerscore)==2){
halfdelay(1);
printw("*****CONGRATULATIONS!****\n");
printw(" You bet the\n");
@ -201,9 +149,9 @@ void showscores(byte playerrank){
char pname[60] = {0};
long pscore=0;
byte rank=0;
rewind(scorefile);
rewind(score_file);
printw(">*>*>Top %d<*<*<\n",SAVE_TO_NUM);
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
while( rank<SAVE_TO_NUM && fscanf(score_file,"%s : %ld\n",pname,&pscore) == 2){
if(rank == playerrank)
printw(">>>");
printw("%d) %s : %ld\n",rank+1,pname,pscore);
@ -568,7 +516,7 @@ int main(int argc, char** argv){
refresh();
mvprintw(len+5,0,"Game over! Press a key to see the high scores:");
getch();
showscores(scorewrite());
show_scores(save_score());
printw("Game over! Wanna play again?(y/n)");
curs_set(1);
input=getch();

View File

@ -18,6 +18,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include <signal.h>
#include <unistd.h>
#include "config.h"
#include "common.h"
#define SAVE_TO_NUM 10
#define MINLEN 10
#define MAXLEN 24
@ -60,74 +61,22 @@ long score;
chtype colors[6]={0};
FILE* scorefile;
char error[150]={0};
void logo(void){
mvaddstr(1,0,"|\\/|");
mvaddstr(2,0,"| |UNCHER");
}
byte scorewrite(void){// only saves the top 10, returns the place in the chart
bool deforno;
if( !getenv("MNCH_SCORES") && (scorefile= fopen(MNCH_SCORES,"r")) ){
deforno=1;
}
else{
deforno=0;
if( !(scorefile = fopen(getenv("MNCH_SCORES"),"r")) ){
sprintf(error,"No accessible score files found. You can make an empty text file in %s or set MNCH_SCORES to such a file to solve this.",MNCH_SCORES);
return -1;
}
}
char namebuff[SAVE_TO_NUM][60];
long scorebuff[SAVE_TO_NUM];
byte save_score(void){
return fallback_to_home("muncher_scores",score,SAVE_TO_NUM);
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
long fuckingscore =0;
char fuckingname[60]={0};
byte location=0;
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
strcpy(namebuff[location],fuckingname);
scorebuff[location] = fuckingscore;
++location;
memset(fuckingname,0,60);
fuckingscore=0;
}
if(deforno)
scorefile = fopen(MNCH_SCORES,"w+");//get rid of the previous text first
else
scorefile = fopen(getenv("MNCH_SCORES"), "w+") ;
if(!scorefile){
strcpy(error, "The file cannot be opened in w+. ");
return -1;
}
byte itreached=location;
byte ret = -1;
bool wroteit=0;
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;++location){
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
ret=location;
wroteit=1;
}
if(location<SAVE_TO_NUM-wroteit && location<itreached)
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
}
fflush(scorefile);
return ret;
}
void showscores(byte playerrank){
void show_scores(byte playerrank){
erase();
logo();
if(*error){
mvaddstr(3,0,error);
if(playerrank==FOPEN_FAIL){
mvaddstr(3,0,"Could not open score file");
printw("\nHowever, your score is %ld.",score);
refresh();
return;
@ -135,11 +84,11 @@ void showscores(byte playerrank){
if(playerrank == 0){
char formername[60]={0};
long formerscore=0;
rewind(scorefile);
fscanf(scorefile,"%*s : %*d\n");
rewind(score_file);
fscanf(score_file,"%*s : %*d\n");
move(3,0);
byte b=0;
if ( fscanf(scorefile,"%s : %ld\n",formername,&formerscore)==2){
if ( fscanf(score_file,"%s : %ld\n",formername,&formerscore)==2){
halfdelay(1);
printw("*****CONGRATULATIONS!****\n");
printw(" You bet the\n");
@ -180,9 +129,9 @@ void showscores(byte playerrank){
char pname[60] = {0};
long pscore=0;
byte rank=0;
rewind(scorefile);
rewind(score_file);
printw(">*>*>Top %d<*<*<\n",SAVE_TO_NUM);
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
while( rank<SAVE_TO_NUM && fscanf(score_file,"%s : %ld\n",pname,&pscore) == 2){
if(rank == playerrank)
printw(">>>");
printw("%d) %s : %ld\n",rank+1,pname,pscore);
@ -498,7 +447,7 @@ int main(int argc, char** argv){
refresh();
mvprintw(len+5,0,"Game over! Press a key to see the high scores:");
getch();
showscores(scorewrite());
show_scores(save_score());
printw("Game over! Wanna play again?(y/n)");
curs_set(1);
input=getch();

80
pipes.c
View File

@ -18,6 +18,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include <time.h>
#include <signal.h>
#include "config.h"
#include "common.h"
#define UP 1
#define RIGHT 2
#define DOWN 4
@ -44,8 +45,6 @@ int py,px,fy,fx;//p: pointer f: fluid
bitbox tocome[5]={0};//the row of pipes in the left side
chtype green=A_BOLD;//will use bold font instead of green if colors are not available
long score;
FILE* scorefile;
char error [150]={0};
void logo(void){
mvprintw(0,0," _ ");
@ -53,68 +52,16 @@ void logo(void){
mvprintw(2,0,"| IPES");
}
byte scorewrite(void){// only saves the top 10, returns the place in the chart
bool deforno;
if( !getenv("PP_SCORES") && (scorefile= fopen(PP_SCORES,"r")) ){
deforno=1;
}
else{
deforno=0;
if( !(scorefile = fopen(getenv("PP_SCORES"),"r")) ){
sprintf(error,"No accessible score files found. You can make an empty text file in %s or set PP_SCORES to such a file to solve this.",PP_SCORES);
return -1;
}
}
byte save_score(void){
return fallback_to_home("pipes_scores",score,SAVE_TO_NUM);
char namebuff[SAVE_TO_NUM][60];
long scorebuff[SAVE_TO_NUM];
memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) );
memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) );
long fuckingscore =0;
char fuckingname[60]={0};
byte location=0;
while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location<SAVE_TO_NUM ){
strcpy(namebuff[location],fuckingname);
scorebuff[location] = fuckingscore;
++location;
memset(fuckingname,0,60);
fuckingscore=0;
}
if(deforno)
scorefile = fopen(PP_SCORES,"w+");//get rid of the previous text first
else
scorefile = fopen(getenv("PP_SCORES"), "w+") ;
if(!scorefile){
strcpy(error, "The file cannot be opened in w+. ");
return -1;
}
byte itreached=location;
byte ret = -1;
bool wroteit=0;
for(location=0;location<=itreached && location<SAVE_TO_NUM-wroteit;++location){
if(!wroteit && (location>=itreached || score>=scorebuff[location]) ){
fprintf(scorefile,"%s : %ld\n",getenv("USER"),score);
ret=location;
wroteit=1;
}
if(location<SAVE_TO_NUM-wroteit && location<itreached)
fprintf(scorefile,"%s : %ld\n",namebuff[location],scorebuff[location]);
}
fflush(scorefile);
return ret;
}
void showscores(byte playerrank){
void show_scores(byte playerrank){
erase();
logo();
if(*error){
mvaddstr(SY,SX,error);
if(playerrank==FOPEN_FAIL){
mvaddstr(SY,SX,"Couldn't open scorefile");
mvprintw(SY+1,SX,"However, your score is %ld.",score);
refresh();
return;
@ -122,9 +69,9 @@ void showscores(byte playerrank){
if(playerrank == 0){
char formername[60]={0};
long formerscore=0;
rewind(scorefile);
fscanf(scorefile,"%*s : %*d");
if ( fscanf(scorefile,"%s : %ld",formername,&formerscore)==2 && formerscore>0){
rewind(score_file);
fscanf(score_file,"%*s : %*d");
if ( fscanf(score_file,"%s : %ld",formername,&formerscore)==2 && formerscore>0){
byte a = (len-9)/2;
attron(A_BOLD);
mvprintw(SY,SX, "**** ***");
@ -159,8 +106,8 @@ void showscores(byte playerrank){
char pname[60] = {0};
long pscore=0;
byte rank=0;
rewind(scorefile);
while( rank<SAVE_TO_NUM && fscanf(scorefile,"%s : %ld\n",pname,&pscore) == 2){
rewind(score_file);
while( rank<SAVE_TO_NUM && fscanf(score_file,"%s : %ld\n",pname,&pscore) == 2){
move(SY+1+rank,SX+1);
attron(green);
if(rank == playerrank)
@ -394,7 +341,6 @@ int main(int argc, char** argv){
flow=0;
fast=0;
score=0;
memset(error,0,150);
memset(board,0,len*wid);
fy=1+(rand()%(len-2) );
fx=1+(rand()%(wid-2) );
@ -525,7 +471,7 @@ int main(int argc, char** argv){
if(input == 'N' || input=='n' || input =='q')
sigint_handler(EXIT_SUCCESS);
showscores(scorewrite());
show_scores(save_score());
mvprintw(len+2,0,"Press a key to exit:");
refresh();
getch();
@ -543,7 +489,7 @@ int main(int argc, char** argv){
draw(board);
mvprintw(len+2,0,"Game over! Press a key to see the high scores:");
getch();
showscores(scorewrite());
show_scores(save_score());
mvprintw(len+2,0,"Game over!");
printw(" Wanna play again?(y/n)");
input=getch();