怎么用c++来写人机猜拳小游戏程序?包括三个类,电脑类,person类,判断输赢。注意不是java

Python026

怎么用c++来写人机猜拳小游戏程序?包括三个类,电脑类,person类,判断输赢。注意不是java,第1张

#include <iostream>

#include <string>

#include <ctime>

using namespace std

class computer //电脑类

{

public:

int a

    computer()  //构造函数

{

}

    void inni1()  //出拳

{

srand(time(0))

a = rand()%3

}

void display1()  //输出出拳

{

if(a==0)

cout<<"电脑:石头\n"

else if(a==1)

cout<<"电脑:剪刀\n"

else

cout<<"电脑:布\n"

}

}

class person  //人类

{

public:

int b

    person()  //构造函数

{

}

    void inni2()  //出拳

{

cout<<"请输入0、1或者2\n"

<<"0表示:石头\t1表示:剪刀\t2表示:布\n"

cin>>b

}

void display2()  //输出出拳

{

if(b==0)

cout<<"你:石头\n"

else if(b==1)

cout<<"你:剪刀\n"

else

cout<<"你:布\n"

}

}

class evaluate:public computer,public person  //判断输赢类

{

public:

void pan_duan()

{

switch(a)

{

case 0:

if(b==0)

cout<<"平局\n"

else if(b==1)

cout<<"你输了!!\n"

else

cout<<"你赢了!!\n"

break

case 1:

if(b==1)

cout<<"平局\n"

else if(b==2)

cout<<"你输了!!\n"

else

cout<<"你赢了!!\n"

break

case 2:

if(b==2)

cout<<"平局\n"

else if(b==0)

cout<<"你输了!!\n"

else

cout<<"你赢了!!\n"

break

}

}

}

int main()

{

evaluate eva    //定义结果判断对象

    eva.inni2()     //人类出拳

eva.display2()  //输出人的出拳

eva.inni1()     //电脑出拳

eva.display1()  //输出电脑的出拳

eva.pan_duan() //输出判断结果

//system("pause")

return 0

}

自己纯手打,老半天才弄出来啊

import java.awt.BorderLayout

import java.awt.Dimension

import java.awt.GridBagConstraints

import java.awt.GridBagLayout

import java.awt.event.ActionEvent

import java.util.Random

import javax.swing.AbstractAction

import javax.swing.Box

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

import javax.swing.JTextField

public class Demo2 extends JFrame {

private JLabel lb1, lb2, lb3, lb4// 提示标签

private JTextField ta1, ta2// 两个文本框

private JButton b1, b2, b3// 三个按钮

private JPanel p1, p2// 两个JPanel面板

public Demo2() {

// 初始化所有组件

lb1 = new JLabel("欢迎使用人机猜拳程序")

lb2 = new JLabel("你出拳: ")

lb3 = new JLabel("电脑出拳:")

lb4 = new JLabel("结果")

ta1 = new JTextField()

ta1.setPreferredSize(new Dimension(60, 60))// 设置大小

ta1.setEditable(false)//设置不可编辑

ta2 = new JTextField()

ta2.setPreferredSize(new Dimension(60, 60))

ta2.setEditable(false)//设置不可编辑

b1 = new JButton("剪刀")

b2 = new JButton("石头")

b3 = new JButton("布")

p1 = new JPanel()

p2 = new JPanel()

// 设置第一个面板内容

Box box = Box.createVerticalBox()

Box box1 = Box.createHorizontalBox()

box1.add(lb2)

box1.add(ta1)

box1.add(lb3)

box1.add(ta2)

box.add(lb1)

box.add(Box.createVerticalStrut(40))

box.add(box1)

box.add(Box.createVerticalStrut(10))

box.add(lb4)

box.add(new JLabel())

p1.add(box)

// 设置第二个面板

p2.setLayout(new GridBagLayout())// 使用GridBagLayout布局管理器

p2.setPreferredSize(new Dimension(0, 60))

GridBagConstraints g2 = new GridBagConstraints()

g2.fill = GridBagConstraints.BOTH

g2.weightx = 1.0

g2.weighty = 1.0

g2.gridx = 0

g2.gridy = 0

p2.add(b1, g2)

g2.gridx = 1

p2.add(b2, g2)

g2.gridx = 2

p2.add(b3, g2)

//为3个按钮添加事件

b1.addActionListener(new buttonAction())

b2.addActionListener(new buttonAction())

b3.addActionListener(new buttonAction())

this.getContentPane().add(p1)

this.getContentPane().add(p2, BorderLayout.SOUTH)

this.setTitle("机器人猜拳游戏")

this.setSize(300, 300)

this.setVisible(true)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

//事件类

class buttonAction extends AbstractAction{

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==b1){

ta1.setText("剪刀")

init(ta1.getText())

}else if(e.getSource()==b2){

ta1.setText("石头")

init(ta1.getText())

}else if(e.getSource()==b3){

ta1.setText("布")

init(ta1.getText())

}

}

// 模拟电脑出拳,产生三个随机数。0代表剪刀,1代表石头,2代表布

public String getQuan(){

String str=""

int num=new Random().nextInt(3)

if(num==0){

str="剪刀"

}else if(num==1){

str="石头"

}else if(num==2){

str="布"

}

return str

}

// 判断输赢方法

public String isying(String s1,String s2){

String s=""

if(s1.equals(s2)){

s="平局"

}else if(s1.equals("剪刀")&&s2.equals("布")){

s="你赢"

}else if(s1.equals("石头")&&s2.equals("剪刀")){

s="你赢"

}else if(s1.equals("布")&&s2.equals("石头")){

s="你赢"

}else{

s="电脑赢"

}

return s

}

public void init(String wo){

String sy=""// 保存输赢结果

String dncq=getQuan()//电脑出拳

if(wo.equals(dncq)){

sy="平局"

}else if(wo.equals("剪刀")&&dncq.equals("布")){

sy="你赢"

}else if(wo.equals("石头")&&dncq.equals("剪刀")){

sy="你赢"

}else if(wo.equals("布")&&dncq.equals("石头")){

sy="你赢"

}else{

sy="电脑赢"

}

ta2.setText(dncq)// 电脑出拳

lb4.setText("结果:"+sy)

}

}

public static void main(String[] args) {

new Demo2()

}

}

先建立个Game包

然后我做的是分了5个类来做的

TestStartGuess 类

package com.game.guess

public class TestStartGuess {

/**

* 人机互动版猜拳游戏

* 程序入口

*/

public static void main(String[] args) {

Game game=new Game()

game.initial()

game.startGame()

}

}

2.Person 类

package com.game.guess

import java.util.Scanner

/**

* 用户类

*阶段1完成

* @param <Scanner>

*/

public class Person {

String name ="匿名"//名字

int score =0//积分

/**

* 出拳

*@return出拳结果:1.剪刀 2.石头 3.布

*/

public int showFist(){

//接收用户的选择

Scanner input =new Scanner(System.in)

System.out.print("\n请出拳:1.剪刀 2.石头 3.布 (输入相应数字):")

int show=input.nextInt()

//输出出拳结果,并返回

switch(show){

case 1:

System.out.println("你出拳:剪刀")

break

case 2:

System.out.println("你出拳:石头")

break

case 3:

System.out.println("你出拳:布")

break

}

return show

}

}

3.Computer 类

package com.game.guess

/**

*计算机类

*阶段2完成

*/

public class Computer{

  String name="电脑"//名字

  int score = 0//积分

  /**

  *出拳

  *@return 出拳结果:1.剪刀 2.石头 3.布

  */

  public int showFist(){

           //产生随机数

           int show =(int)(Math.random()*10)%3+1//产生随机数,表示电脑出拳

           //输出出拳结果并返回

    switch(show){

case 1:

System.out.println(name+"你出拳:剪刀")

break

case 2:

System.out.println(name+"你出拳:石头")

break

case 3:

System.out.println(name+"你出拳:布")

break

}

return show

}

}

4.Game 类

package com.game.guess

import java.util.Scanner

/**

* 游戏类类完全版

* 阶段7:功能扩展

* @param <computer>

*

*/

public class Game<computer>{

Person person//甲方

Computer computer //乙方

int count//对战次数

/**

* 初始化

*/

public void initial(){

person=new Person()

computer=new Computer()

count=0

}

/**

* 开始游戏

*/

@SuppressWarnings("resource")

public void startGame(){

System.out.println("-------欢迎进入游戏世界-------\n")

System.out.println("\n\t\t***************")

System.out.println("\t\t**猜拳,开始 **")

System.out.println("\t\t***************")

System.out.println("\n\n出拳规则:1.剪刀,2.石头,3.布")

Scanner input=new Scanner(System.in)

String exit="n"//退出系统

do{

initial()//初始化

/*选择对方角色*/

System.out.print("请选择对方角色:(1:刘备,2:孙权,3:曹操):")

int role=input.nextInt()

if(role==1){

computer.name="刘备"

}else if(role==2){

computer.name="孙权"

}else if(role==3){

computer.name="曹操"

}

//扩展功能1:输入用户姓名

/*输入用户姓名*/

System.out.print("请输入你的姓名:")

person.name=input.next()

System.out.println(person.name+"VS"+computer.name+"对战\n")

//扩展功能1结束

System.out.print("要开始吗?(y/n)")

String start=input.next()//开始每一局游戏

int perFist//用户出的拳

int compFist//计算机出的拳

while(start.equals("y")){

/*出拳*/

   perFist=person.showFist()

   compFist=computer.showFist()

   /*裁决*/

   if((perFist==1&&compFist==1)||(perFist==2&&compFist==2)||(perFist==3&&compFist==3)){

       System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!\n") //平局

              }else if((perFist==1&&compFist==3)||(perFist==2&&compFist==1)||(perFist==3&&compFist==2)){

           System.out.println("结果:恭喜,你赢了!") //用户赢

                    person.score++

              }else{

                System.out.println("结果说:^_^,你输了,真笨!\n") //计算机赢

                computer.score++

              }

              count++

              System.out.println("\n是否开始下一轮(y/n):")

              start=input.next()

              }

/*显示结果*/

showResult()

//扩展功能3:循环游戏,知道退出系统

System.out.print("\n要开始下一局吗?(y/n):")

exit=input.next()

       System.out.println()

       //扩展功能3结束

}while(!exit.equals("n"))

System.out.println("系统退出!")

}

/**

* 显示比赛结果

*/

public void showResult(){

/*显示对战次数*/

System.out.println("-------------------------------")

          System.out.println(computer.name+"VS"+person.name)

          System.out.println("对战次数:"+count)

       

          //扩展功能2:显示最终的得分

          System.out.println("\n姓名\t得分")

          System.out.println(person.name+"\t"+person.score)

System.out.println(computer.name+"\t"+computer.score+"\n")

   //扩展功能2结束

          /*显示对战结果*/

          int result=calcResult()

          if(result==1){

          System.out.println("结果:打成平手,下次再和你一分高下!")

          }else if(result==2){

          System.out.println("结果:恭喜恭喜!") //用户获胜

          }else{

          System.out.println("结果:呵呵,笨笨,下次加油啊!") //计算机获胜

          }

System.out.println("--------------------------------")

}

/**

* 计算比赛结果

* @return1:战平; 2:用户赢; 3:电脑赢

*/

public int calcResult(){

if(person.score==computer.score){

return 1//战平

}else if(person.score>computer.score){

return 2//用户赢

}else{

return 3//电脑赢

}

}

}

5.Start 类

package com.game.guess

public class StartGuess {

public static void main (String[] args){

Game c = new Game()

c.initial()

c.startGame()

}

}

然后编译执行就OK了

希望能帮到你