當前位置:首頁 » 編程語言 » c語言小程序游戲

c語言小程序游戲

發布時間: 2025-08-17 18:23:09

1. 撲克牌問題(c語言

其實這是典型的利用鏈表求解的問題,但是此題用鏈表的話還需要應用一個長度為13的數組做輔助,所以我直接用數組來進行演示,將在手中的牌進行標記,將放到桌子上的牌進行賦值。
我在很小的時候我的姥爺就曾給我表演過這個魔術,當時我自己用撲克牌弄了將近兩個小時才終於知道了這13張牌的順序是什麼,有纖姿興趣的話你可以自己試一試,很有意思。
下面的代碼你可以進行調試來幫助理解整個過程
#include <stdio.h>
void main()
{
int poker[13];//十三張撲克牌
for(int i = 0;i<13;i++)//初始化
poker[i] = 0;//0代表牌還在手中,不為0代表放到了桌子上
int remain = 13;//手中剩餘的撲克數
int j = 12;//從最下邊開始抽牌
int order = 1;//當order為2時,將此時的牌放到桌子上
int number = 1;//從A開始
while(remain != 0)
{
if(j == -1)
{
j = 12;//返回最後一張牌
continue;
}
if(poker[j] != 0)//如果牌已不在手中,則到下一張牌
{
j--;
continue;
}
if(order == 2)
{
poker[j]= number++;//將撲克賦值
remain--;//手中剩餘的牌數減1
order = 1;
continue;
}
order++;
j--;
}
//輸出一開始小明手中撲克牌的順序
for(i = 0; i < 13;i++)
printf("%d ",poker[i]);
}
想要寫出一個程序重點在於理解這個過程,只有對這個過程熟悉了旁豎慶才有可能把運握它用代碼來演示出來。

2. 會C語言的朋友幫個忙啊 幫我編個小程序

這些源碼我都有啊,差不多吧。先貼2個吧,我是在網上下的,
貪食蛇
#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/*得分*/
int gamespeed=50000;/*游戲速度自己調整*/
struct Food
{
int x;/*食物的橫坐標*/
int y;/*食物的縱坐標*/
int yes;/*判斷是否要出現食物的變數*/
}food;/*食物的結構體*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的節數*/
int direction;/*蛇移動方向*/
int life;/* 蛇的生命,0活著,1死亡*/
}snake;
void Init(void);/*圖形驅動*/
void Close(void);/*圖形結束*/
void DrawK(void);/*開始畫面*/
void GameOver(void);/*結束游戲*/
void GamePlay(void);/*玩游戲具體過程*/
void PrScore(void);/*輸出成績*/
/*主函數*/
void main(void)
{
Init();/*圖形驅動*/
DrawK();/*開始畫面*/
GamePlay();/*玩游戲具體過程*/
Close();/*圖形結束*/
}
/*圖形驅動*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
/*開始畫面,左上角坐標為(50,40),右下角坐標為(610,460)的圍牆*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設置線型*/
for(i=50;i<=600;i+=10)/*畫圍牆*/
{
rectangle(i,40,i+10,49); /*上邊*/
rectangle(i,451,i+10,460);/*下邊*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*左邊*/
rectangle(601,i,610,i+10);/*右邊*/
}
}
/*玩游戲具體過程*/
void GamePlay(void)
{
randomize();/*隨機數發生器*/
food.yes=1;/*1表示需要出現新食物,0表示已經存在食物*/
snake.life=0;/*活著*/
snake.direction=1;/*方嚮往右*/
snake.x[0]=100;snake.y[0]=100;/*蛇頭*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*節數*/
PrScore();/*輸出得分*/
while(1)/*可以重復玩游戲,壓ESC鍵結束*/
{
while(!kbhit())/*在沒有按鍵的情況下,蛇自己移動身體*/
{
if(food.yes==1)/*需要出現新食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)/*食物隨機出現後必須讓食物能夠在整格內,這樣才可以讓蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*畫面上有食物了*/
}
if(food.yes==0)/*畫面上有食物了就要顯示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)/*蛇的每個環節往前移動,也就是貪吃蛇的關鍵演算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四個方向,通過這個判斷來移動蛇頭*/
switch(snake.direction)
{
case 1:snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++)/*從蛇的第四節開始判斷是否撞到自己了,因為蛇頭為兩節,第三節不可能拐過來*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();/*顯示失敗*/
snake.life=1;
break;
}
}
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||
snake.y[0]>455)/*蛇是否撞到牆壁*/
{
GameOver();/*本次游戲結束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1)/*以上兩種判斷以後,如果蛇死就跳出內循環,重新開始*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以後*/
{
setcolor(0);/*把畫面上的食物東西去掉*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一節先放在看不見的位置,下次循環就取前一節的位置*/
snake.node++;/*蛇的身體長一節*/
food.yes=1;/*畫面上需要出現新的食物*/
score+=10;
PrScore();/*輸出新得分*/
}
setcolor(4);/*畫出蛇*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最後一節*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/
if(snake.life==1)/*如果蛇死就跳出循環*/
break;
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*按ESC鍵退出*/
break;
else
if(key==UP&&snake.direction!=4)
/*判斷是否往相反的方向移動*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戲結束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
/*輸出成績*/
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
/*圖形結束*/
void Close(void)
{
getch();
closegraph();
}

計算器
#include <dos.h> /*DOS介面函數*/
#include <math.h> /*數學函數的定義*/
#include <conio.h> /*屏幕操作函數*/
#include <stdio.h> /*I/O函數*/
#include <stdlib.h> /*庫函數*/
#include <stdarg.h> /*變數長度參數表*/
#include <graphics.h> /*圖形函數*/
#include <string.h> /*字元串函數*/
#include <ctype.h> /*字元操作函數*/
#define UP 0x48 /*游標上移鍵*/
#define DOWN 0x50 /*游標下移鍵*/
#define LEFT 0x4b /*游標左移鍵*/
#define RIGHT 0x4d /*游標右移鍵*/
#define ENTER 0x0d /*回車鍵*/
void *rar; /*全局變數,保存游標圖象*/
struct palettetype palette; /*使用調色板信息*/
int GraphDriver; /* 圖形設備驅動*/
int GraphMode; /* 圖形模式值*/
int ErrorCode; /* 錯誤代碼*/
int MaxColors; /* 可用顏色的最大數值*/
int MaxX, MaxY; /* 屏幕的最大解析度*/
double AspectRatio; /* 屏幕的像素比*/
void drawboder(void); /*畫邊框函數*/
void initialize(void); /*初始化函數*/
void computer(void); /*計算器計算函數*/
void changetextstyle(int font, int direction, int charsize); /*改變文本樣式函數*/
void mwindow(char *header); /*窗口函數*/
int specialkey(void) ; /*獲取特殊鍵函數*/
int arrow(); /*設置箭頭游標函數*/
/*主函數*/
int main()
{
initialize();/* 設置系統進入圖形模式 */
computer(); /*運行計算器 */
closegraph();/*系統關閉圖形模式返迴文本模式*/
return(0); /*結束程序*/
}
/* 設置系統進入圖形模式 */
void initialize(void)
{
int xasp, yasp; /* 用於讀x和y方向縱橫比*/
GraphDriver = DETECT; /* 自動檢測顯示器*/
initgraph( &GraphDriver, &GraphMode, "" );
/*初始化圖形系統*/
ErrorCode = graphresult(); /*讀初始化結果*/
if( ErrorCode != grOk ) /*如果初始化時出現錯誤*/
{
printf("Graphics System Error: %s\n",
grapherrormsg( ErrorCode ) ); /*顯示錯誤代碼*/
exit( 1 ); /*退出*/
}
getpalette( &palette ); /* 讀面板信息*/
MaxColors = getmaxcolor() + 1; /* 讀取顏色的最大值*/
MaxX = getmaxx(); /* 讀屏幕尺寸 */
MaxY = getmaxy(); /* 讀屏幕尺寸 */
getaspectratio( &xasp, &yasp ); /* 拷貝縱橫比到變數中*/
AspectRatio = (double)xasp/(double)yasp;/* 計算縱橫比值*/
}
/*計算器函數*/
void computer(void)
{
struct viewporttype vp; /*定義視口類型變數*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*操作數和計算結果變數*/
char cnum[5],str2[20]={""},c,temp[20]={""};
char str1[]="1230.456+-789*/Qc=^%";/* 定義字元串在按鈕圖形上顯示的符號 */
mwindow( "Calculator" ); /* 顯示主窗口 */
color = 7; /*設置灰顏色值*/
getviewsettings( &vp ); /* 讀取當前窗口的大小*/
width=(vp.right+1)/10; /* 設置按鈕寬度 */
height=(vp.bottom-10)/10 ; /*設置按鈕高度 */
x = width /2; /*設置x的坐標值*/
y = height/2; /*設置y的坐標值*/
setfillstyle(SOLID_FILL, color+3);
bar( x+width*2, y, x+7*width, y+height );
/*畫一個二維矩形條顯示運算數和結果*/
setcolor( color+3 ); /*設置淡綠顏色邊框線*/
rectangle( x+width*2, y, x+7*width, y+height );
/*畫一個矩形邊框線*/
setcolor(RED); /*設置顏色為紅色*/
outtextxy(x+3*width,y+height/2,"0."); /*輸出字元串"0."*/
x =2*width-width/2; /*設置x的坐標值*/
y =2*height+height/2; /*設置y的坐標值*/
for( j=0 ; j<4 ; ++j ) /*畫按鈕*/
{
for( i=0 ; i<5 ; ++i )
{
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x+width, y+height ); /*畫一個矩形條*/
rectangle( x, y, x+width, y+height );
sprintf(str2,"%c",str1[j*5+i]);
/*將字元保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =x+width+ (width / 2) ; /*移動列坐標*/
}
y +=(height/2)*3; /* 移動行坐標*/
x =2*width-width/2; /*復位列坐標*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y); /*移動游標到x,y位置*/
arrow(); /*顯示游標*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); /*設置str2為空串*/
while((v=specialkey())!=45) /*當壓下Alt+x鍵結束程序,否則執行下面的循環*/
{
while((v=specialkey())!=ENTER) /*當壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT); /*顯示游標圖象*/
if(v==RIGHT) /*右移箭頭時新位置計算*/
if(x>=x0+6*width)
/*如果右移,移到尾,則移動到最左邊字元位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} /*否則,右移到下一個字元位置*/
if(v==LEFT) /*左移箭頭時新位置計算*/
if(x<=x0)
{
x=x0+6*width;
m=4;
} /*如果移到頭,再左移,則移動到最右邊字元位置*/
else
{
x=x-width-width/2;
m--;
} /*否則,左移到前一個字元位置*/
if(v==UP) /*上移箭頭時新位置計算*/
if(y<=y0)
{
y=y0+4*height+height/2;
n=3;
} /*如果移到頭,再上移,則移動到最下邊字元位置*/
else
{
y=y-height-height/2;
n--;
} /*否則,移到上邊一個字元位置*/
if(v==DOWN) /*下移箭頭時新位置計算*/
if(y>=7*height)
{
y=y0;
n=0;
} /*如果移到尾,再下移,則移動到最上邊字元位置*/
else
{
y=y+height+height/2;
n++;
} /*否則,移到下邊一個字元位置*/
putimage(x,y,rar,XOR_PUT); /*在新的位置顯示游標箭頭*/
}
c=str1[n*5+m]; /*將字元保存到變數c中*/
if(isdigit(c)||c=='.') /*判斷是否是數字或小數點*/
{
if(flag==-1) /*如果標志為-1,表明為負數*/
{
strcpy(str2,"-"); /*將負號連接到字元串中*/
flag=1;
} /*將標志值恢復為1*/
sprintf(temp,"%c",c); /*將字元保存到字元串變數temp中*/
strcat(str2,temp); /*將temp中的字元串連接到str2中*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,str2); /*顯示字元串*/
}
if(c=='+')
{
num1=atof(str2); /*將第一個操作數轉換為浮點數*/
strcpy(str2,""); /*將str2清空*/
act=1; /*做計算加法標志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*顯示字元串*/
}
if(c=='-')
{
if(strcmp(str2,"")==0) /*如果str2為空,說明是負號,而不是減號*/
flag=-1; /*設置負數標志*/
else
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,""); /*將str2清空*/
act=2; /*做計算減法標志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*畫矩形*/
outtextxy(5*width,height,"0."); /*顯示字元串*/
}
}
if(c=='*')
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,""); /*將str2清空*/
act=3; /*做計算乘法標志值*/
setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*顯示字元串*/
}
if(c=='/')
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,""); /*將str2清空*/
act=4; /*做計算除法標志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*顯示字元串*/
}
if(c=='^')
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,""); /*將str2清空*/
act=5; /*做計算乘方標志值*/
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*畫矩形*/
outtextxy(5*width,height,"0."); /*顯示字元串*/
}
if(c=='%')
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,""); /*將str2清空*/
act=6; /*做計算模運算乘方標志值*/
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*畫矩形*/
outtextxy(5*width,height,"0."); /*顯示字元串*/
}
if(c=='=')
{
num2=atof(str2); /*將第二個操作數轉換為浮點數*/
switch(act) /*根據運算符號計算*/
{
case 1:result=num1+num2;break; /*做加法*/
case 2:result=num1-num2;break; /*做減法*/
case 3:result=num1*num2;break; /*做乘法*/
case 4:result=num1/num2;break; /*做除法*/
case 5:result=pow(num1,num2);break; /*做x的y次方*/
case 6:result=fmod(num1,num2);break; /*做模運算*/
}
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆蓋結果區*/
sprintf(temp,"%f",result); /*將結果保存到temp中*/
outtextxy(5*width,height,temp); /*顯示結果*/
}
if(c=='c')
{
num1=0; /*將兩個操作數復位0,符號標志為1*/
num2=0;
flag=1;
strcpy(str2,""); /*將str2清空*/
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆蓋結果區*/
outtextxy(5*width,height,"0."); /*顯示字元串*/
}
if(c=='Q')exit(0); /*如果選擇了q回車,結束計算程序*/
}
putimage(x,y,rar,XOR_PUT); /*在退出之前消去游標箭頭*/
return; /*返回*/
}
/*窗口函數*/
void mwindow( char *header )
{
int height;
cleardevice(); /* 清除圖形屏幕 */
setcolor( MaxColors - 1 ); /* 設置當前顏色為白色*/
setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); /* 設置視口大小 */
height = textheight( "H" ); /* 讀取基本文本大小 */
settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*設置文本樣式*/
settextjustify( CENTER_TEXT, TOP_TEXT );/*設置字元排列方式*/
outtextxy( MaxX/4, 2, header ); /*輸出標題*/
setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*設置視口大小*/
drawboder(); /*畫邊框*/
}
void drawboder(void) /*畫邊框*/
{
struct viewporttype vp; /*定義視口類型變數*/
setcolor( MaxColors - 1 ); /*設置當前顏色為白色 */
setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*設置畫線方式*/
getviewsettings( &vp );/*將當前視口信息裝入vp所指的結構中*/
rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*畫矩形邊框*/
}
/*設計滑鼠圖形函數*/
int arrow()
{
int size;
int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*定義多邊形坐標*/
setfillstyle(SOLID_FILL,2); /*設置填充模式*/
fillpoly(8,raw); /*畫出一游標箭頭*/
size=imagesize(4,4,16,16); /*測試圖象大小*/
rar=malloc(size); /*分配內存區域*/
getimage(4,4,16,16,rar); /*存放游標箭頭圖象*/
putimage(4,4,rar,XOR_PUT); /*消去游標箭頭圖象*/
return 0;
}
/*按鍵函數*/
int specialkey(void)
{
int key;
while(bioskey(1)==0); /*等待鍵盤輸入*/
key=bioskey(0); /*鍵盤輸入*/
key=key&0xff? key&0xff:key>>8; /*只取特殊鍵的掃描值,其餘為0*/
return(key); /*返回鍵值*/
}


3. 用C語言編寫的小游戲代碼是什麼

「猜數字小游戲」,每個數字後按空格,最後按回車確認

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int a[4],b[4];

int count=0; //計算猜測次數

void csh( ); //初始化

void start( ); //開始游戲

int main( )

{ csh( );

start( );

}

void csh( ) //初始化

{ printf(" 猜 數 字 小 游 戲 ");

printf(「 猜四個數字,如數字與順序都正確記為A,數字正確位置不對記為B. 」);

}

void start( ) //開始游戲

{int m,n; //m是完全猜對的個數,n是順序不對的個數

while(1)

{srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )

while(1) { for(int i=0;i<4;i++) a[i]=rand( )%10; //rand( )函數每次隨機產生一個0-9的數

if( (a[3]!=a[2]&&a[3]!=a[1]&&a[3]!=a[0])&&

(a[2]!=a[1]&&a[2]!=a[0])&&a[1]!=a[0] ) break; } //4個隨機數各自不相等

printf(" 請依次輸入4個一位整數: ");

while(1)

{for(int i=0;i<4;i++) scanf(「%d」,&b[i]);

printf(" 你輸入的是:%d %d %d %d ",b[0],b[1],b[2],b[3]);

m=0;n=0;

for(int i=0;i<4;i++)

{for(int j=0;j<4;j++)

{ if(b[i]==a[j]&&i==j)m=m+1; if(b[i]==a[j]&&i!=j)n=n+1; }

}

count=count+1;

printf(" %dA %dB 你試了%d次 ",m,n,count);

if(m==4)break;

if(count==8){ count=0; break; }

}

printf(" ");

if(m==4)printf(" 你猜對了(^-^)! 就是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

else printf(" 你輸了(T-T)!哈哈!應該是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

int z;

printf(" (要繼續嗎?1或0) ");

scanf(「%d」,&z);

if(z==0) break;

}

}

4. 用C++編寫的小游戲源代碼

五子棋的代碼:

#include<iostream>

#include<stdio.h>

#include<stdlib.h>

#include <time.h>

using namespace std;

const int N=15; //15*15的棋盤

const char ChessBoardflag = ' '; //棋盤標志

const char flag1='o'; //玩家1或電腦的棋子標志

const char flag2='X'; //玩家2的棋子標志

typedef struct Coordinate //坐標類

{

int x; //代錶行

int y; //代表列

}Coordinate;

class GoBang //五子棋類

{

public:

GoBang() //初始化

{

InitChessBoard();

}

void Play() //下棋

{

Coordinate Pos1; // 玩家1或電腦

Coordinate Pos2; //玩家2

int n = 0;

while (1)

{

int mode = ChoiceMode();

while (1)

{

if (mode == 1) //電腦vs玩家

{

ComputerChess(Pos1,flag1); // 電腦下棋

if (GetVictory(Pos1, 0, flag1) == 1) //0表示電腦,真表示獲勝

break;

PlayChess(Pos2, 2, flag2); //玩家2下棋

if (GetVictory(Pos2, 2, flag2)) //2表示玩家2

break;

}

else //玩家1vs玩家2

{

PlayChess(Pos1, 1, flag1); // 玩家1下棋

if (GetVictory(Pos1, 1, flag1)) //1表示玩家1

break;

PlayChess(Pos2, 2, flag2); //玩家2下棋

if (GetVictory(Pos2, 2, flag2)) //2表示玩家2

break;

}

}

cout << "***再來一局***" << endl;

cout << "y or n :";

char c = 'y';

cin >> c;

if (c == 'n')

break;

}

}

protected:

int ChoiceMode() //選擇模式

{

int i = 0;

system("cls"); //系統調用,清屏

InitChessBoard(); //重新初始化棋盤

cout << "***0、退出 1、電腦vs玩家 2、玩家vs玩家***" << endl;

while (1)

{

cout << "請選擇:";

cin >> i;

if (i == 0) //選擇0退出

exit(1);

if (i == 1 || i == 2)

return i;

cout << "輸入不合法" << endl;

}

}

void InitChessBoard() //初始化棋盤

{

for (int i = 0; i < N + 1; ++i)

{

for (int j = 0; j < N + 1; ++j)

{

_ChessBoard[i][j] = ChessBoardflag;

}

}

}

void PrintChessBoard() //列印棋盤,這個函數可以自己調整

{

system("cls"); //系統調用,清空屏幕

for (int i = 0; i < N+1; ++i)

{

for (int j = 0; j < N+1; ++j)

{

if (i == 0) //列印列數字

{

if (j!=0)

printf("%d ", j);

else

printf(" ");

}

else if (j == 0) //列印行數字

printf("%2d ", i);

else

{

if (i < N+1)

{

printf("%c |",_ChessBoard[i][j]);

}

}

}

cout << endl;

cout << " ";

for (int m = 0; m < N; m++)

{

printf("--|");

}

cout << endl;

}

}

void PlayChess(Coordinate& pos, int player, int flag) //玩家下棋

{

PrintChessBoard(); //列印棋盤

while (1)

{

printf("玩家%d輸入坐標:", player);

cin >> pos.x >> pos.y;

if (JudgeValue(pos) == 1) //坐標合法

break;

cout << "坐標不合法,重新輸入" << endl;

}

_ChessBoard[pos.x][pos.y] = flag;

}

void ComputerChess(Coordinate& pos, char flag) //電腦下棋

{

PrintChessBoard(); //列印棋盤

int x = 0;

int y = 0;

while (1)

{

x = (rand() % N) + 1; //產生1~N的隨機數

srand((unsigned int) time(NULL));

y = (rand() % N) + 1; //產生1~N的隨機數

srand((unsigned int) time(NULL));

if (_ChessBoard[x][y] == ChessBoardflag) //如果這個位置是空的,也就是沒有棋子

break;

}

pos.x = x;

pos.y = y;

_ChessBoard[pos.x][pos.y] = flag;

}

int JudgeValue(const Coordinate& pos) //判斷輸入坐標是不是合法

{

if (pos.x > 0 && pos.x <= N&&pos.y > 0 && pos.y <= N)

{

if (_ChessBoard[pos.x][pos.y] == ChessBoardflag)

{

return 1; //合法

}

}

return 0; //非法

}

int JudgeVictory(Coordinate pos, char flag) //判斷有沒有人勝負(底層判斷)

{

int begin = 0;

int end = 0;

int begin1 = 0;

int end1 = 0;

//判斷行是否滿足條件

(pos.y - 4) > 0 ? begin = (pos.y - 4) : begin = 1;

(pos.y + 4) >N ? end = N : end = (pos.y + 4);

for (int i = pos.x, j = begin; j + 4 <= end; j++)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i][j + 1] == flag&&

_ChessBoard[i][j + 2] == flag&&_ChessBoard[i][j + 3] == flag&&

_ChessBoard[i][j + 4] == flag)

return 1;

}

//判斷列是否滿足條件

(pos.x - 4) > 0 ? begin = (pos.x - 4) : begin = 1;

(pos.x + 4) > N ? end = N : end = (pos.x + 4);

for (int j = pos.y, i = begin; i + 4 <= end; i++)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j] == flag&&

_ChessBoard[i + 2][j] == flag&&_ChessBoard[i + 3][j] == flag&&

_ChessBoard[i + 4][j] == flag)

return 1;

}

int len = 0;

//判斷主對角線是否滿足條件

pos.x > pos.y ? len = pos.y - 1 : len = pos.x - 1;

if (len > 4)

len = 4;

begin = pos.x - len; //橫坐標的起始位置

begin1 = pos.y - len; //縱坐標的起始位置

pos.x > pos.y ? len = (N - pos.x) : len = (N - pos.y);

if (len>4)

len = 4;

end = pos.x + len; //橫坐標的結束位置

end1 = pos.y + len; //縱坐標的結束位置

for (int i = begin, j = begin1; (i + 4 <= end) && (j + 4 <= end1); ++i, ++j)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j + 1] == flag&&

_ChessBoard[i + 2][j + 2] == flag&&_ChessBoard[i + 3][j + 3] == flag&&

_ChessBoard[i + 4][j + 4] == flag)

return 1;

}

//判斷副對角線是否滿足條件

(pos.x - 1) >(N - pos.y) ? len = (N - pos.y) : len = pos.x - 1;

if (len > 4)

len = 4;

begin = pos.x - len; //橫坐標的起始位置

begin1 = pos.y + len; //縱坐標的起始位置

(N - pos.x) > (pos.y - 1) ? len = (pos.y - 1) : len = (N - pos.x);

if (len>4)

len = 4;

end = pos.x + len; //橫坐標的結束位置

end1 = pos.y - len; //縱坐標的結束位置

for (int i = begin, j = begin1; (i + 4 <= end) && (j - 4 >= end1); ++i, --j)

{

if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j - 1] == flag&&

_ChessBoard[i + 2][j - 2] == flag&&_ChessBoard[i + 3][j - 3] == flag&&

_ChessBoard[i + 4][j - 4] == flag)

return 1;

}

for (int i = 1; i < N + 1; ++i) //棋盤有沒有下滿

{

for (int j =1; j < N + 1; ++j)

{

if (_ChessBoard[i][j] == ChessBoardflag)

return 0; //0表示棋盤沒滿

}

}

return -1; //和棋

}

bool GetVictory(Coordinate& pos, int player, int flag) //對JudgeVictory的一層封裝,得到具體那個玩家獲勝

{

int n = JudgeVictory(pos, flag); //判斷有沒有人獲勝

if (n != 0) //有人獲勝,0表示沒有人獲勝

{

PrintChessBoard();

if (n == 1) //有玩家贏棋

{

if (player == 0) //0表示電腦獲勝,1表示玩家1,2表示玩家2

printf("***電腦獲勝*** ");

else

printf("***恭喜玩家%d獲勝*** ", player);

}

else

printf("***雙方和棋*** ");

return true; //已經有人獲勝

}

return false; //沒有人獲勝

}

private:

char _ChessBoard[N+1][N+1];

};

(4)c語言小程序游戲擴展閱讀:

設計思路

1、進行問題分析與設計,計劃實現的功能為,開局選擇人機或雙人對戰,確定之後比賽開始。

2、比賽結束後初始化棋盤,詢問是否繼續比賽或退出,後續可加入復盤、悔棋等功能。

3、整個過程中,涉及到了棋子和棋盤兩種對象,同時要加上人機對弈時的AI對象,即涉及到三個對象。

5. 給我提供個小游戲的C 語言代碼

本原代碼是基於C語言的原程序。是經典中的小游戲。-primitive code is based on the C language of the original procere. Classic is a small game.
一個小游戲,用C語言編寫的:俄羅斯方塊.C原碼及應用程序都在裡面哦 -a small game using the C language : Russian cubes. The original C code and application proceres inside oh
十全十美游戲原程序,c語言-perfect game program, language c
上數據結構時,自己用C語言做的小游戲,做得不好,請大家原諒。-structure on the data they used C language to the small games, done well, please forgive me.
大家都耍過文曲星中的猜數字的游戲吧 !! 最近我在學習C語言。寫了個菜鳥的C語言的猜數字的游戲程序的原代碼-rings off the viewing of the game! ! I recently learning C language. Wrote a birdie C language viewing of the games original code proceres
這是我在大學二年級學習C語言課程時,作為「練筆」而編寫的一個小程序(當時在我眼裡可卻是一個大程序!)其主要特點有:1、正真做到了全中文界面(不需要UCDOS操作系支持) 2、大量的圖形特技(如圖像的顯隱技術、圖像穿插技術、多任務模擬技術等) 3、純C語言打造(不含任何C++知識) 4、實現了街機「俄羅斯方塊」絕大部分功能(如動畫、聲音、速度變化) 5、用戶可根據自據的習慣自由地調整「游戲操作鍵」 6、方法獨特,全部語句和技術都是我本人原創,沒有參考過任何相關代碼 7、防「跟蹤調試」技術,防「版權篡改」技術 8、……-
這個程序是模仿Windows中的掃雷小游戲製作的,該程序只是實現了掃雷游戲的主體部分,諸如計分、升級部分都沒有做。這個程序可以作為初學者學習C語言繪圖和游戲的實例。 該程序在Turbo C2.0 下編譯通過 由於掃雷游戲是用滑鼠操作的,而Turbo C中提供的滑鼠驅動程序在Windows xp下不可用,所以我隨源程序提供了一個滑鼠驅動的頭文件,須將將該頭文件復制到Turbo C2.0 的安裝目錄下的「include」文件夾中方可編譯或運行,也可自行修改原文件使之包含該投文件。 註:該滑鼠驅動程序是我在網上找到的,其出處我已無法考證,如果侵犯了作者的權利還請作者與我聯系。 由於在我的電腦上Turbo C圖形環境下顯示數字會有問題(估計是系統問題),所以程序中雷周圍的數字1-8我用a-h代替,看不順眼的可以自己修改原程序。-
c語言 猜拳游戲的原代碼就是這個 已經測試成功了呀-language of the original game is the code has been tested successfully ah
俄羅斯方塊對戰版c語言原代碼。希望大家能喜歡。是比較簡單的一個代碼,游戲開發高手請指教。-Tetris screen version of the original C language code. Hope you will like. It is a relatively simple code, game development experts please advise.
用linuX 下的C語言 運用CURSES編寫的俄羅斯方塊游戲,很好,這個是本人原創,值得參考-linuX use the C language CURSES prepared by the Russian box game, well, this is the original, worthy of reference

6. c語言小程序--黑白棋

你想做什麼?
人人對戰的代碼我有,但是人機對戰的AI寫不好
是個國際難題
//=====othello=====
#include<iostream>
#include<cstdio>

using namespace std;

int board[8][8],saveboard[60][8][8];
int cx,cy,col,pass,empty,black,white;

void init(){ //initialization
memset(board,-1,sizeof(board));
board[3][3]=0;
board[3][4]=1;
board[4][4]=0;
board[4][3]=1;
col=0;
pass=0;
empty=60;
black=2;
white=2;
}

int input(){
char s[1000]="";
scanf("%s",&s);
if(s[0]>='a' && s[0]<='h')
cy=s[0]-'a';
else if(s[0]>='A' && s[0]<='H')
cy=s[0]-'A';
else return 0;
if(s[1]>='1' && s[1]<='8'){
cx=s[1]-'1';
return 1;
}
return 0;
}

int judge(int x,int y){
int i,j,temp;
temp=(col+1)%2;
//left && up
if(board[x-1][y-1]==temp){
for(i=x-1,j=y-1; i>=0 && j>=0; i--,j--){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//up
if(board[x-1][y]==temp){
for(i=x-1; i>=0; i--){
if(board[i][y]<0) break;
if(col==board[i][y]) return 1;
}
}
//right && up
if(board[x-1][y+1]==temp){
for(i=x-1,j=y+1; i>=0 && j<8; i--,j++){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//right
if(board[x][y+1]==temp){
for(j=y+1; j<8; j++){
if(board[x][j]<0) break;
if(col==board[x][j]) return 1;
}
}
//right && down
if(board[x+1][y+1]==temp){
for(i=x+1,j=y+1; i<8 && j<8; i++,j++){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//down
if(board[x+1][y]==temp){
for(i=x+1; i<8; i++){
if(board[i][y]<0) break;
if(col==board[i][y]) return 1;
}
}
//left && down
if(board[x+1][y-1]==temp){
for(i=x+1,j=y-1; i<8 && j>=0; i++,j--){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//left
if(board[x][y-1]==temp){
for(j=y-1; j>=0; j--){
if(board[x][j]<0) break;
if(col==board[x][j]) return 1;
}
}
return 0;
}

void move(int x,int y){
int i,j,temp,count;
temp=(col+1)%2;
count=0;
//left && up
if(board[x-1][y-1]==temp){
for(i=x-1,j=y-1; i>=0 && j>=0; i--,j--){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[++i][++j]=col;
count++;
}
count--;
break;
}
}
}
//up
if(board[x-1][y]==temp){
for(i=x-1; i>=0; i--){
if(board[i][y]<0) break;
if(col==board[i][y]){
while(i!=x){
board[++i][y]=col;
count++;
}
count--;
break;
}
}
}
//right && up
if(board[x-1][y+1]==temp){
for(i=x-1,j=y+1; i>=0 && j<8; i--,j++){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[++i][--j]=col;
count++;
}
count--;
break;
}
}
}
//right
if(board[x][y+1]==temp){
for(j=y+1; j<8; j++){
if(board[x][j]<0) break;
if(col==board[x][j]){
while(j!=y){
board[x][--j]=col;
count++;
}
count--;
break;
}
}
}
//right && down
if(board[x+1][y+1]==temp){
for(i=x+1,j=y+1; i<8 && j<8; i++,j++){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[--i][--j]=col;
count++;
}
count--;
break;
}
}
}
//down
if(board[x+1][y]==temp){
for(i=x+1; i<8; i++){
if(board[i][y]<0) break;
if(col==board[i][y]){
while(i!=x){
board[--i][y]=col;
count++;
}
count--;
break;
}
}
}
//left && down
if(board[x+1][y-1]==temp){
for(i=x+1,j=y-1; i<8 && j>=0; i++,j--){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[--i][++j]=col;
count++;
}
count--;
break;
}
}
}
//left
if(board[x][y-1]==temp){
for(j=y-1; j>=0; j--){
if(board[x][j]<0) break;
if(col==board[x][j]){
while(j!=y){
board[x][++j]=col;
count++;
}
count--;
break;
}
}
}
board[x][y]=col;
if(col){
black+=count;
white-=count;
black++;
}
else{
black-=count;
white+=count;
white++;
}
empty--;
}

void output(){
char c;
printf(" ");
for(int i=0; i<8; i++){
c='A'+i;
printf("%2c",c);
}
printf("\n");

for(int i=0; i<8; i++){
printf("%d",i+1);
for(int j=0; j<8; j++){
if(board[i][j]==-1)
c=' ';
else if(board[i][j]==0)
c='O';
else
c='X';
printf("%2c",c);
}
printf("\n");
}
printf("Black:%3d White:%3d\n",black,white);
}

int passjudge(){
int f=0;
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
if(board[i][j]<0)
f+=judge(i,j);
return f;
}

void startprint(){
printf("1、New game\n2、setboard\n0、Exit\n");
}

void pvp(){
while(empty && pass<2){
//black or white
col++;
col%=2;
output();
//input
if(!input()){
if(!passjudge()){
printf("Pass!\n");
pass++;
}
else {
col++;
printf("No pass!\nPlease input right stone!\n");
}
continue;
}
if(judge(cx,cy)){
move(cx,cy);
pass=0;
}
else{
col++;
printf("Miss stone\n");
}
}
output();
if(black>white)
printf("Black Win!\n");
else if(black<white)
printf("White Win!\n");
else
printf("Draw Game!\n");
startprint();
}

void setboard(){
char c;
for(int i=0; i<8; i++)
for(int j=0; scanf("%c",&c) && c!='\n'; j++){
if(j>=8) continue;
if(c=='o' && c=='O')
board[i][j]=0;
else if(c=='x' && c=='X')
board[i][j]=1;
}
printf("White start or Black start?\n(W/B)");
scanf("%c",&c);
if(c=='w' || c=='W')
col=1;
if(c=='b' || c=='B')
col=0;
}

int main(int argc, char* argv[]){
int n;
startprint();
while(scanf("%d",&n) && n){
init();
if(n==1)
pvp();
if(n==2){
setboard();
pvp();
}
}
return 0;
}

熱點內容
我的世界國服pvp伺服器ip地址 發布:2025-08-17 19:45:35 瀏覽:497
聊城電腦伺服器 發布:2025-08-17 19:34:59 瀏覽:407
互聯網編程語言 發布:2025-08-17 19:18:40 瀏覽:851
python主流框架 發布:2025-08-17 19:11:51 瀏覽:176
開源海量文件存儲 發布:2025-08-17 19:07:05 瀏覽:193
帶密碼的發票有什麼用 發布:2025-08-17 18:53:18 瀏覽:689
免費php模板下載 發布:2025-08-17 18:47:31 瀏覽:240
ubuntuphp開發 發布:2025-08-17 18:34:44 瀏覽:499
c語言小程序游戲 發布:2025-08-17 18:23:09 瀏覽:801
ios今日頭條源碼 發布:2025-08-17 18:23:02 瀏覽:309