就C语言中 猜拳游戏的代码
DOS模式下的(文字游戏):
#define SHITOU 0
#define JIANDAO 1
#define BU 2
#include
#include
#include
int main()
{
int x,y;
srand ((unsigned)time(NULL));
x = rand() % 3; //随机生成0、1、2
printf ("该你出:0-石头,1-剪刀,2-布
");
scanf ("%d", &y);
switch (x){
case SHITOU:
switch (y){
case SHITOU:
printf("电脑-石头,玩家-石头,平
");
break;
case JIANDAO:
printf("电脑-石头,玩家-剪刀,电脑赢
");
break;
case BU:
printf("电脑-石头,玩家-布,玩家赢
");
break;
}
break;
case JIANDAO:
switch (y){
case SHITOU:
printf("电脑-剪刀,玩家-石头,玩家赢
");
break;
case JIANDAO:
printf("电脑-剪刀,玩家-剪刀,平
");
break;
case BU:
printf("电脑-剪刀,玩家-布,电脑赢
");
break;
}
break;
case BU:
switch (y){
case SHITOU:
printf("电脑-布,玩家-石头,电脑赢
");
break;
case JIANDAO:
printf("电脑-布,玩家-剪刀,玩家赢
");
break;
case BU:
printf("电脑-布,玩家-布,平
");
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
#include
#include
#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
#include
#include
int main()
{
int a,b,i,s,s1;
i=1;
s=0;
s1=0;
srand((unsigned)time(NULL));
printf("猜拳游戏
0石头1剪刀2布
***********
");
printf("规则:共有5局");
printf("请
出
拳
..
");
do
{
scanf("%d",&b);
a=rand()%3;
if(a==0&&b==0)
{printf("电脑出的是拳头,您出的是拳头,平
");s=s+0;}
else if(a==0&&b==1)
printf("电脑出的是拳头,您出的是剪刀,负
");
else if(a==0&&b==2)
{printf("电脑出的是拳头,您出的是布,赢
");s=s+1;}
else if(a==1&&b==0)
{printf("电脑出的是剪刀,您出的是石头,赢
");s=s+1;}
else if(a==1&&b==1)
{printf("电脑出的是剪刀,您出的是剪刀,平
");s=s+0;}
else if(a==1&&b==2)
printf("电脑出的是剪刀,您出的是布,负
");
else if(a==2&&b==0)
printf("电脑出的是布,您出的是石头,负
");
else if(a==2&&b==1)
{printf("电脑出的是布,您出的是剪刀,赢
");s=s+1;}
else if(a==2&&b==2)
{printf("电脑出的是布,您出的是布,平
");s=s+0;}
else
{printf("存在错误
");s=s+0;}
}while(i++>5);
printf("
您的得分为:%d分
",s);
if(s>2)
printf("您赢了");
else
printf("您输了");
return 0;
}
估计这样就达到你的目的了
下面的代码会实现一个猜拳游戏,让你与电脑对决。你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。
启动程序后,让用户出拳,截图:
用户出拳,显示对决结果:截图:
代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
char gamer; // 玩家出拳
int computer; // 电脑出拳
int result; // 比赛结果
// 为了避免玩一次游戏就退出程序,可以将代码放在循环中
while (1){
printf("这是一个猜拳的小游戏,请输入你要出的拳头:\n");
printf("A:剪刀\nB:石头\nC:布\nD:不玩了\n");
scanf("%c%*c",&gamer);
switch (gamer){
case 65: //A
case 97: //a
gamer=4;
break;
case 66: //B
case 98: //b
gamer=7;
break;
case 67: //C
case 99: //c
gamer=10;
break;
case 68: //D
case 100: //d
return 0;
default:
printf("你的选择为 %c 选择错误,退出...\n",gamer);
getchar();
system("cls"); // 清屏
return 0;
break;
}
srand((unsigned)time(NULL)); // 随机数种子
computer=rand()%3; // 产生随机数并取余,得到电脑出拳
result=(int)gamer+computer; // gamer 为 char 类型,数学运算时要强制转换类型
printf("电脑出了");
switch (computer)
{
case 0:printf("剪刀\n");break; //4 1
case 1:printf("石头\n");break; //7 2
case 2:printf("布\n");break; //10 3
}
printf("你出了");
switch (gamer)
{
case 4:printf("剪刀\n");break;
case 7:printf("石头\n");break;
case 10:printf("布\n");break;
}
if (result==6||result==7||result==11) printf("你赢了!");
else if (result==5||result==9||result==10) printf("电脑赢了!");
else printf("平手");
system("pause>nul&&cls"); // 暂停并清屏
}
return 0;
}
代码分析
1) 首先,我们需要定义3个变量来储存玩家出的拳头(gamer)、电脑出的拳头(computer)和最后的结果(result),然后给出文字提示,让玩家出拳。
接下来接收玩家输入:
scanf("%c%*c",&gamer);
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
char s[3][10] = {"石头","剪刀","布"};
int main()
{
int a,b,i;
char ok[10];
srand(time(0));
while(1) {
int sheng = 0,fu = 0,ping = 0;
printf("游戏说明 : 1 代表石头 2 代表剪刀 3 代表布
");
for(i=0;i<5;i++) {
printf("【第%d局】
请出拳:",i+1);
scanf("%d",&a);
b = rand()%3+1;
printf("这一局您出的【%s】,电脑出的【%s】,",s[a-1],s[b-1]);
if(a==b) {
printf("平局!
");//请根据需要自行修改输出内容
ping ++;
}
else if(a - b%3 == 1){
sheng++;
printf("您赢了
");//请根据需要自行修改输出内容
}
else {
fu ++;
printf("您输了
");//请根据需要自行修改输出内容
}
}
printf("【猜拳结果】:");
if(sheng>fu) printf("您赢了!
");
else if(sheng == fu) printf("平局
");
else printf("您输了
");
printf("*************************************************
");
printf("您的战绩:%d胜 %d负 %d平
",sheng,fu,ping);
printf("电脑的战绩:%d胜 %d负 %d平
",fu,sheng,ping);
printf("*************************************************
");
if(sheng>fu) printf("您这次运气不错!");
else printf("您这次运气一般。");
printf("需要再来一次么(y/n)?
");
scanf("%s",ok);
if(ok[0] == 'n' || ok[0] == 'N') break;
}
printf("谢谢使用,祝您生活愉快!
");
return 0;
}
说沫三分: 这是一个简单的猜拳游戏(剪子包子锤),让你与电脑对决.你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负.下面的代码会实现一个猜拳游戏,让你与电脑对决.你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负.启动程...
昌宁县13987493543: C语言编写三局两胜的猜拳游戏编程 - ?
说沫三分: /*假设有A和B进行猜拳 'x'表示剪刀,'y'表示石头,'z'表示布 规则如下: 1、 石头 > 剪刀 2、 布 > 石头 3、 剪刀> 布 */#include <stdio.h>#define WIN 1 #define LOSE -1 #define EQUAL 0/*比赛规则函数*/ int game_rule(char a,char b) {switch(a) ...
昌宁县13987493543: 急需一个C语言 猜拳游戏的源代码!!!! - ?
说沫三分: enum p_r_s{ paper,rock,scissors,game,help,instructions,quit };#include <stdio.h> main() { enum p_r_s player,machine; enum p_r_s selection_by_player(),selection_by_machine(); int win,lose,tie; win=lose=tie=0; instructions_for_the_player(); while...
昌宁县13987493543: C 语言 猜拳 五局三胜 - ?
说沫三分: 伪代码:while( isFin ) { //是否结束 computer = random()%3; //电脑 scanf(player); //玩家 judge(computer, player, &result); //比较 shouldEnd( result, &isFin ); //分出胜负? IsFinAndWantRepeat( &isFin ); //还想再来一次吗 }
昌宁县13987493543: 猜拳游戏C语言编程 - ?
说沫三分: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #define ORIGINGOLD 100 typedef struct { char name[100]; unsigned long score; } User; void Menu() { system("cls"); printf("========================made by 小...
昌宁县13987493543: C语言问题 猜拳游戏?
说沫三分: #include"stdio.h"#include"time.h"#include"stdlib.h"void main(){ int i,wan,dian,wo=0; for(i=0;i<5;i++) { srand((unsigned)time(NULL));//随机数 dian=rand()%3; printf("请出拳(0.剪刀\t1.石头\t2.布):"); scanf("%d",&wan); if(wan==...
昌宁县13987493543: C 语言 猜拳 五局三胜 - ?
说沫三分: 伪代码: while( isFin ) { //是否结束 computer = random()%3; //电脑...
昌宁县13987493543: 猜拳游戏的代码用c语言?
说沫三分: #include <iostream> #include <windows.h> using namespace std; void first_win() { cout<<"第一个人赢!"<<endl; } void secend_win() { cout<<"第二个人赢!"<<endl; } void pingju() { cout<<"平局!"<<endl; } void out1(int i) { switch (i) { case ...
昌宁县13987493543: 怎么用C语言编出猜拳游戏?
说沫三分: 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 ("该你出...
昌宁县13987493543: C语言编程:考猜拳 - ?
说沫三分: 界面很简单. #include #include #define stone 1 #define forfex 2 #define pack 3 /*输入1、2、3分别代表,石头,剪刀,布*/ int main() { int computer; int hand; for(;;) { printf("you hand:\n"); scanf("%d",&hand); if(hand>3) { printf("****Input ...