Check if the matrix is folded from.
The test should make:
image
The code always returns false and it is unclear to me why. What's wrong with the code?
the code:
public class test1 {
public static void main(String[] args) {
int[][] mat = { { 1, 7, 9 }, { 2, 9, 7 }, { 9, 2, 1 } };
boolean flag = true;
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
System.out.print("[" + mat[i][j] + "]");
}
System.out.println();
}
for (int i = mat.length - 1; i > -1; i--) {
for (int j = mat.length - 1; j > -1; j--) {
if (i == j) {
j--;
}
if (mat[i][j] != mat[j][i]) {
flag = false;
System.out.println("mat[i][j]" + mat[i][j] + " " + i + " "
+ j);
j = -1;
i = -1;
}
}
}
if (flag == false) {
System.out.println("Not first folded matrix");
} else {
System.out.println("First folded matrix");
}
}
}
thank you
You can use this function:
public static boolean isFolded(int[][] mat){
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
if (i == j) {
continue;
}
if (mat[i][j] != mat[mat.length - 1 - j][mat.length - 1 - i]) {
System.out.println("mat[i][j] " + mat[i][j] + " i:" + i + " j:"
+ j);
return false;
}
}
}
return true;
}
Call it in you main like:
flag = isFolded(mat);
Related
I have a 2D array (a matrix of 10x10) with values ranging from 0 to -5.
I want a method to be triggered when there is a sequence of a value found within the array.
For example, there is a sequence of two negative 2. I want it to trigger an event/method that will give a bonus score of 4. This should happen only when there are two -2's and not if there is just one -2.
I tried achieving something like that but I cant figure out how to tell the program to only trigger when 'n' number of a value is found within the matrix.
public class Test {
static int board[][] = new int[10][10];
public static void Test() {
int i, j;
board[0][0] = -1;
board[0][1] = -1;
board[1][1] = -2;
board[1][2] = -2;
board[1][3] = -2;
board[1][4] = -2;
for (i = 0; i < board.length; i++) {
System.out.println("");
for (j = 0; j < board.length; j++) {
//board[i][j] = 0;
System.out.print(board[i][j]);
}
}
System.out.println();
}
public static void scanBoard() {
int i, j;
for (i = 0; i < board.length; i++) {
for (j = 0; j < board.length; j++) {
if (board[i][j] == -1) {
System.out.println("Hello");
}
}
}
}
public static void main(String[] args) {
Test(); //prints out whole array
scanBoard(); //scans for
}
}
public class Main {
static final int size = 10;
static int[][] matrix = new int[size][size];
public static void main(String[] args) {
System.out.println("The first matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 3 && j > 3) {
matrix[i][j] = -2; //-2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
System.out.println("\nThe second matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 9 && j > 5) {
matrix[i][j] = 2; //changed it from -2 to 2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
}
static void scanBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] == -2 && (j + 3 < size)) {
if (matrix[i][j + 1] == -2 && matrix[i][j + 2] == -2 && matrix[i][j + 3] == -2) {
System.out.println("\nThere you go, a special effect!".toUpperCase());
}
}
}
}
}
}
I am not sure if this is the result you wished to see according to your request. I hope this helps you. And I did some changes in your code so it will be easier to read (In my opinion lol).
public class Main {
static final int size = 10;
static int[][] matrix = new int[size][size];
public static void main(String[] args) {
System.out.println("The first matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 9 && (j == 0 || j == 1)) {
matrix[i][j] = -2; //-2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
System.out.println("\nThe second matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 8 && (j == 5 || j == 6)) {
matrix[i][j] = 2; //changed it from -2 to 2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
}
static void scanBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] == -2 && (j + 1 < size)) {
if (matrix[i][j + 1] == -2) {
//You can remove the '.toUpperCase()', it's just my personal preference
System.out.println("\nThere you go, a special effect!".toUpperCase());
}
}
}
}
}
}
From what I understood from the problem statement and comments, you want your scanBoard to behave like this:
public static void scanBoard(int value, int frequency) {
int i, j;
if (value <= 0 && value >= -5 && frequency >= 2 && frequency <= 10) {
for (i = 0; i < board.length; i++) {
int rowFrequency = 0;
for (j = 1; j < board.length; j++) {
if (board[i][j] == value && board[i][j - 1] == value) {
rowFrequency++;
} else {
rowFrequency = 0;
}
if (rowFrequency + 1 >= frequency) {
System.out.println("Hello");
}
}
}
}
}
public static void main(String[] args) {
Test(); //prints out whole array
scanBoard(-2, 4); //prints Hello once
scanBoard(-2, 3); //prints Hello twice
scanBoard(-2, 3); //prints Hello thrice
}
How would one go about returning the grid that is printed in the below function/method? I am using a String function/method but I am not sure at all of how I would return what is printed out in this function/method in my main class.
public static String Grid(){
int[][] map = new int[4][4];
map[x][y] = entry;
int counter = 0;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (counter != 2) {
if (j < 2) {
System.out.print("+---");
} else if (j == 2) {
System.out.print("++---+");
} else {
System.out.print("---+");
}
}
else{
if (j < 2) {
System.out.print("+===");
} else if (j == 2) {
System.out.print("++===+");
} else {
System.out.print("===+");
}
}
}
System.out.println();
for (int j = 0; j < map[i].length; j++) {
if (j < 2) {
System.out.print("| " + map[i][j] + " ");
} else if (j == 2) {
System.out.print("|| " + map[i][j]);
} else {
System.out.print(" | " + map[i][j] + " |");
}
}
System.out.println();
counter++;
}
System.out.print("+---+---++---+---+");
System.out.println();
}
Create a StringBuilder
StringBuilder result = new StringBuilder();
Replace every
System.out.println();
with
result.append("\n");
and replace
System.out.print(string);
with
result.append(string);
At the end of the method, add
return result.toString();
Why is my code returning this error? I've made any mistake? I can not see my error in this code. Can anybody help me?
The code is bellow:
public static void minMax(int m[][])
{
int menor = 0;
int maior = 0;
int i = 0;
int j = 0;
int posicao = 0;
for(i = 0; i < 4;i++)
{
for(j = 0; j < 5; j++)
{
if((i == 0) && (j == 0))
{
menor = m[i][j];
posicao = i;
}
else
{
if(m[i][j] < menor)
{
menor = m[i][j];
posicao = i;
}
}
}
}
for (i = posicao;;)
{
for(j = 0; j < 5; j++)
{
if(j == 0)
{
maior = m[i][j];
}
else
{
if(m[i][j] > maior)
{
maior = m[i][j];
}
}
}
}
System.out.println("\n\nThe smallest element of the array: " + menor);
System.out.println("The line of the smallest element: " + posicao);
System.out.printf("MINMAX element %d encountered at the position: [%d][%d]", maior, i, j);
}
The error is pointed by Eclipse in this line:
System.out.println("\n\nThe smallest element of the array: " + menor);
Looks like your for loop defined as for (i = posicao;;) above your prints never finishes, there is no exit case
This question already has answers here:
Algorithm to generate a crossword [closed]
(13 answers)
Closed 6 years ago.
I am working on cross word algorithm to develop a word app. After doing a lot of googling or search on StackOverflow, I was able to reach this point. But yet I am not able to understand the right implementation for algorithm in Java. Below is the class I used.
public class Crosswords {
char[][] cross;
int rows;
int cols;
char[][] numberGrid;
boolean startword;
final char DEFAULT = ' ';
public Crosswords() {
rows = 50;
cols = 50;
cross = new char[rows][cols];
numberGrid = new char [rows][cols];
for (int i = 0; i < cross.length;i++){
for (int j = 0; j < cross[i].length;j++){
cross[i][j] = DEFAULT;
}
}
}
public Crosswords(int ros, int colls) {
rows = ros;
cols = colls;
cross = new char[rows][cols];
numberGrid = new char [rows][cols];
for (int i = 0;i < cross.length; i++){
for (int j = 0; j < cross[i].length; j++){
cross[i][j] = DEFAULT;
}
}
}
public String toString() {
String s = new String();
//String d = new String();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++){
s = s + cross[i][j] + " ";
}
s = s + "\n";
}
return s;
}
public void addWordh(String s, int r, int c) {
int i = 0;
int j = 0;
boolean b = true;
boolean intersectsWord = true;
if (s.length() > cols) {
System.out.println(s + " is longer than the grid. Please try another word.");
return;
}
if (c + s.length() > cols) {
System.out.println(s + " is too long. Please try another word.");
return;
}
if ((r - 2) >= 0) {
if ((cross[r - 1][c - 1 + s.length()] == DEFAULT) || (cross[r - 1][c - 1 + s.length()] == '*')) {
intersectsWord = false;
}
else { intersectsWord = true;}
if (intersectsWord == true) {
System.out.println("The word " + s + " intersects the beginning of another word!");
return;
}
}
for (i = 0; i < s.length(); i++) {
if ((cross[r - 1][c - 1 + i] == DEFAULT) || (cross[r - 1][c - 1 + i] == s.charAt(i))) {
b = true;
}
else {
b = false;
System.out.println("Unable to add " + s + ". Please try another word.");
return;}
}
if (b == true) {
if ((s.length() <= cols) && (c + s.length() <= cols) &&
(cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {
while (j < s.length()) {
cross[r - 1][c - 1 + j] = s.charAt(j);
if (j==0){
startword = true;
}
cross[rows - 1 - (r - 1)][cols - 1 - (c - 1 + j)] = '*';
j++;
}
}
}
}
public void addWordv(String s, int r, int c) {
int i = 0;
int j = 0;
boolean b = true;
boolean intersectsWord = true;
if (s.length() > rows) {
System.out.println(s + " is longer than the grid. Please try another word.");
}
if (r + s.length() > rows) {
System.out.println(s + " is too long. Please try another word.");
}
else {
if ((r - 2) >= 0) {
if ((cross[r - 2][c - 1] == DEFAULT) || (cross[r - 2][c - 1] == '*')) {
intersectsWord = false;
}
else { intersectsWord = true;}
if (intersectsWord == true) {
System.out.println("The word " + s + " intersects the end of another word!");
return;
}
}
if ((cross[r - 1 + s.length()][c - 1] == DEFAULT) || (cross[r - 1 + s.length()][c - 1] == '*')) {
intersectsWord = false;
}
else { intersectsWord = true;}
if (intersectsWord == true) {
System.out.println("The word " + s + " intersects the end of another word!");
return;
}
for (i = 0; i < s.length(); i++) {
if ((cross[r - 1 + i][c - 1] == DEFAULT) || (cross[r - 1 + i][c - 1] == s.charAt(i))) {
b = true;
}
else {
b = false;
System.out.println("Unable to add " + s + ". Please try another word.");
return;}
}
if (b == true) {
if ((s.length() <= rows) && (r + s.length() <= cols) &&
(cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {
while (j < s.length()) {
cross[r - 1 + j][c - 1] = s.charAt(j);
if (j==0){
startword = true;
}
cross[rows - 1 - (r - 1 + j)][cols - 1 - (c - 1)] = '*';
j++;
}
}
}
}
}
public void setNumberGrid(){
numberGrid = new char [rows][cols];
for (int i = 0; i < cross.length; i++){
for (int j=0; j < cross[rows].length; j++){
if (cross[i][j] == DEFAULT){
numberGrid[i][j] = (char) 0;
}
else if (startword == true){
numberGrid[i][j] = (char) -2;
}
else {
numberGrid[i][j] = (char) -1;
}
}
int count = 1;
for (i=0; i < cross.length; i++){
for (int j=0; j < cross[rows].length; j++){
if (numberGrid[i][j] == -2){
numberGrid[i][j] = (char)count;
count++;
}
}
}
}
}
public String printNumberGrid() {
for (int i=0; i < cross.length; i++){
for (int j=0; j < cross[rows].length; j++){
if (numberGrid[i][j] == (char)-1){
numberGrid[i][j] = ' ';
}
else if (numberGrid[i][j] == (char)0){
numberGrid[i][j] = '#';
}
}
}
String d = new String();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++){
d = d + numberGrid[i][j] + " ";
}
d = d + "\n";
}
return d;
}
public static void main(String[] args) {
Crosswords g = new Crosswords();
g.addWordv("rawr", 4, 5);
g.addWordh("bot", 5, 4);
g.addWordv("raw", 7, 5);
g.addWordh("cat", 4, 5);
g.addWordh("bass", 6, 10);
System.out.println(g);
Crosswords c = new Crosswords(20, 20);
c.addWordh("HELLO", 1, 1);
c.addWordv("HAPLOID", 1, 1);
c.addWordh("COMPUTER", 3, 12);
c.addWordv("CAT", 2, 11);
c.addWordv("WOAH", 2, 20);
c.addWordh("PARKING", 20, 5);
c.addWordv("ARK", 17, 6);
c.addWordh("AHOY", 6, 18);
c.addWordv("AHOY", 18, 10);
c.addWordv("ADVANTAGE", 2, 12);
c.addWordv("INTERNAL", 2, 18);
c.addWordh("BANTER", 7, 11);
c.addWordv("BEAGLE", 5, 12);
c.addWordh("BASE", 8, 3);
c.addWordv("BALL", 8, 3);
c.addWordh("LEFT", 10, 3);
c.addWordv("SAFE", 8, 5);
System.out.print(c);
}
}
As you can see in Main method that i am adding the words but also giving the row and column number to place the words like c.addWordv("Safe",8,5); where 8 and 5 is column number.
Now Question is how can i implement cross word algorithm which just take words and place them on board randomly without taking the row and column numbers.
Thanks in advance
EDIT:
I want to modify this class algo the way that i dont have to give away the rows and columns number..
//Pseudo Code
If the crossword size is maxSize and any word's length is stored in wordLength ,then you can use random method as below
int maxSize=20;
int wordLength=4;
Random random =new Random();
int r,c;
//for horizontal
r=random.nextInt(maxSize-wordLength);
c=random.nextInt(maxSize);
//for vertical
r=random.nextInt(maxSize);
c=random.nextInt(maxSize-wordLength);
You can store the row and column and generate the new one if its already present.
I'm more or less new to Java.
I'm making a console-based connect 4 game in java and I'm pretty much done, I'm just struggling with how to check for a diagonal win (four pieces in a row diagonally). My vertical/horizontal win checkers are working fine, but I can't figure out how to do a similar thing for a diagonal-checker. The board is a 2d-array which is printed to the console, and to check for a win, I check for four of the same piece next to each other or above each other.
Here are the bits of board code:
private String board[][] = new String[8][8];
creates array
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = "( )";
}
}
fills the board with blank slots
void displayBoard() {
for (int i = 1; i < 9; i++) {
System.out.print(" " + i + " ");
}
System.out.println();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
And here is the winchecker
boolean winCheck1() {
String p = "(" + piece1 + ")";
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 5; j++) {
if ((board[i][j].equals(p) && board[i][j + 1].equals(p)
&& board[i][j + 2].equals(p) && board[i][j + 3]
.equals(p))) {
this.win1();
return false;
}
}
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 5; j++) {
if ((board[j][i].equals(p) && board[j + 1][i].equals(p)
&& board[j + 2][i].equals(p) && board[j + 3][i]
.equals(p))) {
this.win1();
return false;
}
}
}
this.play2();
return false;
}
Any help would be appreciated. Thanks!
If you want to keep doing it your way:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if ((board[j][i].equals(p) && board[j + 1][i + 1].equals(p)
&& board[j + 2][i + 2].equals(p) && board[j + 3][i + 3]
.equals(p))) {
this.win1();
return false;
}
}
}
for (int i = 3; i < 8; i++) {
for (int j = 0; j < 5; j++) {
if ((board[j][i].equals(p) && board[j + 1][i - 1].equals(p)
&& board[j + 2][i - 2].equals(p) && board[j + 3][i - 3]
.equals(p))) {
this.win1();
return false;
}
}
}
However, it's probably better to do this with loops (iterate over directions).