Add to Favorites    Make Home Page 5360 Online  
 Language Categories  
 Our Services  

Home » C Home » Games and Graphics Home » 14Square : A Othello like logical board game

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title 14Square : A Othello like logical board game
Author B.Meiyalagan
Author Email softalagan [at] yahoo.com,softalagan [at] hotmail.com
Description It's a logical board game with two modes. Human Vs Human and Computer Vs Human(I've lost a lot of times with my code :-)). I hope this will help in improving both Graphics and logical skills.

Category C » Games and Graphics
Hits 367367
Code Select and Copy the Code
/* 14Square */ #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <string.h> #include <dos.h> # include "14s_sounds.h" char pl[2][12],hlp_str1[50],hlp_str2[75]; short int s_mat[14][14]; short int cir_fg=0,ro=7,co=7; short int check(short int ro,short int co,char *cir,short int ch_fg); short int is_empty(short int ro,short int co,short int m[14][14]); short int machine_move(char *,char *); short int menu(char *wh,char *bk); short int possible_moves(); short int confirmed(); short int count(); void about(); void err_snd(); void mnu_snd(); void move_snd(); void open_scr(); void start(short int mc); void put_pointers(char *); void clear_scr(short int my); void enter(char *wh,char *bk); void help_bar(short int mx,short int my); void put_cir(short int ro,short int co,char *); void board(short int mx,short my,char *wh,char *bk); void hlp_screen(short int mx,short int my,char *wh,char *bk); void rect(short int pro,short int pco,short int ro,short int co); void main(void) { short int gdriver = DETECT,gmode,key=0,mc,mx,my; short int register i,j; union REGS in,out; char *wh,*bk,*ptr; /* Tic mark (Pointer)*/ initgraph(&(int)gdriver, &(int)gmode, ""); setlinestyle(0,0,3);setcolor(14); line(15,0,15,25);line(15,0,10,8);line(15,0,20,8); ptr=malloc(imagesize(0,0,25,25)); getimage(0,0,25,25,ptr); cleardevice(); /* Drawing circles and saving them in memory */ setlinestyle(0,0,2); setfillstyle(1,2);bar3d(4,4,21,21,0,0); setfillstyle(SOLID_FILL,WHITE); setcolor(WHITE);circle(12,12,5);floodfill(12,12,WHITE); wh=malloc(imagesize(5,5,20,20));getimage(5,5,20,20,wh); cleardevice(); setfillstyle(1,2);bar3d(4,4,21,21,0,0); setfillstyle(SOLID_FILL,BLACK); setcolor(BLACK);circle(12,12,5);floodfill(12,12,BLACK); bk=malloc(imagesize(5,5,20,20));getimage(5,5,20,20,bk); cleardevice(); mx=getmaxx();my=getmaxy(); open_scr(); mnu: mc=menu(wh,bk); start(mc); initgraph(&(int)gdriver, &(int)gmode, ""); /* Initialization */ for(i=0;i<=13;i++) for(j=0;j<=13;j++) s_mat[i][j]=-1; /* -1 -> not yet be captured */ s_mat[6][6]=1;s_mat[6][7]=0;s_mat[7][6]=0;s_mat[7][7]=1; board(mx,my,wh,bk); help_bar(mx,my); enter(wh,bk); putimage(55,265,ptr,XOR_PUT); count(); /* Counts and prints the points & checks for the completion*/ setcolor(15);rectangle(153+co*25,53+ro*25,153+co*25+20,53+ro*25+19); //Cursor while(1) { key=0; in.h.ah=0; int86(22,&in,&out); key=out.h.ah; switch(key) { case 72 : if(ro!=0){ro--;rect(ro+1,co,ro,co);} else err_snd(); break; /* up */ case 75 : if(co!=0){co--;rect(ro,co+1,ro,co);} else err_snd(); break; /* Left */ case 80 : if(ro<13){ro++;rect(ro-1,co,ro,co);} else err_snd(); break; /* Down */ case 77 : if(co!=13){co++;rect(ro,co-1,ro,co);} else err_snd(); break; /* Right */ case 28 : if ((is_empty(ro,co,s_mat)) &&(check(ro,co,wh,0)!=0)) { if(cir_fg%2==0){ s_mat[ro][co]=1; put_cir(ro,co,wh); check(ro,co,wh,1); } else{ s_mat[ro][co]=0; put_cir(ro,co,bk); check(ro,co,bk,1); } if(count()==1) goto mnu; cir_fg++; put_pointers(ptr); } else err_snd(); if(possible_moves()==0) { register short int lv=1; char t1[50]="There is no possible move for ", t2[50]="Chance goes to "; settextstyle(12,0,3); cir_fg++;/*Checks whether other pler have possible move*/ if(possible_moves()==0) { /* If it's so, complete the game */ outtextxy(205,420,"Game Drawn"); delay(750); goto mnu; } else { /* Other wise bring the old value to cir_fg */ cir_fg--; if(cir_fg%2==0) { strcat(t1,pl[0]);strcat(t2,pl[1]);} else { strcat(t1,pl[1]);strcat(t2,pl[0]);} while(lv<=10) { setcolor(14); outtextxy(175,410,t1);outtextxy(200,420,t2); delay(300);setcolor(0); outtextxy(175,410,t1);outtextxy(200,420,t2); delay(300); lv++; } //While cir_fg++; put_pointers(ptr); }//else of inner if }//If if ((mc==1)&&(cir_fg%2!=0)) { if(machine_move(bk,ptr)==1) /* Returns 1 when board filled */ goto mnu; } break;/* Enter */ case 1 : if(confirmed()) { clear_scr(my); closegraph();restorecrtmode(); exit(0); } else break; break; case 48 : if(confirmed()) goto mnu ; /* back to main menu */ else break; case 59 : hlp_screen(mx,my,wh,bk); board(mx,my,wh,bk); enter(wh,bk); help_bar(mx,my); if(cir_fg%2==0) putimage(55,265,ptr,COPY_PUT); else putimage(555,265,ptr,COPY_PUT); count(); break; /* Help */ case 31 : s_fg =!(s_fg);break; /* s for sound */ default : err_snd(); } } /* While */ } /* Main */ void board(short int mx,short my,char *wh,char *bk) { register short int i; cleardevice(); setcolor(1); settextstyle(7,0,3);outtextxy(265,5,"14SQUARE"); line(265,35,380,35);line(265,38,380,38); setcolor(14);setlinestyle(0,0,2); rectangle(1,1,mx-1,my-1);setcolor(1); rectangle(3,3,mx-3,my-3);// Borders for(i=1;i<=1600;i++) {putpixel(random(639),random(479),random(15)); } //Bk setcolor(10);setlinestyle(0,0,2);setfillstyle(1,2); bar3d(150,50,500,400,2,2);//Board for(i=1;i<=14;i++) { line(150,50+i*25,500,50+i*25); line(150+i*25,50,150+i*25,400); } putimage(53,150,wh,COPY_PUT); putimage(72,150,wh,COPY_PUT); putimage(553,150,bk,COPY_PUT); putimage(572,150,bk,COPY_PUT); settextstyle(1,0,2);setcolor(14); outtextxy(20,175,pl[0]); outtextxy(510,175,pl[1]); } void enter(char *wh,char *bk) { short int register i,j; for(i=0;i<=13;i++) for(j=0;j<=13;j++) { if(s_mat[i][j]==1) put_cir(i,j,wh); if(s_mat[i][j]==0) put_cir(i,j,bk); } } void help_bar(short int mx,short my) { setfillstyle(1,2);setcolor(2); bar(5,my-35,mx-6,my-5); sprintf(hlp_str1,"%c%c%c%c",24,25,26,27); strcat(hlp_str1," Keys : Moves Pointer Enter : Capture"); strcpy(hlp_str2,"S : Sound ON/OFF B : Back to Menu F1 : Help Esc : Quit"); settextstyle(11,0,2);setcolor(1); outtextxy(145,my-28,hlp_str1); outtextxy(50,my-15,hlp_str2); } short int machine_move(char *bk,char *ptr) { short int i,j,ts=0,dum; char *temp; struct { short int r,c,pts; }ta[195],best; for(i=0;i<14;i++) for(j=0;j<14;j++) { if(s_mat[i][j]==-1) { ta[ts].pts=check(i,j,temp,0);/* bk or wh ie we r not going to print */ ta[ts].r=i;ta[ts].c=j; ts++; } }/* Stores all the empty ro,co with its possible pts*/ best=ta[0]; for(i=1;i<ts;i++) if(ta[i].pts>best.pts) best=ta[i]; /* Finds the ro,co with highest pts */ delay(750); if(best.r>ro) for(i=ro;i<=best.r;i++) { if(i==ro) rect(ro,co,i,best.c); else { ro=i;co=best.c; rect(ro-1,co,ro,co); delay(250);} } if(best.r<ro) for(i=ro;i>=best.r;i--) { if(i==ro) rect(ro,co,i,best.c); else { ro=i;co=best.c; rect(ro+1,co,ro,co); delay(250);} } if(best.c>co) for(i=co;i<=best.c;i++) { if(i==co) rect(ro,co,best.r,i); else { ro=best.r;co=i; rect(ro,co-1,ro,co); delay(250);} } if(best.c<co) for(i=co;i>=best.c;i--) { if(i==co) rect(ro,co,best.r,i); else { ro=best.r;co=best.c; rect(ro,co+1,ro,co); delay(250);} } ro=best.r;co=best.c; delay(550); s_mat[ro][co]=0; put_cir(ro,co,bk); /* No need to check cir_fg because computer always going to have Black marble only*/ check(ro,co,bk,1); dum=count();/*count()->Counts and prints points and checks for completion of the game */ cir_fg++; putimage(55,265,ptr,XOR_PUT); putimage(555,265,ptr,XOR_PUT); return(dum); } void rect(short int pro,short int pco,short int ro,short int co) { short int tx,ty,i,j; setcolor(2); tx=153+pco*25,ty=53+pro*25; rectangle(tx,ty,tx+20,ty+19); for(i=0;i<14;i++) for(j=0;j<14;j++){ tx=153+i*25,ty=53+j*25; rectangle(tx,ty,tx+20,ty+19);} setcolor(15); tx=153+co*25,ty=53+ro*25; rectangle(tx,ty,tx+20,ty+19); if(s_mat[ro][co]==-1) move_snd1(); else move_snd2(); } short int is_empty(short int ro,short int co,short int m[14][14]) { if(m[ro][co]==-1) return(1); else return(0); } void put_cir(short int ro,short int co,char *cir) { putimage(155+co*25,55+ro*25,cir,COPY_PUT); cir_snd(); } void put_pointers(char *ptr) { if(cir_fg%2) { putimage(55,265,ptr,XOR_PUT); putimage(555,265,ptr,XOR_PUT); } else { putimage(55,265,ptr,XOR_PUT); putimage(555,265,ptr,XOR_PUT); } } int is_filled() { short int i,j,fg=0; for(i=0;i<14;i++) for(j=0;j<14;j++) if(s_mat[i][j]==-1) { fg++; break; } return((fg)?0:1); } short int possible_moves() { short int i,j,fg=0; char *t; for(i=0;i<14;i++) for(j=0;j<14;j++) if(check(i,j,t,0)!=0) {fg++;break;} /* a position found for possible moving */ if(fg==0) return(0); else return(1); /* Non zero value */ } short int confirmed() { char t_k; while(1) { if(kbhit()) { t_k=getch(); if( (t_k=='y') ||(t_k=='Y')) return(1); else return(0); } setcolor(14); outtextxy(220,420,"Are You Sure (Y/N) :"); delay(275); setcolor(0); outtextxy(220,420,"Are You Sure (Y/N) :"); delay(275); } } short int menu(char *wh,char *bk) { union REGS in,out; int gd=DETECT,gm,i,mn=0,key; char mnu[5][25]={ "Human Vs Human", "Computer Vs Human", " About 14Square", " Help", " Quit " }; initgraph(&gd,&gm,""); cleardevice(); setcolor(14); strt:mn=0; cleardevice(); for(i=1;i<=1500;i++) {putpixel(random(639),random(479),random(15)); } setcolor(14);setfillstyle(1,0); settextstyle(7,0,4);outtextxy(243,25,"14SQUARE"); settextstyle(7,0,2); for(i=0;i<5;i++) { if(i>0){ setfillstyle(1,1);setcolor(15);} bar3d(165,155+i*50,465,(155+i*50)+25,0,0); outtextxy(205,155+i*50,mnu[i]); } while(1) { in.h.ah=0; int86(22,&in,&out); key=out.h.ah; // Gets the scan code switch(key) { case 72 : mnu_snd(); mn--;if(mn==-1) mn=4; break;//up case 80 : mnu_snd(); mn++;if(mn==5) mn=0; break;//Down case 28 : case 13:if(mn==2) { about();goto strt;} if(mn==3) {hlp_screen(639,479,wh,bk);goto strt;} closegraph(); restorecrtmode(); if(mn==4) { clear_scr(479); closegraph(); restorecrtmode(); exit(0); } else return(mn); break; //Enter } for(i=0;i<5;i++) { setfillstyle(1,1); setcolor(15); if(mn==i) { setcolor(14);setfillstyle(1,0); } bar3d(165,155+i*50,465,(155+i*50)+25,0,0); outtextxy(205,155+i*50,mnu[i]); } } } void start(short int mc) { short int t_nme,k; char h_t[10]={'H','T','H','T','H','T','H','T','H','T'}, t[2][12]={ " "," "}; char ch,toss,*tmp=""; clrscr(); gotoxy(33,2);printf(" 14SQUARE "); gotoxy(33,3);printf(" ~~~~~~~~ "); gotoxy(28,4);printf(" COMPUTERISED TOSSING"); gotoxy(28,5);printf(" ~~~~~~~~~~~~ ~~~~~~~"); gotoxy(18,7);printf("(ENTER THE NAMES WITHIN 10 CHARACTERS)"); strcpy(pl[0],"Computer"); if(mc==0){ gotoxy(25,9); printf("Player - 1 : "); gotoxy(43,9); gets(pl[0]); pl[0][10]='

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi ¦Õ¤ ?¤Ð Ãß??ÝÐÝ
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=935


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review