2个回答
展开全部
DOS模式下的(文字游戏):
#define SHITOU 0
#define JIANDAO 1
#define BU 2
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int x,y;
srand ((unsigned)time(NULL));
x = rand() % 3; //随机生成0、1、2
printf ("该你出:0-石头,1-剪刀,2-布\n");
scanf ("%d", &y);
switch (x){
case SHITOU:
switch (y){
case SHITOU:
printf("电脑-石头,玩家-石头,平\n");
break;
case JIANDAO:
printf("电脑-石头,玩家-剪刀,电脑赢\n");
break;
case BU:
printf("电脑-石头,玩家-布,玩家赢\n");
break;
}
break;
case JIANDAO:
switch (y){
case SHITOU:
printf("电脑-剪刀,玩家-石头,玩家赢\n");
break;
case JIANDAO:
printf("电脑-剪刀,玩家-剪刀,平\n");
break;
case BU:
printf("电脑-剪刀,玩家-布,电脑赢\n");
break;
}
break;
case BU:
switch (y){
case SHITOU:
printf("电脑-布,玩家-石头,电脑赢\n");
break;
case JIANDAO:
printf("电脑-布,玩家-剪刀,玩家赢\n");
break;
case BU:
printf("电脑-布,玩家-布,平\n");
break;
}
break;
}
return 0;
}
WINDOWS模式下的,用MFC写成(可视,但我不会画那些图案,只好用文字代替):
(PRS.h)
#define SHITOU 0
#define JIANDAO 1
#define BU 2
class CMyApp: public CWinApp
{
public:
virtual BOOL InitInstance ();
};
class CMainWindow: public CFrameWnd
{
protected:
int m_nPlayer;
int m_nComputer;
int m_nWinner;
static CRect m_Buttons[3];
static CRect m_ViewPart[2];
static CRect m_Text[2];
public:
CMainWindow ();
protected:
int GetIndex(CPoint& point);
void Judge ();
void ComputerTurn();
void DrawGameText (CDC *pDC, int pos);
void DrawButton (CDC *pDC);
void DrawTittle (CDC *pDC);
protected:
afx_msg void OnPaint ();
afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
(PRS.cpp)
#include <afxwin.h>
#include <stdlib.h>
#include <time.h>
#include "PRS.h"
CMyApp theApp;
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_PAINT ()
ON_WM_LBUTTONDOWN ()
END_MESSAGE_MAP ()
CRect CMainWindow::m_Buttons[3] =
{
CRect (0, 540, 200, 600),
CRect (200, 540, 400, 600),
CRect (400, 540, 600, 600)
};
CRect CMainWindow::m_ViewPart[2] =
{
CRect (0, 200, 300, 540),
CRect (300, 200, 600, 540)
};
CRect CMainWindow::m_Text[2] =
{
CRect (0, 0, 300, 200),
CRect (300, 0, 600, 200)
};
CMainWindow::CMainWindow ()
{
CString strWnd = AfxRegisterWndClass (
CS_HREDRAW | CS_VREDRAW,
AfxGetApp () -> LoadStandardCursor (IDC_ARROW),
(HBRUSH)(COLOR_3DFACE + 1),
AfxGetApp () -> LoadStandardIcon (IDI_WINLOGO)
);
CreateEx (0, strWnd, _T("猜拳"),
WS_OVERLAPPED | WS_SYSMENU |
WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL);
CRect rect (0, 0, 600, 600);
CalcWindowRect (&rect);
SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height(),
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
DrawTittle(&dc);
DrawButton(&dc);
CPen pen(PS_SOLID, 5, RGB(0, 255, 255));
CPen* pOldPen = dc.SelectObject (&pen);
dc.MoveTo (300, 200);
dc.LineTo (300, 480);
dc.SelectObject (pOldPen);
}
void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
{
int i;
i = GetIndex(point);
if (i == -1)
return;
switch (i){
case SHITOU:
m_nPlayer = SHITOU;
break;
case JIANDAO:
m_nPlayer = JIANDAO;
break;
case BU:
m_nPlayer = BU;
break;
}
ComputerTurn();
Judge();
}
void CMainWindow::DrawTittle(CDC *pDC)
{
CFont font;
font.CreatePointFont (300, _T("黑体"));
pDC->SetBkMode(TRANSPARENT);
CFont* pOldFont = pDC->SelectObject (&font);
pDC->DrawText (_T("电脑"), -1, &m_Text[0], DT_CENTER | DT_SINGLELINE | DT_VCENTER);
pDC->DrawText (_T("玩家"), -1, &m_Text[1], DT_CENTER | DT_SINGLELINE | DT_VCENTER);
pDC->SelectObject (pOldFont);
}
void CMainWindow::DrawButton(CDC *pDC)
{
CBrush brushes[3];
brushes[0].CreateSolidBrush(RGB(100, 60, 30));
brushes[1].CreateSolidBrush(RGB(20, 120, 90));
brushes[2].CreateSolidBrush(RGB(30, 80, 150));
for (int i = 0; i<3; i++){
pDC->Draw3dRect(&m_Buttons[i], RGB(255, 0, 0), RGB(0, 0, 255));
pDC->FillRect(&m_Buttons[i], &brushes[i]);
}
CFont font;
font.CreatePointFont(300, _T("黑体"));
pDC->SetBkMode (TRANSPARENT);
CFont* pOldFont = pDC->SelectObject(&font);
pDC->DrawText(_T("石"), -1, &m_Buttons[0], DT_VCENTER | DT_SINGLELINE | DT_CENTER);
pDC->DrawText(_T("剪"), -1, &m_Buttons[1], DT_VCENTER | DT_SINGLELINE | DT_CENTER);
pDC->DrawText(_T("布"), -1, &m_Buttons[2], DT_VCENTER | DT_SINGLELINE | DT_CENTER);
}
void CMainWindow::ComputerTurn()
{
srand((unsigned)time(NULL));
m_nComputer = rand()%3;
}
void CMainWindow::Judge()
{
CClientDC dc(this);
CFont font;
font.CreatePointFont (500, _T("黑体"));
dc.SetTextColor(RGB(255, 0, 0));
dc.SetBkMode (TRANSPARENT);
CFont* pOldFont = dc.SelectObject(&font);
switch(m_nComputer){
case SHITOU:
dc.DrawText(_T("石头"), &m_ViewPart[0], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
dc.SetTextColor(RGB(0, 0, 255));
switch(m_nPlayer){
case SHITOU:
dc.DrawText(_T("石头"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 0;
break;
case JIANDAO:
dc.DrawText(_T("剪刀"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 1;
break;
case BU:
dc.DrawText(_T("布"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 2;
break;
}
break;
case JIANDAO:
dc.SetTextColor(RGB(255, 0, 0));
dc.DrawText(_T("剪刀"), &m_ViewPart[0], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
dc.SetTextColor(RGB(0, 0, 255));
switch(m_nPlayer){
case SHITOU:
dc.DrawText(_T("石头"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 2;
break;
case JIANDAO:
dc.DrawText(_T("剪刀"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 0;
break;
case BU:
dc.DrawText(_T("布"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 1;
break;
}
break;
case BU:
dc.SetTextColor(RGB(255, 0, 0));
dc.DrawText(_T("布"), &m_ViewPart[0], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
dc.SetTextColor(RGB(0, 0, 255));
switch(m_nPlayer){
case SHITOU:
dc.DrawText(_T("石头"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 1;
break;
case JIANDAO:
dc.DrawText(_T("剪刀"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 2;
break;
case BU:
dc.DrawText(_T("布"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 0;
break;
}
break;
}
switch(m_nWinner){
case 0:
MessageBox (_T("平局"), _T("Result"));
Invalidate();
break;
case 1:
MessageBox (_T("电脑胜"), _T("Result"));
Invalidate();
break;
case 2:
MessageBox (_T("玩家胜"), _T("Result"));
Invalidate();
break;
}
}
int CMainWindow::GetIndex(CPoint &point)
{
register int i;
for (i = 0; i < 3; i++)
if (m_Buttons[i].PtInRect(point))
return i;
return -1;
}
#define SHITOU 0
#define JIANDAO 1
#define BU 2
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int x,y;
srand ((unsigned)time(NULL));
x = rand() % 3; //随机生成0、1、2
printf ("该你出:0-石头,1-剪刀,2-布\n");
scanf ("%d", &y);
switch (x){
case SHITOU:
switch (y){
case SHITOU:
printf("电脑-石头,玩家-石头,平\n");
break;
case JIANDAO:
printf("电脑-石头,玩家-剪刀,电脑赢\n");
break;
case BU:
printf("电脑-石头,玩家-布,玩家赢\n");
break;
}
break;
case JIANDAO:
switch (y){
case SHITOU:
printf("电脑-剪刀,玩家-石头,玩家赢\n");
break;
case JIANDAO:
printf("电脑-剪刀,玩家-剪刀,平\n");
break;
case BU:
printf("电脑-剪刀,玩家-布,电脑赢\n");
break;
}
break;
case BU:
switch (y){
case SHITOU:
printf("电脑-布,玩家-石头,电脑赢\n");
break;
case JIANDAO:
printf("电脑-布,玩家-剪刀,玩家赢\n");
break;
case BU:
printf("电脑-布,玩家-布,平\n");
break;
}
break;
}
return 0;
}
WINDOWS模式下的,用MFC写成(可视,但我不会画那些图案,只好用文字代替):
(PRS.h)
#define SHITOU 0
#define JIANDAO 1
#define BU 2
class CMyApp: public CWinApp
{
public:
virtual BOOL InitInstance ();
};
class CMainWindow: public CFrameWnd
{
protected:
int m_nPlayer;
int m_nComputer;
int m_nWinner;
static CRect m_Buttons[3];
static CRect m_ViewPart[2];
static CRect m_Text[2];
public:
CMainWindow ();
protected:
int GetIndex(CPoint& point);
void Judge ();
void ComputerTurn();
void DrawGameText (CDC *pDC, int pos);
void DrawButton (CDC *pDC);
void DrawTittle (CDC *pDC);
protected:
afx_msg void OnPaint ();
afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
(PRS.cpp)
#include <afxwin.h>
#include <stdlib.h>
#include <time.h>
#include "PRS.h"
CMyApp theApp;
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_PAINT ()
ON_WM_LBUTTONDOWN ()
END_MESSAGE_MAP ()
CRect CMainWindow::m_Buttons[3] =
{
CRect (0, 540, 200, 600),
CRect (200, 540, 400, 600),
CRect (400, 540, 600, 600)
};
CRect CMainWindow::m_ViewPart[2] =
{
CRect (0, 200, 300, 540),
CRect (300, 200, 600, 540)
};
CRect CMainWindow::m_Text[2] =
{
CRect (0, 0, 300, 200),
CRect (300, 0, 600, 200)
};
CMainWindow::CMainWindow ()
{
CString strWnd = AfxRegisterWndClass (
CS_HREDRAW | CS_VREDRAW,
AfxGetApp () -> LoadStandardCursor (IDC_ARROW),
(HBRUSH)(COLOR_3DFACE + 1),
AfxGetApp () -> LoadStandardIcon (IDI_WINLOGO)
);
CreateEx (0, strWnd, _T("猜拳"),
WS_OVERLAPPED | WS_SYSMENU |
WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL);
CRect rect (0, 0, 600, 600);
CalcWindowRect (&rect);
SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height(),
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
DrawTittle(&dc);
DrawButton(&dc);
CPen pen(PS_SOLID, 5, RGB(0, 255, 255));
CPen* pOldPen = dc.SelectObject (&pen);
dc.MoveTo (300, 200);
dc.LineTo (300, 480);
dc.SelectObject (pOldPen);
}
void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
{
int i;
i = GetIndex(point);
if (i == -1)
return;
switch (i){
case SHITOU:
m_nPlayer = SHITOU;
break;
case JIANDAO:
m_nPlayer = JIANDAO;
break;
case BU:
m_nPlayer = BU;
break;
}
ComputerTurn();
Judge();
}
void CMainWindow::DrawTittle(CDC *pDC)
{
CFont font;
font.CreatePointFont (300, _T("黑体"));
pDC->SetBkMode(TRANSPARENT);
CFont* pOldFont = pDC->SelectObject (&font);
pDC->DrawText (_T("电脑"), -1, &m_Text[0], DT_CENTER | DT_SINGLELINE | DT_VCENTER);
pDC->DrawText (_T("玩家"), -1, &m_Text[1], DT_CENTER | DT_SINGLELINE | DT_VCENTER);
pDC->SelectObject (pOldFont);
}
void CMainWindow::DrawButton(CDC *pDC)
{
CBrush brushes[3];
brushes[0].CreateSolidBrush(RGB(100, 60, 30));
brushes[1].CreateSolidBrush(RGB(20, 120, 90));
brushes[2].CreateSolidBrush(RGB(30, 80, 150));
for (int i = 0; i<3; i++){
pDC->Draw3dRect(&m_Buttons[i], RGB(255, 0, 0), RGB(0, 0, 255));
pDC->FillRect(&m_Buttons[i], &brushes[i]);
}
CFont font;
font.CreatePointFont(300, _T("黑体"));
pDC->SetBkMode (TRANSPARENT);
CFont* pOldFont = pDC->SelectObject(&font);
pDC->DrawText(_T("石"), -1, &m_Buttons[0], DT_VCENTER | DT_SINGLELINE | DT_CENTER);
pDC->DrawText(_T("剪"), -1, &m_Buttons[1], DT_VCENTER | DT_SINGLELINE | DT_CENTER);
pDC->DrawText(_T("布"), -1, &m_Buttons[2], DT_VCENTER | DT_SINGLELINE | DT_CENTER);
}
void CMainWindow::ComputerTurn()
{
srand((unsigned)time(NULL));
m_nComputer = rand()%3;
}
void CMainWindow::Judge()
{
CClientDC dc(this);
CFont font;
font.CreatePointFont (500, _T("黑体"));
dc.SetTextColor(RGB(255, 0, 0));
dc.SetBkMode (TRANSPARENT);
CFont* pOldFont = dc.SelectObject(&font);
switch(m_nComputer){
case SHITOU:
dc.DrawText(_T("石头"), &m_ViewPart[0], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
dc.SetTextColor(RGB(0, 0, 255));
switch(m_nPlayer){
case SHITOU:
dc.DrawText(_T("石头"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 0;
break;
case JIANDAO:
dc.DrawText(_T("剪刀"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 1;
break;
case BU:
dc.DrawText(_T("布"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 2;
break;
}
break;
case JIANDAO:
dc.SetTextColor(RGB(255, 0, 0));
dc.DrawText(_T("剪刀"), &m_ViewPart[0], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
dc.SetTextColor(RGB(0, 0, 255));
switch(m_nPlayer){
case SHITOU:
dc.DrawText(_T("石头"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 2;
break;
case JIANDAO:
dc.DrawText(_T("剪刀"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 0;
break;
case BU:
dc.DrawText(_T("布"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 1;
break;
}
break;
case BU:
dc.SetTextColor(RGB(255, 0, 0));
dc.DrawText(_T("布"), &m_ViewPart[0], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
dc.SetTextColor(RGB(0, 0, 255));
switch(m_nPlayer){
case SHITOU:
dc.DrawText(_T("石头"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 1;
break;
case JIANDAO:
dc.DrawText(_T("剪刀"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 2;
break;
case BU:
dc.DrawText(_T("布"), &m_ViewPart[1], DT_CENTER | DT_VCENTER | DT_SINGLELINE);
m_nWinner = 0;
break;
}
break;
}
switch(m_nWinner){
case 0:
MessageBox (_T("平局"), _T("Result"));
Invalidate();
break;
case 1:
MessageBox (_T("电脑胜"), _T("Result"));
Invalidate();
break;
case 2:
MessageBox (_T("玩家胜"), _T("Result"));
Invalidate();
break;
}
}
int CMainWindow::GetIndex(CPoint &point)
{
register int i;
for (i = 0; i < 3; i++)
if (m_Buttons[i].PtInRect(point))
return i;
return -1;
}
展开全部
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
void
main()
{
int
rand_0(void);
int
game(int
inp);
int
start,yes=1,inp,inp_1=1;
char
y;
while(yes)
/*预防用户输入1或2以外的数据*/
{
printf("1:开始游戏\n2:排行榜\n");
scanf("%d",&start);
if((start!=1)&&(start!=2))
{
printf("请输入1或2\n");
}
else
yes=0;
}
start:
if(start==1)
/*如果用户选择开始游戏……*/
{
printf("你出?\n1:石头\n2:剪刀\n3:布\n");
while(inp_1)
/*预防用户输入别的数据*/
{
scanf("%d",&inp);
if((inp!=1)&&(inp!=2)&&(inp!=3))
{
printf("你出?\n1:石头\n2:剪刀\n3:布\n");
}
else
{
inp_1=0;
switch(game(inp))
{
case
1:printf("\n\n恭喜你,你赢了!\n\n");break;
case
0:printf("\n\n很遗憾,你输了!\n\n");break;
case
2:printf("\n\n平局\n\n");break;
}
}
}
}
inp_1=1;
printf("\n\n是否重新开始游戏?(y/n)");
scanf("%s",&y);
if((y=='y')||y=='Y')
goto
start;
else
return
0;}
int
rand_0(void)
/*取随机数*/
{
int
i,rand_1;
srand((unsigned)time(NULL));
for(i=1;i<=10;i++)
{
rand_1=rand()%4;
if(rand_1==0)
continue;
return(rand_1);
}
}
int
game(int
inp)
{
int
random,win;
/*win变量,1是赢,2是平,0是输*/
random=rand_0();
switch(inp)
{
case
1:if(random==3)
return
win=0;
else
if(random==2)
return
win=1;
else
return
win=2;
case
2:if(random==3)
return
win=1;
else
if(random==2)
return
win=2;
else
return
win=0;
case
3:if(random==3)
return
win=2;
else
if(random==2)
return
win=1;
else
return
win=0;
}
}就做了那么点点。。。
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
void
main()
{
int
rand_0(void);
int
game(int
inp);
int
start,yes=1,inp,inp_1=1;
char
y;
while(yes)
/*预防用户输入1或2以外的数据*/
{
printf("1:开始游戏\n2:排行榜\n");
scanf("%d",&start);
if((start!=1)&&(start!=2))
{
printf("请输入1或2\n");
}
else
yes=0;
}
start:
if(start==1)
/*如果用户选择开始游戏……*/
{
printf("你出?\n1:石头\n2:剪刀\n3:布\n");
while(inp_1)
/*预防用户输入别的数据*/
{
scanf("%d",&inp);
if((inp!=1)&&(inp!=2)&&(inp!=3))
{
printf("你出?\n1:石头\n2:剪刀\n3:布\n");
}
else
{
inp_1=0;
switch(game(inp))
{
case
1:printf("\n\n恭喜你,你赢了!\n\n");break;
case
0:printf("\n\n很遗憾,你输了!\n\n");break;
case
2:printf("\n\n平局\n\n");break;
}
}
}
}
inp_1=1;
printf("\n\n是否重新开始游戏?(y/n)");
scanf("%s",&y);
if((y=='y')||y=='Y')
goto
start;
else
return
0;}
int
rand_0(void)
/*取随机数*/
{
int
i,rand_1;
srand((unsigned)time(NULL));
for(i=1;i<=10;i++)
{
rand_1=rand()%4;
if(rand_1==0)
continue;
return(rand_1);
}
}
int
game(int
inp)
{
int
random,win;
/*win变量,1是赢,2是平,0是输*/
random=rand_0();
switch(inp)
{
case
1:if(random==3)
return
win=0;
else
if(random==2)
return
win=1;
else
return
win=2;
case
2:if(random==3)
return
win=1;
else
if(random==2)
return
win=2;
else
return
win=0;
case
3:if(random==3)
return
win=2;
else
if(random==2)
return
win=1;
else
return
win=0;
}
}就做了那么点点。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询