Skip to content

Instantly share code, notes, and snippets.

@kristianfreeman
Created February 25, 2012 00:16
Show Gist options
  • Save kristianfreeman/1904809 to your computer and use it in GitHub Desktop.
Save kristianfreeman/1904809 to your computer and use it in GitHub Desktop.
homework assignment #3a CS141
-- Homework 3a: The "Riffle Shuffle"
-- Max Smith and Kristian Freeman
-- TA: Caleb Nelson
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
PROCEDURE hw3a is
maxDeck : CONSTANT positive := 52; -- Card decks don't get any larger...
SUBTYPE deckRange is positive RANGE 1..maxDeck;
TYPE deckArray is ARRAY(deckRange) of positive; -- Establishes a standard card deck.
myDeck : deckArray; -- The main deck we'll be working with
topDeck : deckArray; -- This deck deck are used to cut the primary deck.
bottomDeck : deckArray; -- and this
deckSize : positive; -- We use this to determine the size of the deck the user determines.
shuffleCount : positive; -- Self-explanatory
-----------------------------------------------------------
-----------------------------------------------------------
--- Populate function
--- PRE: Takes a deckRange value determined by user, as
--- well as an array to populate.
--- POST: Spits out a numerically ascending array.
-----------------------------------------------------------
-----------------------------------------------------------
PROCEDURE populate (A : OUT deckArray; num : deckRange ) IS
begin
FOR i IN 1..num loop -- Increment through 1 - user given number (max 52)
A(i) := i;
end loop;
end populate;
-----------------------------------------------------------
-----------------------------------------------------------
--- Display function
--- PRE: Takes deck array and user deck size.
--- POST: Spits out a pretty array of cards.
-----------------------------------------------------------
-----------------------------------------------------------
PROCEDURE display (A : IN deckArray; num : IN deckRange ) is
begin
for i in 1..num loop -- Hey, doesn't this look similar to populate?
put(A(i),4);
end loop;
new_line;
end display;
-----------------------------------------------------------
-----------------------------------------------------------
--- Cut function
--- PRE: Takes in a deckArray and deck size, with the
--- intention of splitting and placing them in
--- topDeck and bottomDeck. The extra card is given
--- to the *bottomDeck*.
--- POST: Spits out a top and bottom deck.
-----------------------------------------------------------
-----------------------------------------------------------
PROCEDURE cut (A : IN deckArray; num : IN deckRange; topDeck : OUT deckArray; bottomDeck : OUT deckArray) is
halfSplit : positive;
bottomCount : positive := 1;
topCount : positive := 1;
begin
if (num REM 2 = 0) then -- even. Split cleanly in half.
halfSplit := num / 2;
else -- odd
halfSplit := (num / 2) + 1; -- Gives the majority cards to *top* deck.
end if;
for i in 1..num loop
if (i <= halfSplit) then -- even
topDeck(topCount) := A(i); -- Current topDeck (1,2,3) is incremented.
topCount := topCount + 1; -- As in, incremented right here.
else -- odd
bottomDeck(bottomCount) := A(i);
bottomCount := bottomCount + 1;
end if;
end loop;
end cut;
-----------------------------------------------------------
-----------------------------------------------------------
--- Shuffle function
--- PRE: Takes a topDeck and bottomDeck as well as deck size
--- to shuffle back into a mainDeck.
--- POST: Spits out a shuffled *MAIN* array.
---
--- PS: I'm *really* similar to my friend, "cut".
--- ...sdrawkcab tsuj
-----------------------------------------------------------
-----------------------------------------------------------
PROCEDURE shuffle (topDeck : in deckArray; bottomDeck : in deckArray; num : in deckRange; mainDeck : out deckArray ) is
bottomCount : positive := 1;
topCount : positive := 1;
begin
for i in 1..num loop
if (i REM 2 = 0) then -- Even number. Use bottom pile.
mainDeck(i) := bottomDeck(bottomCount);
bottomCount := bottomCount + 1;
else -- Odd number. Use bottom pile.
mainDeck(i) := topDeck(topCount);
topCount := topCount + 1;
end if;
end loop;
end shuffle;
----------------------------------------------------------
----------------------------------------------------------
--- Main procedure
--- I ask for the deck size, as well as how many times
--- you want to shuffle. Then I *populate*, and *display*,
--- before entering a loop. In the loop, I *cut*, *shuffle*,
--- and then display again.
----------------------------------------------------------
----------------------------------------------------------
begin
new_line;
put("What is the size of the deck? ");
get(deckSize);
populate(myDeck, deckSize); -- Putting this here causes an
-- exception error quickly.
-- It's not pretty, but it's
-- better.
put("How many times would you like to shuffle? ");
get(shuffleCount);
new_line;
put("Initial deck: ");
display(myDeck, deckSize); new_line;
for i in 1..shuffleCount loop
if (i = 1) then -- Good grammar FTW!
put("After "); put(i, 1); put(" shuffle: ");
else
put("After "); put(i, 1); put(" shuffles: ");
end if;
cut(myDeck, deckSize, topDeck, bottomDeck);
shuffle(topDeck, bottomDeck, deckSize, myDeck);
display(myDeck, deckSize);
end loop;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment