Posted
over 10 years
ago
I had developed this game a long ago, and would like to share with the world.Yes, ofcourse, we can optimize the code of c here, but i have concentrated only on output for this. Once, i got the desired result, i haven't looked into any of the line in
... [More]
this code. {Very bad habbit, i need to over come this.}I hope you enjoy it, and correct if any problems occurs.This game is for only 2 players, which will give you the realtime game feel with your opponent.First find the code, and then instructions.C Program-=-=-=-=-=-#include<stdio.h>#include<ncurses.h>#include "/opt/PostgreSQL/9.0/include/libpq-fe.h"#include<stdlib.h>#include<string.h>char symbol[3];PGconn * PGconnect(char ch){PGconn *conn;PGresult *res;FILE *fp;int cnt,i=0;char conn_string[500],hostaddr[32],port[7],dbname[50],user[50],password[50],name[10];const char *paramValues[2];fp=fopen("/tmp/.cred","r");if(ch=='y'){fscanf(fp,"%[^:]s",hostaddr);fscanf(fp,":%[^:]s",port);fscanf(fp,":%[^:]s",dbname);fscanf(fp,":%[^:]s",user);fscanf(fp,":%s",password);sprintf(conn_string,"hostaddr=%s port=%s dbname=%s user=%s password=%s",hostaddr,port,dbname,user,password);}else{fscanf(fp,"%[^\n]s",conn_string);conn_string[0]='\0';fscanf(fp,"\n%[^:]s",hostaddr);fscanf(fp,":%[^:]s",port);fscanf(fp,":%[^:]s",dbname);fscanf(fp,":%[^:]s",user);fscanf(fp,":%s",password);sprintf(conn_string,"hostaddr=%s port=%s dbname=%s user=%s password=%s",hostaddr,port,dbname,user,password);}conn = PQconnectdb(conn_string);if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr,"Not able to connect to the database %s",PQerrorMessage(conn)); return NULL; }if(ch=='y'){res=PQexec(conn,"BEGIN");PQclear(res);res=PQexec(conn,"DECLARE find_taken CURSOR FOR select count(*) from public.pggame_control where taken=true");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(conn)); PQclear(res); }PQclear(res);res = PQexec(conn, "FETCH ALL in find_taken");cnt=atoi(PQgetvalue(res,0,0));PQclear(res);res=PQexec(conn,"END");PQclear(res);if(cnt==0){res=PQexec(conn,"TRUNCATE public.pggame_control");PQclear(res);res=PQexec(conn,"INSERT INTO public.pggame_control values(false,false)");PQclear(res);}printf("Enter Your Name -> ");scanf("%s",name);paramValues[0]=name;printf("Enter Your Symbol -> ");scanf("%s",symbol);paramValues[1]=symbol;res=PQexec(conn,"TRUNCATE public.pggame_status");PQclear(res);res=PQexecParams(conn,"INSERT INTO public.pggame_status(name,symbol,comments,status) values($1,$2,'',false)",2,NULL,paramValues,NULL,NULL,1); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "INSERT failed: %s", PQerrorMessage(conn)); PQclear(res); } PQclear(res);}res=PQexec(conn,"TRUNCATE public.pggame_spots");PQclear(res);res=PQexec(conn,"TRUNCATE public.pggame_positions");PQclear(res);res=PQexec(conn,"INSERT INTO public.pggame_positions values(0,0)");PQclear(res);fclose(fp);return conn;}int main(){int y=1,x=1,i=1,one=0;char ch,x1[10],y1[10],cnt,remote_spot_check[200];PGconn *your_conn,*other_conn;PGresult *res;FILE *log;const char *paramValues[2];log=fopen("/tmp/pggame.log","w");if(!((your_conn=PGconnect('y'))&&(other_conn=PGconnect('o'))))return 1;initscr();keypad(stdscr,TRUE);noecho();x=1;y=1;while(x 10<140){x=x 2;mvprintw(0,x 9,"-=");mvprintw(36,x 9,"-=");y=1;}y=0;while(y<35){ y;mvprintw(y,12,".");mvprintw(y,141,".");}y=2;x=15;refresh();while(1){res=PQexec(your_conn,"BEGIN");PQclear(res);res=PQexec(your_conn,"DECLARE find_taken CURSOR FOR select count(*) from public.pggame_control where taken=true");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(your_conn, "FETCH ALL in find_taken");cnt=atoi(PQgetvalue(res,0,0));PQclear(res);res=PQexec(your_conn,"END");PQclear(res);if(cnt>=1){res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE find_spots CURSOR FOR select count(*) from public.pggame_spots");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in find_spots");cnt=atoi(PQgetvalue(res,0,0));PQclear(res);res=PQexec(other_conn,"END");PQclear(res);if(cnt>=1){res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE get_remote_pos CURSOR FOR select * from public.pggame_spots where ctid=(select max(ctid) from public.pggame_spots)");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in get_remote_pos");y=atoi(PQgetvalue(res,0,0));x=atoi(PQgetvalue(res,0,1));PQclear(res);res=PQexec(other_conn,"END");PQclear(res);res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE get_symbol CURSOR FOR select symbol from public.pggame_status");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in get_symbol");mvprintw(y 1,x 15,"%s",PQgetvalue(res,0,0));PQclear(res);res=PQexec(other_conn,"END");PQclear(res);}res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE find_win CURSOR FOR select count(*) from public.pggame_status where status=true");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(other_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in find_win");cnt=atoi(PQgetvalue(res,0,0));PQclear(res);res=PQexec(other_conn,"END");if(cnt>=1){res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE find_win CURSOR FOR select name from public.pggame_status where status=true");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(other_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in find_win");mvprintw(15,40,"***** %s WIN The Game ***** .... Press Any Key To Exit ... ",PQgetvalue(res,0,0));refresh();PQclear(res);res=PQexec(other_conn,"END");getch();getch();endwin();PQfinish(your_conn);PQfinish(other_conn);return 0;}res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE find_table CURSOR FOR select count(*) from public.pggame_positions");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in find_table");cnt=atoi(PQgetvalue(res,0,0));PQclear(res);res=PQexec(other_conn,"END");PQclear(res);if(cnt==1){res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE find_table CURSOR FOR select * from public.pggame_positions");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in find_table");y=atoi(PQgetvalue(res,0,0));x=atoi(PQgetvalue(res,0,1));PQclear(res);res=PQexec(other_conn,"END");PQclear(res);mvprintw(37,125,"CONTROL ==>> ");mvprintw(y,x,"");}else{mvprintw(37,125,"CONTROL ==>> ");mvprintw(1,1,"");}refresh();usleep(10000);one=0;continue;}mvprintw(37,125,"CONTROL <<== ");mvprintw(y,x,"");refresh();if(cnt==0){res=PQexec(other_conn,"UPDATE public.pggame_control SET taken=true"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Update failed: %s", PQerrorMessage(other_conn)); PQclear(res); } PQclear(res);if(one==0){one=one 1;res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE find_spots CURSOR FOR select count(*) from public.pggame_spots");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in find_spots");cnt=atoi(PQgetvalue(res,0,0));PQclear(res);res=PQexec(other_conn,"END");PQclear(res);if(cnt>=1){res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE get_remote_pos CURSOR FOR select * from public.pggame_spots where ctid=(select max(ctid) from public.pggame_spots)");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in get_remote_pos");y=atoi(PQgetvalue(res,0,0));x=atoi(PQgetvalue(res,0,1));PQclear(res);res=PQexec(other_conn,"END");PQclear(res);res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE get_symbol CURSOR FOR select symbol from public.pggame_status");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in get_symbol");mvprintw(y 1,x 15,"%s",PQgetvalue(res,0,0));PQclear(res);res=PQexec(other_conn,"END");PQclear(res);res=PQexec(other_conn,"BEGIN");PQclear(res);res=PQexec(other_conn,"DECLARE find_table CURSOR FOR select * from public.pggame_positions");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in find_table");y=atoi(PQgetvalue(res,0,0));x=atoi(PQgetvalue(res,0,1));PQclear(res);res=PQexec(other_conn,"END");PQclear(res);mvprintw(y,x,"");refresh();}}res=PQexec(your_conn,"BEGIN");PQclear(res);res=PQexec(your_conn,"DECLARE find_spotted CURSOR FOR select count(*) from public.pggame_control where spot=true");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(your_conn, "FETCH ALL in find_spotted");cnt=atoi(PQgetvalue(res,0,0));PQclear(res);res=PQexec(your_conn,"END");PQclear(res);if(cnt>=1){res=PQexec(your_conn,"UPDATE public.pggame_control SET taken=true where taken=false"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Update failed: %s", PQerrorMessage(your_conn)); PQclear(res); } PQclear(res);res=PQexec(other_conn,"UPDATE public.pggame_control SET taken=false where taken=true"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Update failed: %s", PQerrorMessage(other_conn)); PQclear(res); } PQclear(res);res=PQexec(your_conn,"UPDATE public.pggame_control set spot=false where spot=true");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Update failed: %s", PQerrorMessage(your_conn)); PQclear(res); } PQclear(res);}}ch=getch();if(ch=='q'||ch=='Q')break;switch(ch){case 'a':case 'A':if(x==15)x=137;elsex=x-2;mvprintw(y,x,"");break;case 's':case 'S':if(y==34)y=1;elsey=y 1;mvprintw(y,x,"");break;case 'w':case 'W':if(y==1)y=34;elsey=y-1;mvprintw(y,x,"");break;case 'd':case 'D':if(x==137)x=15;elsex=x 2;mvprintw(y,x,"");break;case 'j':case 'J':res=PQexec(other_conn,"BEGIN");PQclear(res);sprintf(remote_spot_check,"DECLARE find_same_spot CURSOR FOR select count(*) from public.pggame_spots where y=%d and x=%d",y-1,x-15);res=PQexec(other_conn,remote_spot_check);if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(other_conn)); PQclear(res); }PQclear(res);res = PQexec(other_conn, "FETCH ALL in find_same_spot");cnt=atoi(PQgetvalue(res,0,0));PQclear(res);res=PQexec(other_conn,"END");if(cnt==0){echo();mvprintw(y,x,"%s",symbol);sprintf(y1,"%d",y-1);sprintf(x1,"%d",x-15);paramValues[0]=y1;paramValues[1]=x1;res=PQexecParams(your_conn,"INSERT INTO public.pggame_spots(y,x) values($1,$2)",2,NULL,paramValues,NULL,NULL,1); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(log, "INSERT failed: %s", PQerrorMessage(your_conn)); PQclear(res); } PQclear(res);noecho();refresh();}}sprintf(y1,"%d",y);sprintf(x1,"%d",x);paramValues[0]=y1;paramValues[1]=x1;res=PQexecParams(your_conn,"INSERT INTO public.pggame_positions(y,x) values($1,$2)",2,NULL,paramValues,NULL,NULL,1); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(log, "INSERT failed: %s", PQerrorMessage(your_conn)); PQclear(res); } PQclear(res);res=PQexec(your_conn,"BEGIN");PQclear(res);res=PQexec(your_conn,"DECLARE find_win CURSOR FOR select count(*) from public.pggame_status where status=true");if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Erorr %s", PQerrorMessage(your_conn)); PQclear(res); }PQclear(res);res = PQexec(your_conn, "FETCH ALL in find_win");cnt=atoi(PQgetvalue(res,0,0));PQclear(res);res=PQexec(your_conn,"END");if(cnt>=1){mvprintw(15,40,"***** CONGRATULATIONS !! ***** You WIN The Game .... Press Any Key To Exit ...");refresh();getch();getch();PQfinish(your_conn);PQfinish(other_conn);endwin();return 0;}}getch();PQfinish(your_conn);PQfinish(other_conn);endwin();return 0;} [Less]
|