This commit is contained in:
Chloe Fontenot 🏳️‍⚧️ 2023-01-20 00:35:34 -06:00
parent 44950337e6
commit 0495faaa19
2 changed files with 145 additions and 165 deletions

View File

@ -11,8 +11,7 @@ import java.util.Scanner;
*/ */
public class MP1_CalebFontenot { public class MP1_CalebFontenot {
public static int[][] inputArray() public static int[][] inputArray() {
{
int m, n, i, j; int m, n, i, j;
// Create scanner // Create scanner
Scanner input = new Scanner(System.in); Scanner input = new Scanner(System.in);
@ -43,8 +42,7 @@ public class MP1_CalebFontenot {
return null; return null;
} }
public static boolean isConsecutiveFour(int[] values) public static boolean isConsecutiveFour(int[] values) {
{
for (int i = 0; i < values.length - 4; ++i) { for (int i = 0; i < values.length - 4; ++i) {
if (values[i] == values[i + 1] if (values[i] == values[i + 1]
&& values[i + 1] == values[i + 2] && values[i + 1] == values[i + 2]
@ -57,8 +55,7 @@ public class MP1_CalebFontenot {
return false; return false;
} }
public static void main(String[] args) public static void main(String[] args) { //2965
{ //2965
int[][] horizontalTestArray = { // Horizontal int[][] horizontalTestArray = { // Horizontal
{0, 1, 0, 3, 1, 6, 1}, {0, 1, 0, 3, 1, 6, 1},
{0, 1, 6, 8, 6, 0, 1}, {0, 1, 6, 8, 6, 0, 1},
@ -143,8 +140,7 @@ public class MP1_CalebFontenot {
//System.out.println(isConsecutiveFour(intArray1)); //System.out.println(isConsecutiveFour(intArray1));
} }
public static void printArray(int[][] array) public static void printArray(int[][] array) {
{
int rowCounter = 0; int rowCounter = 0;
for (int x = 0; x < array.length; x++) { for (int x = 0; x < array.length; x++) {
for (int y = 0; y < array[x].length; y++) { for (int y = 0; y < array[x].length; y++) {
@ -157,41 +153,33 @@ public class MP1_CalebFontenot {
} }
} }
public static void printArray(int[] array) public static void printArray(int[] array) {
{
for (int i : array) { for (int i : array) {
System.out.print(i + " "); System.out.print(i + " ");
} }
} }
public static boolean isConsecutiveFour(int[][] values) public static boolean isConsecutiveFour(int[][] values) {
{ // Horizontal Checking
int intCounter = 0, y = 0;
// Horizontal checking
// If the same value has been observed 4 times, return true
//System.out.println("values[0].length: " + values[0].length);
//System.out.println("values.length: " + values.length);
System.out.println("Checking for Horizontal matches..."); System.out.println("Checking for Horizontal matches...");
for (int rowIterate = 0; rowIterate < values.length; ++rowIterate) { boolean horizontalTest = horizontalMatch(values);
y = 0;
for (int x = 0; x < values[0].length - 1; ++x) {
++y;
if (values[rowIterate][x] == values[rowIterate][y]) {
intCounter++;
System.out.println(values[rowIterate][x] + " " + values[rowIterate][y] + ", intCounter: " + intCounter);
} else {
intCounter = 0;
System.out.println(values[rowIterate][x] + " " + values[rowIterate][y] + ", intCounter: " + intCounter);
}
if (intCounter == 3) {
System.out.println("Horizontal match!");
return true;
}
}
System.out.println("Checking next line...");
}
// Vertical Checking // Vertical Checking
System.out.println("Checking for Vertical matches..."); System.out.println("Checking for Vertical matches...");
boolean verticalTest = verticalMatch(values);
System.out.println("Checking for Diagonal matches...");
boolean diagonalNorthwest = MarkouCode.topLeftTriangleDiagonalNothwest(values);
boolean diagonalMiddle = MarkouCode.bottomRightTriangleDiagonalNothwest(values);
System.out.println(diagonalNorthwest);
if (horizontalTest || verticalTest || diagonalNorthwest) {
System.out.println("Match found!");
return true;
}
System.out.println("No match found.");
return false;
}
public static boolean verticalMatch(int[][] values) {
int intCounter = 0, y = 0;
for (int rowIterate = 0; rowIterate < values.length; ++rowIterate) { for (int rowIterate = 0; rowIterate < values.length; ++rowIterate) {
y = 0; y = 0;
for (int x = 0; x < values.length - 1; ++x) { for (int x = 0; x < values.length - 1; ++x) {
@ -210,26 +198,33 @@ public class MP1_CalebFontenot {
} }
System.out.println("Checking next line..."); System.out.println("Checking next line...");
} }
System.out.println("Checking for Diagonal matches..."); return false;
int rows = 0; }
for (int numColumns = 0; numColumns < values[0].length; numColumns++) { public static boolean horizontalMatch(int[][] values) {
int i = 0; int intCounter = 0, y = 0;
int j = numColumns; // Horizontal checking
int[] _1DArray = new int[numColumns + 1]; // If the same value has been observed 4 times, return true
for (int numRows = 0; numRows <= rows; ++numRows) { //System.out.println("values[0].length: " + values[0].length);
//System.out.println("values.length: " + values.length);
System.out.println("i: " + i + " j: " + j); for (int rowIterate = 0; rowIterate < values.length; ++rowIterate) {
_1DArray[numRows] = values[i][j]; y = 0;
//printArray(_1DArray); for (int x = 0; x < values[0].length - 1; ++x) {
//System.out.println(values[rows][i] + " " + values[rows][j]); ++y;
++i; if (values[rowIterate][x] == values[rowIterate][y]) {
--j; intCounter++;
System.out.println(values[rowIterate][x] + " " + values[rowIterate][y] + ", intCounter: " + intCounter);
} else {
intCounter = 0;
System.out.println(values[rowIterate][x] + " " + values[rowIterate][y] + ", intCounter: " + intCounter);
}
if (intCounter == 3) {
System.out.println("Horizontal match!");
return true;
}
} }
System.out.println("Checking next line..."); System.out.println("Checking next line...");
rows++;
} }
System.out.println("No match found.");
return false; return false;
} }
} }

View File

@ -11,22 +11,18 @@ package com.calebfontenot.mp1_calebfontenot;
public class MarkouCode { public class MarkouCode {
/** /**
* Traverses the parm array diagonally from top left towards the top and * Traverses the parm array diagonally from top left towards the top and prints the (i,j) indexes of the traversal.
* prints the (i,j) indexes of the traversal.
* *
* @param ar 2D array * @param ar 2D array
*/ */
public static boolean topLeftTriangleDiagonalNothwest(int[][] ar) public static boolean topLeftTriangleDiagonalNothwest(int[][] ar) {
{
System.out.println("diagonal indexes top-triangle of array "); System.out.println("diagonal indexes top-triangle of array ");
int rowCount = 0; int rowCount = 0;
for (int columnCounter = 0; columnCounter < ar[0].length; ++columnCounter) for (int columnCounter = 0; columnCounter < ar[0].length; ++columnCounter) {
{
int i = 0; int i = 0;
int j = columnCounter; int j = columnCounter;
int[] diagonalArray = new int[rowCount + 1]; int[] diagonalArray = new int[rowCount + 1];
for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter) for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter) {
{
diagonalArray[diagonalCounter] = ar[i][j]; diagonalArray[diagonalCounter] = ar[i][j];
if (isConsecutiveFour(diagonalArray)) { if (isConsecutiveFour(diagonalArray)) {
return true; return true;
@ -34,8 +30,7 @@ public class MarkouCode {
System.out.print(i + "," + j + " "); System.out.print(i + "," + j + " ");
++i; ++i;
j--; j--;
if (i == ar.length | j == ar[0].length) if (i == ar.length | j == ar[0].length) {
{
break; break;
} }
} }
@ -53,57 +48,57 @@ public class MarkouCode {
* *
* @param ar * @param ar
*/ */
public static void bottomRightTriangleDiagonalNothwest(int[][] ar) public static boolean bottomRightTriangleDiagonalNothwest(int[][] ar) {
{
System.out.println("diagonal indexes bottom-triangle of array "); System.out.println("diagonal indexes bottom-triangle of array ");
int rowCount = 0; int rowCount = 0;
int numColumns = 0; int numColumns = 0;
int[] diagonalArray = new int[rowCount + 1];
if (ar[0].length == ar.length)//square table if (ar[0].length == ar.length)//square table
{ {
numColumns = ar[0].length - 1; numColumns = ar[0].length - 1;
} } else if (ar[0].length > ar.length)//wide table
else if (ar[0].length > ar.length)//wide table
{ {
numColumns = ar.length - 1; numColumns = ar.length - 1;
} } else //narrow-width rectangle array
else //narrow-width rectangle array
{ {
numColumns = ar[0].length; numColumns = ar[0].length;
} }
for (int columnCounter = 0; columnCounter < numColumns; ++columnCounter) for (int columnCounter = 0; columnCounter < numColumns; ++columnCounter) {
{
int i = ar.length - 1; int i = ar.length - 1;
int j = ar[0].length - 1 - columnCounter; int j = ar[0].length - 1 - columnCounter;
for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter) for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter) {
{
System.out.print(i + "," + j + " "); System.out.print(i + "," + j + " ");
diagonalArray[diagonalCounter] = ar[i][j];
if (isConsecutiveFour(diagonalArray)) {
return true;
}
--i; --i;
j++; j++;
} }
rowCount++; rowCount++;
System.out.println(""); System.out.println("");
return false;
} }
//middle chunk of narrow array //middle chunk of narrow array
System.out.println("-----------------------"); System.out.println("-----------------------");
if (ar.length > ar[0].length) if (ar.length > ar[0].length) {
{
System.out.println("diagonal indexes middle part of array when the array " System.out.println("diagonal indexes middle part of array when the array "
+ "is narrow "); + "is narrow ");
for (int i = 1; i < ar.length - ar[0].length; ++i) for (int i = 1; i < ar.length - ar[0].length; ++i) {
{
int rowIndex = i; int rowIndex = i;
int columnIndex = ar[0].length - 1; int columnIndex = ar[0].length - 1;
for (int j = 0; j < ar[0].length; ++j) for (int j = 0; j < ar[0].length; ++j) {
{
System.out.print(rowIndex + "," + columnIndex + " "); System.out.print(rowIndex + "," + columnIndex + " ");
diagonalArray[diagonalCounter] = ar[i][j];
if (isConsecutiveFour(diagonalArray)) {
return true;
}
rowIndex++; rowIndex++;
columnIndex--; columnIndex--;
@ -115,37 +110,32 @@ public class MarkouCode {
} }
public static boolean isConsecutiveFour(int[][] values) public static boolean isConsecutiveFour(int[][] values) {
{
return true; return true;
} }
public static boolean isConsecutiveFour(int[] values)
{ public static boolean isConsecutiveFour(int[] values) {
for (int i = 0; i < values.length - 4; ++i) { for (int i = 0; i < values.length - 4; ++i) {
if ( values[i] == values[i+1] && if (values[i] == values[i + 1]
values[i+1] == values[i+2] && && values[i + 1] == values[i + 2]
values[i+2] == values[i+3] && values[i + 2] == values[i + 3]) {
) {
return true; return true;
} } else {
else {
return false; return false;
} }
} }
return false; return false;
} }
private static void printIndexesDiagonally(int ar[][]) private static void printIndexesDiagonally(int ar[][]) {
{
topLeftTriangleDiagonalNothwest(ar); topLeftTriangleDiagonalNothwest(ar);
System.out.println("------------------------"); System.out.println("------------------------");
bottomRightTriangleDiagonalNothwest(ar); bottomRightTriangleDiagonalNothwest(ar);
} }
public static void main(String[] args) public static void main(String[] args) {
{ int[][] ar1
int[][] ar1 = = {
{
{ {
1, 1, 1, 1, 8 1, 1, 1, 1, 8
}, },
@ -157,11 +147,10 @@ public class MarkouCode {
}, },
{ {
1, 8, 1, 3, 2 1, 8, 1, 3, 2
}, },};
};
int[][] ar2 = int[][] ar2
{ = {
{ {
1, 1, 1, 1, 1, 1, 1, 1 1, 1, 1, 1, 1, 1, 1, 1
}, },
@ -176,12 +165,10 @@ public class MarkouCode {
}, },
{ {
1, 1, 1, 1, 1, 1, 1, 1 1, 1, 1, 1, 1, 1, 1, 1
}, },};
}; int[][] ar3
= {
int[][] ar3 =
{
{ {
1, 1, 1 1, 1, 1
}, },
@ -211,9 +198,7 @@ public class MarkouCode {
}, },
{ {
1, 1, 1 1, 1, 1
}, },};
};
System.out.println(topLeftTriangleDiagonalNothwest(ar1)); System.out.println(topLeftTriangleDiagonalNothwest(ar1));
/* /*
System.out.println("SQUARE array of size 4x4"); System.out.println("SQUARE array of size 4x4");
@ -228,7 +213,7 @@ public class MarkouCode {
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("\nNARROW array of size 10x3"); System.out.println("\nNARROW array of size 10x3");
printIndexesDiagonally(ar3); printIndexesDiagonally(ar3);
*/ */
} }
} }