當前位置:首頁 » 編程語言 » c語言雙人輪流下棋怎麼寫
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言雙人輪流下棋怎麼寫

發布時間: 2022-10-01 05:08:48

① 使用c語言及VC繪圖庫,在VC環境下,設計一個雙人五子棋對戰單機游戲。

學習不是這樣的。以前做題不管怎麼樣,題會不會,先寫個「解」字再說。你連這一步都沒做到。最起碼,你會建立工程吧,建立一個工程,先做個對話框吧,做個對話框再在上面畫棋盤吧……
這就是工程的分解。你要問的應該是如何用VC做圖,如何響應滑鼠消息,如何記錄下棋的步驟等。一拿到東西就先說不會,問有沒有人會,這樣你永遠是個學生;而且是個成績不好的學生。

② 急需用c語言寫中國象棋的代碼,只要紅色方布局和走棋

#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #include<windows.h>
int x,y,i,j,k,p,q, num = 1, round; //象棋游戲的全局變數
int place_x1 = 0,place_y1 = 0,place_x2 = 0,place_y2 = 0;
int check_x,check_y,check_turn; //基本參數
char ch, turn = 'O',turn1 = 'N',temp,temp1;
char check_1[9][3] ={"車","馬","象","士","將","炮","兵","+-"}; //取棋子時只判斷前8合法
char check_2[9][3] ={"車","馬","相","仕","帥","炮","卒","+-"}; //下棋時多一空位合法
char check[3];
void check_main1(char* temp,char* temp1,char* turn,char* turn1,int *num,int *if_return,char map[100][100])
{ //(象棋函數 判斷 將方 下棋是否合法
check[0] = *temp; check[1] = *temp1; check[2] = '\0'; char a,b;
for ( i = 0; i < 8; i++)
{ if ( strcmp(check_2[i],check) == 0)
{ *temp = *turn; *temp1 = *turn1; *turn = 'O'; *turn1 = 'N';
if( i < 7){ printf(" 帥方的%s被吃\n",check_2[i]); Sleep(500); } *num = *num + 1;
for( k = 4; k <= 8; k = k + 2) //判斷 帥 是否死亡
{ for(j = 15; j <= 23; j= j+ 4)
{ if (map[k][j] == check_2[4][0] && map[k][j+1] == check_2[4][1])
{ place_x2 = k; place_y2 = j; break; } }
if( j <= 23) break;
}
if( k == 10)
{printf(" 帥 被將死 將方獲得勝利\n"); printf("按任意鍵返回菜單");
getch( ); *if_return = 1; return;
}
for( k = 18; k <= 22; k = k + 2) //判斷 將 是否死亡
{for(j = 15; j <= 23; j= j+ 4)
{if(map[k][j] == check_1[4][0] && map[k][j+1] == check_1[4][1])
{place_x1 = k; place_y1 = j; break; } }
if( j <= 23) break;
}
if ( k == 24)
{printf(" 將 被將死 帥方獲得勝利\n"); printf("按任意鍵返回菜單");
getch( ); *if_return = 1; return;
}
if ( place_y1 == place_y2)
{for( k = place_x2 + 2; k <= place_x1 - 2; k = k +2) {if(map[k][place_y1] != '+') break;}
if( k == place_x1)
{if(round == 1) printf(" 將方對將 帥方勝利");
else if( round == 2) printf(" 帥方對將 將方勝利");
printf("按任意鍵返回菜單"); getch( ); *if_return = 1; return;
}
}
break;
}
} // for ( i = 0; i < 8; i++)循環結束
if( i == 8) {printf("不合法的走法\n"); Sleep(500); }
}
void check_main2(char* temp,char* temp1,char* turn,char* turn1,int *num,int *if_return,char map[100][100])
{ //象棋函數 判斷 帥方 下棋是否合法
check[0] = *temp; check[1] = *temp1; check[2] = '\0'; char a,b;
for ( i = 0; i < 8; i++)
{if ( strcmp(check_1[i],check) == 0)
{ *temp = *turn; *temp1 = *turn1; *turn = 'O'; *turn1 = 'N';
if( i < 7) {printf(" 將方的%s被吃",check_1[i]); Sleep(500); } *num = *num + 1;
for( k = 4; k <= 8; k = k + 2) //判斷 帥 是否死亡
{for(j = 15; j <= 23; j= j+ 4)
{if(map[k][j] == check_2[4][0] && map[k][j+1] == check_2[4][1])
{place_x2 = k; place_y2 = j; break; } }
if( j <= 23) break;
}
if( k == 10)
{printf(" 帥 被將死 將方獲得勝利\n");printf("按任意鍵返回菜單"); getch( );
*if_return = 1; return;
}
for( k = 18; k <= 22; k = k + 2) //判斷 將 是否死亡
{for(j = 15; j <= 23; j= j+ 4)
{if(map[k][j] == check_1[4][0] && map[k][j+1] == check_1[4][1])
{place_x1 = k; place_y1 = j; break; } }
if( j <= 23) break;
}
if( k == 24)
{printf(" 將 被將死 帥方獲得勝利\n");printf("按任意鍵返回菜單"); getch( );
*if_return = 1; return; }
if( place_y1 == place_y2)
{for( k=place_x2 + 2; k <= place_x1 - 2; k=k +2) {if(map[k][place_y1] != '+') break; }
if( k == place_x1)
{if(round==1)printf(" 將方對將 帥方勝利");else if(round==2)printf(" 帥方對將 將方勝利");
printf("按任意鍵返回菜單"); getch( ); *if_return = 1; return; }
}
break;
}
} // for ( i = 0; i < 8; i++)循環結束
if( i == 8) {printf("不合法的走法\n"); Sleep(500); }
}
void xiangqi( ) //象棋主程序
{ char map[100][100]= { "[[===================================]]",
"[| ①將 【象棋】 ②帥 |]",
"[[===================================]]",
"[[-----------------------------------]]",
"[[ 車—-馬—-相—-仕—-帥—-仕—-相—-馬—-車]]",
"[[ | | | | \\ | / | | | | ]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[ | | | | / | \\ | | | | ]]",
"[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]",
"[[ | | | | | | | | | ]]",
"[[ 卒—-+-—-卒—-+-—-卒—-+-—-卒—-+-—-卒]]",
"[[ | | | | | | | | | ]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[===================================]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[ | | | | | | | | | ]]",
"[[ 兵—-+-—-兵—-+-—-兵—-+-—-兵—-+-—-兵]]",
"[[ | | | | | | | | | ]]",
"[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]",
"[[ | | | | \\ | / | | | | ]]",
"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",
"[[ | | | | / | \\ | | | | ]]",
"[[ 車—-馬—-象—-士—-將—-士—-象—-馬—-車]]",
"[[-----------------------------------]]",
"[[===================================]]"};
int if_return = 0;
system("mode con cols=45 lines=32"); //迷你界面
system("color 70");
printf("[[==================================]]\n");
printf("[[ -------------------------------- ]]\n");
printf("[[ | | ]]\n");
printf("[[ | 【<<游戲規則>>】 | ]]\n");
printf("[[ | | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 控制wasd雙方輪流控制指針下棋| ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 鍵盤輸入大小寫 ' M ' | ]]\n");
printf("[[ | 都視為確認下棋 | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 為了方便區分棋子 | ]]\n");
printf("[[ | 後手方全設為繁體復雜字體 | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[ | 我已閱讀規則,按任意鍵繼續 | ]]\n");
printf("[[ |------------------------------| ]]\n");
printf("[[==================================]]\n");
getch( ); system("mode con cols=45 lines=32"); //迷你界面
system("color 70");
for ( i = 0; i < 27; i++){ puts(map[i]); Sleep(100); }
x = 6, y = 19; temp = map[x][y]; temp1 = map[x][y+1];
while(num)
{ if (num % 2 == 1 &&num / 2 % 2 == 0){ printf(" 現在是'將'的回合\n");round = 1; }
else if( num %2 == 1){ printf(" 現在輪到'帥'的回合了\n");round = 2; }
ch = getch( );
if ( ch == 's') //下移
{ if ( map[x+1][y]!= '-')
{map[x][y] =temp; map[x][y+1] = temp1; x = x + 2;
temp = map[x][y]; temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if ( ch == 'a') //左移
{ if (map[x][y-1]!=' ')
{map[x][y] =temp; map[x][y+1] = temp1; y = y - 4;
temp = map[x][y]; temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if ( ch == 'w') //上移
{ if ( map[x-1][y]!= '-')
{map[x][y] =temp; map[x][y+1] = temp1; x = x - 2; temp = map[x][y];
temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if ( ch == 'd') //右移
{ if (map[x][y+2]!=']')
{map[x][y] =temp; map[x][y+1] = temp1; y = y + 4; temp = map[x][y];
temp1 = map[x][y+1]; map[x][y] = turn; map[x][y+1] = turn1; }
}
else if( ch == 'm' || ch =='M') //M確認要移動的棋子,或確認要移到的目的地
{ if (num % 2 == 1 && temp != '+' && temp1 != '-') //取棋
{check[0] = temp; check[1] = temp1; check[2] = '\0';
if ( round == 1)
{ for ( i = 0; i < 7; i++) //將方
{ if ( strcmp(check_1[i],check) == 0)
{turn = temp; turn1 = temp1; temp = '+'; temp1 = '-';
check_x = x; check_y = y; check_turn = 10 + i; num++; break; }
}
if( i == 7){ printf("這不是你的棋子\n"); Sleep(500); }
}
else if( round == 2)
{for ( i = 0; i < 7; i++) //帥方
{ if( strcmp(check_2[i],check) == 0)
{turn = temp; turn1 = temp1; temp = '+'; temp1 = '-';
check_x = x; check_y = y; check_turn = 20 + i; num++; break; }
}
if( i == 7){ printf("這不是你的棋子\n"); Sleep(500); }
}
}
else if( num % 2 == 0) //放棋
{ char check_1[8][3] ={"車","馬","象","士","將","炮","卒","+-"};
char check_2[8][3] ={"俥","馬","相","仕","帥","軳","兵","+-"};
//中界 楚河上下坐標 12 15 往下2 往右4
if( check_turn < 20) //將方
{if( check_turn == 10) //車的走法規范
{ if((x == check_x && y == check_y))
{temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而後行\n"); printf("還是你的回合"); Sleep(500); }
else if( y == check_y )
{ if( x > check_x)
{ for(j = check_x + 2; j < x;j = j + 2)
{ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; } }
if( j >= x) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
if( x < check_x){ for(j = check_x - 2; j > x;j = j - 2)
{ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; }
}
if( j <= x)check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
}
else if( x == check_x )
{if( y > check_y)
{for(j = check_y + 4; j < y;j = j + 4)
{if(map[x][j] == '+'); else {printf("不合法的下法\n"); Sleep(500); break; }
}
if( j >= y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
if( y < check_y)
{for(j = check_y - 4; j > y;j = j - 4)
{ if(map[x][j] == '+'); else { printf("不合法的下法\n"); Sleep(500); break; }
}
if( j <= y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map);
}
}
else { printf("不合法的下法\n"); Sleep(500); }
}
if( check_turn == 11) //馬的走法規范
{if((x == check_x && y == check_y))
{ temp = turn; temp1 = turn1; turn = 'O'; turn1 = 'N'; num--;
printf("三思而後行\n"); printf("還是你的回合"); Sleep(500); }
else if( (abs( x - check_x) == 2&& abs( y - check_y) == 8)&& map[check_x][(y+check_y)/2] =='+')
{check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }
else if( (abs( x - check_x) == 4&& abs( y - check_y) == 4)&& map[(x + check_x)/2][check_y] == '+' )
{check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map); }
else { printf("不合法的下法\n");Sleep(500); }
} //其餘代碼在後續

③ c語言的五子棋編碼

我有用qt寫的五子棋,你要是要的話可以發給你

④ C語言五子棋

我瀏覽了一下你的代碼,你對勝利的判斷採用的是檢查整個棋盤的方式,這樣做的最大弊端自然是低效,而且在編寫斜方向判斷的時候比較復雜。我建議你採用下子後判斷的方式,即玩家1下了一個字後,像該子周圍8個方向檢查是否存在5子的情況,只需要一個判斷方法,而且較為簡單。

void chooseONE()
{

printf("第一玩家請選擇下棋位置\n");
printf("第幾個直的\n");
scanf("%d",&co);
printf("第幾個橫的\n");
scanf("%d",&ro);
while(arr[ro][co]==1||arr[ro][co]==2)
{
printf("重復了!!!\n");
printf("第幾個直的\n");
scanf("%d",&co);
printf("第幾個橫的\n");
scanf("%d",&ro);

}

arr[ro][co]=1;
//這里加判斷代碼,建議用一個判斷函數,我給你寫個吧

}
//我這里這個方法寫了個大概,你看一下和你的代碼結合一下就可以了,我用i和j作為for循環的變數,你用來做最大的長寬值,這些你都要改一下。
flag作為判斷是否勝利的標志,playerNum是玩家的標志,分1和2,這個函數只寫了左和左上的判斷,其他方向樓主仿照著寫
int win(int row, int col, int playerNum){
int i = 0,j = 0;
int flag = 1;
if(row - 4 >= 0){
flag = 1;
for(i = row; i >= 0; i--){
if(arr[i][j] != playerNum){
flag = 0;
break;
}
}
if(flag == 1){
return 1;
}
}
if(row - 4 >= 0 && col - 4 >= 0){
flag = 1;
for(i = row ,j = col ; i >= 0&&j>=0; i--,j--){
if(arr[i][j] != playerNum){
flag = 0;
break;
}
}
if(flag == 1){
return 1;
}
}
}

⑤ 用C語言編寫一個五子棋的游戲程序

**********************************************************/
/* 程序中用到的庫函數所在頭文件應用 #include 命令包含進來 */

#include <stdio.h>
#include <bios.h>
#include <ctype.h>
#include <conio.h>
#include <dos.h>

/**********************************************************/
/* 定義符號常量 */

/*定義畫棋盤所需的製表符*/
#define CROSSRU 0xbf /*右上角點*/
#define CROSSLU 0xda /*左上角點*/
#define CROSSLD 0xc0 /*左下角點*/
#define CROSSRD 0xd9 /*右下角點*/
#define CROSSL 0xc3 /*左邊*/
#define CROSSR 0xb4 /*右邊*/
#define CROSSU 0xc2 /*上邊*/
#define CROSSD 0xc1 /*下邊*/
#define CROSS 0xc5 /*十字交叉點*/

/*定義棋盤左上角點在屏幕上的位置*/
#define MAPXOFT 5
#define MAPYOFT 2

/*定義1號玩家的操作鍵鍵碼*/
#define PLAY1UP 0x1157/*上移--'W'*/
#define PLAY1DOWN 0x1f53/*下移--'S'*/
#define PLAY1LEFT 0x1e41/*左移--'A'*/
#define PLAY1RIGHT 0x2044/*右移--'D'*/
#define PLAY1DO 0x3920/*落子--空格鍵*/

/*定義2號玩家的操作鍵鍵碼*/
#define PLAY2UP 0x4800/*上移--方向鍵up*/
#define PLAY2DOWN 0x5000/*下移--方向鍵down*/
#define PLAY2LEFT 0x4b00/*左移--方向鍵left*/
#define PLAY2RIGHT 0x4d00/*右移--方向鍵right*/
#define PLAY2DO 0x1c0d/*落子--回車鍵Enter*/

/*若想在游戲中途退出, 可按 Esc 鍵*/
#define ESCAPE 0x011b

/*定義棋盤上交叉點的狀態, 即該點有無棋子 */
/*若有棋子, 還應能指出是哪個玩家的棋子 */
#define CHESSNULL 0 //沒有棋子
#define CHESS1 'O'//一號玩家的棋子
#define CHESS2 'X'//二號玩家的棋子

/*定義按鍵類別*/
#define KEYEXIT 0/*退出鍵*/
#define KEYFALLCHESS 1/*落子鍵*/
#define KEYMOVECURSOR 2/*游標移動鍵*/
#define KEYINVALID 3/*無效鍵*/

/*定義符號常量: 真, 假 --- 真為1, 假為0 */
#define TRUE 1
#define FALSE 0

/**********************************************************/
/* 定義數據結構 */

/*棋盤交叉點坐標的數據結構*/
struct point
{
int x,y;
};

/**********************************************************/
/*自定義函數原型說明 */
void Init(void);
int GetKey(void);
int CheckKey(int press);
int ChangeOrder(void);
int ChessGo(int Order,struct point Cursor);
void DoError(void);
void DoOK(void);
void DoWin(int Order);
void MoveCursor(int Order,int press);
void DrawCross(int x,int y);
void DrawMap(void);
int JudgeWin(int Order,struct point Cursor);
int JudgeWinLine(int Order,struct point Cursor,int direction);
void ShowOrderMsg(int Order);
void EndGame(void);
/**********************************************************/

/**********************************************************/
/* 定義全局變數 */
int gPlayOrder; /*指示當前行棋方 */
struct point gCursor; /*游標在棋盤上的位置 */
char gChessBoard[19][19];/*用於記錄棋盤上各點的狀態*/
/**********************************************************/

/**********************************************************/
/*主函數*/
void main()
{
int press;
int bOutWhile=FALSE;/*退出循環標志*/

Init();/*初始化圖象,數據*/

while(1)
{
press=GetKey();/*獲取用戶的按鍵值*/
switch(CheckKey(press))/*判斷按鍵類別*/
{
/*是退出鍵*/
case KEYEXIT:
clrscr();/*清屏*/
bOutWhile = TRUE;
break;

/*是落子鍵*/
case KEYFALLCHESS:
if(ChessGo(gPlayOrder,gCursor)==FALSE)/*走棋*/
DoError();/*落子錯誤*/
else
{
DoOK();/*落子正確*/

/*如果當前行棋方贏棋*/
if(JudgeWin(gPlayOrder,gCursor)==TRUE)
{
DoWin(gPlayOrder);
bOutWhile = TRUE;/*退出循環標志置為真*/
}
/*否則*/
else
/*交換行棋方*/
ChangeOrder();
}
break;

/*是游標移動鍵*/
case KEYMOVECURSOR:
MoveCursor(gPlayOrder,press);
break;

/*是無效鍵*/
case KEYINVALID:
break;
}

if(bOutWhile==TRUE)
break;
}

/*游戲結束*/
EndGame();
}
/**********************************************************/

/*界面初始化,數據初始化*/
void Init(void)
{
int i,j;
char *Msg[]=
{
"Player1 key:",
" UP----w",
" DOWN--s",
" LEFT--a",
" RIGHT-d",
" DO----space",
"",
"Player2 key:",
" UP----up",
" DOWN--down",
" LEFT--left",
" RIGHT-right",
" DO----ENTER",
"",
"exit game:",
" ESC",
NULL,
};

/*先手方為1號玩家*/
gPlayOrder = CHESS1;
/*棋盤數據清零, 即棋盤上各點開始的時候都沒有棋子*/
for(i=0;i<19;i++)
for(j=0;j<19;j++)
gChessBoard[i][j]=CHESSNULL;
/*游標初始位置*/
gCursor.x=gCursor.y=0;

/*畫棋盤*/
textmode(C40);
DrawMap();

/*顯示操作鍵說明*/
i=0;
textcolor(BROWN);
while(Msg[i]!=NULL)
{
gotoxy(25,3+i);
cputs(Msg[i]);
i++;
}

/*顯示當前行棋方*/
ShowOrderMsg(gPlayOrder);
/*游標移至棋盤的左上角點處*/
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}

/*畫棋盤*/
void DrawMap(void)
{
int i,j;

clrscr();

for(i=0;i<19;i++)
for(j=0;j<19;j++)
DrawCross(i,j);

}

/*畫棋盤上的交叉點*/
void DrawCross(int x,int y)
{
gotoxy(x+MAPXOFT,y+MAPYOFT);
/*交叉點上是一號玩家的棋子*/
if(gChessBoard[x][y]==CHESS1)
{
textcolor(LIGHTBLUE);
putch(CHESS1);
return;
}
/*交叉點上是二號玩家的棋子*/
if(gChessBoard[x][y]==CHESS2)
{
textcolor(LIGHTBLUE);
putch(CHESS2);
return;
}

textcolor(GREEN);

/*左上角交叉點*/
if(x==0&&y==0)
{
putch(CROSSLU);
return;
}

/*左下角交叉點*/
if(x==0&&y==18)
{
putch(CROSSLD);
return;
}

/*右上角交叉點*/
if(x==18&&y==0)
{
putch(CROSSRU);
return;
}

/*右下角交叉點*/
if(x==18&&y==18)
{
putch(CROSSRD);
return;
}

/*左邊界交叉點*/
if(x==0)
{
putch(CROSSL);
return;
}

/*右邊界交叉點*/
if(x==18)
{
putch(CROSSR);
return;
}

/*上邊界交叉點*/
if(y==0)
{
putch(CROSSU);
return;
}

/*下邊界交叉點*/
if(y==18)
{
putch(CROSSD);
return;
}

/*棋盤中間的交叉點*/
putch(CROSS);
}

/*交換行棋方*/
int ChangeOrder(void)
{
if(gPlayOrder==CHESS1)
gPlayOrder=CHESS2;
else
gPlayOrder=CHESS1;

return(gPlayOrder);
}

/*獲取按鍵值*/
int GetKey(void)
{
char lowbyte;
int press;

while (bioskey(1) == 0)
;/*如果用戶沒有按鍵,空循環*/

press=bioskey(0);
lowbyte=press&0xff;
press=press&0xff00 + toupper(lowbyte);
return(press);
}

/*落子錯誤處理*/
void DoError(void)
{
sound(1200);
delay(50);
nosound();
}

/*贏棋處理*/
void DoWin(int Order)
{
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
nosound();

textcolor(RED+BLINK);
gotoxy(25,20);
if(Order==CHESS1)
cputs("PLAYER1 WIN!");
else
cputs("PLAYER2 WIN!");
gotoxy(25,21);
cputs(" \\<^+^>/");
getch();
}

/*走棋*/
int ChessGo(int Order,struct point Cursor)
{
/*判斷交叉點上有無棋子*/
if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL)
{
/*若沒有棋子, 則可以落子*/
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
textcolor(LIGHTBLUE);
putch(Order);
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
gChessBoard[Cursor.x][Cursor.y]=Order;
return TRUE;
}
else
return FALSE;
}

/*判斷當前行棋方落子後是否贏棋*/
int JudgeWin(int Order,struct point Cursor)
{
int i;
for(i=0;i<4;i++)
/*判斷在指定方向上是否有連續5個行棋方的棋子*/
if(JudgeWinLine(Order,Cursor,i))
return TRUE;
return FALSE;
}

/*判斷在指定方向上是否有連續5個行棋方的棋子*/
int JudgeWinLine(int Order,struct point Cursor,int direction)
{
int i;
struct point pos,dpos;
const int testnum = 5;
int count;

switch(direction)
{
case 0:/*在水平方向*/
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y;
dpos.x=1;
dpos.y=0;
break;
case 1:/*在垂直方向*/
pos.x=Cursor.x;
pos.y=Cursor.y-(testnum-1);
dpos.x=0;
dpos.y=1;
break;
case 2:/*在左下至右上的斜方向*/
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y+(testnum-1);
dpos.x=1;
dpos.y=-1;
break;
case 3:/*在左上至右下的斜方向*/
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y-(testnum-1);
dpos.x=1;
dpos.y=1;
break;
}

count=0;
for(i=0;i<testnum*2+1;i++)
{
if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18)
{
if(gChessBoard[pos.x][pos.y]==Order)
{
count++;
if(count>=testnum)
return TRUE;
}
else
count=0;
}
pos.x+=dpos.x;
pos.y+=dpos.y;
}

return FALSE;
}

/*移動游標*/
void MoveCursor(int Order,int press)
{
switch(press)
{
case PLAY1UP:
if(Order==CHESS1&&gCursor.y>0)
gCursor.y--;
break;
case PLAY1DOWN:
if(Order==CHESS1&&gCursor.y<18)
gCursor.y++;
break;
case PLAY1LEFT:
if(Order==CHESS1&&gCursor.x>0)
gCursor.x--;
break;
case PLAY1RIGHT:
if(Order==CHESS1&&gCursor.x<18)
gCursor.x++;
break;

case PLAY2UP:
if(Order==CHESS2&&gCursor.y>0)
gCursor.y--;
break;
case PLAY2DOWN:
if(Order==CHESS2&&gCursor.y<18)
gCursor.y++;
break;
case PLAY2LEFT:
if(Order==CHESS2&&gCursor.x>0)
gCursor.x--;
break;
case PLAY2RIGHT:
if(Order==CHESS2&&gCursor.x<18)
gCursor.x++;
break;
}

gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}

/*游戲結束處理*/
void EndGame(void)
{
textmode(C80);
}

/*顯示當前行棋方*/
void ShowOrderMsg(int Order)
{
gotoxy(6,MAPYOFT+20);
textcolor(LIGHTRED);
if(Order==CHESS1)
cputs("Player1 go!");
else
cputs("Player2 go!");

gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}

/*落子正確處理*/
void DoOK(void)
{
sound(500);
delay(70);
sound(600);
delay(50);
sound(1000);
delay(100);
nosound();
}

/*檢查用戶的按鍵類別*/
int CheckKey(int press)
{
if(press==ESCAPE)
return KEYEXIT;/*是退出鍵*/

else
if
( ( press==PLAY1DO && gPlayOrder==CHESS1) ||
( press==PLAY2DO && gPlayOrder==CHESS2)
)
return KEYFALLCHESS;/*是落子鍵*/

else
if
( press==PLAY1UP || press==PLAY1DOWN ||
press==PLAY1LEFT || press==PLAY1RIGHT ||
press==PLAY2UP || press==PLAY2DOWN ||
press==PLAY2LEFT || press==PLAY2RIGHT
)
return KEYMOVECURSOR;/*是游標移動鍵*/

else
return KEYINVALID;/*按鍵無效*/
}

⑥ c語言寫一個類似於下棋的程序

幫你寫了個當個字元轉換的

如果是字元串的話請追問. 再幫你修改.

#include

void main()
{
char a;
a=getchar();
if(a>='A' && a<='Z')
a+=32;
else if(a>='a' && a<='z')
a-=32;
putchar(a);
getch();
}
補充:
這是字元串的.

#include

void main()
{
char a[50];
int i=0;

gets(a);
while(a[i])
{
if(a[i] >= 'A' && a[i] <= 'Z')
a[i] += 32;
else if(a[i] >= 'a' && a[i] <= 'z')
a[i] -= 32;
i++;
}

puts(a);
}

追問:
字元串的就不用了。今天的這個題目就是轉換大小寫字元的,如果輸入字母或者是特殊的符號,怎樣加IF語句?

⑦ 使用c語言編寫一個人機對戰下棋程序

#include "stdafx.h"
#include "resource.h"
#include "define.h"
#include "box.h"
#include "moveGeneration.h"

#pragma comment(lib, "winmm.lib")
#include <Mmsystem.h>

#define MAX_LOADSTRING 100

#define BOXBMP_WIDTH 537 /* 棋盤圖片寬度 */
#define BOXBMP_HEIGHT 537 /* 棋盤圖片高度 */
#define SPACE 20 /* 棋盤圖片和窗口邊沿間隔 */
#define UNIT_WIDTH 35 /* 每個棋格寬度 */
#define UNIT_HEIGHT 35 /* 每個棋格高度 */

#define STONE_WIDTH 20 /* 棋子寬度 */

/* 窗口寬度 = 兩邊空白 + 棋盤圖片寬度 + 窗口邊框寬度 */
#define WIN_WIDTH 2*SPACE+BOXBMP_WIDTH+2*GetSystemMetrics(SM_CXDLGFRAME)

/* 窗口高度 = 標題欄高度 + 上下兩邊空白 + 棋盤圖片高度 + 窗口邊框高度 */
#define WIN_HEIGHT GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYMENU)+2*SPACE\
+BOXBMP_HEIGHT+2*GetSystemMetrics(SM_CXDLGFRAME)

//////////////////////////////////////////////////////////////////////
// 全局變數:
static HINSTANCE hInst; // 當前實例
static TCHAR szTitle[MAX_LOADSTRING]; // 標題欄文本
static TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口類名

//////////////////////////////////////////////////////////////////////
// 此代碼模塊中包含的函數的前向聲明:
ATOM InitApplication(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK About(HWND, UINT, WPARAM, LPARAM);
////////////////////////////////////////////////////////////////////// 繪制棋子
void DrawStone(HDC hdc, int column, int row, STONE_TYPE stone)
{
HPEN hpen;
HBRUSH hbrush;

switch (stone)
{
case BLACK_STONE: //黑子
hpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
hbrush = CreateSolidBrush(RGB(0, 0, 0));
break;

case WHITE_STONE: //白子
hpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
hbrush = CreateSolidBrush(RGB(255, 255, 255));
break;

default:
return;
}

SelectObject(hdc, hpen);
SelectObject(hdc, hbrush);
Ellipse(hdc, column*34.9+35, row*34.9+35, column*34.9+55, row*34.9+55);

DeleteObject(hpen);
DeleteObject(hbrush);

return;
}
//////////////////////////////////////////////////////////// 繪制棋盤
void DrawChessBox(HDC hdc, STONE_TYPE box[BOX_ROW][BOX_COL])
{
assert(NULL != box);

HBITMAP hBitmap;
HDC hdcSrc;

hBitmap = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_CHESSBOX));
hdcSrc = CreateCompatibleDC(hdc);
SelectObject(hdcSrc, hBitmap);
BitBlt(hdc, SPACE, SPACE, BOXBMP_WIDTH, BOXBMP_HEIGHT, hdcSrc, 0, 0, SRCCOPY);
DeleteObject(hBitmap);

for(int column=0; column<BOX_COL; column++)
{
for(int row=0; row<BOX_ROW; row++)
{
DrawStone(hdc, column, row, box[column][row]);
}
}
}
////////////////////////////////////////////////////////////////////
//重新開始
static STONE_TYPE NewGame(HWND hWnd, STONE_TYPE box[BOX_ROW][BOX_COL])
{
boxInit(box);//清空
InvalidateRect(hWnd, NULL, FALSE);//重繪
return BLACK_STONE;
}

/**
* @brief 放置棋子
* @param hWnd 窗口句柄
* @param box 當前棋盤
* @param point 當前所下棋子在棋盤中的坐標
* @return TRUE 游戲結束, FALSE 游戲未結束
*/
static BOOL PlaceStone(HWND hWnd, STONE_TYPE box[BOX_ROW][BOX_COL], POINTS point, STONE_TYPE stone)
{
STONE_TYPE successStone = NONE_STONE;

boxSetStone(box, point.x, point.y, stone);

InvalidateRect(hWnd, NULL, FALSE);//重繪
PlaySound(MAKEINTRESOURCE(IDW_TADA), hInst, SND_RESOURCE|SND_SYNC);
successStone = boxIsOver(box, point.x, point.y);

if (WHITE_STONE == successStone)
{
MessageBox(hWnd, TEXT("白子獲勝!"), TEXT("提示"), MB_OK|MB_ICONINFORMATION);

return TRUE;
}
else if (BLACK_STONE == successStone)
{
MessageBox(hWnd, TEXT("黑子獲勝!"), TEXT("提示"), MB_OK|MB_ICONINFORMATION);

return TRUE;
}

return FALSE;
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

MSG msg;
HACCEL hAccelTable;

// 初始化全局字元串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_GOBANG, szWindowClass, MAX_LOADSTRING);
InitApplication(hInstance);

// 執行應用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GOBANG));

// 主消息循環:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
// 函數: InitApplication()
//
// 目的: 注冊窗口類。
//
// 注釋:
//
// 僅當希望
// 此代碼與添加到 Windows 95 中的「RegisterClassEx」
// 函數之前的 Win32 系統兼容時,才需要此函數及其用法。調用此函數十分重要,
// 這樣應用程序就可以獲得關聯的
// 「格式正確的」小圖標。
//
ATOM InitApplication(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BIG));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_GOBANG);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
// 函數: InitInstance(HINSTANCE, int)
//
// 目的: 保存實例句柄並創建主窗口
//
// 注釋:
//
// 在此函數中,我們在全局變數中保存實例句柄並
// 創建和顯示主程序窗口。
/////////////////////////////////////////////////////////清空棋盤
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // 將實例句柄存儲在全局變數中

int nWidth, nHeight;
nWidth = WIN_WIDTH/*585*/;
nHeight = WIN_HEIGHT/*630*/;

hWnd = CreateWindow(szWindowClass, szTitle,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
(GetSystemMetrics(SM_CXSCREEN)-nWidth)/2, (GetSystemMetrics(SM_CYSCREEN)-nHeight)/2,
nWidth, nHeight,
NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}
///////////////////////////////////////////// 「關於」框的消息處理程序。
BOOL CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return (INT_PTR)FALSE;
}

///////////////////////////////////////////////////////////////////

//
// 函數: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 處理主窗口的消息。
//
// WM_COMMAND - 處理應用程序菜單
// WM_PAINT - 繪制主窗口
// WM_DESTROY - 發送退出消息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static STONE_TYPE box[BOX_ROW][BOX_COL];
static STONE_TYPE currentStone = BLACK_STONE;
static BOOL startFlag = FALSE;//設置標志符
static int mode = IDM_MODE_MAN2COMPUTER;

HMENU hMenu = NULL;

switch (message)
{
case WM_CREATE : // 創建窗口
hMenu = GetMenu(hWnd);
CheckMenuItem(hMenu, mode, MF_CHECKED);
break;

case WM_COMMAND:
int wmId, wmEvent;
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);

hMenu = GetMenu(hWnd);

// 分析菜單選擇:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;

case IDM_EXIT:
PostMessage(hWnd,WM_CLOSE,0,0);
break;

case IDM_START://////////////////////////////////////////////////////////開始下棋
currentStone = NewGame(hWnd, box);//先下的棋子顏色
startFlag = TRUE;
break;

case IDM_MODE_MAN2COMPUTER :
CheckMenuItem(hMenu, mode, MF_UNCHECKED);
mode = IDM_MODE_MAN2COMPUTER;
currentStone = NewGame(hWnd, box);
CheckMenuItem(hMenu, mode, MF_CHECKED);
break;

case IDM_MODE_MAN2MAN :
CheckMenuItem(hMenu, mode, MF_UNCHECKED);
mode = IDM_MODE_MAN2MAN;
currentStone = NewGame(hWnd, box);
CheckMenuItem(hMenu, mode, MF_CHECKED);
break;
}
break;

case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);

DrawChessBox(hdc, box);

EndPaint(hWnd, &ps);
break;

case WM_LBUTTONUP: //滑鼠提起
if (!startFlag) // 沒有點擊開始
{
return 0;
}

POINTS point;
point = MAKEPOINTS(lParam);

//判斷滑鼠是否點擊棋盤
if(point.x > 35 && point.x < 542 && point.y > 35 && point.y < 542)
{
//通過滑鼠點擊位置計算出棋子放置位置
point.x = (point.x - 35) / 34;
point.y = (point.y - 35) / 34;

if(boxGetStone(box, point.x, point.y) == NONE_STONE)
{
if (PlaceStone(hWnd, box, point, currentStone))
{
currentStone = NewGame(hWnd, box);
}
else
{
switch (currentStone)
{
case BLACK_STONE :
currentStone = WHITE_STONE;
break;

case WHITE_STONE :
currentStone = BLACK_STONE;
break;

default :
break;
}
}

// 人機對弈,由計算機搜索最優結果
if (mode == IDM_MODE_MAN2COMPUTER)
{
point = moveGeneration(box);
if (PlaceStone(hWnd, box, point, currentStone))
{
currentStone = NewGame(hWnd, box);
}
else
{
currentStone = BLACK_STONE; /* 人機對弈,人執黑子 */
}
}
}
else
{
MessageBox(hWnd, TEXT("此處已放置了棋子"), TEXT("警告"), MB_OK | MB_ICONWARNING);
}
}
break;

case WM_CLOSE:
if(MessageBox(hWnd, TEXT("確認要退出快樂五子棋游戲嗎?"), TEXT("提示"), MB_YESNO | MB_ICONASTERISK)==IDYES)
{
DestroyWindow(hWnd);
}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}

return 0;
}

還是不簡單啊,嘿嘿,初學就要這么復雜的代碼啊,要是好就給分哈

⑧ 用C語言寫一個五子棋游戲

天拉..
我也正在寫五子棋,想不出怎麼才這么點代碼啊???..郁悶.
我也研究下,不知道這五子棋能發揮出什麼樣的下棋水準?

⑨ 編寫一個五子棋的游戲程序,實現人與機對下的功能 求大神發個完整版的能在vc++上運行的C語言版的,謝謝

⑩ 急求論文 c語言開發五子棋的應用

//但是這些代碼由於編譯器的原因,在TC下仍無法運行。請使用VC。
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>int ChessData[15][15] ={0};
int GuangbiaoData[2]={8,8};
int ChessStepData[255][2]={0};
void gotoxy(int x, int y) //gotoxy在TC中是在一個系統自帶的函數,但是在VC中沒有這個函數,所以在網上找到了這個函數以實現同樣的功能。
//只有這一個函數是網上找的,別的全部我自己寫的。
{
COORD c;
c.X=x-1;
c.Y=y-1;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}void GotoChess(int x,int y)
{
x=3*x-2;y=2*y-1;
gotoxy(x,y);
}void Move(int MoveData) //輸入參數為用戶輸入的方向(1表示上,2表示下,3表示左,4表示右)
{
switch (MoveData)
{
case 1:GuangbiaoData[1]-=1;break;
case 2:GuangbiaoData[1]+=1;break;
case 3:GuangbiaoData[0]-=1;break;
case 4:GuangbiaoData[0]+=1;break;
default:printf("Move函數出錯");
}
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}int Get(int *data) //該函數的功能是用戶的按鍵,並轉化為01234567(0表示輸入錯誤,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。)
{ //並返回輸入的用戶號碼(共同鍵返回3),錯誤則返回0
int temp;B: temp=getch();
if (temp==224)
{
temp=getch();
switch (temp)
{
case 72:*data=1;break;
case 80:*data=2;break;
case 75:*data=3;break;
case 77:*data=4;break;
default:goto B;
}
return 2;
}
else
{
switch (temp)
{
case 'w':
case 'W':*data=1;return 1;break;
case 's':
case 'S':*data=2;return 1;break;
case 'a':
case 'A':*data=3;return 1;break;
case 'd':
case 'D':*data=4;return 1;break;
case 13 :*data=5;return 2;break;
case 32 :*data=5;return 1;break;
case 8 :*data=6;break;
case 27 :*data=7;break;
default:*data=0 ;return 0;break;
}
return 3;
}
}
void MoveToEnd()
{
gotoxy(1,30);
}
int LogicBeOut(int a,int b)
{
if (a==-1||a==15||b==-1||b==15) return 1;
else return 0;
}
int win(int v)
{
int i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]+i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]-j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;

return 0;
}void NewShow() //新棋局的開始
{
int i,j;
for (i=0;i<15;i++)
for (j=0;j<15;j++)
ChessData[i][j]=0;
system("cls");
for (i=1;i<=29;i++)
{
for (j=1;j<=43;j++)
if(i%2==1) printf("-");
else if (j%3==1) printf("|");
else printf(" ");
printf("\n");
}
GuangbiaoData[0]=8;GuangbiaoData[1]=8;
MoveToEnd();
printf("現在請用戶1下棋 \n");
printf("用戶1使用 w,s,a,d移動游標,空格鍵落子\n");
printf("用戶2使用各方向鍵移動游標,回車鍵落子\n");
printf("按下Backspace鍵悔棋,按下esc返回主菜單\n");
GotoChess(8,8);
}int BeOut(int data)
{
int Xiuzheng[2],New[2];
switch (data)
{
case 1:Xiuzheng[0]=0;Xiuzheng[1]=-1 ;break;
case 2:Xiuzheng[0]=0;Xiuzheng[1]=1;break;
case 3:Xiuzheng[0]=-1;Xiuzheng[1]=0;break;
case 4:Xiuzheng[0]=1;Xiuzheng[1]=0 ;break;
case 5:Xiuzheng[0]=0;Xiuzheng[1]=0 ;break;
default:printf("BeOut函數出錯");
}
New[0]=GuangbiaoData[0]+Xiuzheng[0];
New[1]=GuangbiaoData[1]+Xiuzheng[1];
if (New[0]>15||New[0]<1||New[1]>15||New[1]<1) return 1;
else return 0;
}void UserChoose(int * choice)
{
system("cls");
printf("________________________________________________________________________________");
printf("________________________________________________________________________________");
printf(" $1.單人游戲 ");
printf(" 2.雙人游戲 ");
printf(" 3.退出遊戲 ");
printf(" 4.游戲幫助 ");
printf("________________________________________________________________________________");
printf("________________________________________________________________________________");
printf(" ");
printf(" 開心五子棋 ");
printf(" ");
printf(" 出品人:張雲聰 ");
printf(" 學號:067108034 ");
printf(" 指導老師:邵艷玲 ");
printf(" ");
printf(" ");
printf(" 溫馨提示,游戲時請把窗口最大化,以達到最佳效果 ");
printf("________________________________________________________________________________");
int temp=0,i=0;
do
{
if ((temp=getch())==224)
{
temp=getch();
if (temp==72&&i!=0)
{
gotoxy(34,3+i);
printf(" ");
i--;
gotoxy(34,3+i);
printf("$");
gotoxy(0,0);
}
else if(temp==80&&i!=3)
{
gotoxy(34,3+i);
printf(" ");
i++;
gotoxy(34,3+i);
printf("$");
gotoxy(0,0);
}
} else if (temp==13) {*choice=i+1;return;}
else if (temp==27) {*choice=3;return;}
else if (temp=='1'||temp=='2'||temp=='3'||temp=='4') {*choice=temp-48;return;}}while(1);
}int CannotDo(int v1,int v2,int MoveData,int choice) //第一個輸入值為按鍵的用戶號,第二個是本應該按鍵的用戶號,第三個為按下鍵的對應值,第四個鍵代表游戲模式。
{
if (v1==3) return 0; //如果用戶輸入的為共用按鍵,則CannotDo為假else if (v1==0) return 1;//如果用戶輸入錯誤,則CannotDo為真
else if (v1!=v2&&choice==2) return 1; //如果不該此用戶輸入,而用戶進行了輸入,則CannotDo為真if (BeOut(MoveData)) return 1; //如果移動出邊界則CannotDo為真
return 0;
}int CannotLuozi() //判斷是否可以落子。
{
if (ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1])
return 1;
else return 0;
}int luozi(int v) //玩家v落子。
{
ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]+=v*2;
if (v==1) printf("O");
else if (v==2) printf("X");
else printf("luozi函數出錯");
if (win(v)) {MoveToEnd();printf("玩家%d獲得了勝利! \n",v);for (int i=1;i<=240;i++) printf(" ");GotoChess(GuangbiaoData[0],GuangbiaoData[1]);getch();return 1;}
MoveToEnd();
printf("現在請用戶%d下棋 ",v%2+1);
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
return 0;
}void HuiQi(int step) //輸入的是當前的要悔的棋是第幾步
{
GuangbiaoData[0]=ChessStepData[step-1][0];
GuangbiaoData[1]=ChessStepData[step-1][1];
ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]=0;

GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
printf("-");
MoveToEnd();
printf("現在請用戶%d下棋 ",(step+1)%2+1);
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}int DataGetAndChoose(int choice)
{
int MoveData=0,i=0,temp; //MoveData 0表示不可移動,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。
while(1)
{
loop: while (temp=Get(&MoveData),CannotDo(temp,i%2+1,MoveData,choice));
switch (MoveData)
{
case 1:
case 2:
case 3:
case 4:Move(MoveData);break;
case 7:return 0;
case 6:
if (i==0) {MoveToEnd();printf("現在無法悔棋 ");GotoChess(GuangbiaoData[0],GuangbiaoData[1]);}
else HuiQi(i--);
break;
case 5:
if (CannotLuozi()) goto loop;
if(luozi(i%2+1)) return 0;
ChessStepData[i][0]=GuangbiaoData[0];
ChessStepData[i][1]=GuangbiaoData[1];
i++;
break;
default:printf("DataGetAndChoose函數出錯");break;
}
}
return 1;
}void ShowHelp()
{
system("cls");
printf("********************************************************************************");
printf("********************************************************************************");
printf("****** 單人游戲供用戶一個人自己與自己下棋研究棋局之用 ******");
printf("****** 雙人游戲中,用戶1使用wsad控制方向,按空格落子 ******");
printf("****** 用戶2按方向鍵控制方向,回車鍵落子 ******");
printf("****** 游戲過程中按esc返回主菜單 ******");
printf("****** 游戲過程中退格鍵悔棋 ******");
printf("****** 雙人模式中某人下棋時,另一個用戶無法控制游標與落子 ******");
printf("****** ******");
printf("****** 幫助 ******");
printf("****** 按任意鍵返回 ******");
printf("********************************************************************************");
printf("********************************************************************************");
getch();
}int main()
{
int choice=0;
system("color 2b");
choose: UserChoose(&choice);
if (choice<1||choice>4) goto choose;
if (choice==3) {gotoxy(1,18);<br>printf("\n謝謝您的使用,再見 "); getch();return 0;}
if (choice==4) {ShowHelp(); goto choose;}
NewShow();
DataGetAndChoose(choice);
main();
return 0;
}