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