#include "cardgames.h"
#include <stdio.h>

int main(void){
	card deck[175];
	char * c;
	int nCards = loadDeck();
	shuffleCards(deck,nCards);
	
	card player1Cards[nCards + 1];
	int player1N = 0;
	card player2Cards[nCards + 1];
	int player2N = 0;

	//deal out half the cards to each user
	card temp;
	int ctr = 0;
	while (drawCard(deck,&temp,&nCards)){
		if (ctr % 2 == 0){
			addToDeck(player1Cards, temp, &player1N);
		}
		else{
			addToDeck(player2Cards, temp, &player2N);
		}
		ctr++;
	}

	//war while one player still has cards
	bool player1Wins = false;
	
	while (player1N != 0 && player2N != 0){
		card p1card;
		card p2card;
	
		bool bothDrew = true;
		if (drawCard(player1Cards,&p1card,&player1N)){
			printf("Player 1 plays: %s\n", cardString(p1card));
		}
		else{
			bothDrew = false;
		}

		if (drawCard(player2Cards, &p2card, &player2N)){
			printf("Player 2 plays: %s\n",cardString(p2card));
		}
		else{
			bothDrew = false;
		}

		if (bothDrew){
			card warDeck[nCards];
			int warDeckN = 0;
			addToDeck(warDeck, p1card, &warDeckN);
			addToDeck(warDeck, p2card, &warDeckN);

			while (compareCards(p1card, p2card) == 0 && player1N > 0 && player2N > 0){
				printf("Cards were equal so players go to war!\n");
				//each player draws three cards, or if a player runs out of cards they automatically lose
				int i = 0;
				for (i = 0; i < 3 && player1N != 0 && player2N != 0; i++){
					card warcard;
					printf("Player 1 has %d cards and player 2 has %d cards.\n",player1N, player2N);
					if (drawCard(player1Cards, &warcard, &player1N)){
						printf("\tPlayer 1 plays: %s\n", cardString(warcard));
						addToDeck(warDeck,warcard,&warDeckN);
					}
					if (drawCard(player2Cards, &warcard, &player2N)){
						printf("\tPlayer 2 plays: %s\n", cardString(warcard));
						addToDeck(warDeck, warcard, &warDeckN);
					} 					
				}
				//if either player ran out of cards we know the winner
				if (player1N == 0){
					player1Wins = false;
					printf("Player 1 ran out of cards.\n");
				}
				else if (player2N == 0){
					player1Wins = true;
					printf("Player 2 ran out of cards.\n");
				}
				//otherwise
				else{
					p1card= warDeck[warDeckN-2];
					p2card= warDeck[warDeckN-1];
					printf("War cards are:\n\tPlayer 1: %s\n\tPlayer 2: %s\n",cardString(p1card),cardString(p2card));
				}
			}

			//if p1card is less than p2card
			if (compareCards(p1card, p2card) > 0 && player1N != 0 && player2N != 0){
				printf("Player 2 takes the deck.\n");
				combineDecks(player2Cards, &player2N, warDeck, &warDeckN);
				shuffleCards(player2Cards, player2N);
			}

			//otherwise player 1 gets the deck
			else if (player1N != 0 && player2N != 0 && compareCards(p1card, p2card) < 0){
				printf("Player 1 takes the deck.\n");
				combineDecks(player1Cards, &player1N, warDeck, &warDeckN);
				shuffleCards(player1Cards, player1N);
			}

			char p = 'q';
			//scanf("%c",&p);
			if (p == 'p'){
				printf("p1deck: \n");
				sortCards(player1Cards, player1N);
				printDeck(player1Cards, player1N);
				fflush(stdout);
				printf("p2deck: \n");
				sortCards(player2Cards, player2N);
				printDeck(player2Cards, player2N);
				fflush(stdout);
			}
			//scanf("%c",&p);

		}

	}

	if (player1Wins || player2N == 0){
		printf("Player 1 wins!\n");
	}
	else{
		printf("Player 2 wins!\n");
	}
	
	return 0;
}
