I'm getting wrong output and also I need a simplified solution - java

I came across a problem. The code is:
Scanner input = new Scanner(System.in);
int time = input.nextInt();
int line = input.nextInt();
input.nextline();
int[][] PrimaryArray = new int[12][11];
int[] Number = new int[12];
boolean[] dualPass = new boolean[12];
for (int i = 0; i < 12; i++) {
dualPass[i] = false;
}
for (int i = 0; i < line; i++) {
int SourceNode = input.nextInt();
int DistNode = input.nextInt();
input.nextLine();
if (SourceNode == 1) {
PrimaryArray[SourceNode][Number[1]] = DistNode;
Number[1]++;
}
if (SourceNode == 2) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[2] = true;
}
if (dualPass[2] != true) {
PrimaryArray[SourceNode][Number[2]] = DistNode;
Number[2]++;
}
dualPass[2] = false;
}
if (SourceNode == 3) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[3] = true;
}
if (dualPass[3] != true) {
PrimaryArray[SourceNode][Number[3]] = DistNode;
Number[3]++;
}
dualPass[3] = false;
}
if (SourceNode == 4) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[4] = true;
}
if (dualPass[4] != true) {
PrimaryArray[SourceNode][Number[4]] = DistNode;
Number[4]++;
}
dualPass[4] = false;
}
if (SourceNode == 5) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[5] = true;
}
if (dualPass[5] != true) {
PrimaryArray[SourceNode][Number[5]] = DistNode;
Number[5]++;
}
dualPass[5] = false;
}
if (SourceNode == 6) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[6] = true;
}
if (dualPass[6] != true) {
PrimaryArray[SourceNode][Number[6]] = DistNode;
Number[6]++;
}
dualPass[6] = false;
}
if (SourceNode == 7) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[7] = true;
}
if (dualPass[7] != true) {
PrimaryArray[SourceNode][Number[7]] = DistNode;
Number[7]++;
}
dualPass[7] = false;
}
if (SourceNode == 8) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[8] = true;
}
if (dualPass[8] != true) {
PrimaryArray[SourceNode][Number[8]] = DistNode;
Number[8]++;
}
dualPass[8] = false;
}
if (SourceNode == 9) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[9] = true;
}
if (dualPass[9] != true) {
PrimaryArray[SourceNode][Number[9]] = DistNode;
Number[9]++;
}
dualPass[9] = false;
}
if (SourceNode == 10) {
for (int j = 0; j < PrimaryArray[DistNode].length; j++) {
if (PrimaryArray[DistNode][j] == SourceNode)
dualPass[10] = true;
}
if (dualPass[10] != true) {
PrimaryArray[SourceNode][Number[10]] = DistNode;
Number[10]++;
}
dualPass[10] = false;
}
}
for (int i = 1; i < 11; i++) {
System.out.println(PrimaryArray[i][1]);
}
I have 2 problems:
about clean code that it is not and very long and has not readability. I want better solution.
about response that is false
for example when the input is :
3 14
1 2
1 5
1 9
1 3
2 8
2 6
5 9
9 4
9 6
3 10
8 11
7 11
10 7
6 7
output must be second member each. I want to say in this code that for example:
when we enter: 1 2
then 2 1
it must ignore 2 1.
output of code is wrong and I don't understand it.

For you first question, even if it's the wrong place there is an answer, just replace your
// ...
for (int i = 0; i < line; i++) {
int sourceNode = input.nextInt();
System.out.println("SourceNode: " + sourceNode);
int distNode = input.nextInt();
System.out.println("DistNode: " + distNode );
input.nextLine();
// you will get same response with following code instead all your if's:
for (int j = 0; j < primaryArray[distNode].length; j++) {
if (primaryArray[distNode][j] == sourceNode)
dualPass[sourceNode] = true;
}
if (dualPass[sourceNode] != true) {
primaryArray[sourceNode][number[sourceNode]] = distNode;
number[sourceNode]++;
}
dualPass[sourceNode] = false;
// end of code change
}
// ...
Your second answer only can be answered if we get more informations, i do not have any Idea what your code should do yet.

Related

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

java - understanding a piece of code in array

why i need to write:
mat.length-1
in the second for loop (the loop it all conditions).
the loop:
for (int i = 0; i < mat.length-1; i++) {
for (int j = 0; j < mat.length-1; j++) {
if (i == j) {
j++;
if (i == mat.length - 1 && j == mat.length - 1) {
break;
}
}
if (i != j && mat[i][j] == mat[j][i]) {
flag = true;
} else {
flag = false;
if (flag == false) {
stop = 1;
i = mat.length - 1;
}
}
}
}
Checking program applies
Complete code:
public class test {
public static void main(String[] args) {
//int[][] mat = { { 9, 2, 4 }, { 2, 9, 7 }, { 4, 7, 9 } };
int[][]mat = { { 9, 2, 3, 4},
{ 2, 9, 6, 3},
{ 3, 6, 9 ,2},
{ 4, 3, 2 ,9}};
boolean flag = true;
int stop = 0;
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 = 0; i < mat.length-1; i++) {
for (int j = 0; j < mat.length-1; j++) {
if (i == j) {
j++;
if (i == mat.length - 1 && j == mat.length - 1) {
break;
}
}
if (i != j && mat[i][j] == mat[j][i]) {
flag = true;
} else {
flag = false;
if (flag == false) {
stop = 1;
i = mat.length - 1;
}
}
}
}
if (stop == 1) {
System.out.println("Not first folded matrix");
} else {
System.out.println("First folded matrix");
}
}
}
this is work but if i change it to
mat.length
It does not work
If I write a negative one then it stops the loop of the i before it reaches the end of the array.
Can explanation?
In your code, these lines serve no purpose as i and j would never be equal to (mat.length-1) in your code.
if (i == mat.length - 1 && j == mat.length - 1) {
break;
}
Change your code to :
for (int i = 0; i < mat.length; i++) {
for (int j = i+1; j < mat.length; j++) {
if (mat[i][j] != mat[j][i]) {
flag = false;
j = mat.length;
i = mat.length;
}
}
}
if (flag == false) {
System.out.println("Not first folded matrix");
} else {
System.out.println("First folded matrix");
}

I am trying to assign a number to each character

import java.util.Scanner;
public class Recursion
{
//variables to hold string values
public static String s1 = new String(new char[10]);
public static String s2 = new String(new char[10]);
public static String s3 = new String(new char[11]);
public static String charSet = new String(new char[11]);
//variables to hold number values
public static int numberOne;
public static int numberTwo;
public static int numberThree;
public static int maxCharCount;
public static int[] numberSet = new int[10];
//function which generates a number
public static void checkForEquality()
{
numberOne = numberTwo = numberThree = 0;
int i;
int j;
for (i = 0; i < s1.length(); i++)
{
for (j = 0; j < maxCharCount; j++)
{
if (s1.charAt(i) == charSet.charAt(j))
{
if (i == 0 && numberSet[j] == 0)
return;
//generate the number
numberOne = (numberOne * 10) + numberSet[j];
}
}
}
for (i = 0; i < s2.length(); i++)
{
for (j = 0; j < maxCharCount; j++)
{
if (s2.charAt(i) == charSet.charAt(j))
{
if (i == 0 && numberSet[j] == 0)
return;
//generate number
numberTwo = (numberTwo * 10) + numberSet[j];
}
}
}
for (i = 0; i < s3.length(); i++)
{
for (j = 0; j < maxCharCount; j++)
{
if (s3.charAt(i) == charSet.charAt(j))
{
if (i == 0 && numberSet[j] == 0)
return;
//generate the number
numberThree = (numberThree * 10) + numberSet[j];
}
}
}
}
public static void display(){
if (numberOne + numberTwo == numberThree) {
//display the output
int i=0;
System.out.println();
System.out.print(" Summation Puzzle solved. ");
System.out.print("n");
System.out.print(s1);
System.out.print("<==>");
System.out.print(numberOne);
System.out.print("n");
System.out.print(s2);
System.out.print("<==>");
System.out.print(numberTwo);
System.out.print("n");
System.out.print(s3);
System.out.print("<==>");
System.out.print(numberThree);
System.out.print("n");
//loop to show the result
for (i = 0; i < maxCharCount; i++)
{
System.out.println(charSet.charAt(i));
System.out.print("<==>");
System.out.print(numberSet[i]);
System.out.print("n");
}
System.exit(0);
}
}
//recursive function which will call itself
public static void Combinations(int indexCounter, int[] availableSet)
{
int i;
if (indexCounter != 0)
{
for (i = 0; i < 10; i++)
{
numberSet[indexCounter] = i;
if (availableSet[i] == 1)
{
availableSet[i] = 0;
Combinations(indexCounter + 1, availableSet);
availableSet[i] = 1;
}
}
}
if (indexCounter == maxCharCount)
checkForEquality();
}
public static void createCharSet()
{
int i;
int setIndex;
int present;
int j;
setIndex = 0;
for (i = 0; i < s1.length(); i++)
{
present = 0;
for (j = 0; j < setIndex; j++)
{
if (s1.charAt(i) == charSet.charAt(j))
{
present = 1;
}
}
if (present == 0)
{
charSet = StringFunctions.changeCharacter(charSet, setIndex++, s1.charAt(i));
}
}
for (i = 0; i < s2.length(); i++)
{
present = 0;
for (j = 0; j < setIndex; j++)
{
if (s2.charAt(i) == charSet.charAt(j))
{
present = 1;
}
}
if (present == 0)
{
charSet = StringFunctions.changeCharacter(charSet, setIndex++, s2.charAt(i));
}
}
for (i = 0; i < s3.length(); i++)
{
present = 0;
for (j = 0; j < setIndex; j++)
{
if (s3.charAt(i) == charSet.charAt(j))
{
present = 1;
}
}
if (present == 0)
{
charSet = StringFunctions.changeCharacter(charSet, setIndex++, s3.charAt(i));
}
}
maxCharCount = setIndex;
}
public static void calculateSummation()
{
int loop;
if (maxCharCount > 10)
{
System.out.print("Please check the input again");
return;
}
else
{
int[] avaliableSet = new int[10];
for (loop = 0; loop < 10; loop++)
{
avaliableSet[loop] = 1;
}
Combinations(0, avaliableSet);
}
}
//main method
public static void main(String[]args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the first String :");
s1 = scan.next();
System.out.print("Enter the second String :");
s2 = scan.next();
System.out.print("Enter the thirsd String :");
s3 = scan.next();
createCharSet();
System.out.print(" result of your 3 three strings = ");
System.out.print(charSet);
calculateSummation();
checkForEquality();
display();
}
}
Every time I run the program it just assigns 0 to each value. I want to be able to assign a value from 1-10 to each non numeric character.
can someone help me out.?
it should look something like this
Sample output
First off, you may find debugging easier if you properly format your code. I remember you posting last night where you experienced similar issues resulting from syntax. You may find that your code is easier to read if you format it like this:
//function which generates a number
public static void checkForEquality(){
numberOne = numberTwo = numberThree = 0;
int i;
int j;
for (i = 0; i < s1.length(); i++){
for (j = 0; j < maxCharCount; j++){
if (s1.charAt(i) == charSet.charAt(j)){
if (i == 0 && numberSet[j] == 0)
return;
//generate the number
numberOne = (numberOne * 10) + numberSet[j];
}
}
}
for (i = 0; i < s2.length(); i++){
for (j = 0; j < maxCharCount; j++){
if (s2.charAt(i) == charSet.charAt(j)){
if (i == 0 && numberSet[j] == 0)
return;
//generate number
numberTwo = (numberTwo * 10) + numberSet[j];
}
}
}
Take a look at just this portion of your code and see if it still properly follows your algorithm. You may find an error which you could've located yourself with properly formatted code. If you take a look at a previous question of yours (which I answered) you'll find a helpful link that guides you to writing better code.
Also, I didn't try to compile this in my own IDE, but you should take a look at Eclipse. A lot of the small errors you're getting could be located automatically by Eclipse.

I can't seem to get a working main method for my project

I am programming a minesweeper game, and having a problem making the main method. I cant seem to get the text based game to output to the screen, allowing the user to interact with it. I have tried several ways, but none have worked.
public class Game {
private Board board;
boolean finish = false;
boolean win = false;
int turn = 0;
public void Jogo() {
board = new Board();
Play(board);
}
public void Play(Board board) {
do {
turn++;
System.out.println("Turn " + turn);
board.show();
finish = board.setPosition();
if (!finish) {
board.openNeighbors();
finish = board.win();
}
} while (!finish);
if (board.win()) {
System.out.println("Congratulations, you let the 10 fields with the mines in " + turn
+ " turns");
board.showMines();
} else {
System.out.println("Mine! You lost!");
board.showMines();
}
}
}
import java.util.Random;
import java.util.Scanner;
public class Board extends Game {
private int[][] mines;
private char[][] boardgame;
private int Line, Column;
Random random = new Random();
Scanner input = new Scanner(System.in);
public void Jogo() {
mines = new int[10][10];
boardgame = new char[10][10];
startMines();
randomMines();
fillTips();
startBoard();
}
public boolean win() {
int count = 0;
for (int line = 1; line < 9; line++)
for (int column = 1; column < 9; column++)
if (boardgame[line][column] == '_')
count++;
if (count == 10)
return true;
else
return false;
}
public void openNeighbors() {
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
if ((mines[Line + i][Column + j] != -1)
&& (Line != 0 && Line != 9 && Column != 0 && Column != 9))
boardgame[Line + i][Column + j] =
Character.forDigit(mines[Line + i][Column + j], 10);
}
public int getPosition(int Line, int Column) {
return mines[Line][Column];
}
public boolean setPosition() {
do {
System.out.print("\nLine: ");
Line = input.nextInt();
System.out.print("Column: ");
Column = input.nextInt();
if ((boardgame[Line][Column] != '_')
&& ((Line < 9 && Line > 0) && (Column < 9 && Column > 0)))
System.out.println("Field already shown");
if (Line < 1 || Line > 8 || Column < 1 || Column > 8)
System.out.println("Choose a number between 1 and 8");
} while ((Line < 1 || Line > 8 || Column < 1 || Column > 8)
|| (boardgame[Line][Column] != '_'));
if (getPosition(Line, Column) == -1)
return true;
else
return false;
}
public void show() {
System.out.println("\n Lines");
for (int Line = 8; Line > 0; Line--) {
System.out.print(" " + Line + " ");
for (int Column = 1; Column < 9; Column++) {
System.out.print(" " + boardgame[Line][Column]);
}
System.out.println();
}
System.out.println("\n 1 2 3 4 5 6 7 8");
System.out.println(" Columns");
}
public void fillTips() {
for (int line = 1; line < 9; line++)
for (int column = 1; column < 9; column++) {
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
if (mines[line][column] != -1)
if (mines[line + i][column + j] == -1)
mines[line][column]++;
}
}
public void showMines() {
for (int i = 1; i < 9; i++)
for (int j = 1; j < 9; j++)
if (mines[i][j] == -1)
boardgame[i][j] = '*';
show();
}
public void startBoard() {
for (int i = 1; i < mines.length; i++)
for (int j = 1; j < mines.length; j++)
boardgame[i][j] = '_';
}
public void startMines() {
for (int i = 0; i < mines.length; i++)
for (int j = 0; j < mines.length; j++)
mines[i][j] = 0;
}
public void randomMines() {
boolean raffled;
int Line, Column;
for (int i = 0; i < 10; i++) {
do {
Line = random.nextInt(8) + 1;
Column = random.nextInt(8) + 1;
if (mines[Line][Column] == -1)
raffled = true;
else
raffled = false;
} while (raffled);
mines[Line][Column] = -1;
}
}
}
public class MineSweeper extends Board {
public static void main(String[] args) {
Game game = new Game();
}
}

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

Categories