How to reverse for loop? - java

I have a method with some logic
first it looked like:
static boolean checkWin(char dot) {
if (map[0][0] == dot && map[0][1] == dot && map[0][2] == dot) {
return true;
}
//more code
if (map[0][2] == dot && map[1][1] == dot && map[2][0] == dot) {
return true;
}
return false;
}
With loops I refactored the method:
static boolean checkWin(char dot) {
for (int i = 0; i <map.length ; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[0][i] == dot && i == 2) {
return true;
}
if (map[1][i] == dot && i == 2) {
return true;
}
if (map[2][i] == dot && i == 2) {
return true;
}
if (map[i][0] == dot && i == 2) {
return true;
}
if (map[i][1] == dot && i == 2) {
return true;
}
if (map[i][2] == dot && i == 2) {
return true;
}
if (map[i][i] == dot && i == 2) {
return true;
}
if (map[0][2] == dot && map[1][1] == dot && map[2][0] == dot) {
return true;
}
}
}
return false;
}
I have a problem with this line of code:
if (map[0][2] == dot && map[1][1] == dot && map[2][0] == dot)
return true;
}
I need to get map[i][?], ? should assume the value 2, 1 and 0 when i equals to 0, 1 and 2 respectively.
How can I achieve it?

Try to decrease it
for (int i = 2; i > = 0; i--) {
}
i will be 2 1 0
P.S
I think you missed 2 there
P.S
If I understand your question correctly, you need to have 2 1 0 inside loop without modifying for itself? In that case try:
for (int i = 3; i < 3; i++) {
... 2 - i
}

Related

How to check if a coordinate is adjacent to another?

I am writing a snake game. I want to write a method to check if the head (index 0) is next to any other body part. I am not sure what is going wrong, but this method does not do anything. I have it set so that if it returns true the game ends (for testing purposes)
Here is my code:
public boolean headNextToBody()
{
boolean xClose = false;
boolean yClose = false;
for(int i = 0; i < bodyParts; i++)
{
if( x[i] == (x[0] + 1) || x[i] == (x[0] - 1))
{
xClose = true;
}
if(y[i] == y[0] + 1 || y[i] == y[0] - 1)
{
yClose = true;
}
}
return (xClose && yClose);
}
Firstly, no need to include the head (at position 0) in the test, as it will always be false.
for(int i = 1; i < bodyParts; i++)
The conditions for adjacency are
(y[i] == y[0]) && (x[i] == (x[0] + 1) || x[i] == (x[0] - 1))
or
(x[i] == x[0]) && (y[i] == (y[0] + 1) || y[i] == (y[0] - 1))
If either of these are true for any non-head point on the snake you can return true immediately, otherwise return false.
You can combine the separate tests into a single if statement:
public boolean headNextToBody()
{
for(int i = 1; i < bodyParts; i++)
{
if(((y[i] == y[0]) && (x[i] == (x[0] + 1) || x[i] == (x[0] - 1))) ||
((x[i] == x[0]) && (y[i] == (y[0] + 1) || y[i] == (y[0] - 1)))
{
return true;
}
}
return false;
}
Alternatively you could shorten the test a little by using some math:
public boolean headNextToBody()
{
for(int i = 1; i < bodyParts; i++)
{
if(1 == ((x[i]-x[0])*(x[i]-x[0]) + (y[i]-y[0])*(y[i]-y[0]))
{
return true;
}
}
return false;
}

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

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;
}

Java - Roman Numeral validity

I am writing a program to add two roman numerals without converting to any bases. I have everything working except I am not sure how to check if my input String is a valid roman numeral or not.
These are the rules to check validity:
Five in a row of any digit is not allowed
Some digits are allowed in runs of up to 4. They are I,X,C, and M. The others (V,L,D) can only appear singly.
Some lower digits can come before a higher digit, but only if they appear singly. E.g. "IX" is ok but "IIIX" is not.
But this is only for pairs of digits. Three ascending numbers in a row is invalid. E.g. "IX" is ok but "IXC" is not.
A single digit with no runs is always allowed
I have not really been able to make much progress with this step and don't have anything working yet. Any help would be great!
Why not use regular expression:
boolean valid = word.matches("^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$");
Look at the post of paxdiablo:
How do you match only valid roman numerals with a regular expression?
Loop through each character in the string.
Use a if conditions to check which character it is.
Use the if conditions to check for the roman numeral rule violations in the adjacent characters to the selected character.
for(int i = 0; i < s.length(); i++){
if (s[i] == 'V'){
**Check if the character before of after is also 'V'. Then it is a violation
}
else if(s[i] == 'L'){
**Conditions for 'L' etc.
}
}
This is what I have come up with based on my rules. Any thoughts on refactoring this to make it simpler?
public static boolean checkValidity (String s1, HashSet<Character> romanNumerals){
HashSet<Character> alreadyContained = new HashSet<Character>();
if (s1.length() == 1 && romanNumerals.contains(s1.charAt(0))){
return true;
}
int i = 0;
while (i < s1.length()){
if (s1.charAt(i) == 'M'){
if (alreadyContained.contains('M')){
return false;
}
int count = 1;
i++;
while (s1.charAt(i) == 'M'){
i++;
count++;
}
alreadyContained.add('M');
if (count >= 5){
return false;
}
}
else if (s1.charAt(i) == 'D'){
if (alreadyContained.contains('D')){
return false;
}
alreadyContained.add('D');
if (!alreadyContained.contains('M')){
alreadyContained.add('M');
}
i++;
if ((i < s1.length()) && (s1.charAt(i) == 'D')){
return false;
}
}
else if (s1.charAt(i) == 'L'){
if (alreadyContained.contains('L')){
return false;
}
alreadyContained.add('L');
if (!alreadyContained.contains('M')){
alreadyContained.add('M');
}
if (!alreadyContained.contains('D')){
alreadyContained.add('D');
}
if (!alreadyContained.contains('C')){
alreadyContained.add('C');
}
i++;
if ((i < s1.length()) && (s1.charAt(i) == 'L')){
return false;
}
}
else if (s1.charAt(i) == 'V'){
if (alreadyContained.contains('V')){
return false;
}
alreadyContained.add('V');
if (!alreadyContained.contains('M')){
alreadyContained.add('M');
}
if (!alreadyContained.contains('D')){
alreadyContained.add('D');
}
if (!alreadyContained.contains('C')){
alreadyContained.add('C');
}
if (!alreadyContained.contains('L')){
alreadyContained.add('L');
}
if (!alreadyContained.contains('X')){
alreadyContained.add('X');
}
i++;
if ((i < s1.length()) && (s1.charAt(i) == 'V')){
return false;
}
}
else if (s1.charAt(i) == 'C'){
if (alreadyContained.contains('C')){
return false;
}
int count = 1;
i++;
if ((i < s1.length()) &&(s1.charAt(i) == 'M' || s1.charAt(i) == 'D')){
i++;
}
else if (i < s1.length() && s1.charAt(i) == 'C'){
while ((i < s1.length()) && (s1.charAt(i) == 'C')){
i++;
count++;
}
}
alreadyContained.add('C');
if (!alreadyContained.contains('M')){
alreadyContained.add('M');
}
if (!alreadyContained.add('D')){
alreadyContained.add('D');
}
if (count >= 5){
return false;
}
}
else if (s1.charAt(i) == 'X'){
if (alreadyContained.contains('X')){
return false;
}
int count = 1;
i++;
if ((i < s1.length()) && (s1.charAt(i) == 'D' || s1.charAt(i) == 'M')){
return false;
}
while ((i < s1.length()) && s1.charAt(i) == 'X'){
i++;
count++;
}
alreadyContained.add('X');
if (count >= 5){
return false;
}
}
else if (s1.charAt(i) == 'I'){
if (alreadyContained.contains('I')){
return false;
}
alreadyContained.add('I');
i++;
int count = 1;
if ((i < s1.length()) && (s1.charAt(i) != 'I' && s1.charAt(i) != 'X' && s1.charAt(i) != 'V')){
return false;
}
else if (i < s1.length() && s1.charAt(i) == 'I'){
while (i < s1.length() && s1.charAt(i) == 'I'){
i++;
count++;
}
if (count >= 4){
return false;
}
}
}
else if (!romanNumerals.contains(s1.charAt(i))){
return false;
}
}
return true;
}

check if array contains certain int BEFORE another int

I need to check if an array contains a 1 and then later in the array contains a 2.
What I have coded only checks if both are in there, not if one is before the other. How could I do this?
if(array[i] == 1)
count++;
else if(array[i] == 2)
count++;
}
if(count > 1)
System.out.print("true");
else
System.out.print("false");
Comparing the index of the values works!
if (nums[i] == 1)
value1 = i;
else if(nums[i] == 2)
value2 = i;
}
if (value2 > value1)
System.out.print("true");
else
System.out.print("false");
This oughta do it!
public void hasOneThenTwo(int[] a) {
bool hasOne = false;
for (int i = 0; i < a.length; ++i) {
if (!hasOne && a[i] == 1) {
hasOne = true;
} else if (hasOne && a[i] == 2) {
return true;
}
}
return false;
}

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;
}

Categories