Thread: War Logic

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    War Logic

    I think my war logic is a little bit off..... first when i tie it goes into an infinite loop so i threw a break in to stop it. Then its supposed to dequeue the first two numbers and compare them and who ever wins gets the numbers enqueued into their queue. Here is my output and my files.

    btw my war never gets past three comparisons. The problem is probably in my while at the bottom of main can someone help please?

    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include "queue.h"
    
    int main()
    {
    	Queue q;
    	Queue qp1;
    	Queue qp2;
    	int i, count=1;
    	int deck[52];
    	int temp;
    	int random;
    	int p1[52];
    	int p2[52];
    	int tmp1, tmp2;
    	srand( time ( 0 ) );
    
    	
    	printf("Welcome to the War card game!");
    
    	init( &q );
    	init( &qp1 );
    	init( &qp2 );
    
    	for(i=0; i<52; i++)
    	{
    		deck[i] = count++;
    	}
    	
    	printf("\nsorted deck: ");
    
    	for(i=0; i<52; i++)
    	{
    		printf("%d ", deck[i]);
    	}
    
    	for(i=0; i<7; i++)
    	{
    		for(i=0; i<52; i++)
    		{
    			random = rand() % 52;
    			temp = deck[i];
    			deck[i] = deck[random];
    			deck[random] = temp;
    		}
    	}
    
    	printf("\nunsorted deck: ");
    	for(i=0; i<52; i++)
    	{
    		printf("%d ", deck[i]);
    	}
    	
    
    
    	for(i=0; i<26; i++)
    	{
    		p1[i] = deck[i];
    	}
    	for(i=0; i<26; i++)
    	{
    		p2[i] = deck[i+26];
    	}
    	printf("\np1: ");
            for(i=0; i<26; i++)
            {
                    printf("%d ", p1[i]);
            }
    
            printf("\np2: ");
            for(i=0; i<26; i++)
            {
                    printf("%d ", p2[i]);
            }
    	printf("\n");
    
    	for(i=0; i<26; i++)
    	{
    		enqueue(&qp1, p1[i]);
    		enqueue(&qp2, p2[i]);
    	}
    
    	printf("Enqueded player1\n");	
    	printArr(&qp1);
    	printf("\nEnqueued player2\n");
    	printArr(&qp2);
    	printf("\n");
    	
    
    	while( p1 != NULL && p2 != NULL)
    	{	
    		tmp1 = dequeue(&qp1);
    		tmp2 = dequeue(&qp2);
    		
    		printf("%d  vs  %d  ", tmp1, tmp2);
    		if( tmp1 > tmp2 )
    		{
    			printf("player 1 won\n");
    			enqueue(&qp1, tmp1);
    			enqueue(&qp1, tmp2);
    			
    		}
    		
    		if( tmp1 < tmp2 )
    		{
    			printf("player 2 won\n");
                            enqueue(&qp2, tmp2);
                            enqueue(&qp2, tmp1);
    		}
    
    		if( tmp1 == tmp2 )
    		{
    			printf("TIE!!\n");
    			break;
    		}
    	}	
    	
    
    	return 0;
    }
    queue.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "queue.h"
    
    void init ( Queue * qq)
    {
    	qq->cap = 52;
    	qq->arr = malloc( sizeof(int) * qq->cap);
    	qq->front = 0;
    	qq->back = 0;
    	qq->elements = 0;
     
    }
    void enqueue( Queue * qq, int num)
    {
    	if(qq->cap == qq->elements)
    	{
    		qq->arr = realloc(qq->arr, sizeof(int) * qq->cap);
    		qq->arr[qq->back++] = num;
    		qq->elements++;
    	}
    	else
    	{
    		qq->arr[ qq->back++ ] = num;
    		qq->elements++;
    	}
    }
    
    int dequeue( Queue * qq)
    {
    	int i;
    
    	if(empty(qq))
    	{
    		printf("ITS EMPTY!!\n");
    	}
    	else
    	{
    
    		qq->elements--;
    		qq->arr[qq->front] = 0;
    		for(i=0; i<qq->elements; i++)
    		{
    			qq->arr[i] = qq->arr[i+1];
    		}
    	}
    	return qq->arr[i];
    }
    
    int empty( Queue * qq)
    {
    	int test = 0;
    
    	if(qq->arr == NULL)
    	{
    		test = 1;
    	}
    	return test;
    }	
    
    int length(Queue * qq)
    {
    	return qq->elements;
    }
    
    void printArr(Queue * qq)
    {
    	int i;
    	
    	for(i=0; i<qq->elements; i++)
    	{
    		printf("%d ", qq->arr[i]);
    	} 
    }
    
    void freeArr( Queue * qq)
    {
    
    	free(qq->arr);
    }
    output
    Code:
    [dodoherty@vulcan war]$ gcc main.c queue.c -o test
    [dodoherty@vulcan war]$ ./test
    Welcome to the War card game!
    sorted deck: 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
    unsorted deck: 10 22 19 5 47 51 52 50 44 12 23 6 43 26 39 20 46 38 18 41 40 37 17 15 3 27 36 30 28 42 21 1 33 2 13 49 11 31 8 35 29 48 16 32 14 34 45 7 25 9 4 24
    p1: 10 22 19 5 47 51 52 50 44 12 23 6 43 26 39 20 46 38 18 41 40 37 17 15 3 27
    p2: 36 30 28 42 21 1 33 2 13 49 11 31 8 35 29 48 16 32 14 34 45 7 25 9 4 24
    Enqueded player1
    10 22 19 5 47 51 52 50 44 12 23 6 43 26 39 20 46 38 18 41 40 37 17 15 3 27
    Enqueued player2
    36 30 28 42 21 1 33 2 13 49 11 31 8 35 29 48 16 32 14 34 45 7 25 9 4 24
    27  vs  24  player 1 won
    27  vs  24  player 1 won
    24  vs  24  TIE!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I wrote a quick one, and here's how the war logic goes, according to your "remove ties":
    Code:
        while( p1->cards > 0 && p2->cards > 0 )
        {
        
            int pc1 = cardpop( p1 );
            int pc2 = cardpop( p2 );
    
            if( CARD(pc1) == CARD(pc2) )
            {
                printf("draw: ");
            }
            else
            if( CARD(pc2) != 1 && CARD(pc1) > CARD(pc2) ) /* p1 wins */
            {
                printf("p1 wins: ");
                cardpush( p1, pc1 );
                cardpush( p1, pc2 );
            }
            else
            {
                printf("p2 wins: ");
                cardpush( p2, pc2 );
                cardpush( p2, pc1 );
            }
            printf( "%d v %d\n", pc1, pc2 );
        }
    Your War is broken in my opinion, because they should never have the exact same card, if you're working from a standard deck. If you have cards 1-52, then C%13 will give you cards from different suits, of the same value. IE:

    1 and 14 are the same card.

    Anyway, that's the logic above. I didn't randomize the deck, but split it every other card to one guy or the next, for a quick pass. This naturally means the player handed all the aces wins, but fair games weren't the goal of my test.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    Thank you so much dude this helps a bunch!

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    my output keeps doing the same after I refigured my logic. it will play 2 hands then the third is a draw and goes into and infinite loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Handling logic and draw cycles
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 07-25-2009, 10:15 AM
  2. Recent Death Syndrome
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 03-29-2004, 06:51 PM
  3. War with Iraq - Read this article if you're interested
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-26-2003, 12:10 AM
  4. No More Technology After World War Three
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-20-2001, 07:02 PM