Java multiple keys pressed - java

I'm trying to move a ball across a frame. I have the up, down, left, and right motion going fine. But when I try to move diagonally, it doesn't seem to work. Here is my code.
First I declare some sets; pressed is the keys pressed, and the others are combinations of keys to move the ball up and left so on so forth;
public Set<Integer> pressed = new HashSet<>();
public Set<Integer> upLeft, upRight, downLeft, downRight;
Next, in my init method I start to add the integer values to the sets and add key listeners to my frame:
private void myInit(){
... // cut down code from above
int[] a = new int[2];
a[0] = 38; a[1] = 37;
upLeft = new HashSet(Arrays.asList(a));
a[1] = 39;
upRight = new HashSet(Arrays.asList(a));
a[0] = 40; a[1] = 37;
downLeft = new HashSet(Arrays.asList(a));
a[1] = 39;
downRight = new HashSet(Arrays.asList(a));
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
});
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
formKeyReleased(evt);
}
});
}
Now here are my methods to handle the key pressed and released:
public synchronized void formKeyPressed(java.awt.event.KeyEvent evt) {
pressed.add(evt.getKeyCode());
System.out.println("Added:"+evt.getKeyCode());
if(pressed.size() == 1) {
System.out.println("Size is 1");
if(evt.getKeyCode() == evt.VK_UP) {
if(y <= 25) {
y = 25;
}
else {
y = y - yUpSpeed;
}
}
if(evt.getKeyCode() == evt.VK_DOWN) {
if(y >= (500-25)) {
y = (500-25);
}
else {
y = y + yDownSpeed;
}
}
if(evt.getKeyCode() == evt.VK_LEFT) {
if(x <= 0) {
x = 0;
}
else {
x = x - xLeftSpeed;
}
}
if(evt.getKeyCode() == evt.VK_RIGHT) {
if(x >= (500-25)) {
x = (500-25);
}
else {
x = x + xRightSpeed;
}
}
}
else if(pressed.size() == 2) {
System.out.println("Size is 2");
if(pressed.containsAll(upLeft)) {
if(y <= 25) {
y = 25;
}
else {
y = y - yUpSpeed;
}
if(x <= 0) {
x = 0;
}
else {
x = x - xLeftSpeed;
}
}
if(pressed.containsAll(upRight)) {
if(y <= 25) {
y = 25;
}
else {
y = y - yUpSpeed;
}
if(x >= (500-25)) {
x = (500-25);
}
else {
x = x + xRightSpeed;
}
}
if(pressed.containsAll(downLeft)) {
if(y >= (500-25)) {
y = (500-25);
}
else {
y = y + yDownSpeed;
}
if(x <= 0) {
x = 0;
}
else {
x = x - xLeftSpeed;
}
}
if(pressed.containsAll(downRight)) {
if(y >= (500-25)) {
y = (500-25);
}
else {
y = y + yDownSpeed;
}
if(x >= (500-25)) {
x = (500-25);
}
else {
x = x + xRightSpeed;
}
}
}
else {
System.out.println("Size is more than 2");
}
public synchronized void formKeyReleased(java.awt.event.KeyEvent evt) {
System.out.println("Removed:"+evt.getKeyCode());
pressed.remove(evt.getKeyCode());
}
The problem is that, from the debug system.out messages, the size of the set pressed is never larger than one. What am I doing wrong?

Check out the KeyBoardAnimation example from Motion Using the Keyboard. It keeps a Map of the keys that are currently pressed and then move the object based on all the keys in the Map.
This approach uses Key Bindings, which is preferred over a KeyListener for the reasons given in the blog.

Related

Heuristics for 3D tic-tac-toe

Currently I'm trying to implement heuristics for a 3D tic-tac-toe but it seems like my counter is way of it,f but I'm unsure where I've done wrong, I'm not gonna post all of the code since it's a lot, but here is a part:
public void countDiagonal(GameState gameState) {
/*
* yz-plane (negativ)
*/
int z;
for (int x = 0; x < GameState.BOARD_SIZE; x++) {
for (int y = 0; y < GameState.BOARD_SIZE; y++) {
z = y;
if (gameState.at(x, y, z) == myPlayer) {
myCounter++;
opponentCounter = 0;
}
if (gameState.at(x, y, z) == opponentPlayer) {
opponentCounter++;
myCounter = 0;
}
if (gameState.at(x, y, z) == Constants.CELL_EMPTY) {
emptyCells++;
}
}
evaluateBoard();
myCounter = 0;
opponentCounter = 0;
emptyCells = 0;
}
The evaluation is done here:
public void evaluateBoard() {
if (myCounter == 1 && emptyCells == 3) {
myScore = myScore + 5;
}
if (myCounter == 2 && emptyCells == 2) {
myScore = myScore + 10;
}
if (myCounter == 3 && emptyCells == 1) {
myScore = myScore + 20;
}
if (myCounter == 4) {
myScore = myScore + 1000;
}
if (opponentCounter == 1 && emptyCells == 3) {
opponentScore = opponentScore + 5;
}
if (opponentCounter == 2 && emptyCells == 2) {
opponentScore = opponentScore + 10;
}
if (opponentCounter == 3 && emptyCells == 1) {
opponentScore = opponentScore + 20;
}
if (opponentCounter == 4) {
opponentScore = opponentScore + 1000;
}
}
When I try to run it, I use alpha-beta prune, but it seems like the calculation are done horrbly wrong, when I use the value, I take myScore - opponentScore and I use an alpha-beta tree with depth 1, but even after only playing one move, I'm down -15 in points, as I'm a noob in java, im therefore asking for help, is there an obvious mistake in my way of trying to evaluate the board?

Flagging Buttons for Minesweeper GUI Game

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.

print 2d array as grid and randomly select one to be a chest then rename that value to be a C

I currently have it so it prints of a 2 by 2 grid and renames my position as P and everything else as - and it hides the chest until i step on it and ask's me if i want to open the chest then ends the game but i want it to rename the randomly generated chest to C so i can see on the grid where it is, Could someone please help me with this issue
/**
* Auto Generated Java Class.
*/
import java.util.*;
public class Adventure {
public static final int rows= 2;
public static String tile = "";
public static final int cols= 2;
public static String input = "";
public static boolean run = true;
public static String[][] map = new String[rows][cols];
public static int xpos = 0;
public static int ypos = 0;
public static Random gen = new Random();
public static final int xx = gen.nextInt(rows);
public static final int yy = gen.nextInt(cols);
public static void main(String[] args) {
for(int x = 0; x < rows; x++) {
for(int y = 0; y < cols; y++) {
map[x][y] = "you see nothing, on the fields of justice";
}
}
map[xx][yy] = "You find a chest: Open?";
while(run) {
displayMap();
System.out.println(map[xpos][ypos]);
input = userinput();
input = input.toLowerCase();
if(input.equals("w")) {
if (ypos < cols - 1) {
ypos++;
}else{
System.out.println("You're at the edge of the map");
}
}else if(input.equals("d")) {
if (xpos < rows - 1) {
xpos++;
}else{
System.out.println("You're at the edge of the map");
}
}else if(input.equals("s")) {
if (ypos > 0) {
ypos--;
}else{
System.out.println("You're at the edge of the map");
}
}else if(input.equals("a")) {
if (xpos > 0) {
xpos--;
}else{
System.out.println("You're at the edge of the map");
}
}else if(input.equals("open")) {
if(map[xpos][ypos].equals("You find a chest: Open?")){
System.out.println("You find a sword");
System.out.println("()==[:::::::::::::>");
run = false;
}else{
System.out.println("There's nothing to open");
}
}else {
System.out.println("Wrong direction, W , A, S , D");
}
}
}
public static String userinput(){
System.out.println("Which direction do you want to move: w , a ,s ,d ? ");
Scanner keyboard = new Scanner(System.in);
return keyboard.nextLine();
}
public static void displayMap() {
for (int y = cols - 1; y >= 0; y--) {
for (int x = 0; x < rows; x++) {
if (y == ypos && x == xpos) {
tile = tile + "P";
}
//Heres where i want to rename the chest to 'C' on the grid
else if (xx && yy) {
tile = tile + "C";
}
else{
tile = tile + "-";
}
}
System.out.println(tile);
tile = "";
}
}
}
Replace else if (xx && yy) with else if (y==yy && x==xx). It's an easy error to make.

Connect Four Win Check Not Working

So I have been working on my final term project which is a Connect Four game. I ran into this issue where i believe my CheckWin logic should work, put is providing me with no output when i run the program. I have my CheckWin() being called in my actionPerformed and I am stuck as to what to do here. As a side note, is there any way to easily increase the size of text inside of a JButton? I would like to increase the size of the "X's" and "O's". I am relatively new at programming so sorry if these are simple fixes. My code is below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Connect implements ActionListener {
private JFrame window = new JFrame();
private JPanel myPanel = new JPanel();
private JPanel myPanelB = new JPanel();
private JButton[][] myButtons = new JButton[6][7];
private JButton[] buttons = new JButton[7];
private boolean win = false;
private int count = 5;
private int count2 = 5;
private int count3 = 5;
private int count4 = 5;
private int count5 = 5;
private int count6 = 5;
private int count7 = 5;
private int countA = 0;
private String letter = "";
public boolean checkHorizontalWin(String letter) {
for (int y = 0; y < myButtons.length; y++) {
// going to length-3 to avoid IndexOutOfBounds exception
for (int x = 0; x < myButtons[y].length - 3; x++) {
if (myButtons[y][x].getText().equals(letter)
&& myButtons[y][x + 1].getText().equals(letter)
&& myButtons[y][x + 2].getText().equals(letter)
&& myButtons[y][x + 3].getText().equals(letter)
) {
return true;
}
}
}
return false;
}
public boolean checkVerticalWin(String letter) {
for (int y = 0; y < myButtons.length - 3; y++) {
for (int x = 0; x < myButtons[y].length; x++) {
if (myButtons[y][x].getText().equals(letter)
&& myButtons[y + 1][x].getText().equals(letter)
&& myButtons[y + 2][x].getText().equals(letter)
&& myButtons[y + 3][x].getText().equals(letter)
) {
return true;
}
}
}
return false;
}
public boolean checkDiagonalToTheLeftWin(String letter) {
for (int y = 0; y < myButtons.length - 3; y++) {
for (int x = 0; x < myButtons[y].length - 3; x++) {
if (myButtons[y][x].getText().equals(letter)
&& myButtons[y + 1][x + 1].getText().equals(letter)
&& myButtons[y + 2][x + 2].getText().equals(letter)
&& myButtons[y + 3][x + 3].getText().equals(letter)
) {
return true;
}
}
}
return false;
}
public boolean checkDiagonalToTheRightWin(String letter) {
for (int y = 0; y < myButtons.length - 3; y++) {
for (int x = 3; x < myButtons[y].length; x++) {
if (myButtons[y][x].getText().equals(letter)
&& myButtons[y + 1][x - 1].getText().equals(letter)
&& myButtons[y + 2][x - 2].getText().equals(letter)
&& myButtons[y + 3][x - 3].getText().equals(letter)
) {
return true;
}
}
}
return false;
}
public Connect(){
window.setSize(800,700);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPanel.setLayout(new GridLayout(1,7));
myPanelB.setLayout(new GridLayout(6,7));
for (int i = 0; i < buttons.length; i ++){
buttons[i] = new JButton();
myPanel.add(buttons[i]);
buttons[i].addActionListener(this);
}
for (int i = 0; i < 6; i ++){
for (int j = 0; j < 7; j ++){
myButtons[i][j] = new JButton();
myPanelB.add(myButtons[i][j]);
}
}
window.add(myPanel, BorderLayout.NORTH);
window.add(myPanelB, BorderLayout.CENTER);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e){
countA++;
if (countA % 2 == 0)
letter = "X";
else
letter = "O";
if (e.getSource() == buttons[0]){
myButtons[count][0].setText(letter);
count --;
}
if (e.getSource() == buttons[1]){
myButtons[count2][1].setText(letter);
count2 --;
}
if (e.getSource() == buttons[2]){
myButtons[count3][2].setText(letter);
count3--;
}
if (e.getSource() == buttons[3]){
myButtons[count4][3].setText(letter);
count4--;
}
if (e.getSource() == buttons[4]){
myButtons[count5][4].setText(letter);
count5--;
}
if (e.getSource() == buttons[5]){
myButtons[count6][5].setText(letter);
count6--;
}
if (e.getSource() == buttons[6]){
myButtons[count7][6].setText(letter);
count7--;
}
if (myButtons[0][0].getText().equals("O") || myButtons[0][0].getText().equals("X")){
buttons[0].setEnabled(false);
}
if (myButtons[0][1].getText().equals("O") || myButtons[0][1].getText().equals("X")){
buttons[1].setEnabled(false);
}
if (myButtons[0][2].getText().equals("O") || myButtons[0][2].getText().equals("X")){
buttons[2].setEnabled(false);
}
if (myButtons[0][3].getText().equals("O") || myButtons[0][3].getText().equals("X")){
buttons[3].setEnabled(false);
}
if (myButtons[0][4].getText().equals("O") || myButtons[0][4].getText().equals("X")){
buttons[4].setEnabled(false);
}
if (myButtons[0][5].getText().equals("O") || myButtons[0][5].getText().equals("X")){
buttons[5].setEnabled(false);
}
if (myButtons[0][6].getText().equals("O") || myButtons[0][6].getText().equals("X")){
buttons[6].setEnabled(false);
if (myButtons[0][6].getText().equals("O") || myButtons[0][6].getText().equals("X")){
buttons[6].setEnabled(false);
if (checkHorizontalWin(letter)
|| checkVerticalWin(letter)
|| checkDiagonalToTheLeftWin(letter)
|| checkDiagonalToTheRightWin(letter)
) {
win = true;
if (win == true) {
JOptionPane.showMessageDialog(null, letter + " has won!");
System.exit(0);
}
}
}
if(win == true){
JOptionPane.showMessageDialog(null, letter + " has won!");
System.exit(0);
} else if(count == 42 && win == false){
JOptionPane.showMessageDialog(null, "tie game");
System.exit(0);
}
}
}
public static void main(String[] args){
new Connect();
}
}
Identation helps.
if (myButtons[0][6].getText().equals("O") || myButtons[0][6].getText().equals("X"))
{
buttons[6].setEnabled(false);
// Missing brace on previous if
// checkXWin methods only invoked inside the above if-statement.
if (checkHorizontalWin(letter)
|| checkVerticalWin(letter)
|| checkDiagonalToTheLeftWin(letter)
|| checkDiagonalToTheRightWin(letter)
) {
win = true;
if (win == true) {
JOptionPane.showMessageDialog(null, letter + " has won!");
ystem.exit(0);
}
}
}
You haven't closed a brace in one of your if statements properly in the actionPerformed method. As a result, your program will only check for a win once the last column is full.
It also seems the condition if (myButtons[0][6].getText().equals("O") || myButtons[0][6].getText().equals("X") is repeated twice. That wouldn't be the cause of your issue but I think you might want to remove one of those statements.
Your brackets on your if statements are incorrectly placed.
if (myButtons[0][6].getText().equals("O") || myButtons[0][6].getText().equals("X")){
buttons[6].setEnabled(false);
if (myButtons[0][6].getText().equals("O") || myButtons[0][6].getText().equals("X")){
buttons[6].setEnabled(false);
On both of theses if statements you have opening brackets but the closing brackets do not come until the end of the actionPerformed method.
This means the code to check for a winner does not get executed unless both of these conditions resulted in true, which is not what you want and I'm pretty sure is impossible...

Images go away after minerals = 0

In my program you can place pictures on the applet if you have the required minerals
you start with 400 and each one is 100 minerals so you place 3 and then on the 4th it will delete all the others
public int Minerals
//Pylons
int xCoord[];
int yCoord[];
int numSquare;
boolean firstPaint;
public void init()
{
Minerals = 400;
pylon = getImage(getDocumentBase(),"image/pylon.png");
//pylons
xCoord = new int[100];
yCoord = new int[100];
numSquare = 0;
firstPaint = true;
}
public void paint(Grapics g)
{
pylons(g);
}
public void pylons(Graphics g)
{
//Building the pylons
if(Minerals >= 100)
{
for (int k = 0; k < numSquare; k++)
{
g.drawImage(pylon,xCoord[k],yCoord[k],85,85,this);
//Makes cursor normal.
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
//Checks if buildPylon is 1 if so it will draw the infoScreen and then set the cursor to pylon
if(buildPylon == 1)
{
g.drawImage(pylon,510,820,100,100,this);
//Use the custom cursor
setCursor(cursor);
}
}
private void handlePylonPlacement()
{
//This takes away 100 minerals and will add 9 population and make buildPylon 0 again
if(decrementMinerals(100))
addPopMax(9);
buildPylon = 0;
}
private boolean decrementMinerals(int amount)
{
//This Is where it takes away the minerals
if(Minerals - amount >= 0) // prevent situation where you go into negative minerals
{
Minerals -= amount;
return true;
}
else
return false;
}
private void addPopMax(int amount)
{
//Add the population (9)
if(popMax + amount <= 72) // restrict addition to pop-max to a sane upper bound
popMax += amount;
}
public boolean mouseDown(Event e, int x, int y) {
//Makes the ints xCoord2 to equal x and same for the other
xCoord2 = x;
yCoord2 = y;
repaint();
if (numClicks == 10) {//Title screen
numClicks++;
repaint();
}
//Checks to see if buildPylon == 1 then builds the pylon if its in the correct place.
if(buildPylon == 1)
{
if(x > 1 && x < 1275 && y > 711 && y < 948) // Checks to see if the person clicked in this area
{}
else if(x > 378 && x < 876 && y < 568 && y < 705) // Checks to see if the person clicked in this area
{}else if (Minerals >= 100)
{
xCoord[numSquare] = x;
yCoord[numSquare] = y;
numSquare++;
handlePylonPlacement(); // call your new handler
repaint();
}
}
return true;
}
Instead of it deleting them i want the pylons to stay painted on the screen... any help?
In handlePylonPlacement(), did you mean
private void handlePylonPlacement()
{
//This takes away 100 minerals and will add 9 population and make buildPylon 0 again
if (decrementMinerals(100)) {
addPopMax(9);
buildPylon = 0;
}
}
?

Categories