-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
89 lines (85 loc) · 3.31 KB
/
Board.cpp
File metadata and controls
89 lines (85 loc) · 3.31 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
83
84
85
86
87
88
89
#include "Board.h"
void Board::createGame(string deckFile){
tableau.createTableau(deckFile);
foundation.createFoundation();
stock.createStock(tableau.pile.cards);
}
void Board::applyCommands(string commandFile, string outputFile){
ofstream output;
output.open (outputFile);
ifstream file(commandFile);
string str;
printBoard(&output);
while (getline(file, str)){
output << endl;
output << str << endl;
if(str == "exit"){
output << endl << "****************************************" << endl << endl << "Game Over!" << endl;
exit(0);
}
else if(str.substr(0,4)==("open")){
if(str=="open from stock"){
if(!stock.openStock()){
output << endl << "Invalid Move!" << endl;
}
}
else{
if(!tableau.pile.openPile(atoi(str.substr(5,1).c_str()))){
output << endl << "Invalid Move!" << endl;
}
}
}
else if(str.substr(0,7) == "move to"){
if(str.substr(19,5) == "waste"){
if(!stock.moveToFoundation(&foundation)){
output << endl << "Invalid Move!" << endl;
}
}
else{
if(!tableau.pile.moveToFoundation(atoi(str.substr(24,1).c_str()), &foundation)){
output << endl << "Invalid Move!" << endl;
}
}
}
else if(str.length()<=16 && (str.substr(0,9)=="move pile")){
if(str.length()<16){
if(!tableau.pile.movePile(atoi(str.substr(10,1).c_str()),
atoi(str.substr(12,1).c_str()), atoi(str.substr(14,1).c_str()))){
output << endl << "Invalid Move!" << endl;
}
}
else{
if(!tableau.pile.movePile(atoi(str.substr(10,1).c_str()),
atoi(str.substr(12,2).c_str()), atoi(str.substr(15,1).c_str()))){
output << endl <<"Invalid Move!" << endl;
}
}
}
else if(str.length()<13 && str.substr(0,10)=="move waste"){
if(!stock.moveWaste(atoi(str.substr(11,1).c_str()) ,&tableau.pile)){
output << endl << "Invalid Move!" << endl;
}
}
else if (str.length()>15 && str.substr(0,15)=="move foundation"){
if(!tableau.pile.moveFoundationToPile(atoi(str.substr(16,1).c_str()), atoi(str.substr(18,1).c_str()), &foundation)){
output << endl << "Invalid Move!" << endl;
}
}
output << endl << "****************************************" << endl << endl;
printBoard(&output);
}
if(foundation.checkWin()){
output << endl << endl << "****************************************" << endl << endl;
output << "You Win!" << endl << endl << "Game Over!" << endl;
}
file.close();
output.close();
}
void Board::printBoard(ofstream* output){
stock.printStock(output);
stock.printWaste(output);
*output << " ";
foundation.printFoundation(output);
*output << endl;
tableau.pile.printPile(output);
}