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!!