當前位置:首頁 » 編程軟體 » 五子棋編程

五子棋編程

發布時間: 2022-01-09 11:55:29

Ⅰ 五子棋的程序用C怎麼編

// Game.cpp: implementation of the Game class. // ////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <string.h> #include <stdlib.h> int array[21][21]; int focus_x; int focus_y; int player1; int player2; int winner; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// int main() { void startGame(); startGame(); return 0; } void init() { int i, j; for (i=0; i<21; i++) { for (j=0; j<21; j++) { array[i][j] = 0; } } player1 = 1; player2 = 0; focus_x = 10; focus_y = 10; } void startGame() { void init(); void printChessBoard(); void control(); int checkGameOver(); init(); while (winner == 0) { printChessBoard(); control(); checkGameOver(); } printChessBoard(); printf("\nPlayer%d is the winner!\n\n", winner); system("pause"); } void printChessBoard() { int i, j; system("cls"); printf("游戲提示:\nwsad為上下左右,每個操作需按Enter確認(比如上要按w再按Enter),走棋為k\n"); for (i=0; i<21; i++) { for (j=0; j<21; j++) { if (i==focus_x && j==focus_y) { printf("\b[%d] ", array[i][j]); } else { printf("%-3d", array[i][j]); } } printf("\n"); } if (winner == 0) { if (player1 == 1) { printf("Player1: "); } else { printf("Player2: "); } } } void control() { void up(); void down(); void left(); void right(); void go(); char input; input = getchar(); if (input == 'w') { up(); } if (input == 's') { down(); } if (input == 'a') { left(); } if (input == 'd') { right(); } if (input == 'k') { go(); } } void up() { if (focus_x <= 0) { focus_x = 0; } else { focus_x--; } } void down() { if (focus_x >= 20) { focus_x = 20; } else { focus_x++; } } void left() { if (focus_y <= 0) { focus_y = 0; } else { focus_y--; } } void right() { if (focus_y >= 20) { focus_y = 20; } else { focus_y++; } } void go() { int chessman = array[focus_x][focus_y]; if (chessman == 0) { if (player1 == 1) { array[focus_x][focus_y] = 1; player1 = 0; player2 = 1; } else { array[focus_x][focus_y] = 2; player1 = 1; player2 = 0; } } } int checkGameOver() { int i, j; int judge(int x, int y); for (i=0; i<21; i++) { for (j=0; j<21; j++) { if (array[i][j] != 0) { if(judge(i, j) == 1) { return 1; } } } } return 0; } int judge(int x, int y) { int horizontal(int x, int y); int vertical(int x, int y); int minus_diagonal(int x, int y); int plus_diagonal(int x, int y); if (horizontal(x, y) == 1) { return 1; } if (vertical(x, y) == 1) { return 1; } if (minus_diagonal(x, y) == 1) { return 1; } if (plus_diagonal(x, y) == 1) { return 1; } return 0; } int horizontal(int x, int y) { int i, chess; chess = 1; if (y <= 16) { for (i=1; i<5; i++) { if (array[x][y] == array[x][y+i]) { chess++; } } } if (chess >= 5) { winner = array[x][y]; return 1; } return 0; } int vertical(int x, int y) { int i, chess; chess = 1; if (x <= 16) { for (i=1; i<5; i++) { if (array[x][y] == array[x+i][y]) { chess++; } } } if (chess >= 5) { winner = array[x][y]; return 1; } return 0; } int minus_diagonal(int x, int y) { int i, chess; chess = 1; if (x<=16 && y<=16) { for (i=1; i<5; i++) { if (array[x][y] == array[x+i][y+i]) { chess++; } } } if (chess >= 5) { winner = array[x][y]; return 1; } return 0; } int plus_diagonal(int x, int y) { int i, chess; chess = 1; if (x>=4 && y<=16) { for (i=1; i<5; i++) { if (array[x][y] == array[x-i][y+i]) { chess++; } } } if (chess >= 5) { winner = array[x][y]; return 1; } return 0; } 本人菜鳥一個,在VC++6.0上寫出此拙劣的程序,其實不能算游戲,只是實現了五子棋的功能,有興趣可以一起研究。

麻煩採納,謝謝!

Ⅱ 用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;/*按鍵無效*/
}

Ⅲ 什麼樣的編程水平可以編寫五子棋、象棋程序

針對問題補充部分:

如果這個程序是人人對戰的,那和人工智慧完全沒有關系。

但是如果你要編寫一個帶機器人可以人機對弈的程序,那這個機器人的設計就是人工智慧了。這個難度會大出數倍的(五子棋可能好一些)。不過也有人因為編寫了戰勝世界冠軍的戰棋類機器人而出名過(當然他們用的硬體也很強),如果樓主能編出來這種程序,估計就可以商用往外賣了。

人工智慧裡面要學的東西很多,從理論框架到具體的機器學習演算法(這個演算法和ACM裡面的簡單演算法完全是兩回事)。如果樓主不是這個專業的, 而編寫這種程序更多的是為了練習編程,那麼還是建議不要涉獵人機對戰的部分了。

如果真的感興趣,建議等真的有很多時間可以開發個帶機器人的2.0版本來(樓主要真能寫出來,加我好友吧,我們可以互相交流一下,哈哈,主要是我向你學習一下)

=========================
我有個同學以前編過一個。自己曾經也試圖編掃雷,寫了一半最後還是放棄了。。。

編程水平的話,首先要有基本的語言知識,能夠相對熟練地編寫百行以上的程序吧。

當然上面的要求太基礎了,要編這種棋類程序還需要兩點技巧:

1. 可視化編程,比如c++的話就需要熟悉win32編程或者MFC編程。這樣才能讓程序可以看到,並且視窗交互。(我也見過一個cmd版本的五子棋,很驚嘆,不過樣子就不好看了)

2. 復雜邏輯的處理。無論是五子棋和象棋,都有自己的規則。這些規則人描述起來都比較麻煩,要正確的反應到程序中,就需要維護很多狀態信息,加入很多判斷函數(比如是否已經獲勝、走法是否合法、等等)。如果你可視化編程已經沒有問題,你會發現這部分邏輯設計難度是遠遠大於語言本身的。

如果以上兩點都可以克服的話,這種程序是可以編出來的。樓主加油吧。

Ⅳ C語言編寫五子棋的程序能給我一個么

#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;/*退出循環標志*/
printf("Welcome www.schoolhacker.com");
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();
ShowOrderMsg(gPlayOrder);
}
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[j]=CHESSNULL;
/*游標初始位置*/
gCursor.x=gCursor.y=0;

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

/*顯示操作鍵說明*/
i=0;
textcolor(BROWN);
while(Msg!=NULL)
{
gotoxy(25,3+i);
cputs(Msg);
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++)/*????????i<testnum*2-1*/
{
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語言編一個五子棋的程序

我也不會 ^_^,不過也許可以找到。但是你要這個做什麼?如果是作業還是自己做好,別人的你要是弄不明白也沒有用。

Ⅵ 用C語言編輯程序五子棋

#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();
ShowOrderMsg(gPlayOrder);
}
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(LIGHTRED);
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("\n");
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++)/*????????i<testnum*2-1*/
{
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;/*按鍵無效*/
}

Ⅶ 五子棋的程序設計.

// 程序「五子棋第三方版.exe」運行時,把以前下棋的棋譜保存在「棋譜.txt」文件中,
// 然後調用本程序。
// 本程序的作用是:首先讀入以前的棋譜,然後調用函數「qxwz()」計算出計算機應該
// 下棋的位置,並保存在c中,然後把應下棋的位置寫入文件「當前棋子.txt」
#include <stdio.h>
typedef struct
{
char x;
char y;
} QZ;
QZ qzwz(); //取得應下棋位置,返回值為QZ類型,其中的x,y兩個成員代表應下棋位置
char qp[19][19]; //用於保存棋盤上每個點的狀態,0--空,1--黑棋,2--白棋
void main()
{
FILE *fp;
int tmp1,tmp2,tmp3;
QZ c;
/* 讀入當前棋譜 */
if((fp=fopen("棋譜.txt","r"))==NULL)
{
printf("[棋譜.TXT]文件打開失敗!\n");
return;
}
while(!feof(fp))
{
tmp1=tmp2=tmp3=-1;
fscanf(fp,"%d%d%d",&tmp1,&tmp2,&tmp3);
if(tmp3!=-1)
{
qp[tmp1][tmp2]=tmp3;
}
}
fclose(fp);
c=qzwz();
if((fp=fopen("當前棋子.txt","w"))==NULL)
{
printf("[當前棋子.TXT]文件打開失敗!\n");
return;
}
fprintf(fp,"%d,%d,%d",c.x,c.y,2); //將應下棋位置傳送到文件中
fclose(fp);
}

QZ qzwz()
{
(把這裡面的程序補充完整就好了)

} 這個程序要求是:
1。只適用於人先下,機器後下的情況。有了「五子棋第三方版.exe」來製作下棋界面,就只需要編寫下棋演算法,而不必考慮其它的問題了。
2.當運行「五子棋第三方版.exe」程序時,該程序接受人下棋位置,然後調用「wzq.exe」程序,由「wzq.exe」程序計算出機器的應對並寫入到「當前棋子.txt」文件中(結束),機器的應對結果由「五子棋第三方版.exe」負責讀入,並等待人下一步的應對。
3.要做的工作就是,完成「wzq.cpp」 中「qxwz()」函數的設計,使之能返回一個比較好的下棋位置。在設計過程中,可以增加全局變數以保存你認為有必要保存的數據,可以增加其它文件,用來保存你認為下次調用還有必要使用的數據,可以增加其它函數以方便下棋位置的計算過程。總之,所給出的「wzq.cpp」程序只是一個框架,在不影響其功能的情況下,可以隨意更改。

Ⅷ 求一個用C語言編寫五子棋程序

#include<stdio.h>
#define N 10
void welcome();
void initqipan();
void showqi(int i);
void save(int p);
void panan(int p);
void heqi();
void over();
int zouqihang();
int zouqilie();
/******************結構體*****************/
struct zuobiao
{
int x[N*N];
int y[N*N];
}wei[N*N];
/******************主函數*****************/
void main()
{
int p=0;
welcome();
initqipan();
for(p=1;p<=N*N;p++)
{
wei[p].x[p]=zouqihang();
wei[p].y[p]=zouqilie();
save(p);
showqi(p);
panan(p);
}
if(p==N*N)
heqi();
over();
}
/******************建立棋盤*****************/
void initqipan()
{
int i,j;
for(i=0;i<N;i++)
{
printf("%d",i);
printf(" ");
}
printf("\n");
for(i=1;i<N;i++)
{
for(j=0;j<N;j++)
{
if(j==0)
printf("%d",i);
else
printf("·");
}
printf("\n");
}
}
/******************顯示棋子*****************/
void showqi(int p)
{
int i,j,k,m;
int a[N*N],b[N*N];
FILE *fp;
fp=fopen("wuzi_list","rb");
for(i=1;i<=N*N;i++)
{
fread(&wei[i],sizeof(struct zuobiao),1,fp);
a[i]=wei[i].x[i];
b[i]=wei[i].y[i];
}
for(m=1;m<p;m++)
{
while(wei[p].x[p]==a[m]&&wei[p].y[p]==b[m])
{
printf("error!\n");
wei[p].x[p]=zouqihang();
wei[p].y[p]=zouqilie();
m=1;
}
}
for(i=0;i<N;i++)
{
printf("%d",i);
printf(" ");
}
printf("\n");
for(i=1;i<N;i++)
{
for(j=1;j<N;j++)
{
if(j==1)
printf("%d",i);
for(k=1;k<=p;k++)
{
if(i==wei[k].x[k]&&j==wei[k].y[k])
{
if(k%2==1)
{printf("○");break;}
else if(k%2==0)
{printf("●");break;}
}
}
if(k>p)printf("·");
else continue;
}
printf("\n");
}
}
/******************走棋行*****************/
int zouqihang()
{
int x;
printf("請輸入要走棋子所在行數!\n");
printf("x=");
scanf("%d",&x);
while(x>N-1||x<1)
{
printf("error!\n");
printf("請輸入要走棋子所在行數!\n");
printf("x=");
scanf("%d",&x);
}
return x;
}
/******************走棋列*****************/
int zouqilie()
{
int y;
printf("請輸入要走棋子所在列數!\n");
printf("y=");
scanf("%d",&y);
while(y>N-1||y<1)
{
printf("error!\n");
printf("請輸入要走棋子所在列數!\n");
printf("y=");
scanf("%d",&y);
}
return y;
}
/******************文件保存*****************/
void save(int i)
{
FILE *fp;
fp=fopen("wuzi_list","wb");
fwrite(&wei[i],sizeof(struct zuobiao),1,fp);
}
/****************判斷輸贏*******************/
void panan(int p)
{
int i,j,k[8]={1,1,1,1,1,1,1,1,};
int a[N*N],b[N*N];
FILE *fp;
fp=fopen("wuzi_list","rb");
for(i=1;i<=p;i++)
{
fread(&wei[i],sizeof(struct zuobiao),1,fp);
a[i]=wei[i].x[i];
b[i]=wei[i].y[i];
}
/*****************判斷行******************/
for(i=1;i<=p;i++)
{
if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j])&&(b[i]==b[j]-1))
{
k[0]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-2))
{
k[0]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-3))
{
k[0]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-4))
{
k[0]++;
continue;
}
else if(k[0]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[0]==5)
break;
k[0]=1;
}
else if(k[0]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j])&&(b[i]==b[j]-1))
{
k[1]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-2))
{
k[1]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-3))
{
k[1]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-4))
{
k[1]++;
continue;
}
else if(k[1]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[1]==5)
break;
k[1]=1;
}
}
/**********************判斷列************************/
for(i=1;i<=p;i++)
{
if(k[0]==5||k[1]==5)
break;
else if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if(k[2]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[2]==5)
break;
k[2]=1;
}
else if(k[2]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if(k[3]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[3]==5)
break;
k[3]=1;
}
}
/****************判斷對角(左上-右下)******************/
for(i=1;i<=p;i++)
{
if(k[0]==5||k[1]==5||k[2]==5||k[3]==5)
break;
else if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]-1))
{
k[4]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]-2))
{
k[4]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]-3))
{
k[4]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]-4))
{
k[4]++;
continue;
}
else if(k[4]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[4]==5)
break;
k[4]=1;
}
else if(k[2]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]-1))
{
k[5]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]-2))
{
k[5]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]-3))
{
k[5]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]-4))
{
k[5]++;
continue;
}
else if(k[5]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[5]==5)
break;
k[5]=1;
}
}
/**********判斷對角(左下-右上)************/
for(i=1;i<=p;i++)
{
if(k[0]==5||k[1]==5||k[2]==5||k[3]==5||k[4]==5||k[5]==5)
break;
else if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j]+1)&&(b[i]==b[j]-1))
{
k[6]++;
continue;
}
else if((a[i]==a[j]+2)&&(b[i]==b[j]-2))
{
k[6]++;
continue;
}
else if((a[i]==a[j]+3)&&(b[i]==b[j]-3))
{
k[6]++;
continue;
}
else if((a[i]==a[j]+4)&&(b[i]==b[j]-4))
{
k[6]++;
continue;
}
else if(k[6]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[6]==5)
break;
k[6]=1;
}
else if(k[6]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j]+1)&&(b[i]==b[j]-1))
{
k[7]++;
continue;
}
else if((a[i]==a[j]+2)&&(b[i]==b[j]-2))
{
k[7]++;
continue;
}
else if((a[i]==a[j]+3)&&(b[i]==b[j]-3))
{
k[7]++;
continue;
}
else if((a[i]==a[j]+4)&&(b[i]==b[j]-4))
{
k[7]++;
continue;
}
else if(k[7]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[7]==5)
break;
k[7]=1;
}
}
}
/****************和棋*******************/
void heqi()
{
printf("************************************\n");
printf(" Tie!!!\n");
printf("************************************\n");
}
/****************游戲結束*******************/
void over()
{
printf("************************************\n");
printf(" game over!!!\n");
printf("************************************\n");
}
/****************游戲開始*******************/
void welcome()
{
printf("************************************\n");
printf(" Welcome!!!\n");
printf("************************************\n");
}

Ⅸ 五子棋c程序設計

#include<stdio.h>
#define N 10
void welcome();
void initqipan();
void showqi(int i);
void save(int p);
void panan(int p);
void heqi();
void over();
int zouqihang();
int zouqilie();
/******************結構體*****************/
struct zuobiao
{
int x[N];
int y[N];
}wei[N];
/******************主函數*****************/
void main()
{
int p=0;
welcome();
initqipan();
for(p=1;p<=N;p++)
{
wei[p].x[p]=zouqihang();
wei[p].y[p]=zouqilie();
save(p);
showqi(p);
panan(p);
}
if(p==N)
heqi();
over();
}
/******************建立棋盤*****************/
void initqipan()
{
int i,j;
for(i=0;i<N;i++)
{
printf("%d",i);
printf(" ");
}
printf("\n");
for(i=1;i<N;i++)
{
for(j=0;j<N;j++)
{
if(j==0)
printf("%d",i);
else
printf("·");
}
printf("\n");
}
}
/******************顯示棋子*****************/
void showqi(int p)
{
int i,j,k,m;
int a[N],b[N];
FILE *fp;
fp=fopen("wuzi_list","rb");
for(i=1;i<=N;i++)
{
fread(&wei[i],sizeof(struct zuobiao),1,fp);
a[i]=wei[i].x[i];
b[i]=wei[i].y[i];
}
for(m=1;m<p;m++)
{
while(wei[p].x[p]==a[m]&&wei[p].y[p]==b[m])
{
printf("錯誤!\n");
wei[p].x[p]=zouqihang();
wei[p].y[p]=zouqilie();
m=1;
}
}
for(i=0;i<N;i++)
{
printf("%d",i);
printf(" ");
}
printf("\n");
for(i=1;i<N;i++)
{
for(j=1;j<N;j++)
{
if(j==1)
printf("%d",i);
for(k=1;k<=p;k++)
{
if(i==wei[k].x[k]&&j==wei[k].y[k])
{
if(k%2==1)
{printf("○");
break;}
else if(k%2==0)
{printf("●");
break;}
}
}
if(k>p)printf("·");
else continue;
}
printf("\n");
}
}
/******************走棋行*****************/
int zouqihang()
{
int x;
printf("請輸入要走棋子所在行數!\n");
printf("x=");
scanf("%d",&x);
while(x>N-1||x<1)
{
printf("錯誤!\n");
printf("請輸入要走棋子所在行數!\n");
printf("x=");
scanf("%d",&x);
}
return x;
}
/******************走棋列*****************/
int zouqilie()
{
int y;
printf("請輸入要走棋子所在列數!\n");
printf("y=");
scanf("%d",&y);
while(y>N-1||y<1)
{
printf("錯誤!\n");
printf("請輸入要走棋子所在列數!\n");
printf("y=");
scanf("%d",&y);
}
return y;
}
/******************文件保存*****************/
void save(int i)
{
FILE *fp;
fp=fopen("wuzi_list","wb");
fwrite(&wei[i],sizeof(struct zuobiao),1,fp);
}
/****************判斷輸贏*******************/
void panan(int p)
{
int i,j,k[8]={1,1,1,1,1,1,1,1,};
int a[N],b[N];
FILE *fp;
fp=fopen("wuzi_list","rb");
for(i=1;i<=p;i++)
{
fread(&wei[i],sizeof(struct zuobiao),1,fp);
a[i]=wei[i].x[i];
b[i]=wei[i].y[i];
}
/*****************判斷行******************/
for(i=1;i<=p;i++)
{
if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j])&&(b[i]==b[j]-1))
{
k[0]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-2))
{
k[0]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-3))
{
k[0]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-4))
{
k[0]++;
continue;
}
else if(k[0]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[0]==5)
break;
k[0]=1;
}
else if(k[0]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j])&&(b[i]==b[j]-1))
{
k[1]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-2))
{
k[1]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-3))
{
k[1]++;
continue;
}
else if((a[i]==a[j])&&(b[i]==b[j]-4))
{
k[1]++;
continue;
}
else if(k[1]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[1]==5)
break;
k[1]=1;
}
}
/**********************判斷列************************/
for(i=1;i<=p;i++)
{
if(k[0]==5||k[1]==5)
break;
else if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]))
{
k[2]++;
continue;
}
else if(k[2]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[2]==5)
break;
k[2]=1;
}
else if(k[2]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]))
{
k[3]++;
continue;
}
else if(k[3]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[3]==5)
break;
k[3]=1;
}
}
/****************判斷對角(左上-右下)******************/
for(i=1;i<=p;i++)
{
if(k[0]==5||k[1]==5||k[2]==5||k[3]==5)
break;
else if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]-1))
{
k[4]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]-2))
{
k[4]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]-3))
{
k[4]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]-4))
{
k[4]++;
continue;
}
else if(k[4]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[4]==5)
break;
k[4]=1;
}
else if(k[2]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j]-1)&&(b[i]==b[j]-1))
{
k[5]++;
continue;
}
else if((a[i]==a[j]-2)&&(b[i]==b[j]-2))
{
k[5]++;
continue;
}
else if((a[i]==a[j]-3)&&(b[i]==b[j]-3))
{
k[5]++;
continue;
}
else if((a[i]==a[j]-4)&&(b[i]==b[j]-4))
{
k[5]++;
continue;
}
else if(k[5]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[5]==5)
break;
k[5]=1;
}
}
/**********判斷對角(左下-右上)************/
for(i=1;i<=p;i++)
{
if(k[0]==5||k[1]==5||k[2]==5||k[3]==5||k[4]==5||k[5]==5)
break;
else if(i%2==1)
{
for(j=1;j<=p;j=j+2)
{
if((a[i]==a[j]+1)&&(b[i]==b[j]-1))
{
k[6]++;
continue;
}
else if((a[i]==a[j]+2)&&(b[i]==b[j]-2))
{
k[6]++;
continue;
}
else if((a[i]==a[j]+3)&&(b[i]==b[j]-3))
{
k[6]++;
continue;
}
else if((a[i]==a[j]+4)&&(b[i]==b[j]-4))
{
k[6]++;
continue;
}
else if(k[6]==5)
{
printf("Player 1 wins!!!\n");
}
else
continue;
}
if(k[6]==5)
break;
k[6]=1;
}
else if(k[6]==5)
break;
else if(i%2==0)
{
for(j=2;j<=p;j=j+2)
{
if((a[i]==a[j]+1)&&(b[i]==b[j]-1))
{
k[7]++;
continue;
}
else if((a[i]==a[j]+2)&&(b[i]==b[j]-2))
{
k[7]++;
continue;
}
else if((a[i]==a[j]+3)&&(b[i]==b[j]-3))
{
k[7]++;
continue;
}
else if((a[i]==a[j]+4)&&(b[i]==b[j]-4))
{
k[7]++;
continue;
}
else if(k[7]==5)
{
printf("Player 2 wins!!!\n");
}
else
continue;
}
if(k[7]==5)
break;
k[7]=1;
}
}
}
/****************和棋*******************/
void heqi()
{
printf("平局\n");
}
/****************游戲結束*******************/
void over()
{
printf("游戲結束!!!\n");
}
/****************游戲開始*******************/
void welcome()
{
printf("歡迎使用!!!\n");
}

這個是10x10的,你改一下了就行 別忘了給分哦

熱點內容
壓縮殼脫殼機 發布:2024-09-21 03:14:33 瀏覽:93
熱血街籃為什麼是伺服器維護中 發布:2024-09-21 03:08:19 瀏覽:937
喇叭怎麼配置功放 發布:2024-09-21 03:06:50 瀏覽:751
為什麼安卓的內存沒有蘋果的內存 發布:2024-09-21 03:06:50 瀏覽:231
swift解壓 發布:2024-09-21 02:31:47 瀏覽:704
移動中心怎麼配置安卓系統 發布:2024-09-21 02:27:16 瀏覽:606
安卓手機舊版app怎麼下載 發布:2024-09-21 02:12:35 瀏覽:799
方舟知道ip怎麼加入伺服器 發布:2024-09-21 02:12:14 瀏覽:791
像素工廠如何加入遠程伺服器 發布:2024-09-21 02:11:00 瀏覽:824
手機服務密碼怎麼獲取 發布:2024-09-21 02:10:55 瀏覽:253