Java - determining is Poker Hand has a Pair - java

I'm trying to evaluate a hand to see if they have a pair and I feel like this is right but i keep getting errors. Any idea on what im doing wrong?
public boolean isPair() {
String[] values = new String[5];
int counter = 0;
//Put each cards numeric value into array
for(int i = 0; i < cards.length; i++){
values[i] = cards[i].toString();
}
//Loop through the values. Compare each value to all values
//If exactly two matches are made - return true
for(int j = 0; j < values.length; j++){
for(int k = 0; k < cards.length; k++){
if(values[j].equals(cards[k].toString()))
counter++;
if(counter == 2)
return true;
}
counter = 0;
}
return false;
}

The first obvious error I can see, is that you re comparing the first card with itself here :
for(int j = 0; j < values.length; j++){
for(int k = 0; k < cards.length; k++){
Your index k should not be verified if it equals j.
Secondly, why are you using a your variable "hand" in the comparison when you bothered to create a array of String containing your hand ?
values[j].equals(cards[k].toString())
You can write :
values[j].equals(values[k])
I dont think it is responsible for any errors, but it s much easier to understand.
And finally, your counter is false. A pair, is by definition two cards of the same value. So, you have to check if a value is present only two times(that means 1 equality) in your hand.
So you'll have :
for(int j = 0; j < values.length; j++){
for(int k = 0; k < cards.length; k++){
if(k!=j){
if(values[j].equals(values[k]))
counter ++;
}
}
if (counter ==1 ) //Counter==1 means the a value matched with an other value in your hand only once, involving one pair.
return true;
else counter = 0;
}
return false;

String[] values = new String[5];
should be
String[] values = new String[cards.length];
However, even better would be to not use values anyway, since it's almost a copy of cards.

Related

Java sudoku Solver Method

I'm trying out this code for a project where we do the fill grid and evaluate methods for a given sudoku grid via three urls that we can interchange within the code. I'm not interested in efficiency at this point because I'm still trying to get a handle on Java. So far this is the code I have for the evaluate method
I understand the logic behind the first two for loops for the box but I'm trying to understand the second two
EDIT: I'm asking if I what I wrote for the box checker is correct because it still returns false even though the written grids are all correct sudoku boards
public boolean evaluate() {
// check row
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = j + 1; k < 9; k++) {
if (gridButton[i][j].getText().equals(gridButton[i][k].getText())) return false;
}
}
}
// check box
for (int i = 0; i < 9; i += 3) {
for (int j = 0; j < 9; j += 3) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
if (gridButton[i][j].getText().equals(gridButton[k][l].getText())) return false;
k += 1;
}
}
}
}
// check col
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = i + 1; k < 9; k++) {
if (gridButton[i][j].getText().equals(gridButton[k][j].getText())) return false;
}
}
}
return false;
}
I worked with my professor for a viable row checker and I adapted it myself for the col checker but I'm still not totally sure about how to write the box checker.
The Box algorithm in your code does 9x9x3x3 == 729 comparisons.
Perhaps a better approach would be to use a HashSet across each row, column, or box. Once a duplicate value is found in the set, it can bail out.
With using a Set, there's 9x3x3 == 81 looksup and insertions to be made. Assumption is that Set insertion and lookup are both O(1)
// check box
HashSet<String> set = new HashSet<String>();
for (int i = 0; i < 9; i++) { // for each major 3x3 box
set.clear(); // start with an empty "set"
// examine each cell in the box. If the cell's value is already in
// "set", then we have a duplicate and should exit. Otherwise, add
// the cell's value to the set
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
int row = (i / 3) + r;
int column = 3*(i%3) + c;
String val = gridButton[row][column].getText();
if (set.contains(val) {
return false; // the value of the cell was already found, bail
}
set.add(val); // not found, add it to the set
}
}
}
You can apply the same technique to the "check row" and "check column" algorithm and have less comparisons to make as well. That's an exercise I'll leave up to you to solve.
And if we were comparing integers instead of text strings, we could use a simple Boolean array instead of a HashSet instance. :)

How to calculate dimensions of a 2d array?

I am looking for the dimensions of a 2d array. Not the fixed values assigned to the array, but the values containing integers. Example:
int a[][] = new int[4][5];
a[1][1] = 1;
a[1][2] = 2;
a[1][3] = -2;
a[1][4] = 0;
a[2][1] = -3;
a[2][2] = 4;
a[2][3] = 7;
a[2][4] = 2;
a[3][1] = 6;
a[3][2] = 0;
a[3][3] = 3;
a[3][4] = 1;
Only 3 variables are used out of 4, and 4 variables used out of 5. How can you tell what is actually there versus what is assigned to be there?
array.length;
array[0].length;
The previous code will only give you the fixed variable assigned to the array. But what if your not using all the variables? In other words I am looking for an output of 3 for columns and 4 for rows.
In your code example, you are using all variables in the array. Just because you haven't assigned anything to them, they are still sitting there.
If you want to determine whether or not you've assigned a value to each element in the array, you need to initialize everything to a default value, and then just compare against the default value.
A variation on that option is to declare them as int?, which is a nullable int. That would allow you to set everything to null, and then you'll know that the elements that are no longer null have had a value assigned to them.
You can't do it dynamically. Once you've initiated your matrix like that (new int[4][5]) you have and matrix of 0.
You must initiate the matrix ant set an invalid value to all posistions, something like -1 or whatever you wan't and iterate over it to discover the result
// using Integer instead of int, which is a nullable object
Integer a[][] = new Integer[4][5];
int cnt1 = 0;
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
if(a[i][j] != null) {
cnt1++;
}
}
}
System.out.println(cnt1 + " array indices are non-null.");
// using int
int b[][] = new int[4][5];
for(int i = 0; i < b.length; i++) {
for(int j = 0; j < b[i].length; j++) {
b[i][j] = -9999;
}
}
int cnt2 = 0;
for(int i = 0; i < b.length; i++) {
for(int j = 0; j < b[i].length; j++) {
// pick a value that will never be assigned
if(b[i][j] != -9999) {
cnt2++;
}
}
}
System.out.println(cnt2 + " array indices are not -9999.");

No three same in row

Task: Given an integer denoting the size of the array.Fill array with integers.
Return true,if the array contains three of same elements not next to each other.
Return false,if the array not contains three of same elements,or contains but next to each other.
public static boolean noThreeInRow(int [] array){
for(int i = 0; i < array.length-1; i++){
if(array.length < 3) System.exit(0);
if(array[i] != array[i+1]){
return true;
}else return false;
}return true;
}
I can't solved this problem.Someone could help me?I am very beginner!
try using a counter and two loops to compare each number.
int counter = 0;
for(int i = 0; i < array.length-1; i++){
if(array.length < 3) System.exit(0);
for (int j = 0; j < array.length-1; j++){
if (array[i] == array [j+2]) counter ++
}
return true
}return true;
Try something like that. But you will have to figure out how to deal with the numbers before array[i] during the j loop. Happy Coding :)

Java 2D array assigning different values at different intervals

I apologize if I'm not clear. I'm new to programming. So lets say I have a char[10][10]. And there are two+ chars I want to assign at intervals for example i[0][0] to i[5][7] have Y and the rest have N. How would I do that if its possible? I've been trying to figure it out for 6+ hours.
One possible solution would be to have a 'for' block that goes through the rows and another 'for' block that goes through the columns. it could be something like
char[] arr= {'Y','N'};
int counter = 0; // <- these are optional depending on what you choose below
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
// i[j][k]= here you should assign the value
counter++;
}
}
The way to assign the value depends on what you want to do. If you want to have it to generate randomly you can do something like i[j][k]= arr[(int)(Math.random()*2)] or if you want to have it alternate between Y and N you could have a counter variable and assign i[j][k]= arr[counter%2] . If you want to assign the first half to 'Y' and the other half to 'N' i[j][k]= (counter<=50)?'Y':'N';. And the particular case you ask would be i[j][k]= (j<=5 && k<=7)?'Y':'N';It really depends much on what you want to do
This can be done with loops.
for(int i = 0; i < 5; i++){
for(int j = 0; j < 7; j++){
i[i][j] = 'N';
}
for(int j = 7; j < 10; j++){
i[i][j] = 'Y';
}
}
for(int i = 5; i < 10; i++){
for(int j = 0; j < 10; j++){
i[i][j] = 'Y';
}
}
char[][] theArray = new char[10][10]
upToX = 5; // limit for rows
upToY = 7; // limit for columns
for(int i = 0; i < 10; i++ ){
for(int j = 0; j< 10; j++ ){
if((i+1)*(j+1) <= (upToX+1)*(upToY+1)){
theArray[i][j] = 'Y';
}
else{
theArray[i][j] = 'N';
}
}
}
Try using for-loops and if-else. Since you are looking for yes/no type values, I just used boolean type in my example
boolean[][] arr = new boolean[10][10];
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; j++) {
if(i < 6 && j < 8)
arr[i][j] = true;
else
arr[i][j] = false;
}
}

Java loop over half of array

I would like to loop over half of an array in Java; this is because the matrix will be completely symmetric. If I loop throw i columns and j rows, every time I do an operation of matrix[i][j] I will do the exact same operation to matrix[j][i]. I should be able to save time by not looping over half of the matrix. Any ideas on the easiest way to do this?
If you're trying to get a triangle:
for(int i=0; i<array.length; i++){
for(int j=0; j<=i; j++){
..do stuff...
}
}
for (i = 0;i < size; ++i) {
for (j = 0; j < i; ++j) {
result = do_operation(i,j);
matrix[i][j] = result;
matrix[j][i] = result ;
}
}
So you invoke the operation method do_operation only once for each pair.
for(int i = 0; i<array.length; i++){
for(int j = 0; j < array[i].length - i; j++){
// operation here
}
}
Maybe I'm missing something here, but let's say you have two arrays representing your rows and columns respectively, and assuming that it's symmetric (as you say):
int dimension = rows.Length;
for(int i=0; i<dimension; i++)
{
int j = (dimension-1) - i; //need dimension-1 to avoid an off-by-one error
DoSomething(matrix[i][j]);
DoSomehting(matrix[j][i]);
}
This solution has the runtime complexity benefit of only iterating over one loop as opposed to two.

Categories