當前位置:首頁 » 編程語言 » c語言猜拳

c語言猜拳

發布時間: 2024-02-07 13:19:13

c語言猜拳小游戲程序求助

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int get_int(void); //規范輸入的函數
int game(int y,int x); //游戲函數
int result(int m,int n); //比較最終結果的函數
int main()
{
int x,y,m,n;
int k = 0;
char q;
printf("1代表石頭;2代表剪刀;3代表布;\n");
printf("請輸入您的選擇.\n");
while(k<3)
{
scanf("%d", &y);
k++;
game(y,x);
}
result(m,n);
system("pause");
return 0;
}
int game(int y,int x)
{
int m = 0; //玩家贏的次數
int n = 0; //電腦贏的次數
srand(time(NULL));
x = rand()%3+1; //取隨機數1~3
if(y==1&&x==3)
{
printf("你出石頭\n");
printf("電腦出布\n");
printf("你輸了\n");
++n; //電腦贏的次數
}
else if(y==1&&x==1)
{
printf("大家都出石頭,平局\n");
++m;
++n;
}
else if(y==1&&x==2)
{
printf("你出石頭\n");
printf("電腦出剪刀\n");
printf("你贏了\n");
++m; //玩家贏的次數
}
if(y==2&&x==1)
{
printf("你出剪刀\n");
printf("電腦出石頭\n");
printf("你輸了\n");
++n;
}
else if(y==2&&x==2)
{
printf("大家都出剪刀,平局\n");
++m;
++n;
}
else if(y==2&&x==3)
{
printf("你出剪刀\n");
printf("電腦出布\n");
printf("你贏了\n");
++m;
}
if(y==3&&x==1)
{
printf("你出布\n");
printf("電腦石頭\n");
printf("你贏了\n");
++m;
}
else if(y==3&&x==2)
{
printf("你出石頭\n");
printf("電腦出剪刀\n");
printf("你輸了\n");
++n;
}
else if(y==3&&x==3)
{
printf("大家都出布,平局\n");
++m;
++n;
}
return m,n; //返回m,n的值
}
int result(int m,int n) //比較最終結果
{
if(m<n)
printf("3局%d勝,你輸了.\n",m);
else if(m>n)
printf("3局%d勝,你贏了.\n",m);
else if(m==n)
printf("一勝一負一平局,旗鼓相當。\n");
return 0;
}//改好了,直接比較三次出結果就行了呀!最後暫停查看下system("pause");

㈡ 求C語言猜拳游戲代碼

#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;
}

㈢ 就C語言中 猜拳游戲的代碼

這是一個簡單的猜拳游戲(剪子包子錘),讓你與電腦對決。你出的拳頭由你自己決定,電腦則隨機出拳,最後判斷勝負。
下面的代碼會實現一個猜拳游戲,讓你與電腦對決。你出的拳頭由你自己決定,電腦則隨機出拳,最後判斷勝負。
啟動程序後,讓用戶出拳,截圖:

用戶出拳,顯示對決結果:截圖:

代碼實現:
#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);

㈣ c語言猜拳游戲問題,求大神幫忙看一下

getchar()放在scanf輸入之前,其他小改動。

#include "stdafx.h"

#include <iostream>

#include <time.h>

using namespace std;


int main()

{

int times,x,user,computer,result,Y=0,T=0,C=0;

char userInput;

cout << "請輸入局數:" << endl;

cin >> times;

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

{

x = i + 1;

printf(" Match %d:Enter R for Rock,P for paper,or S for scissors:", x);

getchar();

scanf_s("%c", &userInput, 1);

switch (userInput)

{

case 82:

user = 2;

break;

case 83:

user = 1;

break;

case 80:

user = 3;

break;

}

srand((unsigned)time(NULL));

computer = rand() % 3 + 1;

if (computer == 1)

{

printf(" The computer chose scissors.");

}

if (computer == 2)

{

printf(" The computer chose rock.");

}

if (computer == 3)

{

printf(" The computer chose paper.");

}

result = computer - user;

if (result == -1 || result == 2)

{

Y += 1;

printf("You win. ");

printf(" Scores: ");

}

else if (result == 1 || result == -2)

{

C += 1;

printf("You lose. ");

printf(" Scores: ");

}

else if (result == 0)

{

T += 1;

printf("You tied. ");

printf(" Score: ");

}

if (T != 0)

{

printf(" Ties-%d", T);

}

if (Y != 0)

{

printf(" You-%d", Y);

}

if (C != 0)

{

printf(" Computer-%d", C);

}

}

printf(" ");

system("pause");

return 0;

}

㈤ 求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 ("該你出: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;
}

熱點內容
國圖資料庫 發布:2024-11-29 10:34:15 瀏覽:540
vpn免流伺服器搭建 發布:2024-11-29 10:26:12 瀏覽:244
c源文件編譯後的擴展名為 發布:2024-11-29 10:08:40 瀏覽:923
腳本自動登錄 發布:2024-11-29 09:55:27 瀏覽:62
安卓的無線網路在哪裡 發布:2024-11-29 09:54:13 瀏覽:652
谷歌瀏覽器打開ftp 發布:2024-11-29 09:46:30 瀏覽:668
熱線女孩ftp 發布:2024-11-29 09:46:29 瀏覽:71
安卓作曲哪個軟體好用 發布:2024-11-29 09:29:30 瀏覽:903
怎麼看撥號伺服器ip 發布:2024-11-29 09:28:17 瀏覽:931
王者配置怎麼樣 發布:2024-11-29 09:22:58 瀏覽:858