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

加载更多