Only allow 2 Buttons to be pressed - java

I am creating a matching card application. That only allows the user to select two cards per round.
Where you see the bricks are where the buttons are and so i have added them to an arraylist, but im not sure how to make it so that they can only press 2 buttons until they press the guess button where it checks if they are a match.
https://pastebin.com/65NqJ0GK
private void card1ButtonActionPerformed(java.awt.event.ActionEvent evt) {
String temp = cards.get(0);
if (temp.equals("0")) {
card1Button.setIcon(a);
} else if (temp.equals("1")) {
card1Button.setIcon(b);
} else if (temp.equals("2")) {
card1Button.setIcon(c);
} else if (temp.equals("3")) {
card1Button.setIcon(d);
} else if (temp.equals("4")) {
card1Button.setIcon(e);
} else if (temp.equals("5")) {
card1Button.setIcon(f);
} else if (temp.equals("6")) {
card1Button.setIcon(g);
} else if (temp.equals("7")) {
card1Button.setIcon(h);
}
count++;
if (count == 1) {
c1 = Integer.parseInt(temp);
change[0] = 0;
} else if (count == 2) {
c2 = Integer.parseInt(temp);
change[0] = 0;
}
}
^^Here is an example of a button code.
Could someone show me some way to do it.

Related

Java maze won't print

I have to make a maze for a java assignment, and I was able to finish most of it. I was provided with an outline of the code that had all the methods. Can someone help me? My issue is that the maze wont print out, and I can't figure out why.
package maze;
public class Maze {
private char direction;
private int r; // x position of the mouse
private int c; //y position of the mouse
private boolean exitFound = false;
public Maze(int[][] arrMaze) {
this.r = arrMaze.length - 1;
this.c = 0;
}
//Prints out the maze without solution
public void displayMaze(int[][] arrMaze)
{
//display the maze putting blank spaces where there are 1's in the array and putting
//another symbol where there are 0's to show the maze without the solution
for(int i=0; i<arrMaze.length; i++){
System.out.println(" ");
for(int j=0; j<arrMaze[i].length; j++){
if(arrMaze[i][j] == 0){
System.out.print("#");
} if(arrMaze[i][j] == 1) {
System.out.print(" ");
} if(arrMaze[i][j] == 2){
System.out.print("#");
} if(arrMaze[i][j] == 3){
System.out.println("~");
}
}
}
}
//displays the Maze with the path taken
public void displayPath(int[][] arrMaze)
{
//show the user how far the mouse has gone since the start.
//The path the mouse has gone will be filled in but the path ahead will not.
for (int i = 0; i < arrMaze.length; i++) {
System.out.println(" ");
for (int j = 0; j < arrMaze[i].length; j++) {
if (arrMaze[i][j] == 3) {
System.out.print("#");
} else if (arrMaze[i][j] == 2) {
System.out.print("~");
} else if (arrMaze[i][j] == 0) {
System.out.print("#");
} else {
}
}
}
}
public boolean takeStep(int[][] newMaze) {
// moveNorth(newMaze)
for (int i = 0; i < newMaze.length; i++) {
System.out.println(" ");
for (int j = 0; j < newMaze[i].length; j++) {
if (newMaze[r][c] == 3) {
moveNorth(newMaze);
System.out.print("~");
} else if (newMaze[r][c] == 2) {
System.out.print("#");
} else {
}
}
}
return isAnExit(newMaze);
}
public void moveNorth(int[][] arrMaze) {
//complete the code here
/*method will check for a 0 or a 1 in the position above the current position
* and then if not a 0 will change the current position to the row above it, but in the same column.
*/
if (arrMaze[r][c - 1] != 0) {
arrMaze[r][c - 1] = 3;
arrMaze[r][c + 1] = 2;
} else {
moveSouth(arrMaze);
}
displayPath(arrMaze);
}
public void moveSouth(int[][] arrMaze)
{
//method will check for a 0 or a 1 in the position below the current position and then if not a 0
//it will change the current position to the row below it, but in the same column.
if (arrMaze[r][c + 1] != 0) {
arrMaze[r][c + 1] = 3;
arrMaze[r][c + 1] = 2;
} else {
moveNorth(arrMaze);
}
displayPath(arrMaze);
}
public void moveEast(int[][] arrMaze) {
//method will check for a 0 or a 1 in the position to the right of  the current position and then if
//not a 0 will change the current position to the column to the right but the same row.
if (arrMaze[r + 1][c] != 0) {
arrMaze[r + 1][c] = 3;
arrMaze[r - 1][c] = 2;
} else {
moveWest(arrMaze);
}
displayPath(arrMaze);
}
public void moveWest(int[][] arrMaze) {
//method will check for a 0 or a 1 in the position to the left of  the current position and then if
//not a 0 will change the current position to the column to the left but the same row.
if (arrMaze[r - 1][c] != 0) {
arrMaze[r - 1][c] = 3;
arrMaze[r + 1][c] = 2;
} else {
}
displayPath(arrMaze);
}
private boolean isAnExit(int[][] arrMaze) {
//method will return true if the user arrives into the last column of the array because there is only one
//location in the last column that is a 1, so if the user reaches the array[i].length then that means that it found an exit.
if (arrMaze[r][c] > arrMaze.length) {
exitFound = true;
} else {
exitFound = false;
}
return exitFound;
}
//finds the path without stopping at every step
//method will show the complete path from start to finish of the maze and the suggested route to the end.
public void findExit(int[][] arrMaze) {
if (arrMaze[r][c] > arrMaze.length) {
for (int i = 0; i < arrMaze.length; i++) {
takeStep(arrMaze);
}
}
}
}
This is the test code. I was provided the test code, and I haven't changed it.
package maze;
import java.util.Scanner;
public class TestMaze
{
public static void main(String[] args)
{
int[][] mazeArray = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
{0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1},
{0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0},
{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
Maze myMaze = new Maze(mazeArray);
boolean keepAsking = true;
Scanner scan = new Scanner(System.in);
String input = "";
myMaze.displayPath(mazeArray);
System.out.println("Maze");
do {
System.out.println("T = Take a step | S = Show path | Q = Quit");
System.out.print("Enter command: ");
input = scan.nextLine();
input.trim();
input.toLowerCase();
if(input.equals("t")) {
keepAsking = !myMaze.takeStep(mazeArray);
System.out.println("Which direction would you like to go? N, S, E, W?");
String direction = scan.nextLine();
if(direction.equalsIgnoreCase("n"))
myMaze.moveNorth(mazeArray);
if(direction.equalsIgnoreCase("s"))
myMaze.moveSouth(mazeArray);
if(direction.equalsIgnoreCase("e"))
myMaze.moveEast(mazeArray);
if(direction.equalsIgnoreCase("w"))
myMaze.moveWest(mazeArray);
}
else if(input.equals("s")) {
myMaze.findExit(mazeArray);
keepAsking = false;
}
else if(input.equals("q")) {
keepAsking = false;
}
else {
System.out.println("ERR: Invalid input");
}
} while(keepAsking);
System.out.println("Quitting program...");
scan.close();
}
}
You need to call displayMaze() (in displayPath()) to print it.
Currently, you're not calling the method, meaning that your code will never print the maze as it's no being instructed to.
Also, where are you assigning values to r and c? I think you meant to use i and j in your displayPath() method ([i][j] instead of [r][c]).
I imagine you're throwing an error somewhere because your r and c values are undefined everywhere they are used. Try adding code to initialize and update these values, then you should see your maze print.

Connect 4 right to left diagonal check

There's probably a very simple solution to this but I can't figure out where I'm doing something wrong. I'm making a small game with android studio that is connect 4. There is a 5x7 matrix of single cells, and 5 imageviews above that when clicked on put a fiche in the right place. Up to this point it's all working fine. When I however have to check if someone won, I thought I could break up the process in 4 main functions: one that check horizontally, one vertically, one diagonally left to right and one diagonally right to left. Now they all work perfectly except the right to left one. I'll post the code below:
private void checkRightLeftDiagonally() {
int winCondition = 0;
boolean goingRight = true;
int y = 1;
int i = 4;
int j = 0;
while (y < 6 && won == false) {
while (i > 0 && j < 7 && won == false) {
if (cells[j][i].getFull() == true && players[playerTurn].getFicheColor() == cells[j][i].getFicheColor()) {
winCondition++;
winningCells.add(cells[j][i]);
} else {
winCondition = 0;
winningCells.clear();
}
if (winCondition == 4) {
won = true;
for (int x = 0; x < 4; x++) {
winningCells.get(x).won();
}
}
i--;
j++;
}
if(goingRight == true)
{
if(y<=4)
{
i=4-y;
j=0;
y++;
}
else
{
goingRight = false;
y=0;
i=0;
j=0+y;
}
}
if(goingRight == false)
{
i=0;
j=0+y;
y++;
}
if(won == false)
{
winCondition = 0;
winningCells.clear();
}
}
if(won == false) {
winCondition = 0;
winningCells.clear();
}
}
And here is one of the arrow imageview code:
imgArrows[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(cells[0][0].getFull() == false && won == false)
{
int i = 0;
while(cells[i][0].getFull() == false)
{
i++;
if(i>6) break;
}
i--;
cells[i][0].ficheDown(players[playerTurn]);
checkVertically();
checkHorizantally();
checkLeftRightDiagonally();
checkRightLeftDiagonally();
playerTurn++;
if(playerTurn==2)
{
playerTurn = 0;
}
}
}
});
I've also made the cell class, which is here if it could help you
public class Cell {
private boolean full;
private Player.FicheColor ficheColor;
private ImageView fiche;
public Cell(Player currentPlayer, ImageView img)
{
full = false;
ficheColor = currentPlayer.getFicheColor();
fiche = img;
img.setAlpha(0f);
}
public void ficheDown(Player currentPlayer)
{
full = true;
ficheColor = currentPlayer.getFicheColor();
switch(ficheColor)
{
case red:
fiche.setImageResource(R.drawable.redfiche);
break;
case blue:
fiche.setImageResource(R.drawable.bluefiche);
break;
case green:
fiche.setImageResource(R.drawable.greenfiche);
break;
case white:
fiche.setImageResource(R.drawable.whitefiche);
break;
case black:
fiche.setImageResource(R.drawable.whitefiche);
break;
}
fiche.setAlpha(1f);
}
public Player.FicheColor getFicheColor()
{
return ficheColor;
}
public boolean getFull()
{
return full;
}
public void won(){
fiche.setColorFilter(Color.GREEN);
}
public void reset()
{
fiche.clearColorFilter();
}
}
Thank a lot, even just for reading
In the end the problem was that in the first paragraph of code the int i needed to be set to 4 and that solved it. Thanks to everyone that tried to help me

How do I stop getting more than one request for an input?

I'm new to this website. Every time I run this program I made in eclipse, I get more than one request for an input. It's a neat little game I think. Here is the code.
import java.util.*;
public class Fight extends Attacker {
public static void main(String[] args) {
Scanner defend = new Scanner(System.in);
String response;
int blocks = 0, possibleblocks = 0;
System.out.println("Commence mental training. Enter \"Done.\" to finish. Ready?");
response = defend.nextLine();
while (response.equals(response) != response.equals("Done.")) {
System.out.println(Hit());
if (Hit() == "Left jab.") {
response = defend.nextLine();
if (response.contains("Right block.")) {
blocks++;
} else {
System.out.println("Wrong.");
}
} else if (Hit() == "Right jab.") {
response = defend.nextLine();
if (response.contains("Left block.")) {
blocks++;
} else {
System.out.println("Wrong.");
}
} else if (Hit() == "Left side punch.") {
response = defend.nextLine();
if (response.contains("Right side block.")) {
blocks++;
} else {
System.out.println("Wrong.");
}
} else if (Hit() == "Right side punch.") {
response = defend.nextLine();
if (response.contains("Left side block.")) {
blocks++;
} else {
System.out.println("Wrong.");
}
} else if (Hit() == "Left uppercut.") {
response = defend.nextLine();
if (response.contains("Step back.")) {
blocks++;
} else {
System.out.println("Wrong. You should have stepped back.");
}
} else if (Hit() == "Right uppercut.") {
response = defend.nextLine();
if (response.contains("Step back.")) {
blocks++;
} else {
System.out.println("Wrong. You should have stepped back.");
}
} else if (Hit() == "Lower right jab.") {
response = defend.nextLine();
if (response.contains("Step back.")) {
blocks++;
} else {
System.out.println("Wrong. You should have stepped back.");
}
}
possibleblocks++;
}
possibleblocks -= 1;
double accuracy = ((double) blocks / (double) possibleblocks) * 100;
System.out.println(blocks + " / " + possibleblocks);
System.out.println("Your total accuracy is " + accuracy + "%.");
defend.close();
}
}
So, this is all in Java. In a gist, it gets an attack kind from the method you'll see next, prints it out, takes in input, checks to see if the input contains the correct blocking type, and calculates accuracy on how well you've blocked over how many times you could've blocked. The following is the class and method that gives the randomizer for the attacking type.
public class Attacker {
public static String Hit() {
int hit = 0 + (int) (Math.random() * 7);
if (hit == 0) {
return ("Left jab.");
} else if (hit == 1) {
return ("Right jab.");
} else if (hit == 2) {
return ("Left side punch.");
} else if (hit == 3) {
return ("Right side punch.");
} else if (hit == 4) {
return ("Left uppercut.");
} else if (hit == 5) {
return ("Right uppercut.");
} else if (hit == 6) {
return ("Lower right jab.");
} else {
return ("On the defensive.");
}
}
}
That creates a randomizer and creates the attack kinds. How can I make it so that it doesn't print out multiple attack types at the same time which then clogs up everything? Thank you.

javafx different actions on one button

I am making a blackjack game and was wondering if I could make it so that I had two different actions on a single button.
This is what I have so far on the hit button, but instead of having the second card show on a double click, I want it to show the second card when you press the button again.
public void hit(MouseEvent event) {
if (event.getClickCount() == 1) {
card5 = deck.dealCard();
pcard3.setImage(card5.getImage());
}
if (event.getClickCount() == 2) {
card6 = deck.dealCard();
pcard4.setImage(card6.getImage());
}
}
You can have an iterator whose value can be increased on each click. And for different values set different functionalities. See the code
int i =0;
public void hit(MouseEvent event) {
if (i%2== 0) {
card5 = deck.dealCard();
pcard3.setImage(card5.getImage());
} else if (i%2 == 1) {
card6 = deck.dealCard();
pcard4.setImage(card6.getImage());
}
i++;
}

Get input from button and validate if its correct or wrong

I'm creating an ATM machine with pin function. The problem is I want to get the input from the button when its pressed by the user and validate if its correct or wrong. When the button is pressed, it will store the result in the string. It will then be used to validate if its correct or wrong. For example: User A pressed 012345. Each number will then be stored to another string for validation. The string is then compared to the pin.
public class atmMachine:
int numberPinButton = 10;
String pin = "012345";
String zero, one, two, three, four, five, six, seven, eight, nine;
public atmMachine:
panel = new JPanel();
pinButton = new JButton[numberPinButton];
for(int i = 0; i < numberPinButton; i++) {
pinButton[i] = new JButton("" + i);
pinButton[i].addActionListener(this);
panel.add(pinButton[i]);
}
enterButton = new JButton("Enter");
enterButton.addActionListener(this);
panel.add(enterBtn);
panel.setBorder(BorderFactory.createTitledBorder("Enter your pin:"));
add(panel, BorderLayout.WEST);
public void actionPerformed:
public void actionPerformed(ActionEvent event) {
if(event.getSource() == pinButton[0]) {
zero = "0";
} else if(e.getSource() == pinButton[1]) {
one = "1";
} else if(e.getSource() == pinButton[2]) {
two = "2";
} else if(e.getSource() == pinButton[3]) {
three = "3";
} else if(e.getSource() == pinButton[4]) {
four = "4";;
} else if(e.getSource() == pinButton[5]) {
five = "5";
} else if(e.getSource() == pinButton[6]) {
six = "6";
} else if(e.getSource() == pinButton[7]) {
seven = "7";
} else if(e.getSource() == pinButton[8]) {
eight = "8";
} else if(e.getSource() == pinButton[9]) {
nine = "9";
}
if(e.getSource() == enterBtn) {
if(???.equals(pin)) {
System.out.println("Correct");
} else {
System.out.println("Wrong");
}
}
}
Have an instance variable-
StringBuffer userKeyString = new StringBuffer();
On action performed, append any digit button pressed-
userKeyString.append(event.getActionCommand());
On action performed, if enter pressed-
if(event.getSource() == enterBtn){
if(pin.equals(userKeyString.toString()){
// Correct pin
} else {
// Incorrect pin
}
userKeyString.setLength(0); // Clear the buffer for next input and validation
} else {
userKeyString.append(event.getActionCommand());
}
You should set the action commands of your buttons-
for(int i = 0; i < numberPinButton; i++) {
pinButton[i] = new JButton("" + i);
pinButton[i].setActionCommand(String.valueOf(i));
pinButton[i].addActionListener(this);
panel.add(pinButton[i]);
}
You can declare String pin as a field like:
String pin;
then in action performed you append to it (pseudo code)
actionPerformed(...){
pin += keyPressed;
if(pin.length > 5){
validatePin(pin);
}
}
Does this answer your question?
But to be honest I would suggest against validation in the action performed method. I'd try to validate the pin after all the numbers were inserted. This way you can save a bit on performance and the code would be more readable. The performance isn't an issue here but it's a good practice as you would check the pin 6 times here instead of just once outside the actionPerformed method.
Like:
for(int i = 0; i < pinLength; i++){
waitForUserToPressButtonOrTimeout();
appendToCurrentPin();
}
validatePin();
Or let the user simply press another button after entering the pin code and link validation to that button. But I think using String pin field and appending to it is an answer to your question.

Categories

Resources