當前位置:首頁 » 編程語言 » 簡陋的c語言猜拳游戲
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

簡陋的c語言猜拳游戲

發布時間: 2022-04-23 18:41:35

1. 就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);

2. 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");

3. C語言 怎麼用C語言設計一個猜拳游戲 剪刀石頭布用1.2.3代替 要玩5局3勝,

#include <stdio.h>
#include <time.h>

int main()
{
int a,b,i,m=0,n=0;
srand(time(NULL));
for(i=0;i<10;i++)
{
a=rand()%3+1;
printf("%d",a);
b=rand()%3+1;
printf("%d\n",b);
if(a>b) m++;
else if(a<b) n++;
if(m>3)
{
printf("a is the winner");
break;
}
if(n>3)
{
printf("b is the winner");
break;
}

}
return 0;
}

4. 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;

}

5. c語言問題 猜拳游戲

整體還是很好的。

問題在這里:

  1. 「scanf("%c,%C",&player1,&player2); 」這一句,第二個%c大寫了,改一下。

  2. scanf的實質是將鍵盤輸入字元存入聲明字元變數時開辟的一段存儲區域。第一局運行正常,而第二局往後你只是單純的想覆蓋輸入,這是不可以的——因為你在上一次使用scanf後沒有清空輸入緩存, 這樣再次使用scanf的時候函數就會認為你已經輸入過了。

  3. 改進只需要在scanf之前加上fflush(stdin)清空輸入緩存。

如圖:

希望幫上忙。

6. C語言 猜拳游戲編程

#include
using
namespace
std;
main
()
{
int
a,b,c,d;
char
e;
cout<<"1表示石頭
2表示剪刀
3表示布."<
>a;
b=rand
()%3+1;
cout<<"電腦出的是"<
>e;
}
cout<
評論
0
0
0
載入更多

7. 求幫忙寫一個C語言的猜拳小游戲

import java.util.Scanner;

public class aaa{
public static void main(String[] args){

int count1 = 0;
int count2 = 0;
String dnc = "";
String nic = "";

while(true){

int dn = (int)(Math.random()*3+1);
int ni = 0;
while(true){
System.out.println("請輸入1-3的數字");
Scanner s1 = new Scanner(System.in);
ni = s1.nextInt();
if(ni>=1 && ni<=3){
break;
}
}

if(ni==1){
nic = "石頭";
}else if(ni==2){
nic = "剪刀";
}else{
nic = "布";
}
if(dn==1){
dnc = "石頭";
}else if(dn==2){
dnc = "剪刀";
}else{
dnc = "布";
}

if(dn==1 && ni==2 || dn==2 && ni==3 || dn==3 && ni==1){
System.out.println("電腦贏了1次!電腦出:"+dnc+",你出:"+nic+"");
count1++;
}else if(dn==ni){
System.out.println("平局!電腦出:"+dnc+",你出:"+nic+"");
}else{
System.out.println("你贏了1次!電腦出:"+dnc+",你出:"+nic+"");
count2++;
}

if(count1==2){
System.out.println("三局兩勝,電腦贏了!");
break;
}else if(count2==2){
System.out.println("三局兩勝,你贏了!");
break;
}
}
}
}

這個是個Java源代碼 是Java文件 在DOS命令框運行 至於C語言不了解 但是C語言和Java語言基本上相似,所以寫了這段代碼 希望可以幫到你

8. 用C語言編寫三局兩勝的猜拳游戲,怎麼編寫

隨機種子產生pc的隨機出拳
srand(time(0));

int pc = rand()%3; //0, 1, 2 石頭剪子布
屏幕輸入自己的結果。
scanf(" %d", &var);
一個負責比較的代碼塊,很簡單的邏輯處理。
一個最多執行三次的循環。
兩個負責記錄勝負次數的變數,針對單一角色,兩勝或者兩負,都會結束游戲。

9. 怎樣用C語言編寫一個猜拳游戲

*幫助做程序主體(開始游戲部分)*/
#include
<iostream.h>
#include
<stdlib.h>
void
main()
{
int
computer,
user,
money,
bet;
money=100;
for(
;
money==0;
)
{
cout
<<
"your
money:
"
<<
money
<<
endl;
cout
<<
"bet:
";
cin
>>
bet;
money=money-bet
if(bet>money)
break;
cout
<<
"set:
";
cin
>>
user;
computer=round(sin(rand())*2+1);
switch(user,computer)
{
.../*這地方你自己填一填*/
}
}
cout
<<
"game
over";
}