Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8058 | ConLenov | 1 | #include |
2 | #include |
||
3 | #include |
||
4 | #include |
||
5 | #include |
||
6 | |||
7 | struct Card_Node{ |
||
8 | char color; |
||
9 | char suit; |
||
10 | int value; // Value = 1 for A, 11 for J, 12 for Q, 13 for K |
||
11 | bool isup; // true for UP, false for DOWN |
||
12 | Card_Node *next; |
||
13 | }; |
||
14 | |||
15 | struct List_Node{ |
||
16 | int listno;//board and foundation lists have indexes |
||
17 | List_Node *next; |
||
18 | Card_Node *cards; |
||
19 | }; |
||
20 | |||
21 | struct List{ |
||
22 | Card_Node *top_head;//Top list head |
||
23 | List_Node *foundation_head;//points first foundation list |
||
24 | List_Node *board_head;//points first board list |
||
25 | FILE *fptr; |
||
26 | |||
27 | void create();// Includes create toplist, boardlists and foundlists |
||
28 | void create_toplist(); |
||
29 | void create_boardlists(); |
||
30 | void create_foundlists(); |
||
31 | void printkart(Card_Node *kart);// prints datas of one kart node |
||
32 | void printlists();// Prints all lists |
||
33 | void printmenu();//Prints choice menu |
||
34 | bool addcardtolist(List_Node *selectedlist, Card_Node *transferedcard, int whichlisttype);//Adds transferedcard to selectedlist, whichlisttype is entered by program |
||
35 | void goster_TopToFoundation();//Gets input and calls related function |
||
36 | void TopToFoundation(char s_color, char s_suit, int s_value);// Moves a card top list to foundation list |
||
37 | void goster_TopToBoard();//Gets input and calls related function |
||
38 | void TopToBoard(int listindex, char s_color, char s_suit, int s_value);//Moves a card from top list to board list |
||
39 | void goster_BoardToBoard();//Gets input and calls related function |
||
40 | void BoardToBoard(int fromwhere, int towhere, char s_color, char s_suit, int s_value);//Moves a card(s) from a fromwhere.list to towhere.list |
||
41 | void goster_BoardToFoundation();//Gets input and calls related function |
||
42 | void BoardToFoundation(int fromwhere);//Moves a last card in fromwhere.list of boardlist to foundation list |
||
43 | void islem_yap();//Wants user to select choice from menu, and calls related functions |
||
44 | bool istopempty, isboardempty;// For controlling end of game |
||
45 | void clear_screen();//Clears screen both windows and linux system |
||
46 | void close();//Deletes all linked lists and linkedlists's all nodes, closes read txt file |
||
47 | }; |
||
48 | |||
49 | List islem; |
||
50 | using namespace std; |
||
51 | |||
52 | int main() |
||
53 | { |
||
54 | char secim; |
||
55 | islem.create(); |
||
56 | if(!islem.fptr)//If cannot find solitaire.txt file, close the program |
||
57 | return 0; |
||
58 | |||
59 | islem.istopempty = false;//If the top list is empty, this value will be true |
||
60 | islem.isboardempty = false;//If the board list is empty, this value will be true |
||
61 | |||
62 | while(1){ |
||
63 | islem.printlists(); |
||
64 | islem.printmenu(); |
||
65 | cin >> secim; |
||
66 | cin.ignore(10000, '\n'); |
||
67 | |||
68 | if(secim == '1'){ |
||
69 | islem.goster_TopToFoundation(); |
||
70 | cin.ignore(10000, '\n'); |
||
71 | getchar(); |
||
72 | } |
||
73 | else if(secim == '2'){ |
||
74 | islem.goster_TopToBoard(); |
||
75 | cin.ignore(10000, '\n'); |
||
76 | getchar(); |
||
77 | } |
||
78 | else if(secim == '3'){ |
||
79 | islem.goster_BoardToBoard(); |
||
80 | cin.ignore(10000, '\n'); |
||
81 | getchar(); |
||
82 | } |
||
83 | else if(secim == '4'){ |
||
84 | islem.goster_BoardToFoundation(); |
||
85 | cin.ignore(10000, '\n'); |
||
86 | } |
||
87 | else if(secim == '9'){ |
||
88 | islem.close(); |
||
89 | printf("\n\nThe game is closed and all cards are deleted from memory.Press enter to exit"); |
||
90 | cin.ignore(10000, '\n'); |
||
91 | getchar(); |
||
92 | break; |
||
93 | } |
||
94 | |||
95 | else |
||
96 | cout << "Invalid choice" << endl; |
||
97 | |||
98 | if(!islem.top_head)// checking top list empty or not |
||
99 | islem.istopempty = true; |
||
100 | |||
101 | List_Node *traverse; |
||
102 | traverse = islem.board_head; |
||
103 | int counter = 0; |
||
104 | while(traverse){//checking board lists is empty or not |
||
105 | if(!traverse->cards) |
||
106 | counter++; |
||
107 | traverse = traverse->next; |
||
108 | if(counter == 7)// counter is 7 if all of 7 boardlists are empty |
||
109 | islem.isboardempty = true; |
||
110 | } |
||
111 | |||
112 | /*When game is completed prints message to user*/ |
||
113 | if(islem.isboardempty && islem.istopempty){//Stop while loop when board and top lists are empty |
||
114 | printf("\n\n\n\t\t\tYOU WIN!!!! THE GAME IS FINISHED! CONGRATULATIONS!\n");// game over message |
||
115 | printf("\t\t\tYOU WIN!!!! THE GAME IS FINISHED! CONGRATULATIONS!\n"); |
||
116 | printf("\t\t\tYOU WIN!!!! THE GAME IS FINISHED! CONGRATULATIONS!\n"); |
||
117 | printf("\nAll cards are deleted from memory.Press enter to exit"); |
||
118 | cin.ignore(1000, '\n'); |
||
119 | islem.close(); |
||
120 | getchar(); |
||
121 | break; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | return 0; |
||
126 | } |
||
127 | |||
128 | void List::create(){ |
||
129 | fptr = fopen("solitaire.txt", "r+"); |
||
130 | if(!fptr){ |
||
131 | cout << "The 'solitaire.txt' file cannot be found. Program will be closed."<< endl; |
||
132 | getchar(); |
||
133 | return; |
||
134 | } |
||
135 | fseek(fptr, 0, SEEK_SET); |
||
136 | create_toplist(); |
||
137 | create_boardlists(); |
||
138 | create_foundlists(); |
||
139 | }//end create function |
||
140 | |||
141 | void List::create_toplist(){ |
||
142 | Card_Node *newnode, *final; |
||
143 | char tempcolor, tempsuit, tempvalue[4], tempisup[4], garbage[10];//temporary values of color,suit,isup. value will be changed to integer and isup(Up or Down) will be changed to boolean variables |
||
144 | top_head = NULL; |
||
145 | |||
146 | while(1){//This while creates top_list with linked list |
||
147 | newnode = new Card_Node; |
||
148 | newnode->next = NULL; |
||
149 | fscanf(fptr, "%c", &tempcolor); |
||
150 | if(tempcolor == '*'){//first star has read |
||
151 | fscanf(fptr, "%6c", garbage);//passes other five stars and goes new line |
||
152 | break; |
||
153 | } |
||
154 | else |
||
155 | newnode->color = tempcolor; |
||
156 | |||
157 | fscanf(fptr, " %c %s %s ", &tempsuit, tempvalue, tempisup); |
||
158 | newnode->suit = tempsuit; |
||
159 | |||
160 | /*Changing type of value char array to integer*/ |
||
161 | if(tempvalue[0] == 'A') newnode->value = 1; |
||
162 | else if(tempvalue[0] == 'J')newnode->value = 11; |
||
163 | else if(tempvalue[0] == 'Q')newnode->value = 12; |
||
164 | else if(tempvalue[0] == 'K')newnode->value = 13; |
||
165 | else |
||
166 | sscanf(tempvalue, "%d", &newnode->value); |
||
167 | |||
168 | /*Changing type of isup char array to boolean*/ |
||
169 | if(strcmp(tempisup, "Up") == 0) |
||
170 | newnode->isup = true; |
||
171 | if(strcmp(tempisup, "Down") == 0) |
||
172 | newnode->isup = false; |
||
173 | |||
174 | if(top_head == NULL){//add first node to empty top list |
||
175 | top_head = newnode; |
||
176 | final = top_head; |
||
177 | } |
||
178 | else{// add node to not empty list |
||
179 | final->next = newnode; |
||
180 | final = final->next; |
||
181 | } |
||
182 | }//end while |
||
183 | } |
||
184 | |||
185 | void List::create_boardlists(){ |
||
186 | Card_Node *newnode, *final;// final points to last node of card list |
||
187 | List_Node *newboardlist, *boardlist_final;// boardlist_final points to last node of board list |
||
188 | char tempcolor, tempsuit, tempvalue[4], tempisup[8], garbage[10]; |
||
189 | |||
190 | int index = 1;// This index represents nth board list |
||
191 | |||
192 | newboardlist = new List_Node;// creating first boardlist node |
||
193 | board_head = newboardlist; |
||
194 | boardlist_final = newboardlist; |
||
195 | newboardlist->listno = index++; |
||
196 | newboardlist->next = NULL; |
||
197 | newboardlist->cards = NULL; |
||
198 | |||
199 | while(!feof(fptr)){//This while creates board multi linked lists |
||
200 | newnode = new Card_Node; |
||
201 | newnode->next = NULL; |
||
202 | fscanf(fptr, "%c", &tempcolor); |
||
203 | if(tempcolor == '*'){//first star has read |
||
204 | fscanf(fptr, "%6c", garbage);//passes other five stars and goes new line |
||
205 | newboardlist = new List_Node; |
||
206 | newboardlist->listno = index++; |
||
207 | newboardlist->next = NULL; |
||
208 | newboardlist->cards = NULL; |
||
209 | boardlist_final->next = newboardlist; |
||
210 | boardlist_final = boardlist_final->next; |
||
211 | continue; |
||
212 | } |
||
213 | else |
||
214 | newnode->color = tempcolor; |
||
215 | |||
216 | fscanf(fptr, " %c %s %s ", &tempsuit, tempvalue, tempisup); |
||
217 | newnode->suit = tempsuit; |
||
218 | if(tempvalue[0] == 'A') newnode->value = 1; |
||
219 | else if(tempvalue[0] == 'J')newnode->value = 11; |
||
220 | else if(tempvalue[0] == 'Q')newnode->value = 12; |
||
221 | else if(tempvalue[0] == 'K')newnode->value = 13; |
||
222 | else |
||
223 | sscanf(tempvalue, "%d", &newnode->value); |
||
224 | |||
225 | if(strcmp(tempisup, "Up") == 0) |
||
226 | newnode->isup = true; |
||
227 | if(strcmp(tempisup, "Down") == 0) |
||
228 | newnode->isup = false; |
||
229 | |||
230 | if(boardlist_final->cards == NULL){//add first node to empty board list |
||
231 | boardlist_final->cards = newnode; |
||
232 | final = boardlist_final->cards; |
||
233 | } |
||
234 | else{// add to not empty board list |
||
235 | final->next = newnode; |
||
236 | final = final->next; |
||
237 | } |
||
238 | }//end while |
||
239 | } |
||
240 | |||
241 | void List::create_foundlists(){ |
||
242 | foundation_head = NULL; |
||
243 | List_Node *newfoundlist, *foundlist_final; |
||
244 | int index = 1;// Spades list index = 1, Hearts index = 2, Diamonds = 3, Clubs = 4 |
||
245 | |||
246 | /*Initializing Spades list*/ |
||
247 | newfoundlist = new List_Node; |
||
248 | newfoundlist->cards = NULL; |
||
249 | newfoundlist->next = NULL; |
||
250 | newfoundlist->listno = index++; |
||
251 | foundation_head = newfoundlist; |
||
252 | foundlist_final = newfoundlist; |
||
253 | |||
254 | /*Initializing other three lists*/ |
||
255 | for(int i = 0 ; i <3; i++) { |
||
256 | newfoundlist = new List_Node; |
||
257 | newfoundlist->cards = NULL; |
||
258 | newfoundlist->next = NULL; |
||
259 | newfoundlist->listno = index++; |
||
260 | foundlist_final->next = newfoundlist; |
||
261 | foundlist_final = foundlist_final->next; |
||
262 | }//end for |
||
263 | }// end function |
||
264 | |||
265 | void List::printkart(Card_Node *kart){//prints datas of kart node |
||
266 | if(!kart->isup ){//Hide card if it is down |
||
267 | printf("X"); |
||
268 | return; |
||
269 | } |
||
270 | char a; |
||
271 | if(kart->value == 1) a='A'; |
||
272 | else if(kart->value == 11) a='J'; |
||
273 | else if(kart->value == 12) a='Q'; |
||
274 | else if(kart->value == 13) a='K'; |
||
275 | else a = '\0'; |
||
276 | |||
277 | if(!a)printf("%c,%c,%d", kart->color, kart->suit, kart->value); |
||
278 | else printf("%c,%c,%c", kart->color, kart->suit, a); |
||
279 | } |
||
280 | |||
281 | void List::printlists(){ |
||
282 | clear_screen(); |
||
283 | Card_Node *ct[7];// Board List Card Node Traverser; ct[0] for 1.list, ct[1] for 2.list .... |
||
284 | Card_Node *foundct[4];//Found List Card Node Traverser, foundct[0] = Spades, foundct[1] = Hearts, foundct[2] = Diamonds, foundct[3] = Clubs |
||
285 | Card_Node *topct;// TopList Card Traverser |
||
286 | List_Node *listtraverse;// List Node Traverser |
||
287 | |||
288 | |||
289 | cout << "Top List:" << endl; |
||
290 | topct = top_head; |
||
291 | while(topct){ |
||
292 | printkart(topct); |
||
293 | printf("|"); |
||
294 | topct = topct->next; |
||
295 | } |
||
296 | |||
297 | cout << endl << "\nBoard Lists:" << endl; |
||
298 | for(int i=1; i <8; i++) |
||
299 | cout << i << ".List\t"; |
||
300 | cout < |
||
301 | |||
302 | listtraverse = board_head; |
||
303 | for(int i = 0; i < 7; i++){// Initalizing ct[] pointers |
||
304 | ct[i] = listtraverse->cards; |
||
305 | listtraverse = listtraverse->next; |
||
306 | } |
||
307 | |||
308 | /*Printing Board List's Cards*/ |
||
309 | for(int i = 0; i < 19; i++){//this for loop traverses lists and goes 19 times down, a list can include cards up to 19 (6 down cards + 13 up cards) |
||
310 | for(int j = 0; j < 7; j++) |
||
311 | { |
||
312 | if(ct[j]){// if ct[j] is not null, print it and go to next node |
||
313 | printkart(ct[j]); |
||
314 | printf("\t"); |
||
315 | ct[j] = ct[j]->next; |
||
316 | } |
||
317 | else// if ct[j] is null, print a tab |
||
318 | printf("\t"); |
||
319 | } |
||
320 | if(ct[0] || ct[1] || ct[2] || ct[3] || ct[4] || ct[5] || ct[6])// After printing a line; |
||
321 | printf("\n");//if at least one card is not null: go new line |
||
322 | else |
||
323 | break;// if all cards in line are null: break outer for loop |
||
324 | }//end outer for |
||
325 | |||
326 | cout << endl << "Foundation Lists:" << endl; |
||
327 | cout << "Spades\tHearts\tDiamnds\tClubs" << endl; |
||
328 | |||
329 | listtraverse = foundation_head; |
||
330 | for(int i = 0; i < 4; i++){// Initalizing foundct[] pointers |
||
331 | foundct[i] = listtraverse->cards; |
||
332 | listtraverse = listtraverse->next; |
||
333 | } |
||
334 | |||
335 | for(int i = 0; i < 13; i++){//this for loop traverses foundation lists (max 13 cards can be in a foundation list) |
||
336 | for(int j = 0; j < 4; j++) |
||
337 | { |
||
338 | if(foundct[j]){// if ct[j] is not null, print it and go to next node |
||
339 | printkart(foundct[j]); |
||
340 | printf("\t"); |
||
341 | foundct[j] = foundct[j]->next; |
||
342 | } |
||
343 | else// if foundct[j] is null, print a tab |
||
344 | printf("\t"); |
||
345 | } |
||
346 | if(foundct[0] || foundct[1] || foundct[2] || foundct[3])// After printing a line; |
||
347 | printf("\n");//if at least one card is not null: go new line |
||
348 | else |
||
349 | break;// if all cards in line are null: break outer for loop |
||
350 | }//end outer for |
||
351 | printf("\n\n"); |
||
352 | }//end function |
||
353 | |||
354 | void List::printmenu(){ |
||
355 | cout << "Choose an operation:" << endl; |
||
356 | cout << "\t1. Select from Top List to Foundation Lists" << endl; |
||
357 | cout << "\t2. Select from Top List to Board Lists" << endl; |
||
358 | cout << "\t3. Move on the Board Lists" << endl; |
||
359 | cout << "\t4. Move from Board List to Foundation List" << endl; |
||
360 | cout << "Please enter your choice (1, 2, 3, or 4), (enter 9 to exit):" ; |
||
361 | } |
||
362 | |||
363 | bool List::addcardtolist(List_Node *selectedlist, Card_Node *transferedcard, int whichlisttype){//whichlisttype is not related with user, it is automatically entered by progra |
||
364 | Card_Node *cardtraverser; |
||
365 | cardtraverser= selectedlist->cards; |
||
366 | if(whichlisttype == 1){//whichlisttype is 1 for top to board list |
||
367 | if(cardtraverser == NULL && transferedcard->value == 13){// if list is empty card's value must be K=13 |
||
368 | selectedlist->cards = transferedcard; |
||
369 | transferedcard->next = NULL; |
||
370 | return true; |
||
371 | } |
||
372 | if(!cardtraverser)// cardtraverse cannot be null here, can be null only for value K=13 |
||
373 | return false; |
||
374 | while(cardtraverser->next) |
||
375 | cardtraverser = cardtraverser->next; |
||
376 | |||
377 | if(!cardtraverser->isup){// if the card is down, color and value between two cards are not important |
||
378 | cardtraverser->next = transferedcard; |
||
379 | transferedcard->next = NULL; |
||
380 | return true; |
||
381 | } |
||
382 | if(cardtraverser->isup)// if the card is up, color and value between two cards are important |
||
383 | if(!(transferedcard->color == cardtraverser->color))// if colors between two adjacent cards are different |
||
384 | if(cardtraverser->value - transferedcard->value == 1 ){//list's last value - transfered card's value - must be 1 |
||
385 | cardtraverser->next = transferedcard; |
||
386 | transferedcard->next = NULL; |
||
387 | return true; |
||
388 | } |
||
389 | } |
||
390 | |||
391 | if(whichlisttype == 2){// whichlisttype is 2 for moving top to foundation list |
||
392 | if(cardtraverser == NULL && transferedcard->value == 1){// if list is empty card's value must be A=1 |
||
393 | selectedlist->cards = transferedcard; |
||
394 | transferedcard->next = NULL; |
||
395 | return true; |
||
396 | } |
||
397 | if(!cardtraverser)// cardtraverse cannot be null here, can be null only for value A=1 |
||
398 | return false; |
||
399 | |||
400 | while(cardtraverser->next) |
||
401 | cardtraverser = cardtraverser->next; |
||
402 | if(transferedcard->value - cardtraverser->value == 1){//if list is not empty, list's last value - card's value must be 1 |
||
403 | cardtraverser->next = transferedcard; |
||
404 | transferedcard->next = NULL; |
||
405 | return true; |
||
406 | } |
||
407 | } |
||
408 | |||
409 | if(whichlisttype == 3){//whichlisttype is 3 for board to board list |
||
410 | if(cardtraverser == NULL && transferedcard->value == 13){// if list is empty card's value must be K=13 |
||
411 | selectedlist->cards = transferedcard; |
||
412 | return true; |
||
413 | } |
||
414 | if(!cardtraverser)// cardtraverse cannot be null here, can be null only for value K=13 |
||
415 | return false; |
||
416 | while(cardtraverser->next) |
||
417 | cardtraverser = cardtraverser->next; |
||
418 | |||
419 | if(!cardtraverser->isup){// if the card is down, color and value between two cards are not important |
||
420 | cardtraverser->next = transferedcard; |
||
421 | return true; |
||
422 | } |
||
423 | if(cardtraverser->isup)// if the card is up, color and value between two cards are important |
||
424 | if(!(transferedcard->color == cardtraverser->color))// if colors between two adjacent cards are different |
||
425 | if(cardtraverser->value - transferedcard->value == 1 ){//list's last value - transfered card's value - must be 1 |
||
426 | cardtraverser->next = transferedcard; |
||
427 | return true; |
||
428 | } |
||
429 | } |
||
430 | if(whichlisttype == 4){// whichlisttype is 4 for moving board to foundation list |
||
431 | if(cardtraverser == NULL && transferedcard->value == 1){// if list is empty card's value must be A=1 |
||
432 | selectedlist->cards = transferedcard; |
||
433 | transferedcard->next = NULL; |
||
434 | return true; |
||
435 | } |
||
436 | if(!cardtraverser)// cardtraverse cannot be null here, can be null only for value A=1 |
||
437 | return false; |
||
438 | |||
439 | while(cardtraverser->next) |
||
440 | cardtraverser = cardtraverser->next; |
||
441 | if(transferedcard->value - cardtraverser->value == 1){//if list is not empty, list's last value - card's value must be 1 |
||
442 | cardtraverser->next = transferedcard; |
||
443 | transferedcard->next = NULL; |
||
444 | return true; |
||
445 | } |
||
446 | } |
||
447 | |||
448 | return false; |
||
449 | } |
||
450 | |||
451 | void List::goster_TopToFoundation(){// wants input from use |
||
452 | char Symbol_of_colors, Symbol_of_suits, tempvalue[4]; |
||
453 | int Symbol_of_numbers; |
||
454 | cout << endl << "Select a card from Top List:"; |
||
455 | scanf("%c %c %s", &Symbol_of_colors, &Symbol_of_suits, tempvalue); |
||
456 | |||
457 | if(tempvalue[0] == 'A') Symbol_of_numbers = 1; |
||
458 | else if(tempvalue[0] == 'J')Symbol_of_numbers = 11; |
||
459 | else if(tempvalue[0] == 'Q')Symbol_of_numbers = 12; |
||
460 | else if(tempvalue[0] == 'K')Symbol_of_numbers = 13; |
||
461 | else |
||
462 | sscanf(tempvalue, "%d", &Symbol_of_numbers); |
||
463 | |||
464 | TopToFoundation(Symbol_of_colors, Symbol_of_suits, Symbol_of_numbers); |
||
465 | } |
||
466 | |||
467 | void List::TopToFoundation(char s_color, char s_suit, int s_value){ |
||
468 | List_Node *listtraverse; |
||
469 | Card_Node *willbemoved = NULL, *cardtraverse, *cardtail, *temptop_head = top_head; |
||
470 | |||
471 | if(top_head == NULL){ |
||
472 | cout << "Top list is empty, you cannot make this move." << endl; |
||
473 | return; |
||
474 | } |
||
475 | |||
476 | cardtraverse = top_head; |
||
477 | cardtail = top_head; |
||
478 | |||
479 | while(cardtraverse){ |
||
480 | if(cardtraverse->color == s_color && cardtraverse->suit == s_suit && cardtraverse->value == s_value){//checking if card's datas are same with user entered datas |
||
481 | willbemoved = cardtraverse; |
||
482 | break; |
||
483 | } |
||
484 | cardtail = cardtraverse; |
||
485 | cardtraverse = cardtraverse->next; |
||
486 | }//end while |
||
487 | |||
488 | if(willbemoved == NULL){ |
||
489 | cout << "Entered card is not in the top list!" << endl; |
||
490 | return; |
||
491 | } |
||
492 | |||
493 | int number; |
||
494 | listtraverse = foundation_head; |
||
495 | if(willbemoved->suit == 'S') number = 0; |
||
496 | if(willbemoved->suit == 'H') number = 1; |
||
497 | if(willbemoved->suit == 'D') number = 2; |
||
498 | if(willbemoved->suit == 'C') number = 3; |
||
499 | |||
500 | for(int i = 0; i < number; i++)//Moving to foundation list according to card's suit |
||
501 | listtraverse=listtraverse->next; |
||
502 | |||
503 | /*Cutting the connection of willbemoved node with other nodes in list*/ |
||
504 | if(cardtraverse == cardtail)//willbemoved node is first node |
||
505 | temptop_head = temptop_head->next; |
||
506 | else // willbemoved node is not first node |
||
507 | cardtail->next = cardtraverse->next; |
||
508 | |||
509 | if(addcardtolist(listtraverse, willbemoved, 2)){// if movement successful, top_head points second card node |
||
510 | top_head = temptop_head; |
||
511 | cout << "Movement is successful!" << endl; |
||
512 | } |
||
513 | else{// if not successful |
||
514 | if(!(cardtraverse == cardtail))// if not first node, the connection between cardtail and willbemoved is recovered |
||
515 | cardtail->next = willbemoved; |
||
516 | cout << "Wrong Movement!" << endl; |
||
517 | } |
||
518 | } |
||
519 | |||
520 | void List::goster_TopToBoard(){// wants input from user for moving card top list to board lis |
||
521 | char Symbol_of_colors, Symbol_of_suits, tempvalue[4]; |
||
522 | int Symbol_of_numbers, a; |
||
523 | cout << "Select a card from Top List:"; |
||
524 | scanf("%c %c %s", &Symbol_of_colors, &Symbol_of_suits, tempvalue); |
||
525 | |||
526 | if(tempvalue[0] == 'A') Symbol_of_numbers = 1; |
||
527 | else if(tempvalue[0] == 'J')Symbol_of_numbers = 11; |
||
528 | else if(tempvalue[0] == 'Q')Symbol_of_numbers = 12; |
||
529 | else if(tempvalue[0] == 'K')Symbol_of_numbers = 13; |
||
530 | else |
||
531 | sscanf(tempvalue, "%d", &Symbol_of_numbers); |
||
532 | |||
533 | cout << "Select the number of the destination Board List:"; |
||
534 | scanf("%d", &a); |
||
535 | TopToBoard(a, Symbol_of_colors, Symbol_of_suits, Symbol_of_numbers); |
||
536 | |||
537 | } |
||
538 | |||
539 | void List::TopToBoard(int listindex, char s_color, char s_suit, int s_value){ |
||
540 | List_Node *listtraverse; |
||
541 | Card_Node *willbemoved = NULL, *cardtraverse, *cardtail, *temptop_head = top_head; |
||
542 | |||
543 | if(top_head == NULL){ |
||
544 | cout << "Top list is empty, you cannot make this move." << endl; |
||
545 | return; |
||
546 | } |
||
547 | |||
548 | cardtraverse = top_head; |
||
549 | cardtail = top_head; |
||
550 | |||
551 | while(cardtraverse){ |
||
552 | if(cardtraverse->color == s_color && cardtraverse->suit == s_suit && cardtraverse->value == s_value){ |
||
553 | willbemoved = cardtraverse; |
||
554 | break; |
||
555 | } |
||
556 | cardtail = cardtraverse; |
||
557 | cardtraverse = cardtraverse->next; |
||
558 | }//end while |
||
559 | |||
560 | if(willbemoved == NULL){ |
||
561 | cout << "Entered card is not in the top list!" << endl; |
||
562 | return; |
||
563 | } |
||
564 | |||
565 | listtraverse = board_head; |
||
566 | for(int i = 1; i < listindex; i++)//Moving to board list according to listindex entered by user |
||
567 | listtraverse = listtraverse->next; |
||
568 | |||
569 | /*Cutting the connection of willbemoved node with other nodes in list*/ |
||
570 | if(cardtraverse == cardtail)//willbemoved node is first node |
||
571 | temptop_head = temptop_head->next; |
||
572 | else // willbemoved node is not first node |
||
573 | cardtail->next = cardtraverse->next; |
||
574 | |||
575 | if(addcardtolist(listtraverse, willbemoved, 1)){// if movement successful, top_head points second card node |
||
576 | top_head = temptop_head; |
||
577 | cout << "Movement is successful!" << endl; |
||
578 | } |
||
579 | else{// if not successful |
||
580 | if(!(cardtraverse == cardtail))// if not first node, the connection between cardtail and willbemoved is recovered |
||
581 | cardtail->next = willbemoved; |
||
582 | cout << "Wrong Movement!" << endl; |
||
583 | } |
||
584 | } |
||
585 | |||
586 | void List::goster_BoardToBoard(){ |
||
587 | char Symbol_of_colors, Symbol_of_suits, tempvalue[4]; |
||
588 | int Symbol_of_numbers, source, destination; |
||
589 | |||
590 | cout << "Select the number of the source Board List:"; |
||
591 | scanf("%d", &source); |
||
592 | cout << "Select the number of the destination Board List:"; |
||
593 | scanf("%d", &destination); |
||
594 | |||
595 | cin.ignore(1000, '\n'); |
||
596 | cout << "Select a card from the selected source Board List to move:"; |
||
597 | scanf("%c %c %s", &Symbol_of_colors, &Symbol_of_suits, tempvalue); |
||
598 | |||
599 | if(tempvalue[0] == 'A') Symbol_of_numbers = 1; |
||
600 | else if(tempvalue[0] == 'J')Symbol_of_numbers = 11; |
||
601 | else if(tempvalue[0] == 'Q')Symbol_of_numbers = 12; |
||
602 | else if(tempvalue[0] == 'K')Symbol_of_numbers = 13; |
||
603 | else |
||
604 | sscanf(tempvalue, "%d", &Symbol_of_numbers); |
||
605 | BoardToBoard(source, destination, Symbol_of_colors, Symbol_of_suits, Symbol_of_numbers); |
||
606 | } |
||
607 | |||
608 | void List::BoardToBoard(int fromwhere, int towhere, char s_color, char s_suit, int s_value){ |
||
609 | List_Node *sourcelisttraverse = board_head, *targetlisttraverse = board_head; |
||
610 | Card_Node *willbemoved = NULL, *cardtraverse, *cardtail, *temp_head; |
||
611 | |||
612 | for(int i = 1; i < fromwhere; i++)// list goes fromwhere times next |
||
613 | sourcelisttraverse = sourcelisttraverse->next; |
||
614 | temp_head = sourcelisttraverse->cards; |
||
615 | cardtraverse = temp_head; |
||
616 | cardtail = temp_head; |
||
617 | |||
618 | |||
619 | while(cardtraverse){ |
||
620 | if(cardtraverse->isup)// Dont move cards if the entered card is down |
||
621 | if(cardtraverse->color == s_color && cardtraverse->suit == s_suit && cardtraverse->value == s_value){ |
||
622 | willbemoved = cardtraverse; |
||
623 | break; |
||
624 | } |
||
625 | cardtail = cardtraverse; |
||
626 | cardtraverse = cardtraverse->next; |
||
627 | }//end while |
||
628 | |||
629 | if(willbemoved == NULL){ |
||
630 | cout << "Wrong Movement!" << endl; |
||
631 | return; |
||
632 | } |
||
633 | |||
634 | for(int i = 1; i < towhere; i++)// list goes towhere times next |
||
635 | targetlisttraverse = targetlisttraverse->next; |
||
636 | |||
637 | /*Cutting the connection of willbemoved node with other nodes in list*/ |
||
638 | if(cardtraverse == cardtail)//willbemoved node is first node |
||
639 | temp_head = NULL; |
||
640 | else // willbemoved node is not first node |
||
641 | cardtail->next = NULL; |
||
642 | |||
643 | if(addcardtolist(targetlisttraverse, willbemoved, 3)){// if movement successful, top_head points second card node |
||
644 | sourcelisttraverse->cards = temp_head; |
||
645 | if(!cardtail->isup)// if the card behind of the moved card in source is DOWN, turn it to UP |
||
646 | cardtail->isup = true; |
||
647 | cout << "Movement is successful!" << endl; |
||
648 | return; |
||
649 | } |
||
650 | if(!(cardtraverse == cardtail)){// if not first node, the connection between cardtail and willbemoved is recovered |
||
651 | cardtail->next = willbemoved; |
||
652 | cout << "Wrong Movement!" << endl; |
||
653 | } |
||
654 | |||
655 | } |
||
656 | |||
657 | void List::goster_BoardToFoundation(){ |
||
658 | int source; |
||
659 | cout << "Select the number of the source Board List:"; |
||
660 | scanf("%d", &source); |
||
661 | cin.ignore(1000, '\n'); |
||
662 | BoardToFoundation(source); |
||
663 | } |
||
664 | |||
665 | void List::BoardToFoundation(int fromwhere){ |
||
666 | List_Node *sourcelisttraverse = board_head, *targetlisttraverse = foundation_head; |
||
667 | Card_Node *willbemoved = NULL, *cardtraverse, *cardtail, *temp_head; |
||
668 | for(int i = 1; i < fromwhere; i++)//goes to list user wants |
||
669 | sourcelisttraverse = sourcelisttraverse->next; |
||
670 | temp_head = sourcelisttraverse->cards; |
||
671 | cardtraverse = temp_head; |
||
672 | cardtail = temp_head; |
||
673 | |||
674 | while(cardtraverse->next){// go to last card |
||
675 | cardtail = cardtraverse; |
||
676 | cardtraverse = cardtraverse->next; |
||
677 | }//end while |
||
678 | |||
679 | willbemoved = cardtraverse;// last card in card list |
||
680 | |||
681 | |||
682 | int number; |
||
683 | if(willbemoved->suit == 'S') number = 0; |
||
684 | if(willbemoved->suit == 'H') number = 1; |
||
685 | if(willbemoved->suit == 'D') number = 2; |
||
686 | if(willbemoved->suit == 'C') number = 3; |
||
687 | |||
688 | for(int i = 0; i < number; i++)//Moving between foundation lists according to card's suit |
||
689 | targetlisttraverse=targetlisttraverse->next; |
||
690 | |||
691 | /*Cutting the connection of willbemoved node with other nodes in list*/ |
||
692 | if(cardtraverse == cardtail)//willbemoved node is first node |
||
693 | temp_head = NULL; |
||
694 | else // willbemoved node is not first node |
||
695 | cardtail->next = NULL; |
||
696 | |||
697 | if(addcardtolist(targetlisttraverse, willbemoved, 4)){// if movement successful, board list's card head points second card node |
||
698 | sourcelisttraverse->cards = temp_head; |
||
699 | if(!cardtail->isup)// if the card behind of the moved card in source is DOWN, turn it to UP |
||
700 | cardtail->isup = true; |
||
701 | cout << "Movement is successful!" << endl; |
||
702 | return; |
||
703 | } |
||
704 | if(!(cardtraverse == cardtail)){// if moving not successful and card is not first node, the connection between cardtail and willbemoved is recovered |
||
705 | cardtail->next = willbemoved; |
||
706 | cout << "Wrong Movement!" << endl; |
||
707 | } |
||
708 | } |
||
709 | |||
710 | void List::clear_screen(){//Clears the system |
||
711 | #ifdef _WIN32//If the operation system is windows use "cls" |
||
712 | std::system("cls"); |
||
713 | #else//for other systems use "clear" |
||
714 | std::system ("clear"); |
||
715 | #endif |
||
716 | } |
||
717 | |||
718 | void List::close(){//Deletes all linked lists and linkedlists's all nodes |
||
719 | List_Node *listtraverse; |
||
720 | Card_Node *cardtraverse; |
||
721 | |||
722 | /*Deleting top list nodes*/ |
||
723 | cardtraverse = top_head; |
||
724 | while(top_head){ |
||
725 | cardtraverse = top_head; |
||
726 | top_head = top_head->next; |
||
727 | delete cardtraverse; |
||
728 | } |
||
729 | |||
730 | /*Deleting board list nodes*/ |
||
731 | listtraverse = board_head; |
||
732 | while(board_head){ |
||
733 | cardtraverse = board_head->cards; |
||
734 | while(board_head->cards){ |
||
735 | cardtraverse = board_head->cards; |
||
736 | board_head->cards = board_head->cards->next; |
||
737 | delete cardtraverse; |
||
738 | } |
||
739 | listtraverse = board_head; |
||
740 | board_head = board_head->next; |
||
741 | delete listtraverse; |
||
742 | } |
||
743 | |||
744 | /*Deleting foundation list nodes*/ |
||
745 | listtraverse = foundation_head; |
||
746 | while(foundation_head){ |
||
747 | cardtraverse = foundation_head->cards; |
||
748 | while(foundation_head->cards){ |
||
749 | cardtraverse = foundation_head->cards; |
||
750 | foundation_head->cards = foundation_head->cards->next; |
||
751 | delete cardtraverse; |
||
752 | } |
||
753 | listtraverse = foundation_head; |
||
754 | foundation_head = foundation_head->next; |
||
755 | delete listtraverse; |
||
756 | } |
||
757 | |||
758 | fclose(fptr);//Closing reading txt file |
||
759 | }><>><>><>><>>>><>><>><>><>><>>><>><>>><>><>><>><>><>><>><>>><>><>><>><>><>><>><>><>><>><>>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>>>>><>><>><>><>><>>>> |
||
760 |