Ⅰ 用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;

}

}

Ⅱ C語言 游戲 代碼

「坑人的無限」(一):

#include<iostream>
#include<windows.h>
#include<ctime>
#include<cstdlib>
#include<conio.h>
using namespace std;
int a;
class Screen
{
private:
int n;
public:
Screen()
{
n=5;
}
void move1()//注意只是循環輸出各個數字,不能對循環輸出再進行循環(如果對循環輸出0123456789再進行循環,則move1就變成一個無限循環的函數,則下面的screen循環就進行不下去了)
{
for(int i=0;i<10;++i)
{
cout<<char(1)<<" ";
}
}
void move2()
{
char i;
for(i='a';i<='z';++i)
{
cout<<char(1)<<" ";
}
}
void screen()
{
int t;
while(!kbhit())
{
t=time(0)%(2*n);//如果是放在循環外面的話,time(0)的值就一直不變,放在循環裡面,一秒鍾進行一次判斷,一秒鍾進行一次循環
if(t<n)
move1();
else
move2();
}
}
};
int main(){
cout<<"歡迎來到「無限 」游戲"<<char(1)<<endl;
cout<<"下面會輸出無限個笑臉"<<char(1)<<endl;
cout<<"按'enter'取消"<<endl;
Sleep(4000);
Screen s;
s.screen();
cout<<endl<<"哈哈!!控制不住了吧!"<<char(1)<<endl;
cout<<"接下來會更讓你喪心病狂的!"<<char(1)<<endl;
cout<<"但是堅持過後必有彩蛋!!!!!!加油!!";
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
Sleep(10000);
for(int as=0;as<=50;as++){
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(2)<<" ";
}
cout<<endl;
}
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(1)<<" ";
}
cout<<endl;
}
}
cout<<"你居然堅持下來了!"<<char(1)<<" "<<char(2)<<endl<<"不可思議呀!"<<endl;
for(int i=0;i<=10;i++){
cout<<"-----------------------------------------------"<<endl;
}
cout<<"敬請期待!等待無限游戲(二)!";
return 0;
}

Ⅲ c語言各種小游戲的代碼,能發給我嗎

DEV C++寫的

#include<windows.h>
#include<io.h>
#include<direct.h>
#include<string.h>

struct_finddata_tFile[100];
charcStr[100][260];
intfilenum=0,picnum=0;
HDChDC=NULL,mDC=NULL;
HBITMAPhBitmap=NULL;
intFirst_Run=0;
intxpos,ypos;
voidCALLBACKmovewindow(HWNDhWnd,UINTnMsg,UINTnTimerid,DWORDdwTime)
{
POINTpoi;
LPRECTinfo;
GetCursorPos(&poi);
MoveWindow(hWnd,(poi.x-xpos),(poi.y-ypos),371,342,FALSE);
return;
}


voidfindbmp()
{
longhandle;
handle=_findfirst("E:\DancingMiku\Picture\*.bmp",&File[0]);
if(handle==-1L)
{
MessageBox(NULL,"Thereisnofilewithbmpinthe「E:/DancingMiku」!!","Warn!",MB_OKCANCEL);
exit(0);
}
else
{
picnum++;
while(_findnext(handle,&File[picnum])==0)
picnum++;
}
inti;
picnum--;
for(i=0;i<=picnum;i++)
{
sprintf(cStr[i],"E:\DancingMiku\Picture\%s",File[i].name);
}
}


voidCALLBACKTimer(HWNDhWnd,UINTnMsg,UINTnTimerid,DWORDdwTime)
{
hBitmap=LoadImage(NULL,cStr[filenum%picnum],IMAGE_BITMAP,204,200,LR_LOADFROMFILE);
SelectObject(mDC,hBitmap);
BitBlt(hDC,0,0,204,200,mDC,0,0,SRCCOPY);
DeleteObject(hBitmap);
//DeleteDC(mDC);
//DeleteDC(hDC);
filenum++;
return;
}
voidCALLBACKTimer2(HWNDhWnd,UINTnMsg,UINTnTimerid,DWORDdwTime)
{
KillTimer(hWnd,1);
SetTimer(hWnd,1,30,Timer);
}
LRESULTCALLBACKWinProc(HWNDhWnd,UINTMsg,WPARAMwParam,LPARAMlParam)
{
switch(Msg)
{
caseWM_RBUTTONUP:
{
if(First_Run==0)
{
First_Run=1;
MessageBox(NULL,"這是一個測試的軟體,你的點擊意味著某些事情即將發生,至於是什麼事情,哼哼...","提醒",MB_OKCANCEL);
}
break;

}

caseWM_LBUTTONDOWN:
{
POINTtempos;
GetCursorPos(&tempos);
ScreenToClient(hWnd,&tempos);
xpos=tempos.x;
ypos=tempos.y;
SetTimer(hWnd,3,0,movewindow);
break;
}
caseWM_LBUTTONUP:
{
KillTimer(hWnd,3);
break;
}

}
returnDefWindowProc(hWnd,Msg,wParam,lParam);
}

intWINAPIWinMain(HINSTANCEhInstance,
HINSTANCEhPreInstance,
LPSTRIpCmdLine,
intnShowCmd)
{
char*cName="DancingMiku";
WNDCLASSEXmywindow;
HWNDhWnd;
MSGMsg;
HMENUhMenu;


mywindow.cbClsExtra=0;
mywindow.cbWndExtra=0;
mywindow.cbSize=sizeof(WNDCLASSEX);
mywindow.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
mywindow.hCursor=LoadImage(NULL,"E:\DancingMiku\Pointer\Arrow.ani",IMAGE_CURSOR,0,0,LR_LOADFROMFILE);
mywindow.hIcon=LoadImage(NULL,"E:\DancingMiku\Icon\01.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
mywindow.hIconSm=LoadImage(NULL,"E:\DancingMiku\Icon\01.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
mywindow.hInstance=hInstance;
mywindow.lpfnWndProc=WinProc;
mywindow.lpszClassName=(LPSTR)cName;
mywindow.lpszMenuName=NULL;
mywindow.style=CS_HREDRAW;

RegisterClassEx(&mywindow);

// hMenu=CreatePopupMenu();
// AppendMenu(hMenu,MF_ENABLED,MF_STRING,"New");
intheight,width;
height=GetSystemMetrics(SM_CYSCREEN);
width=GetSystemMetrics(SM_CXSCREEN);
hWnd=CreateWindowEx((WS_EX_TOPMOST|WS_EX_TOOLWINDOW|WS_EX_LAYERED),cName,"DancingMiku",(WS_SIZEBOX|WS_THICKFRAME),(width-50),100,371,324,NULL,NULL,hInstance,NULL);
if(hWnd==NULL)
{
MessageBox(hWnd,"Thereisaerror!!","warn",MB_OKCANCEL);
return0;
}


//SetLayeredWindowAttributes(hWnd,RGB(165,201,247),210,LWA_COLORKEY);
SetLayeredWindowAttributes(hWnd,RGB(255,255,255),200,(LWA_ALPHA|LWA_COLORKEY));
SetWindowLong(hWnd,GWL_STYLE,GetWindowLong(hWnd,GWL_STYLE)&(~(WS_CAPTION|WS_SIZEBOX)));
ShowWindow(hWnd,nShowCmd);
//下面是搜索文件的
if(_access("E:\DancingMiku",0)==-1)
{
intresult;
result=MessageBox(hWnd,"Thereisaerror!ShouldIcreatefilesformyself?","warn",MB_YESNO);
switch(result)
{
caseIDYES:
{
_mkdir("E:\DancingMiku\");
_mkdir("E:\DancingMiku\Picture\");
_mkdir("E:\DancingMiku\Pointer\");
_mkdir("E:\DancingMiku\Icon\");
break;
}
caseIDNO:
{
MessageBox(hWnd,"Nofilemeansnoruning!","Warn",MB_OK);
return0;
}
}
}
elsefindbmp();
if(hDC==NULL)
hDC=GetDC(hWnd);
if(mDC==NULL)
mDC=CreateCompatibleDC(hDC);
SetTimer(hWnd,2,1000,Timer2);
UpdateWindow(hWnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
returnMsg.message;
}

Ⅳ 求C語言小游戲源程序

簡易「推箱子」游戲C代碼:

#include <stdio.h>

#include <conio.h>

#include<stdlib.h>

#include<windows.h>

int m =0; //m代表第幾關

struct maps{short a[9][11]; };

struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5關,每關9行

0,1,1,1,1,1,1,1,0,0,0,

0,1,0,0,0,0,0,1,1,1,0,

1,1,4,1,1,1,0,0,0,1,0, //0空地,1牆

1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人

1,0,3,3,1,0,4,0,1,1,0, //3是目的地

1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)

0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,

0,0,1,1,1,1,0,0,0,0,0,

0,0,1,5,0,1,1,1,0,0,0,

0,0,1,0,4,0,0,1,0,0,0,

0,1,1,1,0,1,0,1,1,0,0,

0,1,3,1,0,1,0,0,1,0,0,

0,1,3,4,0,0,1,0,1,0,0,

0,1,3,0,0,0,4,0,1,0,0,

0,1,1,1,1,1,1,1,1,0,0,

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,1,1,1,1,1,1,1,0,

0,0,1,1,0,0,1,0,5,1,0,

0,0,1,0,0,0,1,0,0,1,0,

0,0,1,4,0,4,0,4,0,1,0,

0,0,1,0,4,1,1,0,0,1,0,

1,1,1,0,4,0,1,0,1,1,0,

1,3,3,3,3,3,0,0,1,0,0,

1,1,1,1,1,1,1,1,1,0,0,

0,1,1,1,1,1,1,1,1,1,0,

0,1,0,0,1,1,0,0,0,1,0,

0,1,0,0,0,4,0,0,0,1,0,

0,1,4,0,1,1,1,0,4,1,0,

0,1,0,1,3,3,3,1,0,1,0,

1,1,0,1,3,3,3,1,0,1,1,

1,0,4,0,0,4,0,0,4,0,1,

1,0,0,0,0,0,1,0,5,0,1,

1,1,1,1,1,1,1,1,1,1,1,

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,1,1,1,1,1,1,0,0,

0,1,1,1,0,0,0,0,1,0,0,

1,1,3,0,4,1,1,0,1,1,0,

1,3,3,4,0,4,0,0,5,1,0,

1,3,3,0,4,0,4,0,1,1,0,

1,1,1,1,1,1,0,0,1,0,0,

0,0,0,0,0,1,1,1,1,0,0,

0,0,0,0,0,0,0,0,0,0,0 };

void DrMap( ) //繪制地圖

{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隱藏游標的設置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

printf(" 推箱子");

printf(" ");

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

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

{switch (map[m].a[i][j])

{case 0: printf(" "); break;

case 1: printf("■"); break;

case 3: printf("◎");break;

case 4: printf("□"); break;

case 5: printf("♀"); break; //5是人

case 7: printf("□"); break; //4 + 3箱子在目的地中

case 8: printf("♀");break; // 5 + 3人在目的地中

}

}

printf(" ");

}

}

void gtxy(int x, int y) //控制游標位置的函數

{ COORD coord;

coord.X = x;

coord.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void start( ) //開始游戲

{ int r, c; //存儲人的下標

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

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

{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下標

}

char key;

key = getch( );

switch (key)

{case 'W':

case 'w':

case 72:

if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)

{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置輸出字元

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)

{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)

{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;

map[m]. a [r][c] -= 5; }

} break;

case 'S':

case 's':

case 80:

if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)

{ gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)

{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)

{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'A':

case 'a':

case 75:

if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)

{ gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)

{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)

{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'D':

case 'd':

case 77:

if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)

{ gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)

{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)

{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

}

}

int ifwan( ) //是否完成(1是0否)

{ if(m==0){if(map[m].a[5][2]==7&& map[m].a[5][3]==7&&

map[m].a[6][2]==7&& map[m].a[6][3]==7) return 1;}

if(m==1){if(map[m].a[5][2]==7&& map[m].a[6][2]==7&&

map[m].a[7][2]==7) return 1;}

if(m==2){if(map[m].a[7][1]==7&& map[m].a[7][2]==7&& map[m].a[7][3]==7&&

map[m].a[7][4]==7&& map[m].a[7][5]==7) return 1;}

if(m==3){if(map[m].a[4][4]==7&& map[m].a[4][5]==7&& map[m].a[4][6]==7&&

map[m].a[5][4]==7&& map[m].a[5][5]==7&& map[m].a[5][6]==7) return 1;}

if(m==4){if(map[m].a[3][2]==7&& map[m].a[4][1]==7&& map[m].a[4][2]==7&&

map[m].a[5][1]==7&& map[m].a[5][2]==7) return 1;}

return 0;

}

int main( ) //主函數

{ while (1)

{ system("cls");

DrMap( );

while (1)

{ start( );

if(ifwan()){printf("07");break;} //完成後響鈴

}

m+=1;

}

return 0;

}

Ⅳ 用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];

};

(5)c游戲代碼大全擴展閱讀:

設計思路

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

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

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

Ⅵ c語言小游戲代碼

「貪吃蛇」C代碼,在dev C++試驗通過(用4個方向鍵控制)

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <Windows.h>

#define W 78 //游戲框的寬,x軸

#define H 26 //游戲框的高,y軸

int dir=3; //方向變數,初值3表示向「左」

int Flag=0; //吃了食物的標志(1是0否)

int score=0; //玩家得分

struct food{ int x; //食物的x坐標

int y; //食物的y坐標

}fod; //結構體fod有2個成員

struct snake{ int len; //蛇身長

int speed; //移動速度

int x[100]; //蛇身某節x坐標

int y[100]; //蛇身某節y坐標

}snk; //結構體snk有4個成員

void gtxy( int x,int y) //控制游標移動的函數

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y); //以下聲明要用到的幾個自編函數

void csh( ); //初始化界面

void keymove( ); //按鍵操作移動蛇

void putFod( ); //投放食物

int Over( ); //游戲結束(1是0否)

void Color(int a); //設定顯示顏色的函數

int main( ) //主函數

{ csh( );

while(1)

{ Sleep(snk.speed);

keymove( );

putFod( );

if(Over( ))

{ system(「cls」);

gtxy(W/2+1,H/2); printf(「游戲結束!T__T」);

gtxy(W/2+1,H/2+2); printf(「玩家總分:%d分」,score);

getch( );

break;

}

}

return 0;

}

void csh( ) //初始化界面

{ int i;

gtxy(0,0);

CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下兩行是隱藏游標的設置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<=W;i=i+2) //橫坐標要為偶數,因為這個要列印的字元佔2個位置

{Color(2); //設定列印顏色為綠色

gtxy(i,0); printf("■"); //列印上邊框

gtxy(i,H); printf("■"); //列印下邊框

}

for(i=1;i<H;i++)

{ gtxy(0,i); printf("■"); //列印左邊框

gtxy(W,i); printf("■"); //列印右邊框

}

while(1)

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

fod.x=rand()%(W-4)+2; //隨機函數rand( )產生一個從0到比」(W-4)」小1的數再加2

fod.y=rand()%(H-2)+1; //隨機函數rand( )產生一個從0到比」(H-2)」小1的數再加1

if (fod.x%2==0) break; //fod.x是食物的橫坐標,要是2的倍數(為偶數)

}

Color(12); //設定列印顏色為淡紅

gtxy(fod.x,fod.y); printf("●"); //到食物坐標處列印初試食物

snk.len=3; //蛇身長初值為3節

snk.speed=350; //刷新蛇的時間,即移動速度初值為350毫秒

snk.x[0]=W/2+1; //蛇頭橫坐標要為偶數(因為W/2=39)

snk.y[0]=H/2; //蛇頭縱坐標

Color(9); //設定列印顏色為淡藍

gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭

for(i=1;i<snk.len;i++)

{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];

gtxy(snk.x[i],snk.y[i]); printf("■"); //列印蛇身

}

Color(7, 0); //恢復默認的白字黑底

return;

}

void keymove( ) //按鍵操作移動蛇

{ int key;

if( kbhit( ) ) //如有按鍵輸入才執行下面操作

{ key=getch( );

if (key==224) //值為224表示按下了方向鍵,下面要再次獲取鍵值

{ key=getch( );

if(key==72&&dir!=2)dir=1; //72表示按下了向上方向鍵

if(key==80&&dir!=1)dir=2; //80為向下

if(key==75&&dir!=4)dir=3; //75為向左

if(key==77&&dir!=3)dir=4; //77為向右

}

if (key==32)

{ while(1) if((key=getch( ))==32) break; } //32為空格鍵,這兒用來暫停

}

if (Flag==0) //如沒吃食物,才執行下面操作擦掉蛇尾

{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }

int i;

for (i = snk.len - 1; i > 0; i--) //從蛇尾起每節存儲前一節坐標值(蛇頭除外)

{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }

switch (dir) //判斷蛇頭該往哪個方向移動,並獲取最新坐標值

{ case 1: snk.y[0]--; break; //dir=1要向上移動

case 2: snk.y[0]++; break; //dir=2要向下移動

case 3: snk.x[0]-=2; break; //dir=3要向左移動

case 4: snk.x[0]+=2; break; //dir=4要向右移動

}

Color(9);

gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭

if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物則執行以下操作

{ printf("7"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //7是響鈴

else Flag = 0; //沒吃到食物Flag的值為0

if(snk.speed<150) snk.speed= snk.speed+5; //作弊碼,不讓速度無限加快

}

void putFod( ) //投放食物

{ if (Flag == 1) //如吃到食物才執行以下操作,生成另一個食物

{ while (1)

{ int i,n= 1;

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

fod.x = rand( ) % (W - 4) + 2; //產生在游戲框范圍內的一個x坐標值

fod.y = rand( ) % (H - 2) + 1; //產生在游戲框范圍內的一個y坐標值

for (i = 0; i < snk.len; i++) //隨機生成的食物不能在蛇的身體上

{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }

if (n && fod.x % 2 == 0) break; //n不為0且橫坐標為偶數,則食物坐標取值成功

}

Color(12); //設定字元為紅色

gtxy(fod.x, fod.y); printf("●"); //游標到取得的坐標處列印食物

}

return;

}

int Over( ) //判斷游戲是否結束的函數

{ int i;

Color(7);

gtxy(2,H+1); printf(「暫停鍵:space.」); //以下列印一些其它信息

gtxy(2,H+2); printf(「游戲得分:%d」,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇頭觸碰左右邊界

if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇頭觸碰上下邊界

for (i = 1; i < snk.len; i++)

{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇頭觸碰自身

return 0; //沒碰到邊界及自身時就返回0

}

void Color(int a) //設定顏色的函數

{ SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),a ); }

Ⅶ 求一個用C語言編寫的小游戲代碼

/*也不知道你是什麼級別的,我是一個
新手
,剛接觸
編程語言
,以下是我自己變得一個小
程序
,在所有c語言的編譯器(vc++6.0、turbo…………)上都能運行,你還可以進一步改進。這是一個類似
貪吃蛇

小游戲

祝你好運
*/
/*貪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int
head=3
,tail=0;
int
main()
{
int
i,j,k=0;
int
zuobiao[2][80];
long
start;
int
direction=77;
int
gamespeed;
int
timeover;
int
change(char
qipan[20][80],int
zuobiao[2][80],char
direction);
zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*處理棋盤*/
char
qipan[20][80];//
定義
棋盤
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]='
';//
初始化
棋盤
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
qipan[19][i]='_';
qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置
qipan[1][4]='#';
printf("This
is
a
game
of
a
SNAKE.
GOOD
LUCK
TO
YOU
!
");
printf("Input
your
game
speed,please.(e.g.300)
");
scanf("%d",&gamespeed);
while(direction!='q')
{
system("cls");
for(i=0;i<20;i++)//列印出棋盤
for(j=0;j<80;j++)
printf("%c",qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
{
getch();
direction=getch();
}
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
{
return
0;
system("cls");
printf("GAME
OVER!
");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME
OVER!
");
}
}
return
0;
}
int
change(char
qipan[20][80],int
zuobiao[2][80],char
direction)
{
int
x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return
0;
if(qipan[x][y]!='
')
return
0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]='
';
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]='*';
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]='#';
return
1;
}

Ⅷ dev-c++中的c語言游戲代碼是什麼

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define U 1
#define D 2
#define L 3
#define R 4 //蛇的狀態,U:上 ;D:下;L:左 R:右

typedef struct SNAKE { //蛇身的一個節點
int x;
int y;
struct SNAKE *next;
} snake;

//全局變數//
int score=0,add=10;//總得分與每次吃食物得分。
int status,sleeptime=200;//每次運行的時間間隔
snake *head, *food;//蛇頭指針,食物指針
snake *q;//遍歷蛇的時候用到的指針
int endgamestatus=0; //游戲結束的情況,1:撞到牆;2:咬到自己;3:主動退出遊戲。

//聲明全部函數//
void Pos();
void creatMap();
void initsnake();
int biteself();
void createfood();
void cantcrosswall();
void snakemove();
void pause();
void gamecircle();
void welcometogame();
void endgame();
void gamestart();

void Pos(int x,int y)//設置游標位置
{
COORD pos;
HANDLE hOutput;
pos.X=x;
pos.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
}

void creatMap()//創建地圖
{
int i;
for(i=0; i<58; i+=2) { //列印上下邊框
Pos(i,0);
printf("■");
Pos(i,26);
printf("■");
}
for(i=1; i<26; i++) { //列印左右邊框
Pos(0,i);
printf("■");
Pos(56,i);
printf("■");
}
}

void initsnake()//初始化蛇身
{
snake *tail;
int i;
tail=(snake*)malloc(sizeof(snake));//從蛇尾開始,頭插法,以x,y設定開始的位置//
tail->x=24;
tail->y=5;
tail->next=NULL;
for(i=1; i<=4; i++) {
head=(snake*)malloc(sizeof(snake));
head->next=tail;
head->x=24+2*i;
head->y=5;
tail=head;
}
while(tail!=NULL) { //從頭到為,輸出蛇身
Pos(tail->x,tail->y);
printf("■");
tail=tail->next;
}
}

int biteself()//判斷是否咬到了自己
{
snake *self;
self=head->next;
while(self!=NULL) {
if(self->x==head->x && self->y==head->y) {
return 1;
}
self=self->next;
}
return 0;
}

void createfood()//隨機出現食物
{
snake *food_1;
srand((unsigned)time(NULL));
food_1=(snake*)malloc(sizeof(snake));
while((food_1->x%2)!=0) { //保證其為偶數,使得食物能與蛇頭對其
food_1->x=rand()%52+2;
}
food_1->y=rand()%24+1;
q=head;
while(q->next==NULL) {
if(q->x==food_1->x && q->y==food_1->y) { //判斷蛇身是否與食物重合
free(food_1);
createfood();
}
q=q->next;
}
Pos(food_1->x,food_1->y);
food=food_1;
printf("■");
}

void cantcrosswall()//不能穿牆
{
if(head->x==0 || head->x==56 ||head->y==0 || head->y==26) {
endgamestatus=1;
endgame();
}
}

void snakemove()//蛇前進,上U,下D,左L,右R
{
snake * nexthead;
cantcrosswall();

nexthead=(snake*)malloc(sizeof(snake));
if(status==U) {
nexthead->x=head->x;
nexthead->y=head->y-1;
if(nexthead->x==food->x && nexthead->y==food->y) { //如果下一個有食物//
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //如果沒有食物//
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(status==D) {
nexthead->x=head->x;
nexthead->y=head->y+1;
if(nexthead->x==food->x && nexthead->y==food->y) { //有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //沒有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(status==L) {
nexthead->x=head->x-2;
nexthead->y=head->y;
if(nexthead->x==food->x && nexthead->y==food->y) { //有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //沒有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(status==R) {
nexthead->x=head->x+2;
nexthead->y=head->y;
if(nexthead->x==food->x && nexthead->y==food->y) { //有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
score=score+add;
createfood();
} else { //沒有食物
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL) {
Pos(q->x,q->y);
printf("■");
q=q->next;
}
Pos(q->next->x,q->next->y);
printf(" ");
free(q->next);
q->next=NULL;
}
}
if(biteself()==1) { //判斷是否會咬到自己
endgamestatus=2;
endgame();
}
}

void pause()//暫停
{
while(1) {
Sleep(300);
if(GetAsyncKeyState(VK_SPACE)) {
break;
}

}
}

void gamecircle()//控制游戲
{

Pos(64,15);
printf("不能穿牆,不能咬到自己\n");
Pos(64,16);
printf("用↑.↓.←.→分別控制蛇的移動.");
Pos(64,17);
printf("F1 為加速,F2 為減速\n");
Pos(64,18);
printf("ESC :退出遊戲.space:暫停游戲.");
Pos(64,20);
//
status=R;
while(1) {
Pos(64,10);
printf("得分:%d ",score);
Pos(64,11);
printf("每個食物得分:%d分",add);
if(GetAsyncKeyState(VK_UP) && status!=D) {
status=U;
} else if(GetAsyncKeyState(VK_DOWN) && status!=U) {
status=D;
} else if(GetAsyncKeyState(VK_LEFT)&& status!=R) {
status=L;
} else if(GetAsyncKeyState(VK_RIGHT)&& status!=L) {
status=R;
} else if(GetAsyncKeyState(VK_SPACE)) {
pause();
} else if(GetAsyncKeyState(VK_ESCAPE)) {
endgamestatus=3;
break;
} else if(GetAsyncKeyState(VK_F1)) {
if(sleeptime>=50) {
sleeptime=sleeptime-30;
add=add+2;
if(sleeptime==320) {
add=2;//防止減到1之後再加回來有錯
}
}
} else if(GetAsyncKeyState(VK_F2)) {
if(sleeptime<350) {
sleeptime=sleeptime+30;
add=add-2;
if(sleeptime==350) {
add=1; //保證最低分為1
}
}
}
Sleep(sleeptime);
snakemove();
}
}

void welcometogame()//開始界面
{
Pos(40,12);

//
printf("歡迎來到貪食蛇游戲!");
Pos(40,25);
//
system("pause");
system("cls");
Pos(25,12);
printf("用↑.↓.←.→分別控制蛇的移動, F1 為加速,2 為減速\n");
Pos(25,13);
printf("加速將能得到更高的分數。\n");
system("pause");
system("cls");
}

void endgame()//結束游戲
{

system("cls");
Pos(24,12);
if(endgamestatus==1) {
printf("對不起,您撞到牆了。游戲結束.");
} else if(endgamestatus==2) {
printf("對不起,您咬到自己了。游戲結束.");
} else if(endgamestatus==3) {
printf("您的已經結束了游戲。");
}
Pos(24,13);
printf("您的得分是%d\n",score);
exit(0);
}

void gamestart()//游戲初始化
{
system("mode con cols=100 lines=30");
welcometogame();
creatMap();
initsnake();
createfood();
}

int main()
{
gamestart();
gamecircle();
endgame();
return 0;
}

Ⅸ 求c語言的「紙牌游戲」代碼

貪心演算法,首先計算平均值,然後從左往右掃描,不夠平均值的從右邊拿,次數回+1;超過平均值的往右邊答放,次數+1。#include
int
main(){
int
n,i,move=0;
long
a[101],num=0;
scanf("%d",&n);
for
(i=0;i
num){
a[i+1]+=(a[i]-num);
move++;
}
if
(a[i]

評論
0

0

載入更多