?? game.c
字號:
{command=bioskey(0);
if(command==VK_ENTER) return ENTER;
else if(command==VK_DOWN)
{drawmat(jin24S,24,300,350,RED);
drawmat(ru24S,24,320,350,RED);
drawmat(tui24S,24,300,380,WHITE);
drawmat(chu24S,24,320,380,WHITE);
break;
}
}
while(1) /*當“退出”突出顯示時,用戶按“Enter"和向上方向鍵有效*/
{command=bioskey(0);
if(command==VK_ENTER) return QUITE;
else if(command==VK_UP) break;
}
}
}
void draw(int x,int y,int MorC)
/*根據MorC的值在第一、二維的坐標分別為x、y的位置下一子*/
{
int vx,vy;
int color;
chsman[x][y]=MorC;/*下子*/
if(MorC==P1) color=mancolor;
else color=computercolor;
switch(x)
{case 0:vy=170;break;
case 1:vy=240;break;
case 2:vy=310;
}
switch(y)
{case 0:vx=250;break;
case 1:vx=320;break;
case 2:vx=390;
}
setcolor(color);
circle(vx,vy,25);
}
/*根據鍵盤輸入的z的值確定下子的第一維(x)和第二維坐標*/
int convert(int *x,int *y,char z)
{
switch(z)
{case 0x31:*x=2;*y=0;break;
case 0x32:*x=2;*y=1;break;
case 0x33:*x=2;*y=2;break;
case 0x34:*x=1;*y=0;break;
case 0x35:*x=1;*y=1;break;
case 0x36:*x=1;*y=2;break;
case 0x37:*x=0;*y=0;break;
case 0x38:*x=0;*y=1;break;
case 0x39:*x=0;*y=2;
}
return 0;
}
int man(void) /*人按1-9鍵下棋,小鍵盤上的1-9鍵分別對應于棋盤上的九個格*/
{
int row,col,test=1;
char k=0x30;
while(test==1||chsman[row][col]!=0)
{
while(k<0x31||k>0x39)
k=bioskey(0);
convert(&row,&col,k);
test=0;
k=0x30;
}
draw(row,col,P1);
return 0;
}
void judge(void)
{
int row, col;
int i;
/*分別用三個數組存儲 棋盤上各行,各列,或者兩條線上的數組元素的值的和(以下稱之為權值)*/
int rskAndAtkLevlRow[3] = {0}, rskAndAtkLevlCol[3] = {0}, rskAndAtkLevlSlsh[2] = {0};
if(stepFlg == 0){
/*計算機下的第一子特別處理,優先下a[1][1]位置*/
stepFlg = 1;
if(chsman[1][1] == P1){
draw(0,0,P2);
return;
}
else{
draw(1,1,P2);
return;
}
}
stepFlg++;
/*計算各行,各列以及兩斜線的權值*/
for(row = 0; row < 3; row++){
for(col = 0; col < 3; col++){
rskAndAtkLevlRow[row] += chsman[row][col];
}
}
for(col = 0; col < 3; col++){
for(row = 0; row < 3; row++){
rskAndAtkLevlCol[col] += chsman[row][col];
}
}
rskAndAtkLevlSlsh[0] = chsman[0][0] + chsman[1][1] + chsman[2][2];
rskAndAtkLevlSlsh[1] = chsman[0][2] + chsman[1][1] + chsman[2][0];
/*若權值為-2(計算機下了兩了,另一位置為空),計算機攻擊*/
/*攻擊某一行*/
for(row = 0; row < 3; row++){
if(rskAndAtkLevlRow[row] == -2){
chkAndPutDwnRow(row)
}
}
/*攻擊某一列*/
for(col = 0; col< 3; col++){
if(rskAndAtkLevlCol[col] == -2){
chkAndPutDwnCol(col)
}
}
/*攻擊斜率為-1的斜線*/
if(rskAndAtkLevlSlsh[0] == -2){
for(row = 0, col = 0; row < 3; row++, col++){
if(chsman[row][col]==0)
{draw(row,col,P2);return;}
}
}
/*攻擊斜率為1的斜線*/
if(rskAndAtkLevlSlsh[1] == -2){
for(row = 0, col = 2; row < 3; row++, col--)
if(chsman[row][col]==0) {draw(row,col,P2);return;}
}
/*若權值為2(人下了兩子,另一位置為空),計算機防守*/
/*在某一行上防守*/
for(row = 0; row < 3; row++){
if(rskAndAtkLevlRow[row] == 2)
chkAndPutDwnRow(row)
}
/*在某一列上防守*/
for(col = 0; col< 3; col++){
if(rskAndAtkLevlCol[col] == 2)
chkAndPutDwnCol(col)
}
/*在斜率為-1的斜線上防守*/
if(rskAndAtkLevlSlsh[0] == 2){
for(row = 0, col = 0; row < 3; row++, col++)
if(chsman[row][col]==0) {draw(row,col,P2);return;}
}
/*在斜率為1的斜線上防守*/
if(rskAndAtkLevlSlsh[1] == 2){
for(row = 0, col = 2; row < 3; row++, col--)
if(chsman[row][col]==0) {draw(row,col,P2);return;}
}
/*若上述條件都不符,計算機在棋盤四角上下一子*/
for(row = 0; row < 3; row++){
for(col = 0; col < 3; col++){
if(chsman[row][col] == 0 && ((row == 0 && col == 0) || (row == 0 && col == 2) ||
(row == 2 && col == 0) || (row == 2 && col == 2)))
{draw(row,col,P2);return;}
}
}
/*若上述條件都不符,計算機在棋盤與數字鍵2、4、6、8的位置上下一子*/
for(row = 0; row < 3; row++){
for(col = 0; col < 3; col++){
if(chsman[row][col] == 0 && ((row == 0 && col == 1) || (row == 1 && col == 0) ||
(row == 1 && col == 2) || (row == 2 && col == 1)))
{draw(row,col,P2);return;}
}
}
}
void chessboard()
/*進入游戲后畫出棋盤*/
{int i,j;
cleardevice();
setcolor(BACKGROUND);
for(j=0;j<=getmaxy();j++)
line(0,j,getmaxx(),j); /*用循環語句畫背景防止屏幕閃爍*/
setcolor(RED);
line(215, 205, 425, 205);
line(215, 275, 425, 275);
line(285, 135, 285, 345);
line(355, 135, 355, 345);
drawmat(ji24S,24,145,110,computercolor); /*在棋盤邊寫"計算機"與"人",先下一方用黑色字,另一方白色字*/
drawmat(suan24S,24,165,110,computercolor);
drawmat(Ji24S,24,185,110,computercolor);
drawmat(ren24S,24,460,110,mancolor);
}
/*判斷游戲是否結束,若結束則輸出結果并返回1,否則返回零*/
int chkresult(void)
{int i;
for(i = 0; i < 3; i++)
if(chsman[i][0] + chsman[i][1] + chsman[i][2] == -3 || /*計算機贏*/
chsman[0][i] + chsman[1][i] + chsman[2][i] == -3 ||
chsman[0][0] + chsman[1][1] + chsman[2][2] == -3 ||
chsman[0][2] + chsman[1][1] + chsman[2][0] == -3)
{drawmat(ying24S,24,165,150,RED);
drawmat(shu24S,24,460,150,WHITE);
return 1;
}
for(i = 0; i < 3; i++)
if(chsman[i][0] + chsman[i][1] + chsman[i][2] == 3 || /*人贏*/
chsman[0][i] + chsman[1][i] + chsman[2][i] == 3 ||
chsman[0][0] + chsman[1][1] + chsman[2][2] == 3 ||
chsman[0][2] + chsman[1][1] + chsman[2][0] == 3)
{drawmat(ying24S,24,460,150,RED);
drawmat(shu24S,24,165,150,WHITE);
return 1;
}
if(stepFlg==5)/*計算機已下五子(計算機先下)或四子(人先下), 和棋*/
{drawmat(he24S,24,293,110,RED);
drawmat(qi24S,24,324,110,RED);
return 1;
}
return 0;
}
void compete() /*進入游戲,并對本函數對下棋的一些參數初始化*/
{int i,j;
int result;
stepFlg = 0;
for(i=0;i<3;i++) /*初始化棋盤狀態*/
for(j=0;j<3;j++)
chsman[i][j]=0;
randomize();
firstone=random(2); /*隨機讓人或計算機先下第一子*/
if(firstone==MAN) {mancolor=BLACK;computercolor=WHITE;} /*先下者用黑子*/
else {mancolor=WHITE;computercolor=BLACK;}
chessboard(); /*畫棋盤*/
while(1)
{
if(firstone==MAN) /*先后下子直至結束*/
{man();
if(chkresult()==1) {getch();return;}
judge();
if(chkresult()==1) {getch();return;}
}
else
{judge();
if(chkresult()==1) {getch();return;}
man();
if(chkresult()==1) {getch();return;}
}
}
}
int main(void)
{ int k=ENTER;
initgr(); /* BGI初始化 */
while(k==ENTER)
{k=pictureone();
if(k==QUITE) break;
else compete();
}
closegraph();
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -