程设作业 魔兽世界之一:备战

面向对象的练手,写的逻辑混乱,我不好说。

基本思想是怪物封装一下,玩家封装一下。乱改代码的时候把一些东西丢到public里去了,其实不应该。感觉最好是把变量private下,用专门的函数调用(代码有这种想法)。

其实并没有把两个类建立实质联系,而是通过num数组记录了一个顺序,建立了player到warrior出怪顺序的映射。不知道这样好不好。

没有纠结构造函数,避免初始化,直接写个用来赋值的函数就好了,,车到山前就绕路)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;

class warrior {
public:
	char name[10];
	int Hp;
	int count[2];
	warrior(){
		Hp = count[0] = count[1] = 0;
	}
	void make(char *s,int hp) {
		strcpy(name, s); Hp = hp; count[0]=count[1] = 0;
	}
	void printname() {
		cout << name;
	}
	int getHp() {
		return this->Hp;
	}
	void add(int x) {
		++count[x];
	}
	int getcount(int x) {
		return count[x];
	}
}Warrior[5];
class player {
private:
	int number;
	int restM;
	char name[10];
	int pos;
	int num[5];
	int countwarrior;
public:
	int flag;
	player(char *s,int m,int n) {
		strcpy(name, s); restM = m; flag = pos = countwarrior=0; number = n;
	}
	void makenum(int i, int key) {
		num[i] = key;
	}
	void print(int t, int _num) {
		if (t < 10)cout << "00" << t;
		else if (t < 100)cout << "0" << t;
		else cout << t;
		cout << " ";
		cout << name;
		cout << " ";
		Warrior[_num].printname();
		cout << " ";
		cout << countwarrior;
		cout << " born with strength ";
		cout << Warrior[_num].Hp;
		cout << ",";
		cout << Warrior[_num].count[number];
		cout << " ";
		Warrior[_num].printname();
		cout << " in ";
		cout << name;
		cout << " headquarter" << endl;
	}
	void failprint(int t) {
		if (t < 10)cout << "00" << t;
		else if (t < 100)cout << "0" << t;
		else cout << t;
		cout << " ";
		cout << name;
		cout << " headquarter stops making warriors" << endl;
	}
	bool get(int t) {
		if (flag)return true;
		bool havegot = false;
		for (int i = 1; i <= 5; i++) {
			//cout <<pos<<" "<< num[pos] << endl;
			if (Warrior[num[pos]].Hp<= restM) {
				havegot = true;
				Warrior[num[pos]].add(number); ++countwarrior;
				print(t,num[pos]);
				restM -= Warrior[num[pos]].Hp;
				++pos;pos%=5;
				break;
			}
			++pos; pos %= 5;
		}
		if (!havegot) {
			
			flag = 1;
			return false;
		}
		return true;
	}
};
int T, M;
int M_red, M_blue;
char name[5][10] = { "dragon","ninja","iceman","lion","wolf" };
char Name[2][10] = { "red","blue" };
int Hp[5];
int numred[5] = { 2,3,4,1,0 };
int numblue[5] = { 3,0,1,2,4 };
int main() {
	
	cin >> T;
	int n = 0;
	while (T--) {
		int t = 0;
		++n;
		cout << "Case:" << n << endl;
		cin >> M;
		for (int i = 0; i < 5; i++)cin >> Hp[i];
		for (int i = 0; i < 5; i++) {
			Warrior[i].make(name[i],Hp[i]);
		}
		player R(Name[0],M,0), B(Name[1],M,1);
		for (int i = 0; i < 5; i++) {
			R.makenum(i, numred[i]);
			B.makenum(i, numblue[i]);
		}
		while (1) {
			if (R.flag && B.flag)break;
			if (!R.get(t))R.failprint(t);
			if (!B.get(t))B.failprint(t);
			++t;
		}
	}
	return 0;

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值