The goal of this excercise is to implement the "de brujin" magic trick
https://golem.ph.utexas.edu/category/2015/01/mathematics_and_magic_the_de_b.html
https://www.youtube.com/watch?v=EWG6e-yBL94
https://www.youtube.com/watch?v=g71wG6RrTL8
!! their card encoding may not corresponds to the one proposed in this excercise!!
Classes to implement:
_______________________________
Class MyDeck:
Property of MyDeck:
-cardList ArrayList
Additional constructor of MyDeck:
Define a constructor which accepts a LFSR as parameter and produces a deck (list of objects Cards) as follows:
creates a card using the register [see Card constructors]
if the card is not in MyDeck, add the card at the end of the list of cards and update the register,
else stop.
Methods of MyDeck:
+void shuffle() {}
The method corresponds to the "Pile shuffle" (see https://en.wikipedia.org/wiki/Shuffling ).
The method produces a random index n, with 0 distributeCards () {}
Define a method which creates a list of cards containing the first 5 cards of the deck.
_______________________________
Class Seed:
Property of Seed:
-seed boolean[2]
Constructor of Seed:
Define a constructor which takes as parameter an array of booleans of lenght 2
Methods of Seed:
Define a method getter and setter for the property.
Override the method toString such that it prints:
the string "hearts" if seed is [0,0]
the string "diamonds" if seed is [0,1]
the string "clubs" if seed is [1,0]
the string "spades" if seed is [1,1]
define a method
+boolean getColor() {}
which returns
>false if the seed corresponds to an hearth or a diamond and
>true if it corresponds to a club or a spade
and it prints "red" or "black" accordingly with the card seed
_______________________________
Class Number:
Property of Number:
-number boolean[3]
Constructor of Number:
Define a constructor which takes in input arrays of booleans of lenght 3
Methods of Number:
Define a method getter and setter for the property.
Override the method toString such that it prints the integer n (in base 10)
corresponding to the integer n (in base 2) accordingly with the following convention
[0,0,1] = 1
[0,1,0] = 2
` [1,0,0] = 4
for example [1,0,1] = 5
Attention: Set the boolean array [0,0,0] to be printed as the number 8
_______________________________
Class Card
Properties of Card:
-Seed seed
-Number number
Constructor of Card:
Card(BitArray bitArray) {}
Define a constructor which takes an object bitArray of type BitArray and
uses the first two elements to define the card seed and the last 3 to define the card number.
Methods of Card:
Define getters and setters methods for the properties seed and the number.
Override the function toString() in order to print the card (number and seed) using the toString methods of the
classes Seed and Number
Define the method getColor() retunring the color of the card (using the method getColor() defined in the class Seed )
boolean getColor() {}
_______________________________
Class BitArray
Property of BitArray
-boolean[5]
Constructor of BitArray:
Define a contructor
+ BitArray (boolean b0, boolean b1, boolean b2, boolean b3, boolean b4)
Methods of BitArray:
Define getters and setters methods fro the property
_______________________________
Class LFSR
Properties of LFSR:
- BitArray register
- BitArray coefs (this property should be final)
Constructor of LFSR:
define a constructor which takes two BitArray: one for the register and one for the coefs
Methods of LSFR:
+void updateLSFR() {}
which:
-computes the feedback from the register and the coefs (see old assignement correction)
-shifts the register TO THE LEFT (for example register[1]=register[2])
-stores the feedback at the end fo the array, that is in register[4]
+Getter and setters for register
_______________________________
In the main method of the Main Class:
initialize an object lfsr of type LFSR with initialRegister [0,0,0,0,1] and coefs [1,0,1,0,0]
create an object of type MyDeck using lfsr
shuffle the deck of cards at leas one time
create an ArrayList containing the 5 card picked by the volonteers
(use the method distributeCards() of MyDeck)
Print the volonteers' card color (print them using the getColor() method)
and store it into an object of type BitArray, named volonteersColors
Use volonteersColors to guess the cards:
re-initialize the lfsr by re-setting the register on the value volonteersColors.
define an array Card[5] named guessingCards.
The first card is created by using the Card constructor with BitArray parameter volonteersColors
the successive 4 cards are obtained similarly, by updating the lfsr before each iteration
Check if it is correct:
the elements of the list of cards created by distributedCard() should be the same as the ones in guessingCards
print "Magic!" if the magic trick works