Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MemoryLane
- {
- private Domino[] board;
- /**
- * Constructs a Memory Lane game with max^2 + max Dominoes.
- *
- * Example: max <-- 2
- * [1] [1] [1] [1] [2] [2]
- * [1] [1] [2] [2] [2] [2]
- *
- * Example: max <-- 3
- * [1] [1] [1] [1] [1] [1] [2] [2] [2] [2] [3] [3]
- * [1] [1] [2] [2] [3] [3] [2] [2] [3] [3] [3] [3]
- *
- * Postcondition: board is random
- *
- * @param max the largest number of pips on the Dominoes
- */
- public MemoryLane(int max)
- {
- this.board = new Domino[(int)(Math.pow(max, 2) + max)];
- int index = 0;
- for (int i = 1; i <= max; i++)
- {
- board[index++] = new Domino(i,i);
- for (int j = 1; j <= max; j++)
- {
- board[index++] = new Domino(i,j);
- }
- }
- shuffle();
- }
- /**
- * Shuffles board
- * Precondition: board does not contain null elements
- *
- * @return
- */
- private void shuffle()
- {
- Domino[] tempBoard = new Domino[board.length]; //creates new array and reinputs board
- for (int i = 0; i < board.length; i++)
- {
- int randy = (int) (Math.random()* board.length);
- while(board[randy] == null)
- randy = (int) (Math.random()* board.length);
- tempBoard[i] = board[randy];
- board[randy] = null;
- }
- board = tempBoard;
- }
- /**
- * Reveals the Dominoes at index i and k if they match
- *
- * @param i
- * @param k
- * @return true if the Dominoes at i and k match; false otherwise
- */
- public boolean guess(int i, int k)
- {
- if (board[i].equals(board[k]))
- {
- board[i].setReveal(true);
- board[k].setReveal(true);
- }
- return board[i].equals(board[k]);
- }
- /**
- *
- * Precondition: the elements at the specified indexes are not null
- *
- * @param indexes variable arguments; any number of indexes;
- * indexes if effectively an int[]
- * @return
- */
- public String peek(int... indexes)
- {
- String whitHubbard = ""; //basically str or rtn
- for (int index: indexes)
- whitHubbard += "[" + board[index].getTop() + "]";
- whitHubbard += "\n";
- for (int index : indexes)
- whitHubbard += "[" + board[index].getBottom() + "]";
- return whitHubbard;
- }
- /**
- * @return true if the number of revealed Dominoes is equal to the
- * length of the board
- */
- public boolean gameOver()
- {
- int counter = 0;
- for (Domino domino : board)
- if (domino.isReveal())
- counter++;
- return counter == board.length;
- }
- /**
- * Returns a String representation of the board in the following format:
- *
- * 0 1 2 3 4 5
- * [top] [ ] [top] [ ] [top] [top]
- * [bot] [ ] [bot] [ ] [bot] [bot]
- *
- * Note: the top and bottom numbers of an unrevealed Domino are represented with
- * an empty space
- *
- * @return
- */
- public String toString()
- {
- String no = " ";
- int bruh = board.length;
- for (int i = 0; i < bruh; i++)
- no += i + " ";
- no += "\n";
- for (int i = 0; i < bruh; i++)
- {
- if(board[i].isReveal())
- no += "[" + board[i].getTop() + "]";
- else
- no += "[ ]";
- }
- no += "\n";
- for (int i = 0; i < bruh; i++)
- {
- if(board[i].isReveal())
- no += "[" + board[i].getBottom() + "]";
- else
- no += "[ ]";
- }
- return no;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement