C++实现石头剪刀布

C++实现了个石头剪子布功能具有数据的读写功能即显示游戏记录功能

要充分地分析和理解问题本身,弄清要求做什么。在确定解决方案框架过程中,考虑怎样使程序结构清晰、合理、简单和易于调试,并确定每个函数的简单功能,以及函数之间的调用关系。
综上 新手写的 欢迎指出问题~

以下是computer people类的声明和实现

computer 和 people类主要功能为 input show read write功能

以下函数为电脑的input核心算法

srand((unsigned)time(0));
  for (int i = 0; i < 10; i++) {
      radom = rand() % 3;
      }
  //computer.h
  #pragma once
  #include"project.h"
  class Computer:virtual public project
 {
  public:
      Computer() {};
      virtual ~Computer() {};
      void Input();
     void Show()const;
     void Read(fstream& f)const;
     void Write(fstream& f)const;
 };
  //computer.cpp
  #include "Computer.h"
  void Computer::Input() {
     srand((unsigned)time(0));
     for (int i = 0; i < 10; i++) {
          radom = rand() % 3;
      }
      switch (radom) {
      case 0:setbase(0);
         break;
     case 1:setbase(1);
        break;
     case 2:setbase(2);
         break;
     }
 }
 void Computer::Show()const {
     cout << "电脑出的是:" << getbase() << endl;
 }
 void Computer::Read(fstream& f) const {
     f.read((char*)this, sizeof(Computer));
 }
void Computer::Write(fstream& f)const {
     f.write((char*)this, sizeof(Computer));
}
 //people.h
  #pragma once
  #include"project.h"
  class people:virtual public project
  {
  public:
  people() {};
  virtual ~people() {};
  void Input();
  void Show()const;
 void Read(fstream& f)const;
 void Write(fstream& f)const;
 };

 //people.cpp
 #include "people.h"
  void people::Input() {
      cout << "请输入石头(1),剪刀(2),布(3)" << endl;
      cin >> num;
      switch (num) {
      case 1:setbase(0);
          break;
      case 2:setbase(1);
         break;
     case 3:setbase(2);
         break;
     default:
         cout << "输入失败!" << endl;
         Input();
     }
 }
 void people::Show() const {
     cout << "您出的是:" << getbase() << endl;
 }
 void people::Read(fstream&f) const {
     f.read((char*)this, sizeof(people));
 }
 void people::Write(fstream& f)const {
     f.write((char*)this, sizeof(people));
 }

以下是PROJECT.H主要实现游戏规则的的定义

  //PROJECT.H
  #pragma once
  #include<iostream>
  #include<string>
  #include<time.h>
  #include<fstream>
  using namespace std;
  enum base { rock, scissor, cloth };
  class project
 {
 protected:
     int num;
     int radom;
public:
     virtual ~project() {};
     virtual void Input() = 0;
     void setbase(int b) {
         num = (base)b;
     }
     int getnum() {
         return num;
     }
     string getbase() const {
         string c;
         if (num == 0) {
             c = "石头";
         }
         else if (num == 1) {
             c = "剪刀";
         }
         else
             c = "布";
         return c;
     }
     virtual void Show()const = 0;
     virtual void Read(fstream& f)const = 0;
     virtual void Write(fstream& f)const = 0;
 };

以下是systemm主要实现游戏功能的定义和声明
为什么叫systemm呢 因为system是关键字会报错的

  //systemm.h
  #pragma once
  #include"people.h"
  #include"Computer.h"
  #include"usertype.h"
  class systemm 
  {
  protected:
      fstream file;
     usertype* usertable;
     int maxSize;                                            
     int count;        
 public:
     systemm();
    ~systemm();
     void Adduser(const usertype& e);
     void adddate();
     void run();
     void show();
 };

//systemm.cpp
#include "systemm.h"
static double wins = 0.0;
bool inline UserSaysYes()
{
    char flag = 'N';
    cin >> flag;
    if (flag == 'y' || flag == 'Y')
    {
        return true;
    }
    else
    {
        return false;
    }
}
systemm::systemm()
{
    ifstream userfile("user.idx", ios::binary);                
    if (!userfile.fail())
    {
        userfile.seekg(0, ios::end);                            
        count = userfile.tellg() / sizeof(systemm);            
        maxSize = count + 100;
        usertable = new usertype[maxSize];                    
        userfile.seekg(0, ios::beg);                            
        int i = 0;                                                
        userfile.read((char*)& usertable[i++], sizeof(usertype));
        while (!userfile.eof())
        {        
            userfile.read((char*)& usertable[i++], sizeof(usertype));
        }
        userfile.close();                                        
    }
    else
    {        
        count = 0;                                                
        maxSize = count + 100;                        
        usertable = new usertype[maxSize];                    
    }
    ifstream iFile("user.dat");                                
    if (iFile.fail())
    {                                                            //打开文件失败,表示文件不存在
        ofstream oFile("user.dat");                            //建立输出文件
        if (oFile.fail())throw("打开文件失败!");                //抛出异常
        oFile.close();                                            //关闭文件
    }
    else
    {    
        iFile.close();                                            //关闭文件
    }
    file.open("user.dat", ios::in | ios::out | ios::binary);        //以读写的方式打开文件
    if (file.fail())throw("打开文件失败!");                        //抛出异常
}
systemm::~systemm()
{
    ofstream userfile("user.idx", ios::binary);            
    for (int i = 0; i < count; i++)
    {
        userfile.write((char*)& usertable[i], sizeof(usertype));
    }
    userfile.close();                                    
    file.close();                                            
}
void systemm::Adduser(const usertype& e)
{
    if (count >= maxSize)        //扩展缓存区长度,主要是为了防止缓存区溢出
    {
        maxSize += 100;
        usertype* temusertable = new usertype[maxSize];
        for (int i = 0; i < count; i++)
        {
            temusertable[i] = usertable[i];
        }
        delete[]usertable;
        usertable = temusertable;
    }
    //添加元素
    usertable[count++] = e; //加入到索引表中
}
void systemm::adddate()
{
    Computer* computer = new Computer;
    people* peo = new people;
    usertype computeritem;    
    computeritem.usertype = '1';
    usertype peopleitem;
    peopleitem.usertype = '2';
    int ss = 3;
    int f = 0;
    file.seekg(0, ios::end);        //将dat文件的指针指向末尾,为下一步的增加数据做准备
    do                            //第一个循环,来确认是否继续输入
    {
        do {
            if (f > 0) {
                Computer* computer = new Computer;
                people* peo = new people;
            }
            computer->Input();
            peo->Input();
            if (computer->getnum() == 0 && peo->getnum() == 1) {
                computer->Show();
                peo->Show();
                ss = 0;
            }
            else if (computer->getnum() == 1 && peo->getnum() == 2) {
                computer->Show();
                peo->Show();
                ss = 0;
            }
            else if (computer->getnum() == 2 && peo->getnum() == 0) {
                computer->Show();
                peo->Show();
                ss = 0;
            }
            else if (computer->getnum() == 0 && peo->getnum() == 2) {
                computer->Show();
                peo->Show();
                ss = 1;
            }
            else if (computer->getnum() == 1 && peo->getnum() == 0) {
                computer->Show();
                peo->Show();
                ss = 1;
            }
            else if (computer->getnum() == 2 && peo->getnum() == 1) {
                computer->Show();
                peo->Show();
                ss = 1;
            }
            else if (computer->getnum() == 0 && peo->getnum() == 0) {
                computer->Show();
                peo->Show();
                ss = 2;
            }
            else if (computer->getnum() == 1 && peo->getnum() == 1) {
                computer->Show();
                peo->Show();
                ss = 2;
            }
            else if (computer->getnum() == 2 && peo->getnum() == 2) {
                computer->Show();
                peo->Show();
                ss = 2;
            }
            if (ss == 2) {
                cout << "平局,请继续";
                computeritem.num = computer->getnum();
                peopleitem.num = peo->getnum();
                computeritem.position = file.tellg();
                Adduser(computeritem);
                computer->Write(file);
                delete computer;
                file.seekg(0, ios::end);             
                peopleitem.position = file.tellg();
                Adduser(peopleitem);
                peo->Write(file);
                delete peo;
            }
            f++;
        } while (ss == 2);
        if (ss == 1) {
            cout << "你输了" << endl;
            computeritem.num = computer->getnum();
            peopleitem.num = peo->getnum();
            computeritem.position = file.tellg();
            Adduser(computeritem);
            computer->Write(file);
            delete computer;
            file.seekg(0, ios::end);
            peopleitem.position = file.tellg();
            Adduser(peopleitem);
        }
        else {
            cout << "你赢了" << endl;
            wins += 1;
            computeritem.num = computer->getnum();
            peopleitem.num = peo->getnum();
            computeritem.position = file.tellg();
            Adduser(computeritem);
            computer->Write(file);
            delete computer;
            file.seekg(0, ios::end);
            peopleitem.position = file.tellg();
            Adduser(peopleitem);
        }
        peo->Write(file);
        cout << "是否要继续添加?";
        delete peo;
    } while (UserSaysYes());
}
void systemm::show() {
    Computer* computer = new Computer;
    people* peo = new people;
    int pos;
    file.seekg(0, ios::beg);
    for (pos = 0; pos < count; pos++) {
        if (usertable[pos].usertype == '1') {
            cout << "第" << pos + 1 << "局" << endl;
            file.seekg(usertable[pos].position, ios::beg);
            computer->Read(file);
            computer->Show();
        }
        else {
            cout << "第" << pos + 1 << "局" << endl;
            file.seekg(usertable[pos].position, ios::beg);
            peo->Read(file);
            peo->Show();
        }
    }
    cout << "胜率为:" << (wins / (pos + 1)) * 100 << "%" << endl;
    if (pos == 0) {
        cout << "数据为空" << endl;
    }
}
void systemm::run() {
    int select;
    do
    {
        cout << "请选择" << endl;
        cout << "1.开始/继续游戏" << endl;
        cout << "2.显示游戏记录" << endl;
        cout << "3.退出游戏" << endl;
        cin >> select;
        while (cin.get() != '\n');

        switch (select)
        {
        case 1:
            adddate();
            break;
        case 2:
            show();
            break;
        case 3:
            break;
        default:
            cout << "输入失败!" << endl;
        }
    } while (select != 3);
}

以下是usertype 类
即为索引表类

  //undertype.h
  #pragma once
  #include <string>
  class usertype
  {
  public:
      int position;
      char usertype;
      int num;
 };

以下是main函数

  //main.cpp
  #include"systemm.h"
 #include"usertype.h"
  int main(void) {
      try
      {
          systemm obj;
          obj.run();
      }
     catch (char* error)
     {
         cout << error;
     }
     system("PAUSE");
     return 0;
 }

该项目的写入是电脑挨着人电脑挨着人一个一个储存下去
然而读取数据的话是从开始读到结尾
因此我给电脑和人分别给usertype赋值以防读取错误的数据调用错误的方法

我在其它博客也粗略的写过这篇博客 网址为:https://www.cnblogs.com/hsjj/p/game1.html

河北工业大学计算机软件技术基础(VC) 课程设计任务书 题目:人机对战——石头剪刀布 目的与要求 目的 通过编写该程序,培养学生综合利用C++语言进行程序设计的能力,加强函数的运用及学生对软件工程方法的初步认识,提高软件系统分析能力和程序文档建立、归纳总结的能力,培养学生利用系统提供的标准函数及典型算法进行设计,并对Windows平台下的控制台进行深入的了解。 基本要求 要求用C++语言编程,在Visual C++环境下调试完成; 要求划分功能模块,各个功能分别使用函数来完成; 源代码程序要求必要的注释。 设计方法和基本原理 课题功能描述 编写一个人机进行石头剪刀布的小游戏。要求能够进行完一轮猜拳后,显示统计信息。每一轮游戏的过程是首先由玩家选择要出的拳,然后计算机选择出拳,输出本轮游戏的胜负情况,然后输出统计信息(玩家胜利次数、计算机胜利次数和平局次数),然后每轮猜拳结束后,询问玩家是否要继续游戏。游戏界面如下: 主要技术问题的描述 随机函数 随机函数名为rand(),使用时必须包含头文件stdlib.h。 创新要求 在基本要求达到后,进行创新设计: 程序要保证计算机的胜利次数至少是玩家胜利次数的1.5倍。 课程设计的考核方式及评分方法 考核方式 (1) 学生要提交书面课程设计报告(A4纸打印);并将设计报告的电子文档、.cpp源文件和.h头文件放到一个文件夹里上传到所对应班级的学生名称相应文件夹中。 (2) 课程设计结束时,在机房当场验收。教师提供测试数据,由学生运行所设计的系统,检查运行结果是否正确,并回答教师提出的有关问题。 评分方法 根据出勤率、课程设计期间纪律、课程设计运行结果、课程设计报告及答辩情况综合评分。 书写设计报告的要求(详细内容见“设计报告模板”) 课程设计的有关文档“设计报告模板”和“课程设计要求”请在下载任务书处下载。    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值