-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
82 lines (68 loc) · 2.46 KB
/
main.cpp
File metadata and controls
82 lines (68 loc) · 2.46 KB
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
#include "cards.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
CardBST::Card convertStringToCard(string line) {
char suit = line[0]; //starts at index 1, returns character at zeroth index
string value = line.substr(2); //starts at index 2 (after space), returns substring remainder of string (value string)
CardBST::Card newCard(suit, value);
return newCard;
}
int main(int argv, char** argc){
if(argv < 3){
cout << "Please provide 2 file names" << endl;
return 1;
}
ifstream cardFile1 (argc[1]);
ifstream cardFile2 (argc[2]);
string line;
if (cardFile1.fail() || cardFile2.fail() ){
cout << "Could not open file " << argc[2];
return 1;
}
CardBST aliceBST, bobBST; //alice is cardFile1, bob is CardFile 2
//Read each file and insert them into a tree
while (getline (cardFile1, line) && (line.length() > 0)){
aliceBST.insert(convertStringToCard(line));
}
cardFile1.close();
while (getline (cardFile2, line) && (line.length() > 0)){
bobBST.insert(convertStringToCard(line));
}
cardFile2.close();
CardBST::Card aliceGuess = aliceBST.getSmallestCard(); //starting at Alice's smallest card, use this to iterate forward hand
CardBST::Card bobGuess = bobBST.getLargestCard(); //starting at Bob's largest card, use this to iterate back through hand
while(!(aliceGuess== CardBST::Card()) && !(bobGuess == CardBST::Card())) {
while(!(aliceGuess == CardBST::Card())) {
if(bobBST.contains(aliceGuess)) { //Alice guesses
cout << "Alice picked matching card " << aliceGuess.suit << " " << aliceGuess.value << endl;
aliceBST.remove(aliceGuess);
bobBST.remove(aliceGuess);
aliceGuess = aliceBST.getSmallestCard();
bobGuess = bobBST.getLargestCard();
break;
}
else
aliceGuess = aliceBST.getSuccessor(aliceGuess);
}
while(!(bobGuess == CardBST::Card())) {
if(aliceBST.contains(bobGuess)) { //Bob guesses
cout << "Bob picked matching card " << bobGuess.suit << " " << bobGuess.value << endl;
aliceBST.remove(bobGuess);
bobBST.remove(bobGuess);
bobGuess = bobBST.getLargestCard();
aliceGuess = aliceBST.getSmallestCard();
break;
}
else
bobGuess = bobBST.getPredecessor(bobGuess);
}
}
cout << endl << "Alice's cards:" << endl;
aliceBST.printInOrder();
cout << endl;
cout << "Bob's cards:" << endl;
bobBST.printInOrder();
return 0;
}