1. create a class called MemoryBoard. a. MemoryBoard will represent a grid of me
ID: 3757809 • Letter: 1
Question
1. create a class called MemoryBoard.
a. MemoryBoard will represent a grid of memory squares. it needs to track all of the squares on the board, the cars set it's using, the width and the height of the board, the number of turns that have been taken, and the current player's id.
2. create a arraylist to store the squares on the board. it should store Square objects. the class should also store the width(an int), the number of turns taken(an int), the set of cards(a CardSet), and the current player's id(an int).
3. make this class inherit jPanel
4. make MemoryBoard implement the Board interface.
5. Implement the following methods from the board interface:
a. getBoardWidth- gets the width of the board and returns the variable.
b. getBoardHeight- gets te height of the board and returns the variable.
c. getTurns- gets the number of turns used and returns the variable.
d. getCurrentPlayer- gets the id of the curent player and returns the variable.
e. setCurrentPlayer- sets the current player's id and update the variable.
f. getTotalNumMatches- counts the number of matches made in the game. return 0, will be implmented later.
g. getMatchesByPlayer- returns the first copy of the cards that were matched by a player. return null, implement later.
h. findCard- finds a card on the board. return null, implement later.
i. getSquare- returns a swuare at the coordinates given. If both the x and y are valid ( the x and y are betwee 0 and te board's width/height), return the square from the arraylist that corresponds to that x ans y. if eighter or both x and y is not valid return null.
j. getPossibleMatches- determines the number of possible matches that can be made in the game. First, determine the number of squares on the board(use the width and height) and divide by 2. return the smaller of this number and the maximum number od cards in your CardSet variable.
k. resetBoard- ressets the board to its original state. first, remove all the cards from te square on the board. using an iterato loop over your araylist and call the removeCard method on each square. using your CardSet, get a new shuffle of the cards.Store arraylsit in a local variable. Using a counting loop, loop over all of the cards in the shuffle arraylist. in the loop, get a Card from that arraylist and get a square from your arraylist of squares. call the square's setCard method and give it the card. the card at position 0 should go into the square at position 0 and so forth. set the number of turns to 0.
6. create a constructor that takes a CardSet and two ints( width and height). Store the CardSet in the appropriate variable. if either of the width or height is 0 or less, both the with and the height should be set to 5(use a constant). set the curent player id to 0. create a new arraylist and store it in the class variable. Set the preffered size of the board to 105*width x 105*height.
use a loop to ceate all the squares in the board. count from 0 to the number of squares on the board. in the loop create a new MemorySquare object and store it in a local variable.use local variable as parameter. add this square to your arraylist of Aquares and to the MemoryBoard. Set the preferred size of the square to be 100 x 100.
call resetBoard to initialize the board.
Explanation / Answer
public static MemoryBord implements Board extends jPanel
{
int width, height, noOfTurns, curPlayerId ;
Set<Integer> CardSet = new HashSet<Integer>();
Arraylist squares = new Arraylist<Integer>();
MemoryBoard(Set CS, int width, int height)
{
CardSet = addAll(CS);
if(width<=0) width=5;
if(height<=0) height=5;
curPlayerId = 0;
ArrayList NewList = new ArrayList<Integer>(105*105);
for(i=0; i<width*height; i++)
{
/* As I dont have definition of MemorySquare */
/* I am assuming that MemorySquare is a class with */
/* `integer value` parameter */
MemorySquare element = new MemorySquare();
element.value = 0;
NewList.add(element.value);
}
}
getBoardWidth()
{
return width;
}
getBoardHeight()
{
return height;
}
getTurns()
{
return noOfTurns;
}
getCurrentPlayer()
{
return curPlayerId;
}
setCurrentPlayer(int a)
{
curPlayerId = a;
}
getTotalNumMatches()
{
return 0;
}
getMatchesByPlayer()
{
return null;
}
findCard(String cardName)
{
return null;
}
int getSquare(int x, int y)
{
if((x>=0 && x<=width) && (y>=0 && y<=height))
{
return squares.get(width*x + y);
}
return 0;
}
getPossibleMatches(String cardName)
{
noOfSq = (width* height)/2;
if(noOfSq < CardSet.size()) return noOfSq;
else return CardSet.size();
}
void resetBoard()
{
for(int elem : squares){
removeCard(elem);
}
/*
Some Code Here
*/
}
}
The resetBoard function is not complete.
You have mentioned that :-
*Remove all the cards from Arralylist squares
*Assign it to temp list
*Reshuffle the temp & do new assingments
But, when we have already empptied the Arraylist Squares, what is the point in shuffle ?
Please clear the above statements, so that resetBoard function can be completed
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.