python猜拳游戏编程代码背景意义

python猜拳游戏编程代码背景意义,第1张

python猜拳游戏编程代码背景意义是用面向对象的思想做一个游戏。用面向对象的思想玩家猜拳:1剪刀2石头3布玩家输入一个1-3的数字电脑出拳:随机产生一个1-3的数字,提示电脑出拳结果本局对战结束,输出谁赢,是否继续输出结果:玩家赢几局电脑赢几局平局几次游戏结束。

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个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((player=selection_by_player())!=quit)

switch(player){

case paper:

case rock:

case scissors:

machine=selection_by_machine()

if(player==machine){

++tie

printf("\n a tie")

}

else if(you_won(player,machine)){

++win

printf("\n you won")

}

else{

++lose

printf("\n i won")

}

break

case game:

game_status(win,lose,tie)

break

case instructions:

instructions_for_the_player()

break

case help:

help_for_the_player()

break

}

game_status(win,lose,tie)

printf("\n\nBYE\n\n")

}

instructions_for_the_player()

{

printf("\n%s\n\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s",

"PAPER,ROCK,SCISSORS",

"In this game",

"p is for paper,",

"r is for rock,",

"s is for scissors.",

"Both the player and the machine will choose one",

"of p,r,or s. If the o choices are the same,",

"then the game is a tie. Otherwise:",

"\"paper covers the rock\" (a win for paper),",

"\"rock breaks the scissors\" (a win for rock),",

"\"scissors cut the paper\" (a win for scissors).")

printf("\n\n%s\n\n%s\n%s\n%s\n%s\n\n%s\n\n%s",

"There are other allowable inputs:",

"g for game status (the number of wins so far),",

"h for help,",

"i for instructions (reprin these instructions),",

"q for quit (to quit the game).",

"This game is played repeatedly until q is entered.",

"Good luck!")

}

enum p_r_s selection_by_player()

{

char c

enum p_r_s player

printf("\n\ninput p,r,or s:")

while((c=getchar())==''||c=='\n'||c=='t')

switch(c){

case 'p':

player=paper

break

case 'r':

player=rock

break

case 's':

player=scissors

break

case 'g':

player=game

break

case 'i':

player=instructions

break

case 'q':

player=quit

break

default:

player=help

}

return(player)

}

enum p_r_s selection_by_machine()

{

static int i

i=++i%3

return((i==0)? paper:((i==1)? rock:scissors))

}

you_won(player,machine)

enum p_r_s player,machine

{

int victory

if(player==paper)

victory=machine==rock

else if(player==rock)

victory=machine==scissors

else/*player==scissors*/

victory=machine==paper

return(victory)

}

game_status(win,lose,tie)

{

printf("\nGAME STATUS")

printf("\n\n%7d%s\n%7d%s\n%7d%s\n%7d%s",

win,"games won by you",

lose,"games won by me",

tie,"game tied",

win+lose+tie,"games played:")

}

help_for_the_player()

{

printf("\n%s\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s",

"the following characters can be used for input:",

" p for paper",

" r for rock",

" s for scissors",

" g to find out the game status",

" h to print this list",

" i to reprint the instructions for this game",

" q to quit this game")

}

package test

import java.util.Random

import java.util.Scanner

/**

 * 猜拳游戏思路 

 * 1、定义输入函数 

 * 2、提示用户输入猜拳数值 

 * 3、定义随机一个数作为电脑数值 

 * 4、判断[用户输入数值]与 [电脑随机数值] 

 * 5、能够相等就是打平,不能相等就利用&&、||逻辑符判断输赢 

 * 6、设定数值1-石头 2-剪刀  3-布

 */

public class CaiQuanYouXi {

 public static void main(String[] args) {

  Scanner in=new Scanner(System.in)//定义输入函数in,Scanner包功能,输入数值用的

  System.out.println("--------------猜拳游戏---------------")

  System.out.println("请输入一个数值:1、石头 2、剪刀 3、布")//提示输入数值 

  System.out.println(" ")//空行

  int x=in.nextInt()//让用户输入X的数值 

  Random on=new Random()//定义电脑的随机数值的函数on 

  int y=on.nextInt(3)+1//定义y随机函数数值范围(1--3)

  if(x>=4||x==0){   //判断用户是否输入非1--3范围 

   System.out.println("亲,请正确输入:1、石头 2、剪刀 3、布。你输入了:"+x)  

  }else{   

   /*下面是判断用户输入x的数值 嵌套if*/ 

   if(x==y){   

    if(x==1){ //判断打平的情况 

     System.out.println("你:石头------电脑:石头    PK:很幸运打平手") 

    }else if(x==2){ 

     System.out.println("你:剪刀------电脑:剪刀   PK:很幸运打平手") 

    }else { 

     System.out.println("你:布------电脑:布    PK:很幸运打平手") 

    } 

   }else if(x==1&&y==2||x==2&&y==3||x==3&&y==1){ //开始判断赢的情况 

    if(x==1&&y==2){ 

     System.out.println("你:石头------电脑:剪刀    PK:恭喜您,赢了!") 

    }else if(x==2&&y==3){ 

     System.out.println("你:剪刀------电脑:布   PK:恭喜您,赢了!") 

    }else {

     System.out.println("你:布------电脑:石头    PK:恭喜您,赢了!")

    } 

   }else {//开始判断输的情况 

    if(x==1&&y==3){ 

     System.out.println("你:石头------电脑:布    PK:很遗憾,输了!") 

    }else if(x==2&&y==1){ 

     System.out.println("你:剪刀------电脑:石头    PK:很遗憾,输了!") 

    }else { 

     System.out.println("你:布------电脑:剪刀    PK:很遗憾,输了!") 

    } 

   }

  }

 }

}

运行后的效果展示:

--------------猜拳游戏---------------

请输入一个数值:1、石头 2、剪刀 3、布

1

你:石头------电脑:布    PK:很遗憾,输了!

--------------猜拳游戏---------------

请输入一个数值:1、石头 2、剪刀 3、布

4

亲,请正确输入:1、石头 2、剪刀 3、布。你输入了:4


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8107442.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-13
下一篇2023-04-13

发表评论

登录后才能评论

评论列表(0条)

    保存