Removing four of a king - java

I'm writing code for a GoFish game and everytime I get four of a kind I get an out of bounds error.
Here is what I have. I need to find four and then remove it. What I am trying to do with this code is go through each card and check if there is four of it in my hand.
int c = 0;
for (int k = 0; k < hand.size(); k++) {
c = 0;
for (int z = 0; z < hand.size(); z++) {
if(hand.get(k).getRank().equalsIgnoreCase(hand.get(z).getRank())) {
c++;
if (c==4) {
for(int m = z; z > m-4; z-- ) {
hand.remove(m);
}
}
}
}
}

Related

Bidimentional Array

Hi all my program consist of an 2 Dimension array,im reading 2 cordinates in a loop and triying to check if those cordinates in the array are alredy been filled with a asterisc,if this is true y want to re-enicialize my array with the default value "-", and if there is not an asterisc in that specified position y want to fill it in with a asterisc,im not sure if im going for the correct aproach.
this is part of my code.
thanks all.
String[][] matrix = new String[5][5];
String asterisc = "*";
String defaultValue = "_";
Scanner sc = new Scanner(System.in);
int a, b;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
matrix[i][j] = defaultValue;
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
System.out.print(matrix[i][j] + "|");
}
System.out.println();
}
a = 0;
b = 0;
while (a >= 0 && b >= 0 && a < matrix.length && b < matrix.length) {
a = sc.nextInt();
b = sc.nextInt();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
if (matrix[a][b].equals(asterisc)) {
matrix[i][j] = defaultValue;
} else {
matrix[a][b] = asterisc;
}
}
}
}
There are unfortunately many things wrong with your code.
Also you have not explained what your algorithm is trying to do.
In brief, to set all asterisks to defaults, you can do
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if matrix[i][j].equals(asterisc){
matrix[i][j]=defaultValue;
}
}
System.out.println();
}
But
why are you using a while loop?
Why are you using a scanner?
Why are a and b initialised at zero, yet need to be greater than zero for the loop?
Are you really trying to re-initialise your whole array, every time the (a,b) item is asterisc?
I think it is not true.Suppose you have typed "6 6" in the terminal,then the variable a=6,and b=6,which is greater than the array length,and the program will throw a exception.I think the thing you may want to do can follow this codeļ¼š
while(true){
a = sc.nextInt();
b = sc.nextInt();
if(a<0||a>matrix.length||b<0||b>matrix.lenght)
break;
}

How can I simplify boolean Algebra Expressions without using outside Java addons?

I am trying to take a Boolean algebra expression generated from another part of the project and simplify it before I output it. This Boolean expression will only include or statements due to the nature of the rest of the program.
An example would be starting with an expression like aC + bC + BC + Ab + Ac + AC + AB and ending with A + C. The lower case letters indicate not e.g. a means not A.
I have struggled for hours trying to figure this out, but every time I solve one case I ruin another. I imagine there is some recursive function for this problem but I struggled to find anything elsewhere.
Edit: To be clear, I understand how to simplify the expression on paper, I just am unable to simplify it using Java.
Thanks in advance.
public static void mutate (ArrayList<String> Final, ArrayList<String> Factored, String a){
int multiples = 0;
String holder = "";
for (int i = 0; i < Final.size(); i++){
if (Final.get(i).contains(a)){
multiples++;
if (multiples == 1)
holder = Final.get(i);
if (multiples >=2)
Factored.add(Final.get(i));
}
}
if (multiples > 1)
Factored.add(holder);
}
public static void simplify (ArrayList<String> Factored, char a, ArrayList<String> Main){
String temp = "";
int size = Factored.size();
int cnt = 0;
int cnt2 = 0;
int holder = 0;
System.out.println(Factored);
int menial = 0;
if (Factored.size() == 0)
menial = 1;
else{
for (int i = 0; i < size; i++){ //Gets rid of leading letters
if(Factored.get(i).charAt(0) == a){
temp = Factored.get(i).substring(1);
Factored.add(temp);
}
}
for (int i = 0; i < size; i++){ //Gets rid of leading letters
if(Factored.get(i).charAt(1) == a){
temp = Factored.get(i).substring(1);
Factored.add(temp);
}
}
for (int j = 0; j < size; j++){
Factored.remove(j - cnt);
cnt++;
}
for (int i = 0; i < Factored.size(); i++){ //gets rid of duplicates if they exist
for (int j = i + 1; j < Factored.size();j++)
if (Factored.get(i).equals(Factored.get(j))){
cnt2++;
if (cnt2 == 1)
holder = i;
}
}
if (cnt2 >= 1)
Factored.remove(holder);
String temp1 = "";
int size1 = Factored.size();
for (int i = 0; i < size1; i++){ //Makes it uppercase
temp1 = Factored.get(i).toUpperCase();
Factored.add(temp1);
}
int cnt3 = 0;
for (int i = 0; i <size1; i++){
Factored.remove(i-cnt3);
cnt3++;
}
int sizeOver2 = Factored.size()/2;
int holder1 = 0; int holder2 = 0;
for (int q = 0; q < sizeOver2; q++){
int cnt4 = 0;
for (int i = 0; i < Factored.size(); i++){ //gets rid of duplicates if they exist now that there are caps
for (int j = i + 1; j < Factored.size();j++)
if (Factored.get(i).equals(Factored.get(j))){
cnt4++;
if (cnt4 == 1)
holder1 = i;
holder2 = j;
}
if (cnt4 >= 1){
Factored.remove(holder1);
Factored.remove(holder2 - 1);
}
}
}
if (Factored.size() == 0)
Main.add(Character.toString(a));
else if (Factored.size() > 1)
menial = 5;
else
for (int i = 0; i < Factored.size(); i++)
Main.add(Factored.get(i));
}}
}
Edit2: I have added my code above. I apologize for the slopiness in advance, it got pretty messy towards the end. The Factored arraylist has all of the elements of the boolean expression separated E.g. the first element is aC, and the second is bC, etc. My plan was to find duplicates and try to factor them out, but after coding all of this I realized the egregious logic error. It has to be more complicated than that and at this point I'm lost in my own code.

Can you give me idea on how to code this program using array and for loops?

I was supposed to make a program using for loop and arrays now the output program will need to show something like this
0-1-0-0-1-0-0-0-1-0-0-0-0-1
Notice that the 0 is adding every after the 1's show so far im stuck with this code can you help me please...
int[] binary = new int[150];
int b = 0;
int x = 3;
for(int a = 0; a < binary.length; a=a+2) {
box[a] =1;
}
for(int i =0; i < binary.length; i++){
for(b = 0; b < binary.length; b = b+x){
if(box[b] == 1) {
box[b] =0; // this condition changes the value of 1 to 0 if the binary is already "1".
}
else {
box[b] =1; // if the value of the binary is 0 it changes it to 1.
}
}
x++; // Putting this changes the value of the x making x=4 so that the next time the for loop runs it adds the int b to 4..(NOT SURE IF THIS IS RIGHT THOUGH)
}
for(int c =0; c < binary.length; c++) {
System.out.print(binary[c]); // "this should print the output "
}
My problem is making it show something like this
01001000100001000001
You're mixing multiple variable names, since you assign the values to box you should probably remove binary - so something like,
int[] box = new int[150];
int b = 0;
int x = 3;
for (int a = 0; a < box.length; a = a + 2) {
box[a] = 1;
}
for (int i = 0; i < box.length; i++) {
for (b = 0; b < box.length; b = b + x) {
if (box[b] == 1) {
box[b] = 0;
} else {
box[b] = 1;
}
}
x++;
}
for (int c = 0; c < box.length; c++) {
System.out.print(box[c]); // "this should print the output "
}
Output is
101101111011111101111111101111111111011111111111101111111111111101111111111111111011111111111111111101111111111111111111101111111111111111111111011111
I'm not sure to fully understand what you want. From your post I only understand that your goal is to have an output composed of 0 and 1 only and starting with a 0 immediately followed by a 1. The number of zeros following a 1 is successively increased by 1 : so you have after that 2 zeros followed by a 1 and then 3 zeros followed by a 1, 4 zeros followed by a 1 and so one : 0-1-00-1-000-1-0000-1.
Am I right ?
If that is what you want, try something like that :
int num_zero = 1;
int cptr = 0;
int[] arr = new int[20];
for(int j = 0; j < arr.length; j++){
if(cptr == num_zero){
arr[j] = 1;
num_zero = 1+ cptr;
cptr = 0;
}else{
arr[j] = 0;
++cptr;
}
}
Here is the output :
01001000100001000001
Try this:
int[] binary = new int[150];
int onesNextIndex = 1;
int onesGap = 2;
for(int i = 0; i < binary.length; i++){
if(i == onesNextIndex){
binary[i] = 1;
onesNextIndex += onesGap + 1;
onesGap++;
}else{
binary[i] = 0;
}
}
You can test it with:
for(int x : binary){
System.out.print(x);
}
it will print: 0100100010000100000100000010000000100000000..... etc
if you want it the other way around then :
int[] binary = new int[150];
int zerosNextIndex = 1;
int zerosGap = 2;
for(int i = 0; i < binary.length; i++){
if(i == zerosNextIndex){
binary[i] = 0;
zerosNextIndex += zerosGap + 1;
onesGap++;
}else{
binary[i] = 1;
}
}
this will print: 101101110111101111101111110111111101111111101111.......etc
Hope this helps

How to dynamically control the for-loop nested level?

I am implementing some algorithm, in which the number of loop nested levels is determined by the input.
For example, if the input is 2-dimensional, then there two nested for-loops, as below:
for(int i=0; i<N; i++) {
for(int j=i+1; j<N; j++) {
if(table[i][j] == -1) {
for(int c=0; c<C; c++) {
int ii = table[i][c];
int jj = table[j][c];
sort(ii, jj);
if((T[ii][jj] != -1 && T[ii][jj] < l)) {
T[i][j] = l;
break;
}
}
}
}
}
If the input is 3-dimensional, then it would be something like below:
for(int i=0; i<N; i++) {
for(int j=i+1; j<N; j++) {
for(int k=j+1; k<N; k++) {
if(table[i][j][k] == -1) {
for(int c=0; c<C; c++) {
int ii = table[i][c];
int jj = table[j][c];
int kk = table[k][c];
sort(ii, jj, kk);
if((T[ii][jj][kk] != -1 && T[ii][jj][kk] < l)) {
T[i][j][k] = l;
break;
}
}
}
}
}
If there are only these two case, then I can write two versions of nested for-loops. But the dimensions of input could be any value between 2 and N. In this case, how to control the nested loop level dynamically, or is there any alternative to go around of this?
The only real way to do this is to use recursion.
You write a method containing a single for loop, each time around the loop if it needs to go deeper then the method calls itself with the right settings for that nested loop to be run.
Recursion is already been explained here. However, there is another solution as well. Using only one big loop containing a tiny inner loop.
int n = ...;
int dim = ...;
// Raise n to the power of dim: powN = n^dim
long powN = 1;
for (int i = 0; i < dim; ++i) powN *= n;
int[] indices = new int[dim];
for (long i = 0; i < powN; ++i)
{
// Calculate the indices
long bigI = i;
for (int k = 0; k < dim; ++k)
{
indices[k] = bigI % n;
bigI /= n;
}
// Now all your indices are stored in indices[]
}
I was suggesting something like this :
public static void recursiveLoop(int N, int level, int a){
if (level<0)
return;
for (int i=a; i<N; i++){
System.out.println("Level is : "+ level+ " i: "+i );
recursiveLoop(N,level-1,i+1);
}
}
You may explain what you really want to do.
If the Outer for loops are doing nothing but controlling a count, then your Nested for loops are simply a more complicated way of iterating by a count that could be handled by a Single for loop.
like:
for (x = 0; x < 8; x++) {
for (y = 0; y < 10; y++) {
for (z = 0; z < 5; z++) {
DoYourStuffs();
}
}
}
Is equivalent to:
for (x = 0; x < 8*10*5; x++) {
DoYourStuffs();
}

How do i complete this question with 2-dimensional array in java?

Hey guys, im working through the Introduction to Programming in Java book and one of the exercises is this:
Empirical shuffle check. Run
computational experiments to check
that our shuffling code works as
advertised. Write a program
ShuffleTest that takes command-line
arguments M and N, does N shuffles of
an array of size M that is initialized
with a[i] = i before each shuffle, and
prints an M-by-M table such that row i
gives the number of times i wound up
in position j for all j. All entries
in the array should be close to N/M.
Now, this code just outputs a block of zeros...
public class ShuffleTest2 {
public static void main(String[] args) {
int M = Integer.parseInt(args[0]);
int N = Integer.parseInt(args[1]);
int [] deck = new int [M];
for (int i = 0; i < M; ++i)
deck [i] = i;
int [][] a = new int [M][M];
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
a[i][j] = 0 ;
for(int n = 0; n < N; n++) {
int r = i + (int)(Math.random() * (M-i));
int t = deck[r];
deck[r] = deck[i];
deck[i] = t;
for (int b = 0; b < N; b++)
{
for (int c = 0; c < M; c++)
System.out.print(" " + a[b][c]);
System.out.println();
}
}
}
}
}
}
What am i doing wrong? :(
Thanks
So a is like a history? As you are now it is always filled with zeroes just like you initialized, you never assign to it! After the "shuffling" for loop you need to set
A[i][POSITION] = CARD_VALUE
Meaning that after i-th shuffle, card CARD_VALUE is in position POSITION. I don't want to give you all the specifics, but it will take another for loop, and the nested for-loop for printing needs to be independent of any other loop, occuring when everything else is done.
Looks like you have a few things concerning the for-loops that you need to look over carefully. Trace the program flow manually or with a debugger and you'll notice that some of those braces and code blocks need to be moved.
--TRY THIS--
public class ShuffleTest2 {
public static void main(String[] args) {
int M = Integer.parseInt(args[0]);
int N = Integer.parseInt(args[1]);
int [] deck = new int [M];
int [][] a = new int [M][M];
for (int i = 0; i < M; i++) { //initialize a to all zeroes
for (int j = 0; j < M; j++) {
a[i][j] = 0 ;
}
}
for(int i = 0; i < N; i++) //puts the deck in order, shuffles it, and records. N times
{
for (int j = 0; j < M; j++) //order the deck
deck[j] = j;
for(int j = 0; j < M; j++) { //shuffle the deck (same as yours except counter name)
int r = j + (int)(Math.random() * (M-j));
int t = deck[r];
deck[r] = deck[j];
deck[j] = t;
}
for(int j = 0; j < M; j++) //record status of this deck as described
{
int card_at_j = deck[j]; //value of card in position j
a[card_at_j][j]++; //tally that card_at_j occured in position j
}
} //big loop ended
for (int b = 0; b < M; b++) //print loop. a is MxM, so limit of N was wrong.
{
for (int c = 0; c < M; c++)
{
System.out.print(" " + a[b][c]);
System.out.println();
}
} //print loop ended
} //main() ended
} //class ended

Categories

Resources