C語言編程基礎,簡單的五子棋小遊戲
C語言是面向過程的,而C++是面向對象的。C和C++的區別:
C是一個結構化語言,它的重點在於演算法和數據結構。C程序的設計首要考慮的是如何通過一個過程,對輸入(或環境條件)進行運算處理得到輸出(或實現過程(事務)控制)。
C++,首要考慮的是如何構造一個對象模型,讓這個模型能夠契合與之對應的問題域,這樣就可以通過獲取對象的狀態信息得到輸出或實現過程(事務)控制。 所以C與C++的最大區別在於它們的用於解決問題的思想方法不一樣。之所以說C++比C更先進,是因為「 設計這個概念已經被融入到C++之中 」。
C與C++的最大區別:在於它們的用於解決問題的思想方法不一樣。之所以說C++比C更先進,是因為「 設計這個概念已經被融入到C++之中 」,而就語言本身而言,在C中更多的是演算法的概念。那麼是不是C就不重要了,錯!演算法是程序設計的基礎,好的設計如果沒有好的演算法,一樣不行。而且,「C加上好的設計」也能寫出非常好的東西。
為了提高大家的興趣,可以把這段代碼運行下。
# include
# include
# include
# include//插入輸入輸出頭文件
# include//字元數組的函數定義的頭文件
# include//stdlib 頭文件即standard library標準庫頭文件stdlib 頭文件里包含了C、C++語言的最常用的系統函數該 ,文件包含了C語言標準庫函數的定義.
# define SPA 0//在C或C++語言源程序中允許用一個標識符來表示一個字元串,稱為「宏」。 「define」為宏定義命令。
# define MAN 1
# define COM 2
/* 空位置設為0 ,玩家下的位置設為1 ,電腦下的位置設為2 */
#define ORDEX 15
int qipan[ORDEX][ORDEX]; //自己定義的預處理函數,以二維數組形式構建棋盤 /* 15*15的棋盤 */
typedef struct//typedef的作用是在C中定義一個結構體類型
{
int iFlag;
int iX, iY;
}ScmPiece;
int g_iPieceLen;
ScmPiece g_ScmGame1, g_ScmGame2;
void start(); /* 程序的主要控制函數 */
void draw(); /* 畫棋盤 */
int Victory( ScmPiece * pScmPiece ); /* 判斷勝利 p q為判斷點坐標 */
void AI(int *p,int *q); /* 電腦下子 p q返回下子坐標 */
int value(int p,int q); /* 計算空點p q的價值 */
int qixing(int n,int p,int q); /* 返回空點p q在n方向上的棋型 n為1-8方向 從右順時針開始數 */
void yiwei(int n,int *i,int *j); /* 在n方向上對坐標 i j 移位 n為1-8方向 從右順時針開始數 */
void InitGame(){
int i;
g_iPieceLen = ORDEX * ORDEX;
g_ScmGame1.iX = 0;
g_ScmGame1.iY = 0;
g_ScmGame1.iFlag = COM;
g_ScmGame2.iX = 7;
g_ScmGame2.iY = 7;
g_ScmGame2.iFlag = MAN;
for( i=0; i
*( (int*)qipan + i ) = SPA;
}
int main()
{
小編推薦一個學C語言/C++的學習裙【 二六三,六八八,二七六 】,無論你是大牛還是小白,是想轉行還是想入行都可以來了解一起進步一起學習!裙內有開發工具,很多乾貨和技術資料分享!
char k;
do{
InitGame();
start();
printf("還要再來一把嗎?輸入y或n:"); getchar(); scanf("%c",&k);
while(k!="y"&&k!="n"){ printf("輸入錯誤,請重新輸入
"); scanf("%c",&k); }
system("cls");
}while(k=="y");
printf("謝謝使用!
");
return 0;
}
void MakePiece( ScmPiece * pScmPiece, int iGameFlag ){
if( iGameFlag )
{
printf("請下子:");
while( scanf( "%d%d", &pScmPiece->iX, &pScmPiece->iY ) )
{
if( ( pScmPiece->iX iX > ORDEX-1 ) || ( pScmPiece->iY iY > ORDEX-1 ) )
printf( "坐標錯誤!請重新輸入:");
else if( qipan[pScmPiece->iX][pScmPiece->iY] )
printf( "該位置已有子!請重新輸入:");
else break;
}
}
qipan[pScmPiece->iX][pScmPiece->iY] = pScmPiece->iFlag;
--g_iPieceLen;
system("cls");
draw();
if( iGameFlag == 0 )
printf("電腦下在%d %d
", pScmPiece->iX, pScmPiece->iY );
}
void start()
{
int choice;
//ScmPiece ScmGameTemp1, ScmGameTemp2;/* a1 b1儲存玩家上手坐標 c1 d1儲存電腦上手坐標 */
printf(" ╔═══════════════════════════════╗
");
printf(" ║ ║
");
printf(" ║ 歡迎使用五子棋對戰程序 祝您玩的愉快挑戰無極限 ║
");
printf(" ║ ║
");
printf(" ║ ._______________________. ║
");
printf(" ║ | _____________________ | ║
");
printf(" ║ | I I | ║
");
printf(" ║ | I I | ║
");
printf(" ║ | I 五 子 棋 I | ║
");
printf(" ║ | I I | ║
");
printf(" ║ | I___________________I | ║
");
printf(" ║ !_______________________! ║
");
printf(" ║ ._[__________]_. ║
");
printf(" ║ .___|_______________|___. ║
");
printf(" ║ |::: ____ | ║
");
printf(" ║ | ~~~~ [CD-ROM] | ║
");
printf(" ║ !_____________________! ║
");
printf(" ║ ║
");
printf(" ║ ║
");
printf(" ║ 寒 星 溪 月 疏 星 首,花 殘 二 月 並 白 蓮。 ║
");
printf(" ║ 雨 月 金 星 追 黑 玉,松 丘 新 宵 瑞 山 腥。 ║
");
printf(" ║ 星 月 長 峽 恆 水 流,白 蓮 垂 俏 雲 浦 嵐。 ║
");
printf(" ║ 黑 玉 銀 月 倚 明 星,斜 月 明 月 堪 稱 朋。 ║
");
printf(" ║ 二 十 六 局 先 棄 二,直 指 游 星 斜 彗 星。 ║
");
printf(" ║ ║
");
printf(" ║ ║
");
printf(" ║ 1.人機對戰 2.人人對戰 ║
");
printf(" ║ ║
");
printf(" ╚═══════════════════════════════╝
");
printf(" 請輸入1或2:");
while( scanf( "%d", &choice ), choice!=1 && choice!=2 )
printf( "輸入錯誤,請重新輸入:" );
system("cls");
if( choice == 1 )
{ /* 人機模式 */
printf(" 歡迎使用五子棋人機對戰!
下子請輸入坐標(如13 6)中間以空格分隔。
");
draw();
printf("先下請按1,後下請按2:");
while( scanf( "%d", &choice ), choice!=1 && choice!=2 )
printf( "輸入錯 誤,請重新輸入:" );
if( choice == 2 )
MakePiece( &g_ScmGame2, 0 );
choice = 1;
}
else
{
printf("歡迎使用五子棋人人對戰!下子請輸入坐標(如13 6)。悔棋請輸入15 15。
");
draw();
}
while( g_iPieceLen )
{
MakePiece( &g_ScmGame1, 1 );
if( Victory( &g_ScmGame1 ) )
{
printf("電腦神馬的都是浮雲!!!
");
return;
} /* 玩家贏 */
if( choice == 1 )
{
AI( &g_ScmGame2.iY, &g_ScmGame2.iX );
MakePiece( &g_ScmGame2, 0 );
if( Victory( &g_ScmGame2 ) )
{ /* 電腦贏 */
int iChoice;
printf("要悔棋嗎?請輸入y或n:");
STARTFLAGE:
setbuf( stdin, NULL );
scanf( "%c", &iChoice );
switch( (char)iChoice ){
case "y":
case "Y":
g_iPieceLen += 2;
qipan[g_ScmGame2.iX][g_ScmGame2.iY] = SPA;
qipan[g_ScmGame1.iX][g_ScmGame1.iY] = SPA;
system("cls"); draw();
break;
case "n":
case "N":
printf( "下不過電腦很正常,請不要灰心!!!
" );
return;
default:
printf("輸入錯誤,請重新輸入:");
goto STARTFLAGE;
}
小編推薦一個學C語言/C++的學習裙【 二六三,六八八,二七六 】,無論你是大牛還是小白,是想轉行還是想入行都可以來了解一起進步一起學習!裙內有開發工具,很多乾貨和技術資料分享!
}
}
else
{
MakePiece( &g_ScmGame2, 1 );
if( Victory( &g_ScmGame2 ) )
{
printf("電腦神馬的都是浮雲!!!
");
return;
}
}
}
printf("和局
");
}這些是C/C++能做的
伺服器開發工程師、人工智慧、雲計算工程師、信息安全(黑客反黑客)、大數據 、數據平台、嵌入式工程師、流媒體伺服器、數據控解、圖像處理、音頻視頻開發工程師、遊戲伺服器、分散式系統、遊戲輔助等