بازی رو اصلاح کردم, یک نفرش کردم, بازیکن کامپیوتر اضافه کردم, سعی کردم تا جایی که بشه کسی نتونه بازیکن منو شکست بده , فکر کردین کم الکیه بازیکنم؟؟ می دونم کد ممکنه باگ داشته باشه , فعلا درست حسابی تستش نکردم.
فردا در مورد تغییرات و مشکلات کد اصلی تا جایی که لازم بود برای تغییرات بدونم توضیح میدم.
کد:#include<stdio.h> #include<conio.h> #include <windows.h> #include<cstdlib> using std::rand; using std::srand; #include<ctime> using std::time; void clrscr() { HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord = {0, 0}; DWORD count; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hStdOut, &csbi); FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count); SetConsoleCursorPosition(hStdOut, coord); } void gotoxy ( short x, short y ) { COORD coord = {x, y}; SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord ); } void kadr(); void randomO(); int fPP(int); void show(); void get(); void help(); int rtn(char); int won_(int); const int i1=30,i2=i1+3,i3=i2+3,j1=10,j2=j1+2,j3=j2+2; char xo[3][3],f; void main() { int i,j=0,won=0; clrscr(); for(;j<3;j++) for(i=0;i<3;xo[i][j]=' ',i++); kadr(); do{ gotoxy(26,5); printf("please type x:"); f='X'; }while( (f!='O')&&(f!='X')); printf("%c",f); int PP=1; for (int i=0;i<10,!(won=won_(PP));i++){ if(PP==1) get(); if (i==0) { if(xo[2][2]==' ') xo[2][2]='O'; else xo[1][1]='O'; PP=fPP(PP); } PP=fPP(PP); show(); } gotoxy(30,20); switch(won){ case 1:printf("X is won !!! \n"); break; case 2:printf("O is won !!! \n"); break; case 3:printf("Eqaul \n"); break; } getch(); }// END void kadr(){ int i,j; for(j=j1+1;j<=j3;j+=2) for(i=i1-1;i<=i3+1;gotoxy(i,j),printf("~"),i++); for(i=i1+1;i<=i3;i+=4) for(j=j1;j<=j3;gotoxy(i,j),printf("!"),j++); }//end of kadr void show(){ int i,j,x=0,y=0; clrscr(); kadr(); for(j=j1;j<=j3;j+=2,y++) for(i=i1,x=0;i<=i3;i+=3,x++) { gotoxy(i,j); printf("%c",xo[x][y]); } }//end of show int won_(int cpuID){ bool type=false; char x[3]; up : show(); int cnt=0; for(int i=0;i<3;i++) { cnt=0; if(xo[0][i]==xo[1][i]) if(xo[0][i]!=' ') cnt++; if(xo[1][i]==xo[2][i]) if(xo[1][i]!=' ') cnt++; if(xo[0][i]==xo[2][i]) if(xo[0][i]!=' ') cnt++; if (cnt==3 ) return rtn(xo[0][i]); if ((cnt==1) && (cpuID==2)) { for (int cpuW=0;cpuW<3;cpuW++) if (xo[cpuW][i]==' ') { xo[cpuW][i]='O'; cpuID=1; type=true; goto up; } } cnt=0; if(xo[i][0]==xo[i][1]) if(xo[i][0]!=' ') cnt++; if(xo[i][1]==xo[i][2]) if(xo[i][1]!=' ') cnt++; if(xo[i][0]==xo[i][2]) if(xo[i][0]!=' ') cnt++; if (cnt==3 ) return rtn(xo[i][0]); if ((cnt==1) && (cpuID==2)) { for (int cpuW=0;cpuW<3;cpuW++) if (xo[i][cpuW]==' ') { xo[i][cpuW]='O'; cpuID=1; type=true; goto up; } } x[i]=xo[i][i]; cnt=0; } cnt=0; if(x[0]==x[1]) if(x[1]!=' ') cnt++; if(x[1]==x[2]) if(x[1]!=' ') cnt++; if(x[0]==x[2]) if(x[0]!=' ') cnt++; if (cnt==3 ) return rtn(x[0]); if ((cnt==1) && (cpuID==2)) { for (int cpuW=0;cpuW<3;cpuW++) if (xo[cpuW][cpuW]==' ') { xo[cpuW][cpuW]='O'; cpuID=1; type=true; goto up; } } for(int j=2,i=0;i<3;j--,i++) x[i]=xo[i][j]; if(x[0]==x[1]) if(x[1]!=' ') cnt++; if(x[1]==x[2]) if(x[1]!=' ') cnt++; if(x[0]==x[2]) if(x[0]!=' ') cnt++; if (cnt==3 ) return rtn(x[0]); if ((cnt==1) && (cpuID==2)) { for(int j=2,i=0;i<3;j--,i++) if (xo[i][j]==' ') { xo[i][j]='O'; cpuID=1; type=true; goto up; } } if ((cpuID==2) && !(type)) randomO(); for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(xo[i][j]==' ') return 0; return 3; }//end of won_ void get(){ char c='k'; while(c=='k'){ show(); gotoxy(2,7); printf("your keys:"); gotoxy(8,3); printf("Welcom to Mahdi Bayat Game"); gotoxy(10,9); printf("\n 7!8!9 \n --!-!--\n 4!5!6\n --!-!--\n 1!2!3 "); gotoxy(12,7); printf(" player : %c",f); if((c=getche())=='k')c='k'; switch(c) { case 'k':help(); break; case '1':if(xo[0][2]==' ') xo[0][2]=f; else continue; break; case '2':if(xo[1][2]==' ') xo[1][2]=f; else continue; break; case '3':if(xo[2][2]==' ') xo[2][2]=f; else continue; break; case '4':if(xo[0][1]==' ') xo[0][1]=f; else continue; break; case '5':if(xo[1][1]==' ') xo[1][1]=f; else continue; break; case '6':if(xo[2][1]==' ') xo[2][1]=f; else continue; break; case '7':if(xo[0][0]==' ') xo[0][0]=f; else continue; break; case '8':if(xo[1][0]==' ') xo[1][0]=f; else continue; break; case '9':if(xo[2][0]==' ') xo[2][0]=f; else continue; break; } } } void help(){ int i,j,x=0; clrscr(); kadr(); for(j=j1;j<=j3;j+=2) for(i=i1;i<=i3;i+=3) { gotoxy(i,j); printf("%d",++x); } getch(); } int rtn(char x) { if(x=='X')return 1; else return 2; } int fPP(int PP) { if (PP==1) return 2; else return 1; } void randomO() { int x,y; str: srand(time(0)); x=rand()%4; y=rand()%4; if(xo[x][y]==' ') xo[x][y]='O'; else goto str; }








پاسخ با نقل قول
Bookmarks