okay so what I have to do is make a java connect four game. One of the methods which is named full is used to check if the board is full or not. If it is it returns true, if not then it returns false. White is used for empty spaces. The problem is I can't compare board and Color.White and I don't know what to do from here. My code is below
public static boolean full(Color[][] board) {
for(int i = 0; i < board.length; i++){
if (board != Color.WHITE){
return true;
} else {
return false;
}
}
}
It is hard to say with only the snippet of code. What kind of object is 'board'?
To me it seems like you should increment through the Color[][] array in a double for-loop and see if any of the elements are equal to Color.WHITE.
public static boolean full(Color[][] board) {
for(int i = 0; i < board.length; i++){
for(int j=0; j<board.length;j++) {
if (board[i][j] == Color.WHITE){
return false;
} else {
return true;
}
}
}
}
Do you have any logs, error messages, or print statements to help debug? Good luck!
Related
I am currently trying to learn the topic of Backtracking in Java. It is really really confusing for me because I am stuck.
The problem is to find ways in which N Queens can be placed in NxN Chess board so that none of the Queens can attack each other. A queen can attack in the same row, same column and diagonally. My code goes like this:
import java.util.Scanner;
class Main {
public static void putZero(int[][] board,int n){
for(int i = 0;i<n;i++){
for(int j=0;j<n;j++){
board[i][j]=0;
}
}
}
public static void printBoard(int[][] board,int n){
for(int i = 0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(board[i][j]);
}
System.out.print("\n");
}
System.out.print("\n\n\n");
}
public static void SolveNQ(int n){
int[][] board = new int[n][n];
putZero(board,n);
if(SolveQUtil(board,0,n)==true){
printBoard(board,n);
}
}
public static boolean isSafe(int row, int col, int[][] board,int n){
int i,j;
for(i=0;i<col;i++){
if(board[row][i]==1)
return false;
}
for(i=row,j = col; i >= 0 && j >= 0; i--, j--){
if(board[i][j]==1)
return false;
}
for (i = row, j = col; j >= 0 && i < n; i++, j--)
if (board[i][j] == 1)
return false;
return true;
}
public static boolean SolveQUtil(int[][] board, int col, int n){
if(col>=n){
return true;
}
else
for(int i=0;i<n;i++){
if(isSafe(i,col,board,n)==true){
board[i][col]=1;
boolean a = SolveQUtil(board,col+1,n);
if(a==true)
return true;
else
board[i][col]=0;
}
}
return false;
}
public static void main(String[] args){
Scanner scan = new Scanner(`enter code here`System.in);
int n = scan.nextInt();;
SolveNQ(n);
}
}
It is producing the result I want, but I am not understanding how this works. In my method SolveQUtil(), the method is called again which is "recursive". When col = 0 is called, the Q1 is placed at [0,0] as there are no existing queens. But when col = 1 is called recursively, it searches for the suitable place and returns 'true'. Now, isn't the SolveNQ() supposed to print the solution every time true is returned? When does it return false? How is this working? I am a beginner and can anyone please explain this to me, step by step? Thank you in advance.
SolveNQ, which does the printing, is not called recursively; SolveQUtil, which SolveNQ calls, and which does not print anything, is recursive.
I'm testing a part of my Sudoku program to test if the row is valid or not. The board from file method makes a 9x9 array, but I can't understand why when I try running this isValidRow() method there is no indication of it being true or false. Why is this so?
public static boolean isValidRow(int[][] grid, int row) {
int i = 0;
int j = 0;
while (i < 9){
while (j < 9){
if (grid[row][i] == grid[row][j]){
if (i == j){
continue;
}
return false;
}
j++;
}
i++;
}
System.out.println("hii");
return true;
}
public static void main(String[] args) throws IOException {
int[][] board = new int[9][9];
boardFromFile(board, "sudoku.txt");
System.out.println(isValidRow(board, 0));
}
You have an infinite loop in isValidRow().
When you first call this function i and j are both zero (0). Thus,
if (grid[row][i] == grid[row][j])
will always evaluate to true. As will:
if (i == j)
The next action is:
continue;
which will start the inner while loop again. And as i and j are unchanged the same thing will happen again and again.
Assume that I have an array with the following values: 0,1,0,0,0,1,0,1,1
I am currently looping over my array and replacing 1's with 0's. However I would to break out of this loop if there are 2 1's left in my array. I don't really have much in terms of code but this is a stub of what I've been working on
if(//There are more than 2 1s ){
return true; //carry on looping
}
return false; //break the loop
I have no idea how to differentiate between the 0's and the 1's and so I am quite confused with how to get this to work. Any ideas would be appreciated.
One possible solution is to start by writing a utility method to test if a given value at a specific position is unique from every subsequent position in the array like,
private static boolean testUnique(int[] arr, int i) {
int t = arr[i];
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] == t) {
return false;
}
}
return true;
}
Then you can iterate the array from the left to the right, checking if every value is unique like
public static boolean hasDuplicate(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if (!testUnique(arr, i)) {
return false;
}
}
return true;
}
Using your array,
public static void main(String[] args) {
int[] arr = { 0, 1, 0, 0, 0, 1, 0, 1, 1 };
System.out.println(hasDuplicate(arr));
}
That is false. Alternatively, you might find it easier if you first sort your array.
public int[] testDuplicatesofOne(int[] arr)
{
int count=0;
for(int i=0;i<arr.length-1;i++)
{
count+=(arr[i]>0?1:0);
}
if(count>=2)
{
for(int j=0;j<arr.length-1;j++)
{
if(count>2)
{
if(arr[j]==1)
{
arr[j]=0;
count--;
}
}else
{
break;
}
}
}
}
Hi Lukasz try this, Sorry if I have not understood your requirement properly.
I've got a problem, I'm getting a "Dead Code" warning in Eclipse and I really don't know why. The code is from my Connect Four project, to be more precise it's from the Class that checks if somebody has won. This method checks all the horizontal winning possibilities for red. The code is the following:
/**
* Method to check the horizontal winning possibilities for red
* #return true if red won or false if not
*/
public boolean checkHorRed(){
for(int line = 0; line < 6; line++) {
for(int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
if(gw.buttons[line][column].getIcon().equals(gw.red));
if(gw.buttons[line][column+1].getIcon().equals(gw.red));
if(gw.buttons[line][column+2].getIcon().equals(gw.red));
if(gw.buttons[line][column+3].getIcon().equals(gw.red));
return true;
}
}
return false;
}
The game is even caused to be immediately won because of this method. What's strange about this is that all the other methods in the class that look almost the same don't cause any problems. Here's the method that checks the vertical winning possibilities for yellow, to have a comparison:
/**
* Method to check the vertical winning possibilities for yellow
* #return true or false
*/
public boolean checkVertYel(){
for(int line = 3; line < 6; line++) {
for(int column = 0; column < 7; column++) {
if(gw.buttons[line][column].getIcon().equals(gw.yellow))
if(gw.buttons[line-1][column].getIcon().equals(gw.yellow))
if(gw.buttons[line-2][column].getIcon().equals(gw.yellow))
if(gw.buttons[line-3][column].getIcon().equals(gw.yellow))
return true;
}
}
return false;
}
This one does not cause any problems. Can somebody tell me where the warning comes from? If you need additional information please tell me.
The dead code in your function is the increment statement of your inner for loop (column++). The return true statement will always be executed (if the loop is executed), so the loop increment is never going to happen.
That is your code, but properly formatted:
// ...
for(int column = 0; column < 4; column++) {
//column++ is underlined and causes the "dead Code" warning
if(gw.buttons[line][column].getIcon().equals(gw.red));
if(gw.buttons[line][column+1].getIcon().equals(gw.red));
if(gw.buttons[line][column+2].getIcon().equals(gw.red));
if(gw.buttons[line][column+3].getIcon().equals(gw.red));
return true;
}
// ...
You can easily spot the error: return true will always be executed, so the increment statement of the inner loop will not be executed.
That is how you code should look like:
public boolean checkHorRed() {
for(int line = 0; line < 6; line++) {
for(int column = 0; column < 4; column++) {
//column++ is underlined and causes the "dead Code" warning
if(gw.buttons[line][column].getIcon().equals(gw.red)
&& gw.buttons[line][column+1].getIcon().equals(gw.red)
&& gw.buttons[line][column+2].getIcon().equals(gw.red)
&& gw.buttons[line][column+3].getIcon().equals(gw.red) {
return true;
}
}
}
return false;
}
This is your code after reformatting it:
public boolean checkHorRed() {
for (int line = 0; line < 6; line++) {
for (int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
if (gw.buttons[line][column].getIcon().equals(gw.red)) {
;
}
if (gw.buttons[line][column + 1].getIcon().equals(gw.red)) {
;
}
if (gw.buttons[line][column + 2].getIcon().equals(gw.red)) {
;
}
if (gw.buttons[line][column + 3].getIcon().equals(gw.red)) {
;
}
return true; //this will always happen
}
}
return false;
}
And this is the other:
public boolean checkVertYel() {
for (int line = 3; line < 6; line++) {
for (int column = 0; column < 7; column++) {
if (gw.buttons[line][column].getIcon().equals(gw.yellow)) {
if (gw.buttons[line - 1][column].getIcon().equals(gw.yellow)) {
if (gw.buttons[line - 2][column].getIcon().equals(gw.yellow)) {
if (gw.buttons[line - 3][column].getIcon().equals(gw.yellow)) {
return true;
}
}
}
}
}
}
return false;
}
Basically, you should really not end your if statements with semicolons.
In the above method you have ; after each if statements, where as your second method is correct, which is proper way.
if(gw.buttons[line][column].getIcon().equals(gw.red)); <--
That terminates the if there it self. Your line of code equivalent to
if(condition)
{
}
Which means the code after the if conditions is dead.
Can you use a for loop inside the condition of an if-else statement? For example, something like this...
if(
for(q = 0; q < 10; q++){
values[q]>=values[q+1];
}
)
{
done = 0;
}
This is loading an error I can't seem to place. I want the if statement to check to see if the int[] I called values is in order from greatest to least, and if it is, set int variable done equal to 0.
I only just started taking a programming class and I bet this is a very dumb mistake, but I've been trying to figure this out for a while and some help would be absolutely fantastic.
You should work out your condition first (ie is your array in order), and then feed that in to your if statement. Like so...
boolean isOrdered = true;
for(q = 0; q < 10; q++){
if (values[q]>=values[q+1]){
// in order
}
else {
// not in order
isOrdered = false;
break; // we have found a false, so we can quit out of the for loop
}
}
if (isOrdered){
// do something if the array is in order;
}
You can do it if you refactor the logic into a method that returns a boolean:
if (isInOrder(values)) {
//
}
private static boolean isInOrder(int[] array) {
for (int i = 0; i < array.length - 1; i++)
if (array[i] > array[i+1])
return false;
return true;
}
A for loop can exist inside of an if block
if (true) {
for (int i = 0; i < 5; i++) {
System.out.println("Hello, World!");
}
}
But a for loop can not be the condition of the if block
if( for (int i = 0; i < 5; i++) { } ) { }
A for loop is not a boolean. Every if condition requires a boolean.
No. If requires a boolean test. For loop doesn't return a boolean value. You can instead separate the check into another function.
if( isArrayOrdered(values) ){
done = 0;
}
// assuming values is an array of `int` values
private boolean isArrayOrdered( int[] values){
for(int q = 1; q < values.length; q++){
if( values[q] > values[q-1] ){
return false;
}
}
return true;
}
I think you probably want the alternate nesting, and start with the assumption that done = 0 (that is, that the list is correctly ordered), unless you prove in the loop that it is not true.
public class Play{
public static void main(String[] args) {
done = 0;
for(q = 1; q < 10; q++){
if (values[q-1]<values[q]) {
done = 1;
break;
}
}
}
}
You need to break you logic down into steps.
first of all, a method
boolean isInOrder (int [] values) {
for(int q = 0; q < values.length - 1; q++){
if (values[q] > values[q+1]) {
return false;
}
}
return true;
}
// now lets call this method
if (isInOrder (values)) {
System.out.println ("In Order");
}
else {
System.out.println ("Not In Order");
}
No, you can't. The if condition must evaluate to some boolean value, which doesn't happen with this for loop.
It can only be in the if statement body, like
if(someCondition)
for(int i = 0;i < 10;i++)...
To achieve your goal, a code like this one might help:
boolean ordered = true;
for(int i = 0;i < array.length - 1;i++) {
if(array[i] > array[i+1]) {
ordered = false;
break;
}
}
if(ordered) // your if body here
It is impossible to put the "for loop" statement between if statement "(" and ")".But it is possible if you write "for loop" between if statement "{" and "}".Thanks
The expression inside the "if" condition should evaluate to a boolean or Boolean otherwise the code will not compile.
Check that Java specs page here