怎样用C++制作一个有趣的简单游戏

怎样用C++制作一个有趣的简单游戏... 怎样用C++制作一个有趣的简单游戏 展开
 我来答
花式码农
2007-11-04 · TA获得超过1.7万个赞
知道大有可为答主
回答量:4894
采纳率:0%
帮助的人:4741万
展开全部
1.简单点的:
这游戏老外管它叫 Bulls And Cows(Bulls 是 A,Cows 是 B)。
用 C++ 可以这样写:

#include <iostream>
#include <string>
#include <sstream>
#include <set>
#include <cstdlib>
#include <ctime>
using namespace std;

// Returns bulls and cows scored by guess on secretNumber.
string bullsAndCows( unsigned guess, unsigned secretNumber ) {
stringstream ssGuess,
ssSecretNumber;
ssGuess << guess;
ssSecretNumber << secretNumber;
string strGuess( ssGuess.str( ) ),
strSecretNumber( ssSecretNumber.str( ) );

unsigned bulls = 0,
cows = 0,
i;
for ( i = 0; i < strSecretNumber.length( ); ++i ) // count bulls
if ( strSecretNumber[ i ] == strGuess[ i ] ) {
++bulls;
strGuess.erase( i, 1 ); // erase to ease
strSecretNumber.erase( i, 1 ); // counting of cows
--i;
}
for ( i = 0; i < strSecretNumber.length( ); ++i ) // count cows
if ( strGuess.find( strSecretNumber[ i ] ) != string::npos ) {
++cows;
strGuess.erase( strGuess.find( strSecretNumber[ i ] ), 1 );
strSecretNumber.erase( i, 1 );
--i;
}

return string( 1, '0' + bulls ) + string( 1, '0' + cows );
}

// Returns true iff n is positive, 4-digit, contains no 0 or repeated digit.
bool isValidInput( int n ) {
stringstream ss;
ss << n;
string s = ss.str( );
return 1000 <= n && n <= 9999 &&
s.find( '0' ) == string::npos &&
set<char>( s.begin( ), s.end( ) ).size( ) == 4;
}

void main( ) {
// select a valid random secret number
srand( time( NULL ) );
unsigned secretNumber = 0;
while ( ! isValidInput( secretNumber ) )
secretNumber = rand( );

for ( unsigned guess = 0, guessCount = 1; guess != secretNumber; ++guessCount ) {
cout << "Your guess (#" << guessCount << "): ";
string input;
cin >> input;
if ( input.length( ) != 4 ||
! ( stringstream( input ) >> guess ) ||
! isValidInput( guess ) ) {
cout << "\n\tError: Input must be a positive 4-digit number without "
"0 or repeated digit.\n\n";
--guessCount;
continue;
}
string bullsCows = bullsAndCows( guess, secretNumber );
cout << "\tA: " << bullsCows[ 0 ] << ", B: " << bullsCows[ 1 ] << "\n";
}

cout << "\n\tYou've got it!\n\n";
}

复杂点的,五子棋:

#include "stdafx.h"
#include <stdio.h>
#include <math.h>

//�办跑计��
HINSTANCE hInst;
HBITMAP chess[2];
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow;
int board[10][10];
bool ptab[10][10][192];
bool ctab[10][10][192];
int win[2][192];
int num[2];
bool turn,over;
int winner;

//ㄧΑ��
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
void InitGame();
void ComTurn();

//****�祘Α**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

MyRegisterClass(hInstance);

//磅《�﹍てㄧΑ
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

//笴栏癹伴
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
tNow = GetTickCount();
if(tNow-tPre >= 100)
MyPaint(hdc);
}
}

return msg.wParam;
}

//****)竡の爹�跌怠摸�ㄧΑ*************************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "canvas";
wcex.hIconSm = NULL;

return RegisterClassEx(&wcex);
}

//****�﹍ㄧΑ*************************************
// 囱弦�钡�の㊣� InitGame() ㄧΑ秨﹍囱Ы
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HBITMAP tile,bmp;
int rowNum,colNum;
int i,x,y;

hInst = hInstance;
hWnd = CreateWindow("canvas", "酶瓜跌怠" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

MoveWindow(hWnd,10,10,480,520,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);

bmp = CreateCompatibleBitmap(hdc,450,450);
SelectObject(mdc,bmp);

tile = (HBITMAP)LoadImage(NULL,"tile.bmp",IMAGE_BITMAP,45,45,LR_LOADFROMFILE);
chess[0] = (HBITMAP)LoadImage(NULL,"chess0.bmp",IMAGE_BITMAP,38,38,LR_LOADFROMFILE);
chess[1] = (HBITMAP)LoadImage(NULL,"chess1.bmp",IMAGE_BITMAP,38,38,LR_LOADFROMFILE);

for (i=0;i<100;i++)
{
rowNum = i / 10;
colNum = i % 10;
x = colNum * 45;
y = rowNum * 45;

SelectObject(bufdc,tile);
BitBlt(mdc,x,y,45,45,bufdc,0,0,SRCCOPY);
}

InitGame();
MyPaint(hdc);

return TRUE;
}

//****囱Ы�﹍ㄧΑ***********************************
// 1.砞)囱弦�﹍�篈の莉秤�ず甧
// 2.∕)�����よ
void InitGame()
{
int i,j,k;
int count=0;

over = false;
num[0] = num[1] = 0;

//砞)�产籔筿福���莉秤舱�い�囱�计
for(i=0;i<192;i++)
{
win[0][i] = 0;
win[1][i] = 0;
}

//�﹍て囱弦�篈
for(i=0;i<10;i++)
for(j=0;j<10;j++)
board[i][j] = 2;

//砞)�キよ��莉秤舱�
for(i=0;i<10;i++)
for(j=0;j<6;j++)
{
for(k=0;k<5;k++)
{
ptab[i][j+k][count] = true;
ctab[i][j+k][count] = true;
}
count++;
}

//砞)��よ��莉秤舱�
for(i=0;i<10;i++)
for(j=0;j<6;j++)
{
for(k=0;k<5;k++)
{
ptab[j+k][i][count] = true;
ctab[j+k][i][count] = true;
}
count++;
}

//砞)タ癸à絬よ��莉秤舱�
for(i=0;i<6;i++)
for(j=0;j<6;j++)
{
for(k=0;k<5;k++)
{
ptab[j+k][i+k][count] = true;
ctab[j+k][i+k][count] = true;
}
count++;
}

//砞)は癸à絬よ��莉秤舱�
for(i=0;i<6;i++)
for(j=9;j>=4;j--)
{
for(k=0;k<5;k++)
{
ptab[j-k][i+k][count] = true;
ctab[j-k][i+k][count] = true;
}
count++;
}

//睹计∕)パê�よ��囱�
srand(GetTickCount());
if(rand()%2 == 0)
turn = true;
else
turn = false;
}

//****筿福��ㄧΑ***********************************
// 1.璸衡莉秤だ计
// 2.匡拒程ㄎ�竚秈《��笆�
void ComTurn()
{
int grades[2][10][10];
int m,n,i,max=0;
int u,v;

for(m=0;m<10;m++)
for(n=0;n<10;n++)
{
grades[0][m][n] = 0;
grades[1][m][n] = 0;

if(board[m][n] == 2)
{
for(i=0;i<192;i++)
{
//璸衡�产��囱���莉秤だ计
if(ptab[m][n][i] && win[0][i] != 7)
{
switch(win[0][i])
{
case 0:
grades[0][m][n]+=1;
break;
case 1:
grades[0][m][n]+=200;
break;
case 2:
grades[0][m][n]+=400;
break;
case 3:
grades[0][m][n]+=2000;
break;
case 4:
grades[0][m][n]+=10000;
break;
}
}

//璸衡筿福��囱���莉秤だ计
if(ctab[m][n][i] && win[1][i] != 7)
{
switch(win[1][i])
{
case 0:
grades[1][m][n]+=1;
break;
case 1:
grades[1][m][n]+=220;
break;
case 2:
grades[1][m][n]+=420;
break;
case 3:
grades[1][m][n]+=2100;
break;
case 4:
grades[1][m][n]+=20000;
break;
}
}
}

if(max == 0)
{
u = m;
v = n;
}

if(grades[0][m][n] > max)
{
max = grades[0][m][n];
u = m;
v = n;
}
else if(grades[0][m][n] == max)
{
if(grades[1][m][n] > grades[1][u][v])
{
u = m;
v = n;
}
}

if(grades[1][m][n] > max)
{
max = grades[1][m][n];
u = m;
v = n;
}
else if(grades[1][m][n] == max)
{
if(grades[0][m][n] > grades[0][u][v])
{
u = m;
v = n;
}
}
}
}

board[u][v] = 1; //砞)�筿福�囱�
num[1]++;

if(num[0] == 50 && num[1] == 50)
{
winner = 2; //キも
over = true;
}
else
for(i=0;i<192;i++)
{
if(ctab[u][v][i])
{
win[1][i]++;
ptab[u][v][i] = false;
win[0][i] = 7;

if(win[1][i] == 5)
{
winner = 1;
over = true;
}
}
}
turn = true; //传�产�
}

//****�璹酶瓜ㄧΑ*********************************
// 跌怠禟瓜の陪ボ瘪�
void MyPaint(HDC hdc)
{
int m,n;
char *str;

if(over)
{
switch(winner)
{
case 0:
str = "眤墓� ! �� F1 ��穝秈《笴栏..";
break;
case 1:
str = "筿福墓� ! �� F1 ��穝秈《笴栏..";
break;
case 2:
str = "ぃだ秤玺 ! �� F1 ��穝秈《笴栏..";
break;
}
TextOut(hdc,10,470,str,strlen(str));
}
else if(!turn) //筿福��
{
str = "筿福�σい... ";
TextOut(hdc,10,470,str,strlen(str));
ComTurn();
}
else
{
str = "赣眤��... ";
TextOut(hdc,10,470,str,strlen(str));
}

for(m=0;m<10;m++)
for(n=0;n<10;n++)
{
if(board[m][n] == 0) //禟��产囱�
{
SelectObject(bufdc,chess[0]);
BitBlt(mdc,m*45+3,n*45+3,38,38,bufdc,0,0,SRCCOPY);
}
else if(board[m][n] == 1) //禟�筿福囱�
{
SelectObject(bufdc,chess[1]);
BitBlt(mdc,m*45+3,n*45+3,38,38,bufdc,0,0,SRCCOPY);
}
else //禟���
{
SelectObject(bufdc,chess[1]);
BitBlt(mdc,m*45+3,n*45+3,38,38,bufdc,0,0,WHITENESS);
}
}

BitBlt(hdc,10,10,450,450,mdc,0,0,SRCCOPY);

tPre = GetTickCount();
}

//****瘪�矪瞶ㄧΑ***********************************
// 1.砞)�� F1 龄�穝秨﹍笴栏
// 2.矪瞶�产��菲公オ龄�囱��笆�
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int x,y,m,n,i;

switch (message)
{
case WM_KEYDOWN: //���龄瘪�
switch (wParam)
{
case VK_ESCAPE: //�� Esc 龄
PostQuitMessage( 0 );
break;
case VK_F1: //�� F1 龄
InitGame();
break;
}
case WM_LBUTTONDOWN: //��菲公オ龄瘪�
if(!over)
if(turn)
{
x = LOWORD(lParam); //�眔菲公 X 畒夹
y = HIWORD(lParam); //�眔菲公 Y 畒夹

if(x > 10 && x < 460 && y> 10 && y < 460)
{
m = (int)floor((x-10)/45);
n = (int)floor((y-10)/45);

if(board[m][n] == 2)
{
board[m][n] = 0; //砞)��产�囱�
num[0]++;

if(num[0] == 50 && num[1] == 50)
{
winner = 2; //キも
over = true;
}
else
for(i=0;i<192;i++)
{
if(ptab[m][n][i])
{
win[0][i]++;
ctab[m][n][i] = false;
win[1][i] = 7;

if(win[0][i] == 5)
{
winner = 0;
over = true;
}
}
}
turn = false; //传筿福�
}
}
}
break;
case WM_DESTROY: //跌怠挡�瘪�
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(chess[0]);
DeleteObject(chess[1]);

ReleaseDC(hWnd,hdc);

PostQuitMessage(0);
break;
default: //ㄤウ瘪�
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
智乐悦0kf
2021-01-25 · TA获得超过605个赞
知道小有建树答主
回答量:1011
采纳率:94%
帮助的人:54.5万
展开全部

使用C++,自己制作一个游戏玩!

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
whyjl4261687
2007-11-04 · TA获得超过110个赞
知道答主
回答量:157
采纳率:0%
帮助的人:89.3万
展开全部
去网上下啊.
去找找吧.
网上N多的.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式