Poker five in Java in boolean[][] array [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have problem with recognizing flushes and straights. I operate in 2D boolean array and it must be a boolean array nothing else. Could somebody help me in writing this method?
I already have logically incorrect methods, but I don't know how to solve it:
public static Boolean isFlush(boolean[][] hand) { // 5 cards same color
boolean found = false;
for (int i = 0; i < 4; i++) {
for(int j = 0; j < 9; j++){
if (hand[i][j] == true && hand[i][j+1] == true && hand[i][j+2] == true && hand[i][j+3] == true && hand[i][j+4] == true) {
found = true;
}
}}
System.out.println("Found from flush: " + found);
return found;
}
public static Boolean isStraight(boolean[][] hand) { // straight patter example 4,5,6,7,8
boolean found = false;
int pom = 0;
for(int i = 0; i<4; i++) {
for (int j = 0; j < 9; j++) {
if (hand[i][j] == true || hand[i][j+1] == true || hand[i][j+2] == true || hand[i][j+3] == true || hand[i][j+4])
pom++;
// System.out.println("straight value: "+i);
}
}
return pom==5;
}
Working Straight method but step by step writed
public static Boolean isStraight(boolean[][] hand) { // straight patter example 4,5,6,7,8
boolean found = false;
for (int j = 0; j < 9; j++) {
if ((hand[0][j] == true || hand[1][j] == true || hand[2][j] == true || hand[3][j] == true)
&& (hand[0][j+1] == true || hand[1][j+1] == true || hand[2][j+1] == true || hand[3][j+1] == true)
&& (hand[0][j+2] == true || hand[1][j+2] == true || hand[2][j+2] == true || hand[3][j+2] == true)
&& (hand[0][j+3] == true || hand[1][j+3] == true || hand[2][j+3] == true || hand[3][j+3] == true)
&& (hand[0][j+4] == true || hand[1][j+4] == true || hand[2][j+4] == true || hand[3][j+4] == true ))
found = true;
}
return found;
}

For what it is worth, here is the most efficient way to solve a straight, using DP
public static boolean isStraight(boolean[][] hand) {
int[] straightCounter = new int[13];
for (int j=0; j<13; j++) {
boolean isCard = hand[0][j] || hand[1][j] || hand[2][j] || hand[3][j];
if (isCard) {
if (j==0)
straightCounter[j]=1;
else
straightCounter[j]=straightCounter[j-1]+1;
if (straightCounter[j] == 5)
return true;
if (j==12 && straightCounter[j] == 4 && straightCounter[0] == 1)
return true; // the 10/J/Q/K/A scenario
}
}
return false;
}

something like the following
public static Boolean isFlush(boolean[][] hand) { // 5 cards same color
for (int i = 0; i < 4; i++) {
int count = 0;
for(int j = 0; j < 13; j++) {
if(hand[i][j]) {
count++;
}
}
if(count == 5) {
return true;
}
}
return false;
}
public static Boolean isStraight(boolean[][] hand) { // straight patter
for (int i = 0; i < 9; i++) {
int count = 0;
for(int j = 0; j < 5; j++) {
for(int k = 0; k < 4; k++) {
if (hand[k][i + j]) {
count++;
break;
}
}
}
if(count == 5) {
return true;
}
}
return false;
}

Related

Return true if character immediately before and after the '*' in string are Same

I am a beginner in Java
I don't get why my if statement is wrong
public boolean sameStarChar(String str)
{
int len = str.length();
for(int x = 1; x < length-1; x++)
{
if (str.charAt(x) == '*' && str.charAt(x-1) == str.charAt(x+1))
return true;
}
else
return false;
}
Thank you
First thing start your loop from x=0 because the index of first element in 0.
str.charAt(x-1) == str.charAt(x+1)
this condition return true only if the previous and next character of * are same.
public boolean sameStarChar(String str)
{
int len = str.length();
int no=0,yes=0;
for(int x = 0; x < len-1; x++)
{
if (str.charAt(x) == '*')
{
if((str.charAt(x-1)>='a' && str.charAt(x-1)<='z') && (str.charAt(x+1)>='a' && str.charAt(x+1)<='z'))
{
yes++;
}
else{
no++;
}
}
}
if(no>0)
return false;
else
return true;
}
What I have do is that I uses a flag variable no and yes and keep track of your condition.
Hopefully that helps.

Random Tic Tac Toe checker issue

Alright so I have to make a random tictactoe checker that shows when x wins, when o wins, and when there is a tie. So the issues I'm having is it wont show the ties and it will sometimes say that either x or o won when they didn't. I don't know what to change around in my code because before I did my diagonal check it would print out the ties. Here is the whole code but I'm pretty sure the problem is coming from the checking board part by making xWin and oWin come out true I can't find where its doing that tho.
package test;
import java.util.Scanner;
import java.util.Random;
public class TicTacToe {
public static void main(String[] args) {
System.out.println("Welcome to random Tic Tac Toe Checker. Let's see our randomly generated board.");
int dimension = 3;
char[][] board = new char[dimension][dimension];
Random r = new Random();
for (int i = 0; i < 3; i++) // filling board
{
for (int j = 0; j < 3; j++) {
int choice = r.nextInt(2);
if (choice == 0) {
board[i][j] = 'X';
} else if (choice == 1) {
board[i][j] = 'O';
}
}
}
for (int i = 0; i < 3; i++) // filling board
{
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
boolean xWin = false;// checking board, order horizontal,vertical,left
// and right diagonal
boolean oWin = false;
for (int i = 0; i < 3; i++) {
boolean lineWin = true;
for (int j = 0; j < 3; j++) {
if (board[i][j] != board[i][0]) {
lineWin = false;
}
}
if (lineWin == true) {
if (board[i][0] == 'X') {
xWin = false;
}
if (board[i][0] == 'O') {
oWin = false;
}
}
}
for (int j = 0; j < 3; j++) {
boolean lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[i][j] != board[0][j]) {
lineWin = true;
}
}
if (lineWin == true) {
if (board[0][j] == 'X') {
xWin = true;
}
if (board[0][j] == 'O') {
oWin = true;
}
}
}
boolean lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[0][0] != board[i][i]) {
lineWin = false;
}
if (lineWin == true) {
if (board[0][0] == 'X') {
xWin = true;
}
if (board[0][0] == 'O') {
oWin = true;
}
}
}
lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[0][0] != board[i][2 - i]) {
lineWin = false;
}
if (lineWin == true) {
if (board[0][0] == 'X') {
xWin = true;
}
if (board[0][0] == 'O') {
oWin = true;
}
}
}
if (xWin == false && oWin == false)// printing winners
{
System.out.println("CAT!It's a tie no one wins");
}
if (xWin == true) {
System.out.println("X wins!");
}
if (oWin == true) {
System.out.println("O wins!");
}
}
}
I fixed the boolean errors as well as the diagonal logic:
import java.util.Random;
public class TicTacToe {
private static final int DIMENSION = 3;
public static void main(String[] args) {
System.out.println("Welcome to random Tic Tac Toe Checker. Let's see our randomly generated board.");
char[][] board = new char[DIMENSION][DIMENSION];
final Random r = new Random();
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
int choice = r.nextInt(2);
if (choice == 0) {
board[i][j] = 'X';
} else if (choice == 1) {
board[i][j] = 'O';
}
}
}
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
boolean xWin = false;
boolean oWin = false;
for (int i = 0; i < DIMENSION; i++) {
boolean lineWin = true;
for (int j = 0; j < DIMENSION; j++) {
if (board[i][j] != board[i][0]) {
lineWin = false;
}
}
if (lineWin) {
if (board[i][0] == 'X') {
xWin = true;
}
if (board[i][0] == 'O') {
oWin = true;
}
}
}
for (int j = 0; j < DIMENSION; j++) {
boolean lineWin = true;
for (int i = 0; i < DIMENSION; i++) {
if (board[i][j] != board[0][j]) {
lineWin = false;
}
}
if (lineWin) {
if (board[0][j] == 'X') {
xWin = true;
}
if (board[0][j] == 'O') {
oWin = true;
}
}
}
boolean lineWin = true;
for (int i = 0; i < DIMENSION; i++) {
if (board[0][0] != board[i][i]) {
lineWin = false;
}
}
if (lineWin) {//this check should not be in the loop
if (board[0][0] == 'X') {
xWin = true;
}
if (board[0][0] == 'O') {
oWin = true;
}
}
lineWin = true;
for (int i = 0; i < DIMENSION; i++) {
if (board[DIMENSION - 1][0] != board[i][DIMENSION - 1 - i]) {
lineWin = false;
}
}
if (lineWin) {//this check should not be in the loop
if (board[DIMENSION - 1][0] == 'X') {
xWin = true;
}
if (board[DIMENSION - 1][0] == 'O') {
oWin = true;
}
}
if (xWin == true && oWin == true) {//printing winners
System.out.println("Both players won!");
}
if (xWin == false && oWin == false) {
System.out.println("CAT!It's a tie no one wins");
}
if (xWin == true) {
System.out.println("X wins!");
}
if (oWin == true) {
System.out.println("O wins!");
}
}
}
Note: you can increase DIMENSION for a laugh.
I think the main problem in this file comes around the middle, specifically lines 34 - 66, where you do the initial checks for horizontal and vertical rows of the same character.
for (int i = 0; i < 3; i++) {
boolean lineWin = true;
for (int j = 0; j < 3; j++) {
if (board[i][j] != board[i][0]) {
lineWin = false;
}
}
if (lineWin == true) {
if (board[i][0] == 'X') {
xWin = false; //THESE LINES SHOULD BE CHANGING THE WIN CONDITION TO TRUE
}
if (board[i][0] == 'O') {
oWin = false; //LISTED ABOVE
}
}
}
for (int j = 0; j < 3; j++) {
boolean lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[i][j] != board[0][j]) {
lineWin = true; //THIS LINE SHOULD BE FALSE
}
}
if (lineWin == true) {
if (board[0][j] == 'X') {
xWin = true;
}
if (board[0][j] == 'O') {
oWin = true;
}
}
I listed some changes that will fix the code and make it run the way you want it to. It looks like there were some errors in declaring a boolean to be true or false at the wrong time, so this should be able to fix the problem.
In the first block
if(lineWin == true)
{
if(board[i][0] == 'X')
{
xWin = false;
}
if(board[i][0] == 'O')
{
oWin = false;
}
}
should be changed as follows.
if(lineWin == true)
{
if(board[i][0] == 'X')
{
xWin = true;
}
if(board[i][0] == 'O')
{
oWin = true;
}
}
Furthermore, in the second block, the part
if(board[i][j] != board[0][j])
{
lineWin = true;
}
should be changed as follows.
if(board[i][j] != board[0][j])
{
lineWin = false;
}

How to properly checkDiagonalWin in connect 4

My check vertical win and check horizontal win work perfectly fine, however i dont know what to do with my check diagonal code to make it actually check diagonal. Some guidance would be much appreciated and this is in java. Thank you.
private boolean checkVerticalWin()
{
PieceType type = myBoard[myLastPoint.x][myLastPoint.y];
System.out.println("check vert");
for(int j = 0; j < myNumColumns; j++)
{
for(int i = 0; i < myNumRows; i++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
if(count == 1)
{
myWinBegin = new Point(i,j);
}
}
else
{
myWinBegin = null;
count = 0;
}
System.out.println(count);
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
myWinBegin = null;
return false;
}
private boolean checkHorizontalWin()
{
System.out.println("test");
PieceType type = myBoard[myLastPoint.x][myLastPoint.y];
for(int i = 0; i < myNumRows; i++)
{
for(int j = 0; j < myNumColumns; j++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null)
{
count++;
if (count == 1)
{
myWinBegin = new Point(i,j);
}
}
else
{
myWinBegin = null;
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
myWinBegin = null;
return false;
}
private boolean checkDiagonalWin()
{
PieceType type = myBoard[myLastPoint.x][myLastPoint.y];
for(int i = 0; i < myNumRows; i++)
{
for (int j = 0; j < myNumColumns; j++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
myWinBegin = new Point(i,j);
}
else
{
count = 0;
myWinEnd = new Point(i,j);
}
if(count == myWinLength)
{
return true;
}
}
}
for(int j = 0; j < myNumColumns; j--)
{
for (int i = 0; i < myNumRows; i--)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
myWinBegin = new Point(i,j);
}
else
{
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
for(int j = 0; j < myNumColumns; j++)
{
for (int i = 0; i < myNumRows; i--)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
}
else
{
myWinBegin = new Point(i,j);
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
for(int j = 0; j < myNumColumns; j--)
{
for (int i = 0; i < myNumRows; i++)
{
if(myBoard[i][j] == type && myBoard[i][j] != null )
{
count++;
myWinBegin = new Point(i,j);
}
else
{
count = 0;
}
if(count == myWinLength)
{
myWinEnd = new Point(i,j);
return true;
}
}
}
return false;
}
So basically, you need a start point, you then need to determine in which direction to move
With that idea in hand, you could use something like...
boolean win = true;
for (int count = 0; count < 4; count++) {
if (row < myNumRows && row >= 0 && col < myNumColumns && col >= 0) {
int test = myBoard[row][col];
if (test != check) {
win = false;
break;
}
} else {
break;
}
row += rowDelta;
col += colDelta;
}
As the basic algorithm. All this does is checks each cell from a start point, to a total of 4 cells, the algorithm moves by the specified delta/direction and keeps checking while each cell matches the check value.
Now, I'd wrap this in a simple method
public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) {
boolean win = true;
for (int count = 0; count < 4; count++) {
if (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) {
int test = grid[row][col];
if (test != check) {
win = false;
break;
} else {
break;
}
}
row += rowDelta;
col += colDelta;
}
return win;
}
which makes it simpler to call, know given any point, you can do something like...
int startRow = ...;
int startCol = ...;
int player = ...;
if (didWin(myBoard, player, startRow, startCol, 1, 0) || // Vertical, down
didWin(myBoard, 1, startRow, startCol, 0, 1) || // Right
didWin(myBoard, 1, startRow, startCol, 0, -1) || // Left
didWin(myBoard, 1, startRow, startCol, 1, 1) || // Right/down
didWin(myBoard, 1, startRow, startCol, -1, -1) || // Left/Up
didWin(myBoard, 1, startRow, startCol, 1, -1) || // Down/Left
didWin(myBoard, 1, startRow, startCol, -1, 1) // Up/Right
) {
// You be the winner
}
nb: I've left out check a vertical up direction, because it's unlikely that you could actually win this way
ps: I'd be even more lazy and would just have a didWin method, which did the above checks and returned true or false, but I'm lazy
So, the startRow and startCol would represent the anchor point around which you want to check and would, in this example, represent the last drop.
This example uses a int to represent the player/token, but you could use anything, all this does is compares the token you supply with the values in the array

Java, return true if 'x' is next to 'y' in an array

This is, I imagine, a really simple problem to solve however I just can't figure it out.
An array contains a list of integers and I want to return true if every number 'x' in the array is followed by the number 'y'.
So arrays with {x,3,4,y} or {x,x,y,4,5} or {5,8,x,x} would be false.
Whereas arrays with {x,y,4,1} or {x,y,5,1,x,y} would be true.
This is what I have tried so far:
for (int i = 0; i < nums.length-1; i++)
{
if (nums[i] == x && nums[i+1] == y)
{
return true;
}
else
{
return false;
}
}
return false;
My code however will only work for the first two elements in the array (so 0 and 1). It won't detect any integers further down in the array, so how do I do this?
Thank you.
I want to return true if every number 'x' in the array is followed by the number 'y'.
You need to get rid of the else and modify the checks like so:
for (int i = 0; i < nums.length - 1; i++)
{
if (nums[i] == x && nums[i + 1] != y)
{
return false;
}
}
return true;
Caveats:
This returns true if x is not present in the array. It's unclear from the question whether this is the behaviour you want.
This does not check whether x is the last element of the array. Again, it's not entirely clear what you'd expect to happen if it is.
When the code reaches a return statement it stops executing the function and returns the specified value, that's why your for is only executed once.
Since you want the condition to apply to ALL the elements in the array, you have to return false when you find a 2 that isn't followed by a 3. If the 2 is followed by a 3, you just keep checking the next position.
Like this:
for (int i = 0; i < nums.length-1; i++)
{
if(nums[i] == 2 && nums[i+1] != 3)
{
return false;
}
}
return true;
for (int i = 0; i < nums.length-1; i++) {
if (nums[i] == 2 && nums[i+1] != 3) {
return false;
}
}
return true;
This code will return False iff the 'x' number is not followed by 'y'.
for (int i = 0; i < nums.length-1; i++)
{
if(nums[i] == 2 && nums[i+1] != 3) {
return false;
}
}
return true;
for (int i = 0; i < nums.length-1; i++)
{
if(nums[i] == 2 && nums[i+1] == 3)
{
return true;
}
}
return false;
Your Code is returning in the first iteration always. because you have return written there in both If and else block.
remove the else case or just the return; from else case if there is any other logic inside.
int x = 2;
int y=3;
for (int i = 0; i < nums.length-1; i++) {
if (nums[i] == x && nums[i+1] != y) {
return false;
}
}
return true;
If you do not have x in the array, it will return true. and if you have any x that does not have y after it, it will return false.
Try out Following
boolean flag = false;
for (int i = 0; i <= (nums.length-1); i++) {
if(nums[i] == x) {
if (i == (nums.length-1)) {
if(nums[i] == x) {
flag = false;
}
} else {
if(nums[i+1] == y) {
flag = true;
} else {
flag = false;
break;
}
}
}
}
return flag;
You can try out the following:
if(nums[nums.length - 1] == x)
{
return false;
}
else
{
boolean flag = false;
for(i = 0; i < nums.length - 1; i++)
{
if(nums[i] == x)
{
if(nums[i + 1] == y)
{
flag = true;
}
else
{
flag = false;
break;
}
}
}
return flag;
}

Cant compare using If Statement in Java wild error Markers Undefined for Arguments?

Why does it tell me that I can't compare Ints? I am trying to compare this line here and it wont let me all I get is :
if (counter1 = 0 || counter2 = 0)
{
return false;
}
Here is the rest of my code for reference.
public static boolean checkPassword(String password){
int length;
length = password.length();
if (length < 6 || length > 11){
System.out.println("Password must be 6 - 10 characters long!");
return false;
}
int counter1 = 0;
for (int i = 0; i < password.length(); i++){
if (Character.isLetter(password.charAt(i)))
counter1++;
}
int counter2 = 0;
for (int i = 0; i < password.length(); i++){
if(Character.isDigit(password.charAt(i)))
counter2++;
}
if (counter1 = 0 || counter2 = 0)
{
return false;
}
return true;
}
I keep getting Markers Undefined help me :)
You are not using the equality test == but the assignment operator =. It is a very common beginners mistake.
if (counter1 == 0 || counter2 == 0) {
return false;
}
The statement counter1 = 0 is an assignment, so you're ultimately trying if (0 || 0) but the || operator expects booleans, not integers. Use == instead.
Because in Java = isn't used for comparison. You need to use == This should be
if (counter1 == 0 || counter2 == 0)
{
return false;
}

Categories