1樓:匿名使用者
這是一個玩五子棋的例子:
#include
#include
#include
class five }
int getm()
int getn()
void getpointxy();
void getpointmn();
void machinemn();
void showqipan();
void print();
bool isxywin();
bool ismnwin();
int dangerlevel(int a,int b);
void attack();
}; void five::getpointxy()
else }
void five::getpointmn()
else }
int five::dangerlevel(int a, int b)
if(qipan[a +i][b] == 'x' && a + i <= 19)
break;
} level += (i-1);
for(i = 1 ; a-i>=0; i ++)
if(qipan[a-i][b] == 'x' && (a-i) >= 0)
break;
} level += (i-1);
for(i = 1 ; b+i<=19 ; i ++)
if(qipan[a][b + i] == 'x' && b + i <= 19)
break;
} level += (i-1);
for( i = 1 ; b-i>=0 ; i ++)
if(qipan[a][b - i] == 'x' && (b-i) >= 0)
break;
} for( i = 1 ; a+i<=19&& b+i<=19; i ++)
if(qipan[a + i][b + i] == 'x' && b+i <= 19 && a+i <= 19)
break;
} level += (i-1);
for( i = 1 ; a-i>= 0 && b-i >= 0; i ++)
if(qipan[a - i][b - i] == 'x' && a-i>= 0 && b-i >= 0)
break;
} level += (i-1);
for( i = 1 ; a-i>= 0 && b+i <= 19 ; i ++)
if(qipan[a - i][b + i] == 'x' && a-i>= 0 && b+i <= 19)
break;
} level += (i-1);
for( i = 1 ; a + i<= 19 && b-i >= 0 ; i ++)
if(qipan[a + i][b - i] == 'x' && a + i<= 19 && b-i >= 0)
break;
} level += (i-1);
return level;
} void five::attack()
if(qipan[a +i][b] == 'o' && a + i <= 19)
break;
} level += (i-1);
for(i = 1 ; a-i>=0; i ++)
if(qipan[a-i][b] == 'o' && (a-i) >= 0)
break;
} level += (i-1);
for(i = 1 ; b+i<=19 ; i ++)
if(qipan[a][b + i] == 'o' && b + i <= 19)
break;
} level += (i-1);
for( i = 1 ; b-i>=0 ; i ++)
if(qipan[a][b - i] == 'o' && (b-i) >= 0)
break;
} for( i = 1 ; a+i<=19&& b+i<=19; i ++)
if(qipan[a + i][b + i] == 'o' && b+i <= 19 && a+i <= 19)
break;
} level += (i-1);
for( i = 1 ; a-i>= 0 && b-i >= 0; i ++)
if(qipan[a - i][b - i] == 'o' && a-i>= 0 && b-i >= 0)
break;
} level += (i-1);
for( i = 1 ; a-i>= 0 && b+i <= 19 ; i ++)
if(qipan[a - i][b + i] == 'o' && a-i>= 0 && b+i <= 19)
break;
} level += (i-1);
for( i = 1 ; a + i<= 19 && b-i >= 0 ; i ++)
if(qipan[a + i][b - i] == 'o' && a + i<= 19 && b-i >= 0)
break;
} level += (i-1);
if(level >= max) }
else
continue;
} }qipan[n-1][m-1] = 'x';
} void five::machinemn()
if(qipan[y-1][i] == 'x')
break;
if(qipan[y-1][i] == 'o')
num1++;
} for(i = x-2; i >= 0 ; i --)
if(qipan[y-1][i] == 'x')
if(qipan[y-1][i] == 'o')
num1++;
} ////
for( i = y ; i < y+5 && y+5 < 20 ; i ++)
if(qipan[i][x-1] == 'x')
if(qipan[i][x-1] == 'o')
num2++;
} for(i = y-2; i >= 0 ; i --)
if(qipan[i][x-1] == 'x')
if(qipan[i][x-1] == 'o')
num2++;
} ////
for(i = 0 ; i < 5 && x+i<20 && y+i<20; i++ )
if(qipan[y+i][x+i] == 'x')
if(qipan[y+i][x+i] =='o')
num3++;
} for(i = 0 ; x - i -2>=0 && y-i-2>=0 && i < 5;i++ )
if(qipan[y-i-2][x-i-2] == 'x')
break;
if(qipan[y-i-2][x-i-2] == 'o')
num3++;
} ////
for(i = 0 ; x + i < 20 && y - i - 2 >= 0 && i < 5;i ++)
if(qipan[y-i-2][x+i] == 'x')
if(qipan[y-i-2][x+i] == 'o')
num4++;
} for(i = 0 ; y + i < 20 && x - i - 2 >= 0 && i < 5;i ++)
if(qipan[y+i][x-i-2] == 'x')
if(qipan[y+i][x-i-2] == 'o')
num4++;
} ////
if(num_xy == 0 && num_xy == 0)
else if(num_xy == 1)
attack();
else if(num_xy == 1)
else if(num1 == 4 && a11 == 21 && a12 != 21) //一排四個右邊一頭堵住
else if(num1 == 4 && a12 == 21 && a11 != 21) //一排四個左邊邊一頭堵住
else if(num2 == 4 && a21 == 21 && a22 != 21) //一列四個下邊一頭堵住
else if(num2 == 4 && a22 == 21 && a21 != 21) //一列四個上邊一頭堵住
else if(num3 == 4 && a31 == 21 && a32 != 21) //一左上右下斜列四個右邊一頭堵住
else if(num3 == 4 && a32 == 21 && a31 != 21) //一左上右下斜列四個左邊一頭堵住
else if(num4 == 4 && a41 == 21 && a42 != 21) //一左下右上斜列四個右邊一頭堵住
else if(num4 == 4 && a42 == 21 && a41 != 21) //一左下右上斜列四個左邊一頭堵住
else if(num1==3 && a11 != 21 && a12 != 21) //一排3 個兩頭沒堵
else if(num2==3&& a21 != 21 && a22 != 21) //一列 3 個兩頭沒堵
else if(num3 == 3&& a31 != 21 && a32 != 21) //一左上右下斜列3 個兩頭沒堵
else if(num4==3 && a41 != 21 && a42 != 21) //一左下右上斜列3 個兩頭沒堵
else if(num1 == 2&& a11 != 21 && a12 != 21) //一排2個兩頭沒堵
else if( num2 == 2&& a21 != 21 && a22 != 21) //一列2個兩頭沒堵
else if(num3 == 2&& a31 != 21 && a32 != 21) //一左上右下斜列2 個兩頭沒堵
else if(num4==2 && a41 != 21 && a42 != 21) //一左下右上斜列2個兩頭沒堵
else
attack();
} void five::showqipan()
else
cout << p ;
p++;
} cout << endl;
for(i = 0 ; i < 20 ; i++)
for(int j = 0 ; j < 20 ; j ++)
cout << qipan[i][j]<<' ' ;
cout << endl;
} }void five::print()
else
cout << p ;
p++;
} cout << endl;
for(i = 0 ; i < 20 ; i++)
for(int j = 0 ; j < 20 ; j ++)
cout << endl;
} }bool five::isxywin()
for(i = x-2; i >= 0 ; i --)
for( i = y ; i < y+5 ; i ++)
for(i = y-2; i >= 0 ; i --)
for(i = 0 ; i < 5 && x+i<20 && y+i<20; i++ )
for(i = 0 ; x - i -2>=0 && y-i-2>=0 && i < 5;i++ )
for(i = 0 ; x + i < 20 && y - i - 2 >= 0 && i < 5;i ++)
for(i = 0 ; y + i < 20 && x - i - 2 >= 0 && i < 5;i ++)
if(num1>=5||num2>=5||num3>=5||num4>=5)
return 1;
else
return 0;
} bool five::ismnwin()
for(i = m-2; i >= 0 ; i --)
for( i = n ; i < n+5 ; i ++)
for(i = n-2; i >= 0 ; i --)
for(i = 0 ; i < 5 && m+i<20 && n+i<20; i++ )
for(i = 0 ; m - i -2>=0 && n-i-2>=0 && i < 5;i++ )
for(i = 0 ; m + i < 20 && n - i - 2 >= 0 && i < 5;i ++)
for(i = 0 ; n + i < 20 && m - i - 2 >= 0 && i < 5;i ++)
if(num1>=5||num2>=5||num3>=5||num4>=5)
return 1;
else
return 0;
} void main()
a.getpointmn();
a.print();
if(a.ismnwin()) }
} if(temp == 2)
a.machinemn();
a.print();
cout << "電腦走(" << a.getm() << ',' << a.getn() << ')' << endl;
if(a.ismnwin()) }
} if(temp1 == 2)
a.getpointxy();
a.print();
if(a.isxywin()) }
} }cout << "press any key to exit\n";
getch();}
求c語言程式設計大佬,求一個c語言程式設計大佬
聽不清啊 include int k int sumoffactor int n int i,s 1 for i 2 i iif n i 0 s i n i if i i n s i return s int friendnum int n k sumoffactor n int main int ...
c語言程式設計求輸入,c語言程式設計 求輸入一個0 99的整數,判斷其是否為同構數,要求寫了程式還要有中文解釋其核心語句的作
思路 同構數是會出現在它的平方的右邊的數。具體見 include int main return 0 執行結果 15 62576 include stdio.h int main int argc,char argv 執行結果 include stdio.h int main int n,i,j,a...
求一大一c語言程式設計,大一C語言程式設計 速求
37 yes 56 no 84 no 97 yes 69 no 57 no 81 no 29 yes 35 no 73 yes press any key to continue include include int isprimer int n void lozenge int n,char c...