#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int rand_var (void);

int win_condition(int userchoice, char **arr_options, int *userp, int *cpup, int *countp);
//pointer array containing strings
char *arr_options[] = {"STEN", "SAX", "PASE"};


//Counter variables
int userpoints=0;
int cpupoints=0; 
int  counter = 0;

//pointers to counter variables above
int *userp = &userpoints;
int *cpup = &cpupoints;
int *countp = &counter;

//character array for user input
char buff[256];


int main () 
{  
  while (counter < 10)
    {
      printf("Välj sten [0], sax [1], eller påse[2]:\t");
   
      scanf("%c", buff);
      
      int int_buff = atoi(buff);
      
      if (int_buff <= 2){
	
	switch(buff[0])
	  {
	  case '0':
	    printf("Du har valt %s\n", arr_options[int_buff]);
	    win_condition(int_buff, arr_options, userp, cpup, countp);
	    break;
	    
	  case '1':
	    printf("Du har valt %s\n", arr_options[int_buff]);
	    win_condition(int_buff, arr_options, userp, cpup, countp);
	    break;
	    
	  case '2':
	    printf("Du har valt %s\n", arr_options[int_buff]);
	    win_condition(int_buff, arr_options, userp, cpup, countp);
	    break;
	    
	  default:
	    printf("Välj en siffra mellan 0 - 2, STEN[0], SAX[1] eller PÅSE[2].\n");
	  }
      }
      while (getchar()!='\n'){} //äter upp alla lösa newlines utan att faktiskt göra ngt
      //printf("userpoints %i, cpupoints %d, counter %d\n", userpoints, *cpup, counter);
    }
  
  int i;
  for (i = 0; i < 10; i++){
    printf("*************************************\n");
  }
 
 printf("Game Over\n\nDatorn: %i\nDu    : %i\n", cpupoints, userpoints);
 
  if (userpoints > cpupoints){
    printf("DU VANN!");
  }else if(userpoints == cpupoints){
    printf("OAVGJORT!");
  }else{
    printf("DU FÖRLORADE!");
  }

 
  
  return 0;
}


int rand_var(void)
{
  srand(time(NULL)); 
  return (rand() % 3);
}


int win_condition(int userchoice, char **arr_options, int *userp, int *cpup, int *countp){  
  int cpuchoice = rand_var();
  //printf("%d\n", cpuchoice);
  
  if (cpuchoice == userchoice){
    printf("Datorn valde %s.\nOavgjort\n\n", arr_options[cpuchoice]);
    
  }
  else if(userchoice==cpuchoice - 1 || userchoice == cpuchoice + 2){
    printf("Datorn valde %s.\nDu vinner\n\n", arr_options[cpuchoice]);
    (*userp)++;
    //printf("&userp %p userp %p *userp %i\n", &userp, userp, *userp);
  }
  else if(userchoice == cpuchoice - 2 || userchoice == cpuchoice +1){
    printf("Datorn valde %s.\nDu förlorar\n\n", arr_options[cpuchoice]);
    (*cpup)++;
  }
  
  (*countp)++; //Increment the value in the address pointed to by *countp, in this case 'counter'
  return 0;
}

  /* switch (user_choice){ */
    
  /* case (cpu_choice): */
  /*   Printf("Oavgjort\n"); */
  /*   //counter++; */
  /*   break; */
   
  /* case(cpu_choice - 1): */
  /*   Printf("Du vinner!\n"); */
  /*   break; */
    
  /* case(cpu_choice + 2): */
  /*   Printf("Du vinner!\n"); */
  /*   break; */
    
  /* case (cpu_choice - 2): */
  /*   printf("Du förlorar\n"); */
  /*   break; */
  
  /* case (cpu_choice + 1): */
  /*  printf("Du förlorar\n"); */
  /*  break; */
 
  
