I have worked for hours on this code and finally gotten ver close to completion. I will be adding a Mine Counter and a timer once I figure out how to add the ability to flag mines with right clicking. I learned that JButtons do not recognize right clicks, so I must use a MouseEvent. From there, I'm lost. Can someone give me a nudge in the right direction?
This is what I currently have:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
public class MineSweeper implements ActionListener {
JFrame frame = new JFrame("Mine Sweeper");
JButton reset = new JButton("Reset");
JButton[][] buttons = new JButton[20][20];//button array
JButton[] flags = new JButton[20];
int[][] counts = new int[20][20];
Container grid = new Container(); //grid for buttons
int MineNum = 50;//number of mines
final int MINE = 10;//int value to identify a mine
public MineSweeper() {
frame.setSize(500, 500);
frame.setLayout(new BorderLayout());
frame.add(reset, BorderLayout.NORTH);
reset.addActionListener(this);
//Grid of Buttons
grid.setLayout(new GridLayout(20,20));
for (int a = 0; a<buttons.length; a++) {
for (int b = 0; b < buttons[0].length; b++) {
buttons[a][b] = new JButton("▒");
buttons[a][b].addActionListener(this);
buttons[a][b].addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
mineFlagger(true);
}
}
});
grid.add(buttons[a][b]);
}
}
frame.add(grid, BorderLayout.CENTER);
makeRandomMines();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void mineFlagger (boolean flag){
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons[0].length; j++) {
if(flag == true) {
buttons[i][j].setText("F");
}
}
}
}
public static void main(String[] args) {
new MineSweeper();
}
public void makeRandomMines() {
//initializes list of random pairs
ArrayList<Integer> list = new ArrayList<Integer>();
for (int x = 0; x < counts.length; x++) {
for (int y = 0; y < counts[0].length; y++) {
list.add(x * 100 + y);//changed y to x
}
}
//resets the counts in case reset button is pressed & picks random mines
counts = new int[20][20];
for (int i = 0; i < MineNum; i++) {
int choice = (int) (Math.random() * list.size());
counts[list.get(choice) / 100][list.get(choice) % 100] = MINE;
list.remove(choice);
}
//neighbor counts(how many mines are touching this square.)
for (int x = 0; x < counts.length; x++) {
for (int y = 0; y < counts[0].length; y++) {
int neighborCount = 0;
if (counts[x][y] != MINE) {
if(x > 0 && y > 0 && counts[x - 1][y - 1] == MINE) { //a mine is up & left
neighborCount++;
}
if(y > 0 && counts[x][y - 1] == MINE) { //a mine is up
neighborCount++;
}
if(x < counts.length - 1 && y > 0 && counts[x + 1][y - 1] == MINE) { // a mine is left
neighborCount++;
}
if(x > 0 && counts[x - 1][y] == MINE) { //left
neighborCount++;
}
if(x < counts.length - 1 && counts[x + 1][y] == MINE) { //mine is right
neighborCount++;
}
if(x > 0 && y < counts[0].length - 1 && counts[x - 1][y + 1] == MINE) { //mine is down
neighborCount++;
}
if(y < counts[0].length - 1 && counts[x][y + 1]== MINE) {//mine is up right
neighborCount++;
}
if(x < counts[0].length - 1 && y < counts[0].length - 1 && counts[x + 1][y + 1]== MINE) {//mine is down left
neighborCount++;
}
counts[x][y] = neighborCount;
}
}
}
}
public void lostGame() {
for (int x = 0; x < buttons.length; x++) {
for (int y = 0; y < buttons[0].length; y++) {
if (buttons[x][y].isEnabled()) {
if (counts[x][y] != MINE) {
buttons[x][y].setText(counts[x][y] + "");
buttons[x][y].setEnabled(false);
}
else {
buttons[x][y].setText("✪");
buttons[x][y].setEnabled(false);
}
}
}
}
JOptionPane.showMessageDialog(null, "You Lose!\n" + "You clicked on a mine!", "BOOM!", JOptionPane.INFORMATION_MESSAGE);
}
public void checkWin() {
boolean winner = true;
for (int x = 0; x < counts.length; x++) {
for (int y = 0; y < counts[0].length; y++) {
if (counts[x][y] != MINE && buttons[x][y].isEnabled()) {
winner = false;
}
}
}
if (winner == true) {
JOptionPane.showMessageDialog(frame, "You win!");
}
}
public void zeroCleaner(ArrayList<Integer> toClear) {
if (toClear.size() == 0) {
return;
}
else {
int x = toClear.get(0) / 100;
int y = toClear.get(0) % 100;
toClear.remove(0);
if(x > 0 && y > 0 && buttons[x-1][y-1].isEnabled()) { //up and left
buttons[x - 1][y - 1].setText(counts[x-1][y-1] + "");
buttons[x - 1][y - 1].setEnabled(false);
if (counts[x - 1][y - 1] == 0) {
toClear.add((x-1) * 100 + (y-1));
}
}
if (y > 0 && buttons[x][y-1].isEnabled()) { // up
buttons[x][y - 1].setText(counts[x][y-1] + "");
buttons[x][y - 1].setEnabled(false);
if (counts[x][y - 1] == 0) {
toClear.add(x * 100+(y - 1));
}
}
if (x < counts.length - 1 && y > 0 && buttons[x+1][y-1].isEnabled()) { //up right
buttons[x + 1][y - 1].setText(counts[x+1][y-1] + "");
buttons[x + 1][y - 1].setEnabled(false);
if (counts[x + 1][y - 1] == 0) {
toClear.add((x + 1)*100+(y - 1));
}
}
if(x > 0 && y > 0 && buttons[x-1][y].isEnabled()) { //left
buttons[x - 1][y].setText(counts[x-1][y] + "");
buttons[x - 1][y].setEnabled(false);
if (counts[x-1][y] == 0) {
toClear.add((x-1)*100+y);
}
}
if (x < counts.length - 1 && buttons[x+1][y].isEnabled()) { //right
buttons[x + 1][y].setText(counts[x+1][y] + "");
buttons[x + 1][y].setEnabled(false);
if (counts[x + 1][y] == 0) {
toClear.add((x + 1)*100+y);
}
}
if(x > 0 && y < counts[0].length - 1 && buttons[x-1][y+1].isEnabled()) { //down and left
buttons[x - 1][y + 1].setText(counts[x-1][y+1] + "");
buttons[x - 1][y + 1].setEnabled(false);
if (counts[x-1][y+1] == 0) {
toClear.add((x-1)*100+(y+1));
}
}
if (y < counts[0].length - 1 && buttons[x][y+1].isEnabled()) { // down
buttons[x][y + 1].setText(counts[x][y+1] + "");
buttons[x][y + 1].setEnabled(false);
if (counts[x][y + 1] == 0) {
toClear.add(x * 100+(y + 1));
}
}
if (x < counts.length - 1 && y < counts[0].length - 1 && buttons[x+1][y+1].isEnabled()) { //down right
buttons[x + 1][y + 1].setText(counts[x+1][y+1] + "");
buttons[x + 1][y + 1].setEnabled(false);
if (counts[x + 1][y + 1] == 0) {
toClear.add((x + 1)*100+(y + 1));
}
}
zeroCleaner(toClear);
}
}
#Override
public void actionPerformed(ActionEvent event) {
if(event.getSource().equals(reset)) {
//Resets the playing field
for (int x = 0; x < buttons.length; x++) {
for (int y = 0; y < buttons[0].length; y++) {
buttons[x][y].setEnabled(true);
buttons[x][y].setText("▒");
}
}
makeRandomMines();
}
else {
for (int x = 0; x < buttons.length ; x++) {
for (int y = 0; y < buttons[0].length; y++) {
if(event.getSource().equals(buttons[x][y])) {
if (counts[x][y]== MINE) {
lostGame();
}
else if(counts[x][y] == 0) {
buttons[x][y].setText(counts[x][y]+ "");
buttons[x][y].setEnabled(false);
ArrayList<Integer> toClear = new ArrayList<>();
toClear.add(x*100+y);
zeroCleaner(toClear);
checkWin();
}
else {
buttons[x][y].setText(counts[x][y]+ "");
buttons[x][y].setEnabled(false);
checkWin();
}
}
}
}
}
}
}
This should solve your problem:
for (int a = 0; a<buttons.length; a++) {
for (int b = 0; b < buttons[0].length; b++) {
buttons[a][b] = new JButton("▒");
buttons[a][b].addActionListener(this);
final int finalB = b;
final int finalA = a;
buttons[a][b].addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
mineFlagger(true, finalA, finalB);
}
}
});
grid.add(buttons[a][b]);
}
}
public void mineFlagger (boolean flag, int x, int y){
buttons[x][y].setText("F");
}
Edit: for toggling the flag (lazy implementation)
if (SwingUtilities.isRightMouseButton(e)) {
mineFlagger(finalA, finalB);
}
public void mineFlagger(int x, int y) {
if(buttons[x][y].getText().equals("F")) {
buttons[x][y].setText("▒");
} else if (buttons[x][y].getText().equals("▒")){
buttons[x][y].setText("F");
}
}
If you import 'javax.swing.SwingUtilities' you could use utilities methods:
SwingUtilities.isRightMouseButton(MouseEvent anEvent)
Then when a right mouse button is pressed, get the location of the mouse, find the button at that location, and then use that event to change the picture on that particular button to a picture of a flag.
You could use:
MouseInfo.getPointerInfo().getLocation()
To obtain the location of the mouse relative to the screen.
Related
I have a 15 x 15 2D array of JButton.
When I hold one of those buttons (i.e. Button[i][j]),
I would like to change the color of adjacent buttons (i.e. Button[i-1][j], Button[i+1][j], Button[i][j-1], Button[i][j+1]).
How do I do this?
Below is the part of my implementation of my 2D array. The button doesn't do anything for now.
fb = new JButton[15][15];
for(int i = 0; i < 15; i++){
for(int j = 0; j < 15; j++){
fb[i][j] = new JButton();
fb[i][j].setBackground(Color.WHITE);
fb[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
fb[i][j].setPreferredSize(new Dimension(40, 40));
//fb[i][j].setEnabled(false);
grid.add(fb[i][j]);
}
}
try this block:
if (i - 1 >= 0) {
if (fb[i - 1][j] != null) {
fb[i - 1][j].setBackground(Color.RED);
}
} else if (i + 1 < 15) {
if (fb[i + 1][j] != null) {
fb[i + 1][j].setBackground(Color.RED);
}
} else if (j - 1 >= 0) {
if (fb[i][j - 1] != null) {
fb[i][j - 1].setBackground(Color.RED);
}
} else if (j + 1 < 15) {
if (fb[i][j + 1] != null) {
fb[i][j + 1].setBackground(Color.RED);
}
}
and listener:
fb[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
int x, y;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if(fb[i][j].equals(b)){
x = i;
y = j;
break;
}
}
}
if (x - 1 >= 0) {
if (fb[x - 1][y] != null) {
fb[x - 1][y].setBackground(Color.RED);
}
} else if (x + 1 < 15) {
if (fb[x + 1][y] != null) {
fb[x + 1][y].setBackground(Color.RED);
}
} else if (y - 1 >= 0) {
if (fb[x][y - 1] != null) {
fb[x][y - 1].setBackground(Color.RED);
}
} else if (y + 1 < 15) {
if (fb[x][y + 1] != null) {
fb[x][y + 1].setBackground(Color.RED);
}
}
}
});
Main Class
package edu.bsu.cs121.mamurphy;
public class GameOfLifeMain {
public static void main(String[] args) {
{
new GameOfLifeGUI();
System.out.println();
}
}
}
Tester Class
package edu.bsu.cs121.mamurphy;
import java.awt.Color;
import java.awt.Graphics;
import java.io.File;
import java.util.Scanner;
import javax.swing.JPanel;
public class tester extends JPanel
{
final static int ROW = 25, COL = 75;
final static char DOT = '.';
static char[][] grid = new char[ROW + 2][COL + 2];
static char[][] nextgrid = new char[ROW + 2][COL + 2];
boolean sameFlag;
boolean blankFlag;
public static void init(char[][] grid, char[][] nextgrid) {
for (int row = 0; row <= ROW + 1; row++) {
for (int col = 0; col <= COL + 1; col++) {
grid[row][col] = DOT;
nextgrid[row][col] = DOT;
}
}
}
public static void pause() {
try {
Thread.currentThread();
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
}
public void begin() {
init(grid, nextgrid);
read(grid);
repaint(); // calls paintComponent
pause();
while (sameFlag == true && blankFlag == false) {
nextGen(grid, nextgrid);
}
}
public static void read(char[][] grid) {
Scanner world = new Scanner(System.in);
System.out.println("Type the file name of the world you'd like to create.");
String fileName = world.nextLine();
{
try {
world = new Scanner(new File(fileName));
} catch (Exception ex) {
System.out.println("Please insert a valid file name.");
}
for (int row = 1; row <= ROW; row++) {
String s = world.next();
for (int col = 1; col <= COL; col++) {
grid[row][col] = s.charAt(col - 1);
}
}
}
}
public void print(Graphics g) {
int x, y;
y = 20;
for (int row = 1; row <= ROW; row++) {
x = 20;
for (int col = 1; col <= COL; col++) {
g.drawString("" + grid[row][col], x, y);
x = x + 7;
}
y = y + 15;
}
}
public static int neighbors(char[][] grid, int r, int c) {
// counts the # of closest neighbors that are X's
int count = 0;
for (int row = r - 1; row <= r + 1; row++) {
for (int col = c - 1; col <= c + 1; col++) {
if (grid[row][col] == 'X') {
count++;
}
}
}
if (grid[r][c] == 'X') {
count = count - 1;
}
return count;
}
public static void nextGen(char[][] grid, char[][] nextgrid) {
for (int row = 1; row <= ROW; row++) {
for (int col = 1; col <= COL; col++) {
if (grid[row][col] == 'X') {
int count = 0;
{
if (grid[row][col - 1] == 'X')
count = count + 1;
if (grid[row][col + 1] == 'X')
count = count + 1;
if (grid[row - 1][col] == 'X')
count = count + 1;
if (grid[row - 1][col - 1] == 'X')
count = count + 1;
if (grid[row - 1][col + 1] == 'X')
count = count + 1;
if (grid[row + 1][col - 1] == 'X')
count = count + 1;
if (grid[row + 1][col] == 'X')
count = count + 1;
if (grid[row + 1][col + 1] == 'X')
count = count + 1;
}
if (count == 2 || count == 3) {
nextgrid[row][col] = 'X';
} else
nextgrid[row][col] = DOT;
}
if (grid[row][col] == DOT) {
int count1 = 0;
{
if (grid[row][col - 1] == 'X')
count1 = count1 + 1;
if (grid[row][col + 1] == 'X')
count1 = count1 + 1;
if (grid[row - 1][col] == 'X')
count1 = count1 + 1;
if (grid[row - 1][col - 1] == 'X')
count1 = count1 + 1;
if (grid[row - 1][col + 1] == 'X')
count1 = count1 + 1;
if (grid[row + 1][col - 1] == 'X')
count1 = count1 + 1;
if (grid[row + 1][col] == 'X')
count1 = count1 + 1;
if (grid[row + 1][col + 1] == 'X')
count1 = count1 + 1;
}
if (count1 == 3)
nextgrid[row][col] = 'X';
}
}
}
}
public static void copy(char[][] grid, char[][] nextgrid) {
for (int i = 0; i < ROW + 1; i++) {
for (int j = 0; j < COL + 1; j++) {
grid[i][j] = nextgrid[i][j];
}
}
}
public static boolean isEmpty(char[][] grid, char[][] nextgrid) {
boolean blankFlag = true;
for (int i = 0; i < ROW + 1; i++) {
for (int j = 0; j < COL + 1; j++) {
if (grid[i][j] != DOT) {
blankFlag = false;
}
}
}
return blankFlag;
}
public static boolean isSame(char[][] grid, char[][] nextgrid) {
boolean sameFlag = false;
for (int i = 0; i < ROW + 1; i++) {
for (int j = 0; j < COL + 1; j++) {
if (grid[i][j] == nextgrid[i][j]) {
sameFlag = true;
break;
}
}
}
return sameFlag;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);// erases panel Contents
g.setColor(Color.black);
if (sameFlag == false && blankFlag == false) {
print(g);
} else {
if (sameFlag == true) {
g.drawString("This worldis the same as the last one.", 10, 250);
}
if (blankFlag == true) {
g.drawString("Everyone died. Good job.", 10, 250);
}
}
}
}
GUI Class
package edu.bsu.cs121.mamurphy;
import javax.swing.*;
import java.awt.*;
//
public class GameOfLifeGUI extends JFrame {
public GameOfLifeGUI() {
super("Game of Life");
setSize(600, 445);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
tester test1 = new tester();
test1.setBackground(Color.LIGHT_GRAY);
panel.add(test1);
setContentPane(panel);
setVisible(true);
Temp one = new Temp(test1);
one.start();
}
class Temp extends Thread {
tester anim;
public Temp(tester anim) {
this.anim = anim;
}
public void run()// for each instance of test begin will be executed
{
anim.begin();
}
}
}
Hi stackoverflow. I have been working on this game of life project for the last few days and I have hit one final wall in trying to finish it up. How do I make it proceed to the next generation?
I know for what I am required to do I need to ask the user if they wish to continue to the next generation or not. I know I need to use a scanner and then have a loop that checks to see what the user's input is, as well as a try catch to make sure the program doesn't crash if they type in anything that isn't what the program asks them to.
My only issue is that I do not know where to put this code at in my tester class (if that is actually where it needs to go).
Any help is greatly appreciated.
I'm programing a game using JFrame, JPanel, JButton and actionListener and I'd like to have a button that when pressed restarts the game. Basically I want the the current Jframe to close and to reopen a new different one. I want it to be like a button you press to start a new game.
Here is my code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Start {
public static int a;
public static JButton[][] gumbi = new JButton[15][15];
public static JFrame okno = new JFrame("Nonogram");
public static void main(String[] args) {
okno.setVisible(true);
okno.setSize(800, 800);
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
okno.add(panel);
JPanel polje = new JPanel(new GridLayout(15, 15));
panel.add(polje, BorderLayout.CENTER);
a = 0;
int b = 1;
// String I = I;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (i < 5 && j < 5) {
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.GREEN);
// gumbi[i][j].addActionListener(new Listener(gumbi));
polje.add(gumbi[i][j]);
} else if (i < 5 || j < 5) {
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.YELLOW);
// gumbi[i][j].addActionListener(new Listener(gumbi));
polje.add(gumbi[i][j]);
gumbi[i][j].setEnabled(false);
} else {
if (Math.random() <= 0.5) {
a += 1;
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].setForeground(Color.RED);
// gumbi[i][j].addActionListener(new Listener(gumbi));
gumbi[i][j].setText("3");
polje.add(gumbi[i][j]);
} else {
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].setForeground(Color.WHITE);
// gumbi[i][j].addActionListener(new Listener(gumbi));
gumbi[i][j].setText("4");
polje.add(gumbi[i][j]);
}
}
}
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
gumbi[i][j].addActionListener(new Listener(gumbi));
}
}
int[] array = new int[105];
for (int i = 5; i < 15; i++) {
for (int j = 5; j < 15; j++) {
int num = Integer.parseInt(gumbi[i][j].getText());
array[j + ((i - 5) * 10) - 5] = num;
}
}
int[] array2 = new int[105];
for (int i = 0; i < 100; i++) {
array2[i] = -2;
}
for (int i = 0; i < 100; i++) {
if (array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0) {
b += 1;
} else if ((array[i] == 3 && array[i] != array[i + 1] && i < 99)
|| ((i + 1) % 10 == 0 && array[i] == 3)) {
array2[i] = b;
b = 1;
}
if ((i + 1) % 10 == 0) {
b = 1;
}
}
int x = 0;
int y = 0;
for (int i = 0; i <= 100; i++) {
// if(array2[(i-4) + (10*(j - 5))] != -2){
if (array2[i] != -2 && array[i] != 0) {
gumbi[x + 5][y].setText("" + array2[i]);
y++;
}
if ((i + 1) % 10 == 0) {
x++;
y = 0;
}
}
for (int i = 0; i < 101; i++) {
// System.out.println(array[i]);
if (array2[i] != -2)
System.out.print(array2[i] + " ");
}
for (int i = 5; i < 15; i++) {
for (int j = 5; j < 15; j++) {
int num = Integer.parseInt(gumbi[j][i].getText());
array[j + ((i - 5) * 10) - 5] = num;
}
}
for (int i = 0; i < 100; i++) {
array2[i] = -2;
}
b = 1;
for (int i = 0; i < 100; i++) {
if (array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0) {
b += 1;
} else if ((array[i] == 3 && array[i] != array[i + 1] && i < 99)
|| ((i + 1) % 10 == 0 && array[i] == 3)) {
array2[i] = b;
b = 1;
}
if ((i + 1) % 10 == 0) {
b = 1;
}
}
x = 0;
y = 0;
for (int i = 0; i <= 100; i++) {
if (array2[i] != -2 && array[i] != 0) {
gumbi[y][x + 5].setText("" + array2[i]);
y++;
}
if ((i + 1) % 10 == 0) {
x++;
y = 0;
}
}
System.out.println();
for (int i = 0; i < 105; i++) {
// System.out.println(array[i]);
if (array2[i] != -2)
System.out.print(array2[i] + " ");
}
System.out.println();
for (int i = 0; i < 105; i++) {
// System.out.println(array[i]);
System.out.print(array[i] + " ");
}
gumbi[1][1].setText("?");
gumbi[1][2].setText("C");
for (int i = 5; i < 15; i++) {
for (int j = 5; j < 15; j++) {
// gumbi[i][j].setText(null);
}
}
}
}
and the actionListener:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Listener implements ActionListener {
JButton[][] gumbi = Start.gumbi;
JFrame cestitke = new JFrame("Cestitke!");
JLabel bravo = new JLabel("Bravo, zmagali ste!");
JFrame vprasaj = new JFrame("Navodila");
JPanel navodila = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel restart = new JLabel("Za zaceti novo igro pritisnite tipko R(restart)");
JLabel pregled = new JLabel("Za pregled veljavnosti trenutne pozicije pritisnite tipko C(check)");
public Listener(JButton[][] gumbi) {
this.gumbi = gumbi;
}
public void actionPerformed(ActionEvent e) {
JButton gumb = (JButton) e.getSource();
if (gumb.getBackground() == Color.WHITE) {
gumb.setBackground(Color.BLACK);
} else if (gumb.getBackground() == Color.BLACK) {
gumb.setBackground(Color.WHITE);
}
if(e.getSource() == gumbi[1][1]){
vprasaj.setVisible(true);
vprasaj.setSize(450, 200);
vprasaj.add(navodila);
c.gridx = 0;
c.gridy = 0;
navodila.add(restart, c);
c.gridx = 0;
c.gridy = 1;
navodila.add(pregled, c);;
}
if(e.getSource() == gumbi[1][2]){
for(int i = 5; i < 15; i++){
for(int j = 5; j < 15; j++){
if(gumbi[i][j].getBackground() == Color.BLACK && gumbi[i][j].getForeground() == Color.WHITE){
gumbi[i][j].setBackground(Color.RED);
}
}
}
}
if(gumb.getBackground() == Color.RED && gumb.getForeground() == Color.WHITE){
gumb.setBackground(Color.WHITE);
}
if (gumb.getBackground() == Color.WHITE && gumb.getForeground() == Color.RED) {
Start.a += 1;
gumbi[0][0].setText("" + Start.a);
} else if (gumb.getBackground() == Color.BLACK && gumb.getForeground() == Color.RED) {
Start.a -= 1;
gumbi[0][0].setText("" + Start.a);
if(Start.a == 0){
for(int i = 5; i < 15; i++){
for (int j = 5; j < 15; j++){
gumbi[i][j].setEnabled(false);
}
}
cestitke.add(bravo);
cestitke.setVisible(true);
cestitke.setSize(100, 100);
}
}
if (gumb.getBackground() == Color.BLACK && gumb.getForeground() == Color.WHITE) {
Start.a += 100;
gumbi[0][0].setText("" + Start.a);
} else if (gumb.getBackground() == Color.WHITE && gumb.getForeground() == Color.WHITE) {
Start.a -= 100;
gumbi[0][0].setText("" + Start.a);
if(Start.a == 0){
for(int i = 5; i < 15; i++){
for (int j = 5; j < 15; j++){
gumbi[i][j].setEnabled(false);
}
}
cestitke.add(bravo);
cestitke.setVisible(true);
cestitke.setSize(100, 100);
}
}
}
}
thanks for your time.
Something like this?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.WindowEvent;
public class Start
{
public static int a;
public static JButton[][] gumbi = new JButton[15][15];
public static JFrame okno = null;
public static class Listener
implements ActionListener
{
JButton[][] gumbi = Start.gumbi;
JFrame cestitke = new JFrame("Cestitke!");
JLabel bravo = new JLabel("Bravo, zmagali ste!");
JFrame vprasaj = new JFrame("Navodila");
JPanel navodila = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel restart = new JLabel("Za zaceti novo igro pritisnite tipko R(restart)");
JLabel pregled = new JLabel("Za pregled veljavnosti trenutne pozicije pritisnite tipko C(check)");
public Listener(JButton[][] gumbi)
{
this.gumbi = gumbi;
}
public void actionPerformed(ActionEvent e)
{
JButton gumb = (JButton) e.getSource();
if (gumb.getBackground() == Color.WHITE)
{
gumb.setBackground(Color.BLACK);
}
else if (gumb.getBackground() == Color.BLACK)
{
gumb.setBackground(Color.WHITE);
}
if (e.getSource() == gumbi[1][1])
{
vprasaj.setVisible(true);
vprasaj.setSize(450, 200);
vprasaj.add(navodila);
c.gridx = 0;
c.gridy = 0;
navodila.add(restart, c);
c.gridx = 0;
c.gridy = 1;
navodila.add(pregled, c);;
}
if (e.getSource() == gumbi[1][2])
{
for (int i = 5; i < 15; i++)
{
for (int j = 5; j < 15; j++)
{
if (gumbi[i][j].getBackground() == Color.BLACK && gumbi[i][j].getForeground() == Color.WHITE)
{
gumbi[i][j].setBackground(Color.RED);
}
}
}
}
if (e.getSource() == gumbi[1][3])
{
restartFrame();
}
if (gumb.getBackground() == Color.RED && gumb.getForeground() == Color.WHITE)
{
gumb.setBackground(Color.WHITE);
}
if (gumb.getBackground() == Color.WHITE && gumb.getForeground() == Color.RED)
{
Start.a += 1;
gumbi[0][0].setText("" + Start.a);
}
else if (gumb.getBackground() == Color.BLACK && gumb.getForeground() == Color.RED)
{
Start.a -= 1;
gumbi[0][0].setText("" + Start.a);
if (Start.a == 0)
{
for (int i = 5; i < 15; i++)
{
for (int j = 5; j < 15; j++)
{
gumbi[i][j].setEnabled(false);
}
}
cestitke.add(bravo);
cestitke.setVisible(true);
cestitke.setSize(100, 100);
}
}
if (gumb.getBackground() == Color.BLACK && gumb.getForeground() == Color.WHITE)
{
Start.a += 100;
gumbi[0][0].setText("" + Start.a);
}
else if (gumb.getBackground() == Color.WHITE && gumb.getForeground() == Color.WHITE)
{
Start.a -= 100;
gumbi[0][0].setText("" + Start.a);
if (Start.a == 0)
{
for (int i = 5; i < 15; i++)
{
for (int j = 5; j < 15; j++)
{
gumbi[i][j].setEnabled(false);
}
}
cestitke.add(bravo);
cestitke.setVisible(true);
cestitke.setSize(100, 100);
}
}
}
}
public static void main(String[] args)
{
restartFrame();
}
public static void restartFrame()
{
if (null != okno)
{
okno.dispatchEvent(new WindowEvent(okno, WindowEvent.WINDOW_CLOSING));
okno = null;
}
okno = new JFrame("Nonogram");
okno.setVisible(true);
okno.setSize(800, 800);
okno.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
okno.add(panel);
JPanel polje = new JPanel(new GridLayout(15, 15));
panel.add(polje, BorderLayout.CENTER);
a = 0;
int b = 1;
// String I = I;
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (i < 5 && j < 5)
{
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.GREEN);
// gumbi[i][j].addActionListener(new Listener(gumbi));
polje.add(gumbi[i][j]);
}
else if (i < 5 || j < 5)
{
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.YELLOW);
// gumbi[i][j].addActionListener(new Listener(gumbi));
polje.add(gumbi[i][j]);
gumbi[i][j].setEnabled(false);
}
else
{
if (Math.random() <= 0.5)
{
a += 1;
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].setForeground(Color.RED);
// gumbi[i][j].addActionListener(new Listener(gumbi));
gumbi[i][j].setText("3");
polje.add(gumbi[i][j]);
}
else
{
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].setForeground(Color.WHITE);
// gumbi[i][j].addActionListener(new Listener(gumbi));
gumbi[i][j].setText("4");
polje.add(gumbi[i][j]);
}
}
}
}
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
gumbi[i][j].addActionListener(new Listener(gumbi));
}
}
int[] array = new int[105];
for (int i = 5; i < 15; i++)
{
for (int j = 5; j < 15; j++)
{
int num = Integer.parseInt(gumbi[i][j].getText());
array[j + ((i - 5) * 10) - 5] = num;
}
}
int[] array2 = new int[105];
for (int i = 0; i < 100; i++)
{
array2[i] = -2;
}
for (int i = 0; i < 100; i++)
{
if (array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0)
{
b += 1;
}
else if ((array[i] == 3 && array[i] != array[i + 1] && i < 99)
|| ((i + 1) % 10 == 0 && array[i] == 3))
{
array2[i] = b;
b = 1;
}
if ((i + 1) % 10 == 0)
{
b = 1;
}
}
int x = 0;
int y = 0;
for (int i = 0; i <= 100; i++)
{
if (array2[i] != -2 && array[i] != 0)
{
gumbi[x + 5][y].setText("" + array2[i]);
y++;
}
if ((i + 1) % 10 == 0)
{
x++;
y = 0;
}
}
for (int i = 0; i < 101; i++)
{
// System.out.println(array[i]);
if (array2[i] != -2)
{
System.out.print(array2[i] + " ");
}
}
for (int i = 5; i < 15; i++)
{
for (int j = 5; j < 15; j++)
{
int num = Integer.parseInt(gumbi[j][i].getText());
array[j + ((i - 5) * 10) - 5] = num;
}
}
for (int i = 0; i < 100; i++)
{
array2[i] = -2;
}
b = 1;
for (int i = 0; i < 100; i++)
{
if (array[i] == array[i + 1] && array[i] == 3 && (i + 1) % 10 != 0)
{
b += 1;
}
else if ((array[i] == 3 && array[i] != array[i + 1] && i < 99)
|| ((i + 1) % 10 == 0 && array[i] == 3))
{
array2[i] = b;
b = 1;
}
if ((i + 1) % 10 == 0)
{
b = 1;
}
}
x = 0;
y = 0;
for (int i = 0; i <= 100; i++)
{
if (array2[i] != -2 && array[i] != 0)
{
gumbi[y][x + 5].setText("" + array2[i]);
y++;
}
if ((i + 1) % 10 == 0)
{
x++;
y = 0;
}
}
System.out.println();
for (int i = 0; i < 105; i++)
{
// System.out.println(array[i]);
if (array2[i] != -2)
{
System.out.print(array2[i] + " ");
}
}
System.out.println();
for (int i = 0; i < 105; i++)
{
// System.out.println(array[i]);
System.out.print(array[i] + " ");
}
gumbi[1][1].setText("?");
gumbi[1][2].setText("C");
gumbi[1][3].setText("R");
}
}
I have been working on a versions of Conway's Game of Life for school and I have run into a problem: cells are becoming dead or alive in the wrong places.
How can I fix that?
if (alive == 3 && aryBOARD[x][y] == 0) { //rule 4
aryCHANGE[x][y] = 1;
}
if (alive > 3 && aryBOARD[x][y] == 1) { //rule 3
aryCHANGE[x][y] = 0;
}
if (alive >= 2 && alive <= 3 && aryBOARD[x][y] == 1) { //rule 2
aryCHANGE[x][y] = 1;
}
if (alive < 2 && aryBOARD[x][y] == 1) { //rule 1
aryCHANGE[x][y] = 0;
}
if (dead > 5) { //rule check
aryCHANGE[x][y] = 0;
}
That is where I assume the problem is but if having the whole code would help:
package gameoflife2;
public class Game {
public static void main(String[] args) {
int[][] aryBOARD = new int[5][5];
int x = 0;
int y = 0;
int dead = 0;
int alive = 0;
int i, j;
// Board numbers
// 00011
// 00001
// 01000
// 01100
// 00000
aryBOARD[0][0] = 0;
aryBOARD[0][1] = 0;
aryBOARD[0][2] = 0;
aryBOARD[0][3] = 1;
aryBOARD[0][4] = 1;
aryBOARD[1][0] = 0;
aryBOARD[1][1] = 0;
aryBOARD[1][2] = 0;
aryBOARD[1][3] = 0;
aryBOARD[1][4] = 1;
aryBOARD[2][0] = 0;
aryBOARD[2][1] = 1;
aryBOARD[2][2] = 0;
aryBOARD[2][3] = 0;
aryBOARD[2][4] = 0;
aryBOARD[3][0] = 0;
aryBOARD[3][1] = 1;
aryBOARD[3][2] = 1;
aryBOARD[3][3] = 0;
aryBOARD[3][4] = 0;
aryBOARD[4][0] = 0;
aryBOARD[4][1] = 0;
aryBOARD[4][2] = 0;
aryBOARD[4][3] = 0;
aryBOARD[4][4] = 0;
// end of array
int[][] aryCHANGE = aryBOARD.clone(); // array change is equal to array
// board
// printing array
int rows = 5;
int colums = 5;
for (i = 0; i < rows; i++) {
for (j = 0; j < colums; j++) {
System.out.print(aryBOARD[i][j] + " ");
}
System.out.println("");
}
System.out.println("---------------------------");
// done printing array
// check for dead or alive cells
for (x = 0; x <= 4; x++) {
for (y = 0; y <= 4; y++) {
alive = 0;
dead = 0;
if ((x + 1 < 4) && (x + 1 > 0)) { // right
if (aryBOARD[x + 1][y] == 0) {
dead++;
} else {
alive++;
}
}
if (((y - 1 < 4) && (y - 1 > 0) && (x + 1 < 4) && (x + 1 > 0))) { // bottom
// right
// corner
if (aryBOARD[x + 1][y - 1] == 0) {
dead++;
} else {
alive++;
}
}
if (((y + 1 < 4) && (y + 1 > 0) && (x + 1 < 4) && (x + 1 > 0))) { // top
// right
// corner
if (aryBOARD[x + 1][y + 1] == 0) {
dead++;
} else {
alive++;
}
}
if ((y + 1 < 4) && (y + 1 > 0)) {// top middle
if (aryBOARD[x][y] == 0) {
dead++;
} else {
alive++;
}
}
if (((y + 1 < 4) && (y + 1 > 0) && (x - 1 < 4) && (x - 1 > 0))) {// top
// left
// corner
if (aryBOARD[x - 1][y + 1] == 0) {
dead++;
} else {
alive++;
}
}
if ((x - 1 < 4) && (x - 1 > 0)) {// left
if (aryBOARD[x - 1][y] == 0) {
dead++;
} else {
alive++;
}
}
if (((y - 1 < 4) && (y - 1 > 0) && (x - 1 < 4) && (x - 1 > 0))) {// bottom
// left
// corner
if (aryBOARD[x - 1][y - 1] == 0) {
dead++;
} else {
alive++;
}
}
// x++
if ((y - 1 < 4) && (y - 1 > 0)) {// bottom middle
if (aryBOARD[x][y - 1] == 0) {
dead++;
} else {
alive++;
}
}
// RULES
// 1 Any live cell with fewer than two live neighbors dies, as if caused
// by under-population.
// 2 Any live cell with two or three live neighbors lives on to the next
// generation.
// 3 Any live cell with more than three live neighbors dies, as if by
// overcrowding.
// 4 Any dead cell with exactly three live neighbors becomes a live
// cell, as if by reproduction.
// test alive and dead
if (alive == 3 && aryBOARD[x][y] == 0) {// rule 4
aryCHANGE[x][y] = 1;
}
if (alive > 3 && aryBOARD[x][y] == 1) {// rule 3
aryCHANGE[x][y] = 0;
}
if (alive >= 2 && alive <= 3 && aryBOARD[x][y] == 1) {// rule 2
aryCHANGE[x][y] = 1;
}
if (alive < 2 && aryBOARD[x][y] == 1) {// rule 1
aryCHANGE[x][y] = 0;
}
if (dead > 5) {// rule check
aryCHANGE[x][y] = 0;
}
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < colums; j++) {
System.out.print(aryCHANGE[i][j] + " ");
}
System.out.println("");
}
System.out.println("---------------------------");
} // end main
} // end class
You need to think a lot more about structuring your code and breaking things up into smaller chunks. That will help you a lot, especially when you move onto larger projects in the future.
For example write a simple method to count the number of living cells around a given cell.
Now your main loop through just becomes:
for (int x=0;x<width;x++) {
for (int y=0;y<height;y++) {
switch(countLivingAround(x,y)) {
case 0: // Less than 2 always dies
case 1:
grid(x,y) = 0;
break;
case 2: // Do nothing, keep current state
break;
case 3: // Breed
grid(x,y) = 1;
break;
case 4: // Dies from overcrowding
grid(x,y) = 0;
break;
}
}
}
Your count function can be simple, it just adds up the values at [x-1,y],[x,y+1], etc, remember to check for the edges of the board and handle that case correctly though.
In addition to breaking the logic up into different methods you need to either use a debugger to step thru the code or use printlns to display the x and y values your evaluating (See the sample below) You'll see that some of your calculations on the adjoining cells x and y coordinates need work.
for (x = 0; x <= 4; x++) {
for (y = 0; y <= 4; y++) {
alive = 0;
dead = 0;
System.out.println("evaluating cell x=" + x + ", y = " + y);
if ((x + 1 < 4) && (x + 1 > 0)) {// right
if (aryBOARD[x + 1][y] == 0) {
dead++;
}
else {
alive++;
}
System.out.println(" check 1 x=" + (x + 1) + ", y = " + y);
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I've coded an entire program to play Connect Four, but the algorithm for checking who has won (after each turn) isn't working and I don't know why. I keep getting a weird message when I compile it (exception ArrayIndexOutOfBoundsException). Any help is appreciated.
Here is the code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class connectFourDemo extends Applet
implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
int[][] myGrid = new int[7][6];
// If piece is 0, white. If 1, red. If 2, black.
int xCoord, yCoord; // X and Y co-ordinates for mouse navigation.
int width, height;
int playerTurn = 1; // Player's turn. Default is 1 since red goes first.
int mx, my; // The mouse coordinates.
boolean isButtonPressed = false;
public void init() {
width = getSize().width;
height = getSize().height;
setBackground(Color.yellow);
mx = width / 2;
my = height / 2;
addMouseListener(this);
addMouseMotionListener(this);
}
private int getXValue(int xValue) {
if (xValue < width / 7) return 0;
else if (xValue < width / 7 * 2) return 1;
else if (xValue < width / 7 * 3) return 2;
else if (xValue < width / 7 * 4) return 3;
else if (xValue < width / 7 * 5) return 4;
else if (xValue < width / 7 * 6) return 5;
else return 6;
}
private int getYValue(int yValue) {
if (yValue < width / 6) return 0;
else if (yValue < width / 6 * 2) return 1;
else if (yValue < width / 6 * 3) return 2;
else if (yValue < width / 6 * 4) return 3;
else if (yValue < width / 6 * 5) return 4;
else return 5;
}
public void verticalCheck(int x, int y) {
if (myGrid[x][y] == 1) {
int counter = 1;
for (int i = 5; i >= 0; i--) {
if (myGrid[xCoord][i] == 1) {
System.out.println("Counter one in vertical check is " + counter + ".");
if (myGrid[xCoord][i - 1] == 1 && (i - 1 >= 0)) counter++;
}
}
if (counter == 4) {
System.out.println("Player 1 has won Connect Four vertically!");
}
}
else if (myGrid[x][y] == 2) {
int counter = 1;
for (int i = 5; i >= 0; i--) {
if (myGrid[xCoord][i] == 2) {
System.out.println("Counter two in vertical check is " + counter + ".");
if (myGrid[xCoord][i - 1] == 2 && (i - 1 >= 0)) counter++;
}
}
if (counter == 4) {
System.out.println("Player 2 has won Connect Four vertically!");
}
}
}
public void horizontalCheck(int x, int y) {
if (myGrid[x][y] == 1) {
int counter = 1;
for (int i = 0; i <= 6; i++) {
if (myGrid[i][y] == 1) {
System.out.println("Counter one in horizontal check is " + counter + ".");
if (myGrid[i + 1][y] == 1 && (i + 1 <= 6)) counter++;
}
}
if (counter == 4) {
System.out.println("Player 1 has won Connect Four horizontally!");
}
}
else if (myGrid[x][y] == 2) {
int counter = 1;
for (int i = 0; i <= 6; i++) {
if (myGrid[i][y] == 2) {
System.out.println("Counter two in horizontal check is " + counter + ".");
if (myGrid[i + 1][y] == 2 && (i + 1 <= 6)) counter++;
}
}
if (counter == 4) {
System.out.println("Player 2 has won Connect Four horizontally!");
}
}
}
public void diagonalCheckRight(int x, int y) {
if (myGrid[x][y] == 1) {
int counter = 1;
for (int i = 0; i <= 6; i++) {
for (int j = 5; j >= 0; j--) {
if (myGrid[i][j] == 1) {
System.out.println("Counter one in diagonal check right is " + counter + ".");
if (myGrid[i + 1][j + 1] == 1 && (i + 1 <= 6) && (j + 1 <= 5)) counter++;
else if (myGrid[i - 1][j + 1] == 1 && (i - 1 >= 0) && (j + 1 <= 5)) counter++;
}
}
}
if (counter == 4) {
System.out.println("Player 1 has won Connect Four diagonally!");
}
}
else if (myGrid[x][y] == 2) {
int counter = 1;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (myGrid[i][j] == 2) {
System.out.println("Counter two in diagonal check right is " + counter + ".");
if (myGrid[i + 1][j + 1] == 1 && (i + 1 <= 6) && (j + 1 <= 5)) counter++;
else if (myGrid[i - 1][j + 1] == 1 && (i - 1 >= 0) && (j + 1 <= 5)) counter++;
}
}
}
if (counter == 4) {
System.out.println("Player 2 has won Connect Four diagonally!");
}
}
}
public void diagonalCheckLeft(int x, int y) {
if (myGrid[x][y] == 1) {
int counter = 1;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (myGrid[i][j] == 1) {
System.out.println("Counter one in diagonal check left is " + counter + ".");
if (myGrid[i - 1][j - 1] == 1 && (i + 1 <= 6) && (j - 1 >= 0)) counter++;
}
}
}
if (counter == 4) {
System.out.println("Player 1 has won Connect Four diagonally!");
}
}
else if (myGrid[x][y] == 2) {
int counter = 1;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (myGrid[i][j] == 2) {
System.out.println("Counter one in diagonal check left is " + counter + ".");
if (myGrid[i - 1][j - 1] == 2 && (i <= 6) && (j >= 0)) counter++;
}
}
}
if (counter == 4) {
System.out.println("Player 2 has won Connect Four diagonally!");
}
}
}
public void mouseEntered( MouseEvent e ) {
// Called when the pointer enters the applet's rectangular area.
}
public void mouseExited( MouseEvent e ) {
// Called when the pointer leaves the applet's rectangular area.
}
public void mouseClicked( MouseEvent e ) {
// Called after a press and release of a mouse button with no motion in between.
mx = e.getX();
my = e.getY();
xCoord = getXValue(mx);
yCoord = getYValue(my);
if (myGrid[xCoord][yCoord] == 0 && playerTurn == 1) { // Drop from top, fall to bottom and vice versa.
for (int y = 5; y >= yCoord; y--) {
if (myGrid[xCoord][y] == 0) {
myGrid[xCoord][y] = 1;
y = yCoord - 1;
}
}
verticalCheck(xCoord, yCoord);
horizontalCheck(xCoord, yCoord);
diagonalCheckRight(xCoord, yCoord);
diagonalCheckLeft(xCoord, yCoord);
playerTurn = 2;
}
else if (myGrid[xCoord][yCoord] == 0 && playerTurn == 2) {
for (int y = 5; y >= yCoord; y--) {
if (myGrid[xCoord][y] == 0) {
myGrid[xCoord][y] = 2;
y = yCoord - 1;
}
}
verticalCheck(xCoord, yCoord);
horizontalCheck(xCoord, yCoord);
diagonalCheckRight(xCoord, yCoord);
diagonalCheckLeft(xCoord, yCoord);
playerTurn = 1;
}
}
public void mousePressed(MouseEvent e) { // Called after a button is pressed down.
repaint();
// "Consume" the event so it won't be processed in the
// default manner by the source which generated it.
e.consume();
}
public void mouseReleased(MouseEvent e) { // Called after a button is released.
repaint();
e.consume();
}
public void mouseMoved(MouseEvent e) { // Called during motion when no buttons are down.
mx = e.getX();
my = e.getY();
mx = mx / 50; // Divides applet width by the width of each oval (50).
my = my / 50; // Divides applet height by the height of each oval (50).
showStatus("Mouse in column " + (mx + 1) + ", row " + (my + 1) + ".");
}
public void mouseDragged(MouseEvent e) { // Called during motion with buttons down.
}
public void paint(Graphics g) {
for (int y = 0; y < 6; y++) {
for (int x = 0; x < 7; x++) {
if (myGrid[x][y] == 0) {
g.setColor(Color.white);
}
if (myGrid[x][y] == 1) {
g.setColor(Color.red);
}
if (myGrid[x][y] == 2) {
g.setColor(Color.black);
}
g.fillOval((width / 7) * x + 2, (height / 6) * y + 1, (width / 7) - 4, (height / 6) - 4);
}
}
}
}
Ran the code, your problem is in "diagonalCheckRight", namely this section:
for (int i = 0; i <= 6; i++) {
for (int j = 5; j >= 0; j--) {
if (myGrid[i][j] == 1) {
System.out.println("Counter one in diagonal check right is " + counter + ".");
if (myGrid[i + 1][j + 1] == 1 && (i + 1 <= 6) && (j + 1 <= 5)) counter++;
else if (myGrid[i - 1][j + 1] == 1 && (i - 1 >= 0) && (j + 1 <= 5)) counter++;
}
}
}
Your j index starts at 5, in the if you do myGrid[i+1][j+1] so that means on the first iteration you're accessing myGrid[1][6], however you defined myGrid to be of size [7][6] and so you're out of bounds because the valid indices are: [0..6][0..5].
Also next time look at the error message, my console was showing:
java.lang.ArrayIndexOutOfBoundsException: 6
at Main.diagonalCheckRight(Main.java:134)
I renamed the class to Main, and that 134 is exactly the line number where I found the error.