java processing shuffeling 2d array without classes - java

the school gave me an assignment. I have to make the memory cards game in processing using java.
I am not allowed to use classes. I already created the game and it works, but now I have to add death cards.
I am using a 2d array to load the cards. I have put the death cards in the third iteration of the array.
now the problem is, when I am trying to create a shuffle function, it will only shuffle the cards in its own iteration. so all the death cards will appear at the end.
anybody got any ideas?
for (int i=0; i<3; i++) {
if (i == 0 || i == 1) {
for (int j=0; j<aantalSetjes; j++) {
y[i][j] = yKaart;
x[i][j] = xKaart;
gekozenKaart[i][j] = teller;
teller +=1;
if (teller > aantalSetjes) {
teller = 1;
}
if (xKaart<=(kaartBreedte * getSpeelveldBreedteKeer())-kaartBreedte) {
xKaart += kaartBreedte;
} else if (xKaart>=(kaartBreedte * getSpeelveldBreedteKeer())-kaartBreedte) {
xKaart=15;
yKaart += kaartLengte;
}
}
} else if (i == 2) {
for (int j=0; j<getAantalDoodsKaarten(); j++) {
y[i-1][j+aantalSetjes] = yKaart;
x[i-1][j+aantalSetjes] = xKaart;
gekozenKaart[i][j] = teller;
teller +=1;
if (teller > getAantalDoodsKaarten()) {
teller = 1;
}
if (xKaart<=(kaartBreedte * getSpeelveldBreedteKeer())-kaartBreedte) {
xKaart += kaartBreedte;
} else if (xKaart>=(kaartBreedte * getSpeelveldBreedteKeer())-kaartBreedte) {
xKaart=15;
yKaart += kaartLengte;
}
}
}
}
shuffle()
}
void shuffle() {
int tijdelijk = 0;
int random = 0;
for (int i = 0; i < 3; i++) {
if (i == 0 || i == 1) {
for (int j = 0; j < aantalSetjes; j++) {
random = int(random(0, aantalSetjes));
tijdelijk = gekozenKaart[i][j];
gekozenKaart[i][j] = gekozenKaart[i][random];
gekozenKaart[i][random] = tijdelijk;
}
} else if (i == 2) {
for (int j = 0; j < getAantalDoodsKaarten(); j++) {
random = int(random(0, getAantalDoodsKaarten()));
tijdelijk = gekozenKaart[i][j];
gekozenKaart[i][j] = gekozenKaart[i][random];
gekozenKaart[i][random] = tijdelijk;
}
}
}
}

If it's the classic memory card game, you most probably don't want the data to be organized in the 2d array to begin with. Just combine all of them into one bigger 1d array first and then shuffle the 1d array. Then they will be all mixed through as you wanted.

Related

Ordering a vector of randomly generated numbers

I´m trying to solve a question but i can´t find why my code is not working for this problem. I have generated a random vector of 100 elements and im trying to order them into another. Somehow, my new generated vector is filled with the last index value of the random vector.
int[] vetorAleatory = new int[100];
for (int i = 0; i < vetorAleatory.length; i++) {
vetorAleatory[i] = new Random().nextInt(1000);
}
int[] vetorByOrder = new int[100];
int newVetorPosition = 0;
for (int i = 0; i < 100; i++) {
for (int x = 0; x < 100; x++) {
vetorByOrder[newVetorPosition] = 2000;
if (vetorAleatory[i] < vetorByOrder[newVetorPosition]) {
boolean newEntry = true;
for (int y = 0; y < newVetorPosition; y++) {
if (vetorByOrder[y] == vetorByOrder[newVetorPosition]) {
newEntry = false;
break;
}
}
if (newEntry == true) {
vetorByOrder[newVetorPosition] = vetorAleatory[x];
}
}
if (x == 99) {
newVetorPosition++;
}
}
}
for (int i = 0;i<100;i++) {
System.out.print(vetorAleatory[i] + ", " + vetorByOrder[i] + System.lineSeparator());
}
First you do not need 3 loops to sort an array. You need only 2 and in case of quick search, it is even less than that. You can check this example Array sort and search, or you can use built in Arrays.sort method in Java

Is there a way to have less if-statements?

This program is supposed to print out the number of times each number has been generated of 100 random numbers in the scale of 1 to 10 (then put it in the array).
I can't think of any other way than to have an if-statement for each number.
Is there any way to avoid so many if statements by another code or something?
public static void countNumbers() {
Random generator = new Random();
int arr[] = new int[101];
int add[] = new int[10];
for (int i = 0; i < 100; i++) {
int sum = 0;
arr[i] = generator.nextInt(10)+1;
if(arr[i] ==1){
add[0]++;
}
if(arr[i] ==2){
add[1]++;
}
if(arr[i] ==3){
add[2]++;
}
if(arr[i] ==4){
add[3]++;
}
if(arr[i] ==5){
add[4]++;
}
if(arr[i] ==6){
add[5]++;
}
if(arr[i] ==7){
add[6]++;
}
if(arr[i] ==8){
add[7]++;
}
if(arr[i] ==9){
add[8]++;
}
if(arr[i] ==10){
add[9]++;
}
}
System.out.println(Arrays.toString(add));
System.out.println();
}
You should note that all your if statements have a similar structure :
if(arr[i] ==x){
add[x-1]++;
}
Therefore they can be replaced by add[arr[i]-1]++;.
You only need a single if statement to validate that you don't get out of the bounds of the add array :
if (arr[i] <= 10 && arr[i] >= 1) {
add[arr[i]-1]++;
}
EDIT :
As assylias commented, you don't really need the if statement, since you initialize your arr array to values between 1 and 10.
Yes.
If you notice, you have a repeating pattern:
if(arr[i] == 1){
add[0]++;
}
Then you can replace it with the next one:
add[arr[i] - 1]++;
And the resulting for will look alike:
for (int i = 0; i < 100; i++) {
arr[i] = generator.nextInt(10) + 1;
add[arr[i] - 1]++;
}
Your whole method can be replaced by:
public static void countNumbers() {
Random generator = new Random();
int add[] = new int[10];
for (int i = 0; i < 100; i++) {
add[generator.nextInt(10)]++;
}
System.out.println(Arrays.toString(add));
System.out.println();
}
The other variables (such as the arr array) are not used at the moment so I suggest getting rid of them until you actually need them.
I think it is good to re-use the size of you add[] this way you can keep your code more flexible.
int add[] = new int[10];
for (int i = 0; i < 100; i++) {
int index = generator.nextInt(add.length);
arr[index]++;
}

Bubblesort Java, new to Java [duplicate]

This question already has answers here:
Sort an array in Java
(19 answers)
Closed 7 years ago.
public class App {
public static void main( String[] args ) {
int[] set = {0,0,0,0,0,0,0};
for(int i = 0; i < 7; i++) {
boolean unique = false;
while (!unique) {
int j = 0;
try {
j = JRandom.randInt(1, 45);
}
catch (ParamterException e) {
e.printStackTrace();
}
unique = true;
for(int k = 0 ; k < 7; k++) {
if (j==set [k]) {
unique = false;
break;
}
}
if (unique) {
set [i] = j;
}
}
}
for (int i= 0; i < 6; i++) {
if (i == 5) {
System.out.println(set[i]);
}
else {
System.out.print(set[i]+", ");
}
}
System.out.println("Bonus Ball = " + set[6]);
}
}
I was wondering how I would implement bubble-sort into this code. It works on my machine and produces 6 random numbers + a bonus ball.
2, 34, 25, 14, 39, 13
Bonus Ball = 30
I was aiming to make it print so the numbers are ascending so that it would be like the lotto, the idea anyway.
That's all, thanks.
Can make like this:
for (int i = set.length-1; i >= 0; i --) {
for (int j = 0; j < i; j++) {
if (set[j] > set[j + 1]) {
int aux = set[j];
set[j] = set[j + 1];
set[j + 1] = aux;
}
}
}
The initial loop, in this way also helps, not to waste resources comparing indexes that have already been sorted.
If the use of bubble sort algorithm is not mandatory, just use the Array.sort method already mentioned.
for (i = 0 ; i < set.length() -2; i ++){
for(j = i+1 ; j< set.length()-1 ; j++){
if(set[i]<set[j]){
int dummy = set[i];
set[i] = set[j];
set[j] = dummy;
}
}
}
The above bubble sort is sorting only the 6 generated numbers and your Bonus ball is still present at set[6]
So now rest is your printing function works.
Bubble sort set array :-
int temp = 0;
for(int i = 0;i<set.length-1;i++)
{
for(int j = 0;j<set.length-1;j++)
{
if(set[j]>set[j+1])
{
temp = set[j];
set[j] = set[j+1];
set[j+1] = temp;
}
}
}
//Your set array is sorted using bubble sort at this point.

Least Common Subsequence with optimized space (two columns, since it's Markovian)

I'm trying to write LCS of two String objects using bottom-up dynamic programming. I'm able to get it to work properly with O(mn) space. However, as I could see, I don't need all the previous columns. So, I tried to modify it to get it fit in 2 columns so space become O(m). However, it's not working for all inputs (for example, to this: abcabc and abcbcca). What am I missing here? NOT HW, NOT CONTEST nothing. Practicing DP.
public int longestCommonSubsequence(String input) {
char[] firstStr = this.string.toCharArray();
char[] secondStr = input.toCharArray();
int[][] maxLength = new int[firstStr.length+1][2];
for(int i=0; i <= firstStr.length; i++) {
maxLength[i][0] = 0;
}
for(int j=0; j < 2; j++) {
maxLength[0][j] = 0;
}
for(int i=0; i < firstStr.length; i++) {
for(int j=0; j < secondStr.length; j++) {
if(firstStr[i] == secondStr[j]) {
maxLength[i+1][1] = 1 + maxLength[i][0];
}
else {
maxLength[i+1][1] = maxLength[i][1]>maxLength[i+1][0]?maxLength[i][1]:maxLength[i+1][0];
}
}
//Copy second row to first row
for(int l =0; l < firstStr.length; l++) {
maxLength[l][0] = maxLength[l][1];
}
}
return maxLength[firstStr.length -1][0];
}
This has two problems:
if(firstStr[i] == secondStr[j]) {
maxLength[i+1][1] = 1 + maxLength[i][0];
// check here if maxLength[i+1][1] is greather than the last max length
}
else {
maxLength[i+1][1] = maxLength[i][1]>maxLength[i+1][0]?maxLength[i][1]:maxLength[i+1][0];
// absolutely wrong: maxLength[i+1][1] = 0;
}
Here can see the algorithm with micro optimization.
public static int lcs(String s0, String s1) {
int maxLength = 0;
int [][]lengths = new int[2][s1.length()+1];
for (int i = 0; i < s0.length(); i++) {
for (int j = 0; j < s1.length(); j++) {
if (s0.charAt(i) == s1.charAt(j)) {
lengths[0][j+1] = lengths[1][j] + 1;
if (lengths[0][j+1] > maxLength) {
maxLength = lengths[0][j+1];
}
} else {
lengths[0][j+1] = 0;
}
}
int []temp = lengths[0];
lengths[0] = lengths[1];
lengths[1] = temp;
}
return maxLength;
}

Game of Life Matrix copy

I'm working on a version of Conway's Game of Life and I've created the method for creating a new Generation, where I copy a matrix and look for nearby neighbors. I would however like to split this method in to two seperate methods. A copy method of the matrix and then the original newGeneration would call on copy() for help. This is how my newGeneration method looks like right now.
public void newGeneration() {
temp = new boolean[board.getRows()][board.getCols()];
for (int i = 0; i < board.getRows(); i++) {
for (int j = 0; j < board.getCols(); j++) {
if (board.get(i, j)==true) {
if (getNeighbours(board, i, j) > 3) {
temp[i][j] = false;
} else if (getNeighbours(board, i, j) < 2) {
temp[i][j] = false;
} else{
temp[i][j] = true;
}
} else if (board.get(i, j) == false) {
if (getNeighbours(board, i, j) == 3) {
temp[i][j] = true;
}
}
}
}
for (int i = 0; i < board.getRows(); i++) {
for (int j = 0; j < board.getCols(); j++) {
board.put(i, j, temp[i][j]);
}
}
I want to split this in to two methods, newGeneration() and copy(). I've been working on it for a while now but I seem to screw up with the variables i and j because theyre locally set in the loops. Any help with splitting this method up in to two will be appreciated, thanks!
EDIT:
From some sage advice recommending me of this post, I made something like this
public void newGeneration() {
boolean[][] tempCells = new boolean [board.getRows()][board.getCols()];
for (int row = 0; row < board.getRows(); row++) {
for (int col = 0; col < board.getCols(); col++) {
int n = getNeighbours(board,row,col);
if (n > 3 || n < 2)
tempCells[row][col] = false;
else if (n == 3)
tempCells[row][col] = true;
else
tempCells[row][col] = temp[board.getRows()][board.getCols()];
}
}
}
But it doesn't seem to work properly.
A simple way to get a copy of an array is to clone it. Since clone gives just a shallow copy, it requires explicit cloning for each additional dimension for multidimensional arrays:
public static boolean[][] copy(boolean[][] source) {
boolean[][] copy = source.clone();
for (int i=0; i<copy.length; ++i) {
copy[i] = copy[i].clone();
}
return copy;
}

Categories

Resources