#include <stdlib.h>
#include <assert.h>

#include "cardstack.h"
#include "permute.h"


#define NUM_CARDS 60
#define RACK_SIZE 10

node_t * stock;
node_t * discard;

int racks[2][RACK_SIZE];


/* Set up the game state by shuffling the cards, populating the players' racks,
   and initializing the stock pile (the rest of the shuffled cards) and discard
   pile (from top of stock). */
void
setup_game()
{
  
} // setup_game

/* Clear the game by releasing all memory resources allocated for game play */
void
clear_game()
{
  
} // clear_game


/* Determine whether the given rack has "Gone Rack-O!", i.e., whether its
   contents are sorted in ascending order. */
bool
gone_racko (const int rack[])
{
  return false;
} // gone_racko


/* Print the contents of cards in the rack (with the corresponding indices) to
   the screen. */
void
print_rack (const int rack[])
{

} // print_rack


/* Get a yes or no response from a user in the form of a single typed 'y', 'Y',
   'N', or 'n' followed by a newline. Any excess input is cleared from the
   input stream up to (and including) the new line character.

   Repeats the prompt and solicits input repeatedly until a valid entry is given. 

   Returns true if input is 'y' or 'Y'. 

   Exits with an error message if EOF encountered.
*/
bool
get_choice(char * prompt)
{
  return true;
} // get_stack_choice


/* Prints a prompt for the card to be inserted and returns a slot number in
   [0,RACK_SIZE-1] entered by the user.  

   Repeats the prompt and solicits input repeatedly until a valid entry is given.

   Exits with an error message if EOF encountered.
*/
int
get_slot_number (int card)
{
  return 0;
} // get_slot_number


/* Asks the user where to insert the given card into the given rack, puts the
   given card value into the rack at that position and returns the value of the
   old card at that position. */
int
insert (int card, int rack[])
{
  return 0;
} // insert


/* Takes a card from the top of the stock pile, tells the user what they drew,
   asks whether they want to keep this card for their rack. If they do, the
   card is inserted in the user's selected position and the previous card is
   placed atop the discard pile. If they do not, the unwanted card from the
   stock is placed on the discard pile. */
void
move_from_stock (int rack[])
{

} // move_from_stock


/* Takes the card from the top of the discard stack and inserts it into the
   rack's selected position, placing the newly evicted card from the rack atop
   the discard pile. */
void
move_from_discard(int rack[])
{

} // move_from_discard


/* Main function for the move of one player's turn. User is informed if the
   discard pile is empty and must draw from the stock pile. Otherwise, the user is
   told the top of the discard pile and asked whether to take from the discard
   or from the stock pile, making the appropriate move (with rack inserts as
   appropriate). */
void
make_move (int rack[])
{

} // make_move


/* General skeleton for taking turn: Prints the player's rack, makes the
   player's move, and returns whether the player has won. */
bool game_play (int player)
{
  assert (player==0 || player==1);
  
  return false;
} // game_play


/* Called when the main stock pile is empty. Turns the discard pile over to
   become the stock pile. For example, if the discard stack was (from top to
   bottom) [15, 42, 36, 28], the stock pile becomes [28, 36, 42, 15]. */
void
move_discard()
{
  assert(isEmpty(stock));
  assert(!isEmpty(discard));

  printf ("Stock pile is empty. Moving discard to pile.\n");

  // MOVE PILES HERE
  
} // move_discard


/* Main program. Sets up game. Determines whether either player won by chance,
   then play the game for alternating players until someone wins, moving the
   discard pile when appropriate. */
int
main (void)
{
  assert ( 2*RACK_SIZE < NUM_CARDS ); // Necessary for feasible game

  setup_game();

  if (gone_racko (racks[0])) {
    printf ("Player 0 wins!");
    return 0;
  } else if (gone_racko (racks[1])) {
    printf ("Player 1 wins!");
    return 0;
  }

  int player = random() % 2; // Randomly chosen first player

  // Game play loop
  while (game_play (player) )
  {
    if ( isEmpty(stock) )
      move_discard();
    player = (player + 1) % 2; // Switch player
  }

  printf ("Player %d wins!\n",player);
  clear_game();

  return 0;
} // main
