I'm having trouble shuffling an array of cards.I have about 3 shuffling methods in my program and none of them seem to work. If anyone can help me out with this, that would be great. I have a folder full of cards called 1.png, 2.png.... 52.png
public class CardButton extends JButton{
ImageIcon front, back;
boolean flipped;
public CardButton(ImageIcon front, ImageIcon back)
{
this.front = front;
this.back = back;
flipped = true;
flip();
}
public void flip()
{
flipped = !flipped;
if (flipped)
this.setIcon(front);
else
this.setIcon(back);
}
}
public class CardButtonPanel extends JPanel {
CardButton b, b1, b2, b3;
ImageIcon back, card1, card2, card3;
int count;
ActionListener taskPerformer;
Timer t1;
// Instantiating a new array
CardButton[] array;
public CardButtonPanel() {
int w = 72, h = 86;
back = new ImageIcon(getClass().getResource("b2fv.png"));
Image i1 = back.getImage();
Image i2 = i1.getScaledInstance(w, h, Image.SCALE_DEFAULT);
back.setImage(i2);
array = new CardButton[53];
List list = Arrays.asList(array);
Collections.shuffle(list);
for (int i = 1; i < array.length; i++) {
// int j = shuffle();
card1 = new ImageIcon(getClass().getResource(i + ".png"));
i1 = card1.getImage();
i2 = i1.getScaledInstance(w, h, Image.SCALE_DEFAULT);
card1.setImage(i2);
b1 = new CardButton(card1, back);
b1.addActionListener(new ButtonHandler1());
add(b1);
array[i] = b1;
}
taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
b1.flipped = true;
b1.flip();
}
};
t1 = new Timer(200, taskPerformer);
t1.setRepeats(false);
}
/*
* public void shuffle() { currentCard = 1; for (int i = 1; i<array.length;
* i++) { int second = randomNumbers.nextInt(52);
*
*
* } }
*
* public int randomInteger(int x, int y) { Random rInteger = new Random();
* // int IntegerRandom = rInteger.nextInt((x-y) +1) + x; int IntegerRandom
* = rInteger.nextInt(52); return IntegerRandom; }
*
* public void swapCards(int i, int j) { CardButton temp = array[i];
* array[i] = array[j]; array[j] = temp; }
*
* public void shuffle() { for (int i = 0; i < array.length; i++) { int j =
* randomInteger(i, array.length - 1); } }
*/
/*
* public static int[][] randomize(int rows, int cols) { int[][] temp = new
* int[4][13]; Random randomize = new Random(); // int stuff; for (int i =
* 0; i < temp.length; i++) { // temp[i] = new int[cols]; for (int j = 0; j
* < temp[i].length; j++) temp[i][j] = (int) ((Math.random()) * (rows *
* cols)); // stuff = randomize.nextInt(52); } return temp; }
*/
private class ButtonHandler1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Need to create for loop for printing out how many attempts
int i = 0;
System.out.println(i + 1);
CardButton tempCB = (CardButton) e.getSource();
if (!tempCB.flipped && !t1.isRunning()) {
tempCB.flip();
count++;
}
if (count > 1) {
t1.start();
count = 0;
}
}
}
}
I actually attempted to create another shuffle class.
public class Shuffle {
public static int[] shuffleCards(int[] cards) {
for (int i = 1; i < cards.length; i++) {
int rand = new Random().nextInt(cards.length-i)+i;
int temp = cards[i];
cards[i] = cards[rand];
cards[rand] = temp;
}
return cards;
}
}
I don't know how to implement this into my CardButtonPanel, so I'm not too sure if it works or not.
Use best Fisher Yates shuffling algorithm
Sample code :
import java.util.*;
class Test
{
public static void main(String args[])
{
int[] solutionArray = { 1, 2, 3, 4, 5, 6, 16, 15, 14, 13, 12, 11 };
shuffleArray(solutionArray);
for (int i = 0; i < solutionArray.length; i++)
{
System.out.print(solutionArray[i] + " ");
}
System.out.println();
}
// Implementing Fisher–Yates shuffle
static void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
}
You can do shuffling in two ways:-
1)Fisher Yates Algorithm is one of the great choice.
2)Else You can create the list of cards and make its collection. Then use Collections.shuffle
link for tutorial. One of easiest way I've used so far.
Take look in this question for your ease.
question
Related
I am currently in the middle of trying to code a matching game. I have created a 2-d array for JButton and ImageIcon. I have done a shuffling class name Shuffle this class shuffles the ImageIcon's position in it's array. When starting the game without the base cards, the cards appear to be shuffled. But when I start the game with the base cards and incorporate the action listener, when you click on the card there is no image. If someone could help me figure out this problem it would be very appreciated.
Consider this code:
Method for the frame:
void game() {
Shuffle shuffle = new Shuffle();
shuffle.random2();
ImageIcon base = new ImageIcon("images/BaseCard.png");
int x = 60;
int y = 20;
JFrame frame = obj.frame();
JLabel label = obj.label();
for (int i = 0; i < cards.length; ++i) {
for (int j = 0; j < cards[i].length; ++j) {
cards[i][j] = obj.Comp(base);
cards[i][j].addActionListener(new Clicked(i, j));
cards[i][j].setBounds(x, y, 90, 126);
y = y + 135;
if (y >= 540) {
y = 20;
x = x + 120;
}
frame.add(cards[i][j]);
}
}
frame.add(label);
frame.setLayout(null);
frame.setVisible(true);
}
}
This is the Shuffle class:
class Shuffle {
Matching obj = new Matching();
String peach = "images/peach.png";
String daisy = "images/Baby_daisy.png";
String luigi = "images/Baby_Luigi.png";
String waluigi = "images/Baby_Waluigi.png";
String wario = "images/Baby_Wario.png";
String bowser = "images/BabyBowser.png";
String drybones = "images/DryBones.png";
String shyguy = "images/ShyGuy.png";
String[][] images = {
{peach, daisy, luigi, waluigi},
{wario, bowser, drybones, shyguy},
{peach, daisy, luigi, waluigi},
{wario, bowser, drybones, shyguy}
};
ImageIcon Icons[][] = new ImageIcon[4][4];
void random2() {
for (int i = 0; i < images.length; i++) {
for (int j = 0; j < images[i].length; j++) {
int i1 = (int) (Math.random() * images.length);
int j1 = (int) (Math.random() * images[i].length);
String temp = images[i][j];
images[i][j] = images[i1][j1];
images[i1][j1] = temp;
}
}
for (int i = 0; i < images.length; i++) {
for (int j = 0; j < images[i].length; j++) {
Icons[i][j] = new ImageIcon(images[i][j]);
}
}
}
}
And this is the Action Listener
class Clicked implements ActionListener {
Shuffle shuffle = new Shuffle();
Matching matching = new Matching();
private int i;
private int j;
public Clicked(int i, int j) {
this.i = i;
this.j = j;
}
public void actionPerformed(ActionEvent e) {
JToggleButton tBtn = (JToggleButton) e.getSource();
if (tBtn.isSelected()) {
System.out.println("click");
tBtn.setIcon(shuffle.Icons[i][j]);
} else {
System.out.println("not");
tBtn.setIcon(new ImageIcon("images/BaseCard.png"));
}
}
}
In your Clicked class you're creating a new Shuffle instead of:
public Clicked(Shuffle shuffle) {
this.shuffle = shuffle;
}
which would give you the previously created and shuffled "deck" of cards. You also may want to look into just having the one Clicked and using it for all the cards, just watch out for any threading issues if you start changing your Shuffle.
I'm making a program to play Misere Nim, and I'm running into some problems with memory. I use ArrayLists to hold the possible moves that could be done, and this is causing an OutOfMemory exception. How can I lessen the memory I use when using ArrayLists? Should I be clearing something after every run?
EDIT: added copy method
EDIT: full project can be found at https://github.com/DatOneRam/ScienceFair
do
{
int tempWin = board.playRound();
if (tempWin == 0)
stratWins++;
else
randWins++;
}
while (rounds <= 10000);
public int playRound()
{
Board board = new Board();
NimBot strat = new NimBot(board);
NimBot rand = new NimBot(board);
do
{
strat.makeStrategicMove();
if (board.hasEnded())
break;
rand.makeRandomMove();
}
while (!board.hasEnded());
return board.getPlayer();
}
public void makeStrategicMove()
{
ArrayList<int[]> goodMoves = board.getGoodMoves();
Random rand = new Random();
int[] move;
if (goodMoves.size() != 0)
{
move = goodMoves.get(rand.nextInt(goodMoves.size()));
}
else
{
ArrayList<int[]> simpleMoves = board.getSimpleMoves();
move = simpleMoves.get(rand.nextInt(simpleMoves.size()));
}
board.setPosition(move);
}
public void makeRandomMove()
{
ArrayList<int[]> moves = board.getMoves();
Random rand = new Random();
int[] move = moves.get(rand.nextInt(moves.size()));
board.setPosition(move);
}
public ArrayList<int[]> getMoves()
{
ArrayList<int[]> x = new ArrayList<int[]>();
int j = 0, i = 1;
x.add(0, copy(position));
do
{
x.add(i, copy(position));
if (x.get(i-1)[j] == 0)
j++;
x.get(i)[j] = x.get(i-1)[j] - 1;
i++;
if (x.get(i-1)[j] == 0)
{
j++;
if (j == 5)
{
break;
}
}
}
while(x.get(i - 1)[j] != 0);
for (int r = 0; r < x.size(); r++)
{
x.get(r)[4] = toggle(x.get(r)[4]);
}
x.remove(0);
//resize(x);
return x;
}
public ArrayList<int[]> getSimpleMoves()
{
ArrayList<int[]> x = new ArrayList<int[]>(4);
for (int i = 0; i < 4; i++)
{
x.add(copy(position));
x.get(i)[i] = position[i] - 1;
x.get(i)[4] = toggle(x.get(i)[4]);
if (x.get(i)[i] < 0)
x.get(i)[i] = 0;
}
//resize(x);
return x;
}
public int[] copy(int[] y)
{
int[] z = new int[y.length];
for (int i = 0; i < y.length; i++)
z[i] = y[i];
return z;
}
I apologize if I put too much code here, I wasn't sure how to shorten it.
I am working on a Matrix program on eclipse and I am currently stuck on 2 methods in which I thought were the most simplest of all. The first method that I am working on is to take the sum of 2 different 2D arrays from the #Test cases and returning the sum in a new 2D array. I already have an 2D array instance variable. The reason I am stuck is because the of the parameter in the method. The parameter doesn't give any variable other than the class (Matrix) and the variable (other). So I was wondering how to go about getting this method started and most importantly returning the sum array.
The other method I am stuck on is the transpose method where you must flip the rows and columns of the given 2D array. I know I must create a temp 2D array in order to store the content back into the original 2D array but for some reason it is not passing the test cases. If someone could please help me with these two methods, it would be much appreciated.
import java.util.Arrays;
public class Matrix {
private int[][] array;
private int[][] array2;
private int theRow;
private int theCol;
public Matrix(int[][] arrayOfArrays) {
// TODO Auto-generated constructor stub
array = new int[arrayOfArrays.length][arrayOfArrays[0].length];
for (int r = 0; r < arrayOfArrays.length; r++) {
for (int c = 0; c < arrayOfArrays[r].length; c++) {
array[r][c] = arrayOfArrays[r][c];
}
}
}
public int get(int row, int column) {
return array[row][column];
}
public int getNumberOfRows() {
int nRows = array.length;
return nRows;
}
public int getNumberOfColumns() {
int nCols = array[0].length;
return nCols;
}
public String toString() {
String res = "";
for (int r = 0; r < array.length; r++) {
for (int c = 0; c < array[r].length; c++)
res = res + array[r][c];
}
return res;
}
public Matrix sum(Matrix other) {
return sum;
}
public void scalarMultiply(int scalar) {
for (int r = 0; r < array.length; r++) {
for (int c = 0; c < array[0].length; c++) {
array[r][c] = array[r][c] * scalar;
}
}
}
public void transpose() {
int m = array.length;
int n = array[0].length;
int[][] transpose = new int [n][m];
int temp;
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
transpose[c][r] = array[r][c];
array[r][c] = array[c][r];
array[c][r] = transpose[c][r];
}
}
}
//The test cases for sum method and transpose method
#Test
public void testSum() {
int[][] a1 = { { 1, 2, 3 },
{ 5, 6, 7 } };
Matrix a = new Matrix(a1);
int[][] a2 = { { -2, -2, -2 },
{ 4, 4, 4 } };
Matrix b = new Matrix(a2);
Matrix c = a.sum(b);
assertEquals(-1, c.get(0, 0));
assertEquals(0, c.get(0, 1));
assertEquals(1, c.get(0, 2));
assertEquals(9, c.get(1, 0));
assertEquals(10, c.get(1, 1));
assertEquals(11, c.get(1, 2));
}
#Test
public void testTranspose() {
int[][] a1 = { { 1, 3, 5 },
{ 2, 4, 6 } };
Matrix a = new Matrix(a1);
a.transpose();
assertEquals(1, a.get(0, 0));
assertEquals(2, a.get(0, 1));
assertEquals(3, a.get(1, 0));
assertEquals(4, a.get(1, 1));
assertEquals(5, a.get(2, 0));
assertEquals(6, a.get(2, 1));
}
You need change the dimensions, for example, 2x3 -> 3x2.
import java.util.Arrays;
public class Matrix {
private int[][] array;
private int[][] array2;// remove this
private int theRow;// remove this
private int theCol;// remove this
public void transpose() {
int m = array.length;
int n = array[0].length;
int[][] transpose = new int [n][m];
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
transpose[c][r] = array[r][c];
}
}
array = transpose;
}
}
So far I have tried to create the method below, but when I run it, the new array leaves zeros for the empty spaces. If a find all method is created to work with this how can it be implemented with a binary search instead of a linear search
package bp;
import java.util.Arrays;
public class SortedList implements IUnsortedList {
/**
* The max size of the List.
*/
public static final int MAX_SIZE = 10000;
/**
* The max value of each occurence.
*/
public static final int MAX_VALUE = 10;
/**
* Flag for the amount of items on the list.
*/
private int sizeOfList = 0;
/**
* Variable to define true or false for duplicates.
*/
private boolean duplicatesAllowed = true;
/**
* Array saves the occurences in the list.
*/
private final int[] listItems = new int[MAX_SIZE];
/**
* Variable for the value to find or delete.
*/
private int searchKey;
/**
* Variable for counter in a loop.
*/
private int f;
#Override
public int getSizeOfList() {
return sizeOfList;
}
#Override
public boolean areDuplicatesAllowed() {
return duplicatesAllowed;
}
#Override
public void setDupliatesAllowed(boolean pDuplicatesAllowed) {
duplicatesAllowed = pDuplicatesAllowed;
}
#Override
public void clear() {
sizeOfList = 0;
}
#Override
public void insert(int pValueToInsert) {
//Loop finds the position of the Item
for (f = 0; f < sizeOfList; f++)
if (listItems[f] > pValueToInsert)
break;
//Loop moves the items after the position up
for (int n = sizeOfList; n > f; n-- )
listItems[n] = listItems[n - 1];
//Insert the Value in the right position
listItems[f] = pValueToInsert;
//Increment List size
sizeOfList++;
}
#Override
public void delete(int pValueToDelete) {
int destroyHAHAHA = find(pValueToDelete);
//If it doesnt find it the item
if (destroyHAHAHA==sizeOfList)
System.out.println("I let you down boss, Can't find "
+ pValueToDelete);
//If it does, kill it with fire
else {
for (int n = destroyHAHAHA; n <sizeOfList; n++)
listItems[n] = listItems[n + 1];
sizeOfList--;
}
}
#Override
public void deleteAll(int pValueToDelete) {
int j = 0;
for(int i = 0; i < listItems.length; i++ )
{
if (listItems[i] != pValueToDelete)
listItems[j++] = listItems[i];
}
int [] newArray = new int[j];
System.arraycopy(listItems, 0, newArray, 0, j );
}
#Override
public void initializeWithRandomData(int pSizeOfList) {
// Loop creates an array with certain number of elements
if (duplicatesAllowed) {
for (int n = 0; n < pSizeOfList; ++n) {
insert(listItems[n] = (int) (Math.random() * MAX_VALUE + 1));
}
} else {
int newvalue=0;
for (int n = 0; n < pSizeOfList; ++n) {
listItems[n] = newvalue++;
++sizeOfList;
}
}
}
#Override
public int find(int pValueToFind) {
searchKey = pValueToFind;
int lowNumber = 0;
int highNumber = sizeOfList - 1;
int result;
while (true) {
result = (lowNumber + highNumber) / 2;
if (listItems[result] == searchKey)
return result;
else if (lowNumber > highNumber)
return sizeOfList;
else {
if (listItems[result] < searchKey)
lowNumber = result + 1;
else
highNumber = result - 1;
}
}
}
#Override
public int[] findAll(int pValueToFind) {
//Array with the location of item
int[] answerArray = new int[sizeOfList];
int searchIndex;
int answerIndex = 0;
for (searchIndex = 0; searchIndex < sizeOfList; searchIndex++) {
if (listItems[searchIndex] == pValueToFind) {
answerArray[answerIndex++] = searchIndex;
}
}
if (answerIndex > 0) {
return Arrays.copyOfRange(answerArray, 0, answerIndex);
} else {
return new int[0];
}
}
#Override
public String toString() {
return Arrays.toString(Arrays.copyOfRange(listItems, 0, sizeOfList));
}
public void bubbleshort(){
int out;
int in;
int middle;
for (out = 0; out < sizeOfList - 1; out++) {
middle = out;
for(in = out +1; in < sizeOfList; in++)
if(listItems[in] < listItems[middle])
middle = in;
selectionSort(out, middle);
}
}
public void selectionSort(int one, int two) {
int temporal = listItems[one];
listItems[one] = listItems[two];
listItems[two] = temporal;
}
}
You can use Common langs ArrayUtils.removeElement() or ArrayUtils.removeAll() method to remove all the elements from the array.
Set contains no duplicates. You can use a Set.
Set<T> mySet = new HashSet<T>(Arrays.asList(someArray));
or
Set<T> mySet = new HashSet<T>();
Collections.addAll(mySet, myArray);
I have some problems with getting inheritance to work. In the parent class, the array Coefficients is private. I have some access methods but I still can't get it to work.
import java.util.ArrayList;
public class Poly {
private float[] coefficients;
public static void main (String[] args){
float[] fa = {3, 2, 4};
Poly test = new Poly(fa);
}
public Poly() {
coefficients = new float[1];
coefficients[0] = 0;
}
public Poly(int degree) {
coefficients = new float[degree+1];
for (int i = 0; i <= degree; i++)
coefficients[i] = 0;
}
public Poly(float[] a) {
coefficients = new float[a.length];
for (int i = 0; i < a.length; i++)
coefficients[i] = a[i];
}
public int getDegree() {
return coefficients.length-1;
}
public float getCoefficient(int i) {
return coefficients[i];
}
public void setCoefficient(int i, float value) {
coefficients[i] = value;
}
public Poly add(Poly p) {
int n = getDegree();
int m = p.getDegree();
Poly result = new Poly(Poly.max(n, m));
int i;
for (i = 0; i <= Poly.min(n, m); i++)
result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
if (i <= n) {
//we have to copy the remaining coefficients from this object
for ( ; i <= n; i++)
result.setCoefficient(i, coefficients[i]);
} else {
// we have to copy the remaining coefficients from p
for ( ; i <= m; i++)
result.setCoefficient(i, p.getCoefficient(i));
}
return result;
}
public void displayPoly () {
for (int i=0; i < coefficients.length; i++)
System.out.print(" "+coefficients[i]);
System.out.println();
}
private static int max (int n, int m) {
if (n > m)
return n;
return m;
}
private static int min (int n, int m) {
if (n > m)
return m;
return n;
}
public Poly multiplyCon (double c){
int n = getDegree();
Poly results = new Poly(n);
for (int i =0; i <= n; i++){ // can work when multiplying only 1 coefficient
results.setCoefficient(i, (float)(coefficients[i] * c)); // errors ArrayIndexOutOfBounds for setCoefficient
}
return results;
}
public Poly multiplyPoly (Poly p){
int n = getDegree();
int m = p.getDegree();
Poly result = null;
for (int i = 0; i <= n; i++){
Poly tmpResult = p.multiByConstantWithDegree(coefficients[i], i); //Calls new method
if (result == null){
result = tmpResult;
} else {
result = result.add(tmpResult);
}
}
return result;
}
public void leadingZero() {
int degree = getDegree();
if ( degree == 0 ) return;
if ( coefficients[degree] != 0 ) return;
// find the last highest degree with non-zero cofficient
int highestDegree = degree;
for ( int i = degree; i <= 0; i--) {
if ( coefficients[i] == 0 ) {
highestDegree = i -1;
} else {
// if the value is non-zero
break;
}
}
float[] newCoefficients = new float[highestDegree + 1];
for ( int i=0; i<= highestDegree; i++ ) {
newCoefficients[i] = coefficients[i];
}
coefficients = newCoefficients;
}
public Poly differentiate(){
int n = getDegree();
Poly newResult = new Poly(n);
if (n>0){ //checking if it has a degree
for (int i = 1; i<= n; i++){
newResult.coefficients[i-1]= coefficients[i] * (i); // shift degree by 1 and multiplies
}
return newResult;
} else {
return new Poly(); //empty
}
}
public Poly multiByConstantWithDegree(double c, int degree){ //used specifically for multiply poly
int oldPolyDegree = this.getDegree();
int newPolyDegree = oldPolyDegree + degree;
Poly newResult = new Poly(newPolyDegree);
//set all coeff to zero
for (int i = 0; i<= newPolyDegree; i++){
newResult.coefficients[i] = 0;
}
//shift by n degree
for (int j = 0; j <= oldPolyDegree; j++){
newResult.coefficients[j+degree] = coefficients[j] * (float)c;
}
return newResult;
}
}
Can anyone help me fix my Second class that inherits from the one above? I cant seem to get my multiply and add methods for the second class to work properly.
public class QuadPoly extends Poly
{
private float [] quadcoefficients;
public QuadPoly() {
super(2);
}
public QuadPoly(int degree) {
super(2);
}
public QuadPoly(float [] f) {
super(f);
if (getDegree() > 2){
throw new IllegalArgumentException ("Must be Quadratic");
}
}
public QuadPoly(Poly p){
super(p.coefficients);
for (int i = 0; i < coefficients.length; i++){
if (coefficients[i] < 0){
throw new Exception("Expecting positive coefficients!");
}
}
}
// public QuadPoly(Poly p){
// super(p.coefficients);
//}
public QuadPoly addQuad (QuadPoly p){
return new QuadPoly(super.add(p));
}
public QuadPoly multiplyQuadPoly (QuadPoly f){
if (quadcoefficients.length > 2){
throw new IllegalArgumentException ("Must be Quadratic");
}
return new QuadPoly(super.multiplyPoly(f));
}
I would make the coefficients protected or use an accessor method.
I wouldn't throw a plain checked Exception. An IllegalArgumentException would be a better choice.
What is quadcoefficients? They don't appear to be set anywhere.
You put coefficients private. I wouldn't change this but I would add a getter method into Poly class:
public class Poly {
//somecode here
public float[] getCoefficients(){
return this.coefficients;
}
}
Then I would use it by the getter method in other code;
public QuadPoly(Poly p){
super(p.getCoefficients);
//some more code here
}
Even if you make coefficient protected, you are trying to reach coefficients field of another Object, which is a parameter. So it is not related to inheritance and the problem.