Magic the gathering

Hi everyone,

Have been a fan of Magic the Gathering for some time and thought I would create a simple program to make a deck, display some basic information, but most importantly, randomly draw a hand of 7 cards as many times as you wish.

If you have any suggestions on improving the program, or problem(s) with my code, let me know. Of course, if you want to use it, feel free.

Used Microsoft Visual C++ 2010 Express.

Thanks,

Mike

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
#include <cctype>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <locale>
#include <iomanip>
#include <sstream>

using namespace std; 

const int HAND_SIZE=7;//choose hand size here
const int MAXIMUM_DECK_SIZE=1000;

class Magic{

public:
	Magic(){size=0;}

	void shuffle(){
		srand(time(0));
		for(int i=0;i<size;i++){
			int random=rand()%size;
			string temp=deck[i];
			int temp1=casting_cost[i];

			deck[i]=deck[random];
			casting_cost[i]=casting_cost[random];

			deck[random]=temp;
			casting_cost[random]=temp1;
		}
	}
	void opening_hand()const {
		for(int i=0;i<HAND_SIZE;i++)
			cout<<left<<setw(20)<<deck[i]<<setw(10)<<right<<casting_cost[i]<<endl;
	}
	double average_casting_cost(){//does not count zero casting cost cards
		int sum=0, temp_counter=0;
		for(int i=0;i<size;i++){
			if(casting_cost[i]!=0){
				sum+=casting_cost[i];
				temp_counter++;
			}
		}
		return (static_cast<double>(sum)/temp_counter);
	}
	void print_graphic(int n){
		for(int i=0;i<n;i++)
			cout<<'*';
		cout<<endl;
	}
	void print_graph(){
		string temp;
		sort();
		int temp_int;
		cout<<endl<<endl;
		cout<<setw(20)<<left<<"Card Name"<<setw(10)<<right<<"Casting Cost\n\n";
		for(int i=0;i<size;i++){
			temp=deck[i];
			temp_int=casting_cost[i];
			if(temp!=deck[i+1]){
				cout<<setw(20)<<left<<deck[i]<<setw(10)<<right;
				print_graphic(temp_int);
			}
		}
	}
	void sort(){
		for(int i=0;i<size;i++){
			for(int j=i+1;j<size;j++){
				if(casting_cost[i]>casting_cost[j]){
					int temp=casting_cost[i];
					string temp_string=deck[i];

					casting_cost[i]=casting_cost[j];
					deck[i]=deck[j];

					casting_cost[j]=temp;
					deck[j]=temp_string;
				}
			}
		}
	}
	int get_size(){return size;}
	friend istream& operator >>(istream& in, Magic& m);
	friend ostream& operator <<(ostream& out, Magic& m);

private:
	string deck[MAXIMUM_DECK_SIZE];
	int casting_cost[MAXIMUM_DECK_SIZE];
	int size;
};

int main(){
	
	Magic deck;
	char choice;
	double average_cc;
	int temp_size;
	cout<<"Welcome to the friendly Magic program, create a deck now!\n\n";
	cin>>deck;

	do{
		cout<<"Now that you created a deck, choose\n\n"
			<<"'d' to Draw a hand of "<<HAND_SIZE<<endl;
		cout<<"'a' to display deck\n"
			<<"'s' for Stats\n"
			<<"'q' to Quit\n\n";
			cout<<">>";
			cin>>choice;
			switch(choice){
			
			case 'd': 
				do{
					cout<<"===================Hand Start===================\n\n";
					cout<<setw(20)<<left<<"Card Name"<<setw(10)<<right<<"Casting Cost\n\n";
					deck.shuffle();
					deck.opening_hand();

					cout<<"\n\n====================Hand End====================\n";

					cout<<"\nShffle and draw another hand?(y/n): ";
					cin>>choice;
					cout<<endl;
				}while(choice=='y');
				break;
			
			case 'a': 
				cout<<"The deck is\n\n";
				cout<<deck<<endl;
				break;

			case 's': 
				average_cc=deck.average_casting_cost();
				cout<<"Average excluding 0 casting cost is: "<<average_cc<<endl;
				temp_size=deck.get_size();
				cout<<"Deck size: "<<temp_size<<endl;
				deck.print_graph();
				break;
				
			case 'q': 
				cout<<"Have a nice day, goodbey!\n";
				exit(1);
				break;

			default: break;
			
			}
		cout<<"\nBack to options?(y/n): ";
		cin>>choice;
	}while(choice=='y');
	cout<<"Have a nice day!\n";
	exit(1);


	_getch();
	return 0;
}
ostream& operator <<(ostream& out, Magic& m){
	cout<<left<<setw(20)<<"Name"<<setw(10)<<right<<"Casting Cost\n\n";
	for(int i=0;i<m.size;i++)
		out<<left<<setw(20)<<m.deck[i]<<setw(10)<<right<<m.casting_cost[i]<<endl;
	return out;
}
istream& operator >>(istream& in, Magic& m){

	int dummy, num, cast_cost;
	string card_name;
	char choice, number[20];
	bool correct_entry;
	do{
		do{
			correct_entry=true;
			cout<<left<<setw(17)<<"Number: ";
			cin>>number;
			for(int j=0;j<strlen(number);j++){
				if(!isdigit(number[j]))
					correct_entry=false;
			}
		}while(!correct_entry);
		num=atoi(number);
		if(num<0)
			exit(1);
		
		cin.ignore(1000,'\n');
		cout<<setw(17)<<left<<"Card name: ";
		getline(cin,card_name);

		cout<<setw(17)<<left<<"Casting Cost: ";
		in>>cast_cost;
		while(cast_cost<0){
			cout<<"Can't be negative, enter again: ";
			in>>cast_cost;
		}
		dummy=m.size;
		for(int i=m.size;i<num+dummy;i++){
			m.deck[i]=card_name;
			m.casting_cost[i]=cast_cost;
		}
		m.size+=num;

		cout<<"\nAnother (y/n): ";
		cin>>choice;
		cout<<endl;
	}while(choice=='y');

	return in;
}

Topic archived. No new replies allowed.