I'm writing a tic-tac-toe game for my Java, I really suck at this so far, but got it to work with the examples he gave us in class. The problem I'm having now is that I realized wants us to have at least TWO classes for this program. I have no idea what he means by that or how I convert the code I've already put together into "Two Classes". From the instructions it looks like he wants the board in one class and the game in another class.
Is there a way to split this up into two classes without totally re-writing the whole thing?
/* Standard applet template
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToeGame implements ActionListener {
/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe Game");
private JButton btn1 = new JButton("");
private JButton btn2 = new JButton("");
private JButton btn3 = new JButton("");
private JButton btn4 = new JButton("");
private JButton btn5 = new JButton("");
private JButton btn6 = new JButton("");
private JButton btn7 = new JButton("");
private JButton btn8 = new JButton("");
private JButton btn9 = new JButton("");
private JLabel lblTitle = new JLabel("Tic Tac Toe Game");
private JLabel lblBlank = new JLabel(" ");
private String letter = "";
private int count = 0;
private boolean win = false;
public TicTacToeGame(){
/*Create Window*/
window.setSize(400,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
/*Add Buttons To The Window*/
window.add(btn1);
window.add(btn2);
window.add(btn3);
window.add(btn4);
window.add(btn5);
window.add(btn6);
window.add(btn7);
window.add(btn8);
window.add(btn9);
/*Add The Action Listener To The Buttons*/
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
/*Make The Window Visible*/
window.setVisible(true);
}
public void actionPerformed(ActionEvent a) {
count++;
/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
letter = "<HTML><font color=blue>X</font></HTML>";
} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
letter = "<HTML><font color=red>O</font></HTML>";
}
/*Display X's or O's on the buttons*/
if(a.getSource() == btn1){
btn1.setText(letter);
btn1.setEnabled(false);
} else if(a.getSource() == btn2){
btn2.setText(letter);
btn2.setEnabled(false);
} else if(a.getSource() == btn3){
btn3.setText(letter);
btn3.setEnabled(false);
} else if(a.getSource() == btn4){
btn4.setText(letter);
btn4.setEnabled(false);
} else if(a.getSource() == btn5){
btn5.setText(letter);
btn5.setEnabled(false);
} else if(a.getSource() == btn6){
btn6.setText(letter);
btn6.setEnabled(false);
} else if(a.getSource() == btn7){
btn7.setText(letter);
btn7.setEnabled(false);
} else if(a.getSource() == btn8){
btn8.setText(letter);
btn8.setEnabled(false);
} else if(a.getSource() == btn9){
btn9.setText(letter);
btn9.setEnabled(false);
}
/*Checks to See Who Won*/
//horizontal win
if( btn1.getText() == btn2.getText() && btn2.getText() == btn3.getText() && btn1.getText() != ""){
win = true;
}
else if(btn4.getText() == btn5.getText() && btn5.getText() == btn6.getText() && btn4.getText() != ""){
win = true;
}
else if(btn7.getText() == btn8.getText() && btn8.getText() == btn9.getText() && btn7.getText() != ""){
win = true;
}
//virticle win
else if(btn1.getText() == btn4.getText() && btn4.getText() == btn7.getText() && btn1.getText() != ""){
win = true;
}
else if(btn2.getText() == btn5.getText() && btn5.getText() == btn8.getText() && btn2.getText() != ""){
win = true;
}
else if(btn3.getText() == btn6.getText() && btn6.getText() == btn9.getText() && btn3.getText() != ""){
win = true;
}
//diagonal wins
else if(btn1.getText() == btn5.getText() && btn5.getText() == btn9.getText() && btn1.getText() != ""){
win = true;
}
else if(btn3.getText() == btn5.getText() && btn5.getText() == btn7.getText() && btn3.getText() != ""){
win = true;
}
else {
win = false;
}
/*Show a dialog if someone wins or the game is tie*/
if(win == true){
JOptionPane.showMessageDialog(null, letter + " WINS!");
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "Tie Game!");
}
}
public static void main(String[] args){
new TicTacToeGame();
}
}
There are multiple ways to split this class, but since it's not very big, most of them would end up rewriting much of it.
The cleanest way would be to split logic and UI into (at least) two separate classes:
Class TicTacToeGame would implement the logic: it has a method to query who's turn it is, some methods to query the state of the playing field, a method to enter the next move, a method to query the state of the game (playing, end), a method to query who has won and probably a way to register some listeners
Class TicTacToeUI just takes the information of an instance of the other class and displays it. It also calls the appropriate methods when one button is clicked.
The net effect will probably be more code (since the interaction between the two classes will need some code), but the code will be much cleaner and the game class can be tested independently of the UI.
Simple thing you can do is Make second class that implements ActionListener and then use that class to listen to all action events in your program. This is not the way you want that is one for board and one for game but this can be one for operation and one for presentation.
Joachim gave a nice answer. I just want to add that instead of doing this:
/*Display X's or O's on the buttons*/
if(a.getSource() == btn1){
btn1.setText(letter);
btn1.setEnabled(false);
...
} else if(a.getSource() == btn9){
btn9.setText(letter);
btn9.setEnabled(false);
}
You can do this:
JButton btn = (JButton) a.getSource();
btn.setText(letter);
btn.setEnabled(false);
I think your comments are a good hint as to logical splits. Put each section into a separate method so that your actionPerformed method would be something like the following.
public void actionPerformed(ActionEvent a) {
count++;
letter = calculateCurrentPlayer(count);
displayLetterOnButton(a.getSource());
win = checkForWin();
displayGameOver(win, count);
}
Each of the methods called from actionPerformed would then have a small tightly-defined unit of work to perform. These methods could pretty much be copy-and-paste from the sections of your current actionsPerformed method.
Related
I made a GUI program where it counts certain elements of whatever is entered in the main text field. If the text field is empty, a message should pop up saying that the user should enter text in the text field. I made an if statement if tfMain == null, a JOptionPane message should pop-up, but for some reason it won't. Any tips on why it doesn't pop up?
here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class LabExcer9 extends WindowAdapter implements ActionListener
{
//Container
private Frame f;
private Panel p1,p2,p3;
//Component
private Button bReadAndComp, bClear;
private TextField tfMain,tf1,tf2,tf3,tf4,tf5,tf6;
private Label l1,l2,l3,l4,l5,l6;
public LabExcer9()
{
f = new Frame("Character Counter of Miguel Martin");
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
bReadAndComp = new Button("Read and Compute");
bClear = new Button("Clear ALL Values");
tfMain = new TextField(null);
tf1 = new TextField("0");
tf2 = new TextField("0");
tf3 = new TextField("0");
tf4 = new TextField("0");
tf5 = new TextField("0");
tf6 = new TextField("0");
l1 = new Label("Number of Words ");
l2 = new Label("Number of Characters ");
l3 = new Label("Number of Vowels ");
l4 = new Label("Number of Consonants ");
l5 = new Label("Number of Digits ");
l6 = new Label("Number of Symbols and Spaces ");
}
public void assembleGUI()
{
p1.setLayout(new GridLayout(1,1));
p1.setPreferredSize(new Dimension(200, 200));
p1.add(tfMain);
p2.setLayout(new GridLayout(6,2));
p2.add(l1);
p2.add(tf1);
p2.add(l2);
p2.add(tf2);
p2.add(l3);
p2.add(tf3);
p2.add(l4);
p2.add(tf4);
p2.add(l5);
p2.add(tf5);
p2.add(l6);
p2.add(tf6);
p3.setLayout(new GridLayout(1,2));
p3.add(bReadAndComp);
p3.add(bClear);
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
f.add(p3,BorderLayout.SOUTH);
f.pack();
f.addWindowListener(this);
f.setVisible(true);
//registers
bReadAndComp.addActionListener(this);
bClear.addActionListener(this);
}
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
//gets main text
String textString = tfMain.getText();
//puts words into array
int vowels = 0, consonants = 0, digits = 0, symbolsAndSpaces = 0;
int characters = textString.length();
if(tfMain.getText() == "" || tfMain.getText() == null )
{
if(source == bReadAndComp)
JOptionPane.showMessageDialog(null, "Please Enter Text!");
}
else if(tfMain.getText() != null && tfMain.getText() != "")
{
if(source == bReadAndComp)
{
for(int i = 0;i<textString.length();i++)
{
if(Character.isDigit(textString.charAt(i)))
digits++;
if(Character.isLetterOrDigit(textString.charAt(i)) == false)
symbolsAndSpaces++;
if(isVowel(textString.charAt(i)) == true)
vowels++;
else if(Character.isDigit(textString.charAt(i)) == false && isVowel(textString.charAt(i)) == false && textString.charAt(i) != ' ' && Character.isLetter(textString.charAt(i)) == true)
consonants++;
}
tf1.setText(""+textString.split(" ").length);
tf2.setText(""+characters);
tf3.setText(""+vowels);
tf4.setText(""+consonants);
tf5.setText(""+digits);
tf6.setText(""+symbolsAndSpaces);
//System.out.println(textStringArray[0]+"wat");
//JOptionPane.showMessageDialog(null, "Please Enter Text!");
}
else if (source == bClear)
{
tf1.setText("0");
tf2.setText("0");
tf3.setText("0");
tf4.setText("0");
tf5.setText("0");
tf6.setText("0");
tfMain.setText(null);
}
}
}
public boolean isVowel(char c)
{
if (c == 'a' || c == 'A' ||c== 'e' ||c == 'E' ||c == 'i' ||c == 'I' ||c == 'o' ||c == 'O' ||c == 'u' ||c == 'U')
return true;
else
return false;
}
public static void main(String args [])
{
LabExcer9 GUI = new LabExcer9();
GUI.assembleGUI();
}
}
To compare string please use equals or equalsIgnoreCase.
Replace your
if(tfMain.getText() == "" || tfMain.getText() == null )
statement with the following:
if("".equals(tfMain.getText())){
}
I am making a Tic-Tac-Toe game in Java. I have four classes:
TicTacTester just calls (creates an object) the TicTacToe class.
The TicTacToe class provides the GUI of the game (it's a subclass of JFrame). It also creates the 9 buttons for the JPanel to display and users to click.
The XOButton class defines what the buttons can do and actionPerformed method.
Lastly, the GameEnd class defines what happens when the game ends (a new JFrame is created to display score and give the user 2 buttons: exit and restart).
The problem is when I try to code the contents of what happens when the user clicks "restart". It is suppose to call the resetBoard() method, which is defined in TicTacToe class. The problem is, I do not know the name of the object created from the TicTacToe class (in the tester class' static void main method I just typed "new TicTacToe", didn't need to define a name). I cannot call resetBoard from a static standpoint (i.e. I can't do TicTacToe.resetBoard(); ) because resetBoard needs to be non-static.
What I've tried:
I've tried including in the constructor of the GameEnd class a TicTacToe object. If I do this, the GameEnd object creator has to go into TicTacToe class, so I can use 'this' keyword. This does not work because the GameEnd object needs to be created when the WinCondition is met, which is checked when a button is clicked in the XOButton class.
But if I put the GameEnd object creator in the XOButton class (where is is right now and, supposedly, where it should be), in the constructor for GameEnd(String s, TicTacToe a), I cannot use 'this' keyword for the TicTacToe object.
This is my button class. Most code is not relevant so it has been hidden.
public class XOButton extends JButton implements ActionListener {
//Hidden code
private void winCheck() {
for(int j = 0; j < 3; j++) {
if(board[j][0] == 1 && board[j][1] == 1 && board[j][2] == 1 || board[0][j] == 1 && board[1][j] == 1 && board[2][j] == 1) {
player1Score++;
GameEnd end = new GameEnd("X wins this round!");
finished = true;
break;
}
else if(board[j][0] == 2 && board[j][1] == 2 && board[j][2] == 2 || board[0][j] == 2 && board[1][j] == 2 && board[2][j] == 2) {
player2Score++;
GameEnd end = new GameEnd("O wins this round!");
finished = true;
break;
}
}
if(board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1 || board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1) {
player1Score++;
GameEnd end = new GameEnd("X wins this round!");
finished = true;
}
else if(board[0][0] == 2 && board[1][1] == 2 && board[2][2] == 2 || board[0][2] == 2 && board[1][1] == 2 && board[2][0] == 2) {
player2Score++;
GameEnd end = new GameEnd("O wins this round!");
finished = true;
}
if(turn == 9 && !finished) {
GameEnd end = new GameEnd("This round is a Draw");
finished = true;
}
}
public void resetButton() {
this.setIcon(null);
markSpot(0);
this.clicked = false;
}
public static void resetStatics() {
turn = 0;
finished = false;
}
}
This is the TicTacToe class. Most code is not relevant so it has been hidden.
public class TicTacToe extends JFrame {
//Hidden code
public void resetBoard() {
for(int j = 0; j < 3; j++) {
for(int i = 0; i < 3; i++) {
buttons[j][i].resetButton();
}
}
XOButton.resetStatics();
}
}
This is the GameEnd class. An object of this is created when the WinCondition is met. Most code is not relevant so it has been hidden.
public class GameEnd extends JFrame implements ActionListener {
//Hidden code
public void actionPerformed(ActionEvent e) {
if(e.getSource() == exit) {
System.exit(0);
}
else if(e.getSource() == retry) {
TicTacToe.resetBoard();
}
}
}
//This is the Tester class
public class TicTacTester {
public static void main(String[] args) {
new TicTacToe();
}
}
What I expect to happen is the game board to restart.
It seems like you are breaking the project down too far. If you have a class for a game, keep all of its methods inside that class. The only time I can think of to use separate classes are if you are trying to create this using the Model-View-Controller (MVC) architecture, or the like. This is a common approach for an application using GUI, data access, and controllers.
There might be other times, but this is all I can think of on the top of my head.
From what you are showing, I don't think it applies.
I think this would be a better approach:
TicTacToe.java
public class TicTacToe extends JFrame {
private int scorePlayer1;
private int scorePlayer2;
private boolean initialized; // default = false
// Reset the board
// Reset the markers, etc.
public void resetGame() {}
// Check to see if there is a winner
// If so, announce the winner and return true
// Otherwise, return false.
public boolean winCheck() {}
// Set up frame, buttons, score, etc.
public void newGame(){
// Create however many JPanels you need
// Add it to a single JFrame
// When you're ready...
// and this game has not been initialized for the first time.
// In this example jframe is an instance of JFrame
if (!this.initialized)
jframe.setVisible(true);
}
// Play the game
public void play(){
// For each round,
// a player chooses where to put their "X" or "O"
// add scores, etc.
// Then at the end of each round
if (this.winCheck())
this.resetGame();
}
}
TicTacToeTester.java:
public class TicTacToeTester {
public static void main(String[] args){
TicTacToe game = new TicTacToe();
game.play();
}
}
I hope this helps!
Let me know if you have any questions. :)
I wrote a fairly simple program using a GUI template to learn about adding some form of GUI in java. I completed the program only to discover that it cant be run outside of the IDE without a main method. The program works fine inside Eclipse IDE, but useless otherwise. How do I go about adding a main class so it can be executed as a .jar?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JApplet implements ActionListener {
// Create all labels, textfields and buttons needed (in order)
/**
* This is a tic tac toe game made with the purposes of learning java
*/
private static final long serialVersionUID = 1L;
JButton firstButton;
JButton secondButton;
JButton thirdButton;
JButton fourthButton;
JButton fithButton;
JButton sixthButton;
JButton seventhButton;
JButton eighthButton;
JButton ninthButton;
//this is the position value checker which are later set to x and o
String[] posCheck = { "", "", "", "", "", "", "", "", "" };
// score count JLABELs to display the score inside the game pane.
JLabel scoreP1;
JLabel scoreP2;
// this is used for formatting purposes or something like that, I guess.
JLabel blank;
// score count variables for both players, they both start at 0.
int scoreCount1 = 0, scoreCount2 = 0;
int gamesPlayed = -1;
boolean gameDone = false;
int k;
String prevWinner = "";
// Create any global variables/arrays needed later in the program (final)
Container pane = getContentPane();
//this sets the tile to X or O depending on who's turn it is
String[] symbol = { "O", "X" };
int turnCounter = 0;
int tieChecker = 0;
// Sets up GUI components.
public void init() {
pane.setLayout(new GridLayout(4, 3));
setSize(500, 500);
// Adds jlabel for scores
scoreP1 = new JLabel();
scoreP1.setText("Player (X) score: " + String.valueOf(scoreCount1));
scoreP2 = new JLabel();
scoreP2.setText("Player (O) score: " + String.valueOf(scoreCount2));
blank = new JLabel();
blank.setText(prevWinner);
// these are the JButtons that go in the game panel along with action
// listeners
firstButton = new JButton();
firstButton.addActionListener(this);
secondButton = new JButton();
secondButton.addActionListener(this);
thirdButton = new JButton();
thirdButton.addActionListener(this);
fourthButton = new JButton();
fourthButton.addActionListener(this);
fithButton = new JButton();
fithButton.addActionListener(this);
sixthButton = new JButton();
sixthButton.addActionListener(this);
seventhButton = new JButton();
seventhButton.addActionListener(this);
eighthButton = new JButton();
eighthButton.addActionListener(this);
ninthButton = new JButton();
ninthButton.addActionListener(this);
pane.add(firstButton, 0); // second parameter is the index on pane
pane.add(secondButton, 1);
pane.add(thirdButton, 2);
pane.add(fourthButton, 3);
pane.add(fithButton, 4);
pane.add(sixthButton, 5);
pane.add(seventhButton, 6);
pane.add(eighthButton, 7);
pane.add(ninthButton, 8);
pane.add(scoreP1);
pane.add(blank);
pane.add(scoreP2);
gamesPlayed++;
setContentPane(pane);
} // init method
// checks for mouse clicks here.
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
if (e.getSource() == firstButton)
revealButton(firstButton, 0);
else if (e.getSource() == secondButton)
revealButton(secondButton, 1);
else if (e.getSource() == thirdButton)
revealButton(thirdButton, 2);
else if (e.getSource() == fourthButton)
revealButton(fourthButton, 3);
else if (e.getSource() == fithButton)
revealButton(fithButton, 4);
else if (e.getSource() == sixthButton)
revealButton(sixthButton, 5);
else if (e.getSource() == seventhButton)
revealButton(seventhButton, 6);
else if (e.getSource() == eighthButton)
revealButton(eighthButton, 7);
else if (e.getSource() == ninthButton)
revealButton(ninthButton, 8);
}
} // actionPerformed method
// respond to button pushed
public void revealButton(JButton button, int index) {
// removes the indicated button from pane
pane.remove(button);
// creates a new text field to take the place of the button, makes it
// uneditable and sets background colour
JTextField textField = new JTextField();
textField.setEditable(false);
textField.setBackground(Color.WHITE);
// sets the alignment for the text in the field
textField.setHorizontalAlignment(SwingConstants.CENTER);
// adds the new textfield to the pane at the location of the old button
pane.add(textField, index);
posCheck[index] = symbol[(turnCounter % 2)];
prevWinner = "Player (" + symbol[(turnCounter % 2)]
+ ") is the winner!";
// re-creates pane with new information
setContentPane(pane);
// this sets the text field to either X or O depending who placed last.
textField.setText(symbol[(turnCounter) % 2]);
button.setEnabled(false);
// this is a counter to check if it is a cats game.
tieChecker++;
// check for winner X here
if (((posCheck[0] == "X") && (posCheck[1] == "X") && (posCheck[2] == "X"))
|| ((posCheck[3] == "X") && (posCheck[4] == "X") && (posCheck[5] == "X"))
|| ((posCheck[6] == "X") && (posCheck[7] == "X") && (posCheck[8] == "X"))
|| ((posCheck[0] == "X") && (posCheck[3] == "X") && (posCheck[6] == "X"))
|| ((posCheck[1] == "X") && (posCheck[4] == "X") && (posCheck[7] == "X"))
|| ((posCheck[2] == "X") && (posCheck[5] == "X") && (posCheck[8] == "X"))
|| ((posCheck[0] == "X") && (posCheck[4] == "X") && (posCheck[8] == "X"))
|| ((posCheck[2] == "X") && (posCheck[4] == "X") && (posCheck[6] == "X"))) {
// this part updates the winner score then refreshes the text field
// to reflect the change
scoreCount1++;
scoreP1.setText("Player (X) score: " + String.valueOf(scoreCount1));
blank.setText("Player (X) Is the winner.");
gameDone = true;
turnCounter++;
}
// this checks if O has won the game.
else if (((posCheck[0] == "O") && (posCheck[1] == "O") && (posCheck[2] == "O"))
|| ((posCheck[3] == "O") && (posCheck[4] == "O") && (posCheck[5] == "O"))
|| ((posCheck[6] == "O") && (posCheck[7] == "O") && (posCheck[8] == "O"))
|| ((posCheck[0] == "O") && (posCheck[3] == "O") && (posCheck[6] == "O"))
|| ((posCheck[1] == "O") && (posCheck[4] == "O") && (posCheck[7] == "O"))
|| ((posCheck[2] == "O") && (posCheck[5] == "O") && (posCheck[8] == "O"))
|| ((posCheck[0] == "O") && (posCheck[4] == "O") && (posCheck[8] == "O"))
|| ((posCheck[2] == "O") && (posCheck[4] == "O") && (posCheck[6] == "O"))) {
// this part updates the winner score then refreshes the text field
// to reflect the change
scoreCount2++;
scoreP2.setText("Player (O) score: " + String.valueOf(scoreCount2));
blank.setText("Player (O) Is the winner.");
gameDone = true;
turnCounter++;
}
// this checks if there has been a tie in the game
else if (tieChecker >= 9) {
prevWinner = ("It's a cat's game!");
tieChecker = 0;
gameDone = true;
turnCounter++;
}
// this makes sure the game doesnt end prematurely
else {
gameDone = false;
}
// this if statement is engaged when the game is done and resets the
// board
if (gameDone == true) {
pane.removeAll();
textField.removeAll();
tieChecker = 0;
init();
posCheck[0] = "";
posCheck[1] = "";
posCheck[2] = "";
posCheck[3] = "";
posCheck[4] = "";
posCheck[5] = "";
posCheck[6] = "";
posCheck[7] = "";
posCheck[8] = "";
posCheck[9] = "";
}// end of reset sequence
turnCounter++;
blank.setText("Games Played: " + gamesPlayed);
}
}
If you insist on keeping it as an Applet, make another class and do this:
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
Game g = new Game();
g.init();
frame.getContentPane().add(g);
frame.pack();
frame.setVisible(true);
}
}
You can convert JApplet to JFrame - Change your public init() method to public static void main (String args[]).Then create an instance of JFrame
JFrame frame = new JFrame(); Then add components to it.
or
Add existing applet to JFrame - Adding JApplet into JFrame
So using second method your code would be -
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JApplet implements ActionListener {
// Create all labels, textfields and buttons needed (in order)
/**
* This is a tic tac toe game made with the purposes of learning java
*/
private static final long serialVersionUID = 1L;
JButton firstButton;
JButton secondButton;
JButton thirdButton;
JButton fourthButton;
JButton fithButton;
JButton sixthButton;
JButton seventhButton;
JButton eighthButton;
JButton ninthButton;
//this is the position value checker which are later set to x and o
String[] posCheck = { "", "", "", "", "", "", "", "", "" };
// score count JLABELs to display the score inside the game pane.
JLabel scoreP1;
JLabel scoreP2;
// this is used for formatting purposes or something like that, I guess.
JLabel blank;
// score count variables for both players, they both start at 0.
int scoreCount1 = 0, scoreCount2 = 0;
int gamesPlayed = -1;
boolean gameDone = false;
int k;
String prevWinner = "";
// Create any global variables/arrays needed later in the program (final)
Container pane = getContentPane();
//this sets the tile to X or O depending on who's turn it is
String[] symbol = { "O", "X" };
int turnCounter = 0;
int tieChecker = 0;
// Sets up GUI components.
public void init() {
pane.setLayout(new GridLayout(4, 3));
setSize(500, 500);
// Adds jlabel for scores
scoreP1 = new JLabel();
scoreP1.setText("Player (X) score: " + String.valueOf(scoreCount1));
scoreP2 = new JLabel();
scoreP2.setText("Player (O) score: " + String.valueOf(scoreCount2));
blank = new JLabel();
blank.setText(prevWinner);
// these are the JButtons that go in the game panel along with action
// listeners
firstButton = new JButton();
firstButton.addActionListener(this);
secondButton = new JButton();
secondButton.addActionListener(this);
thirdButton = new JButton();
thirdButton.addActionListener(this);
fourthButton = new JButton();
fourthButton.addActionListener(this);
fithButton = new JButton();
fithButton.addActionListener(this);
sixthButton = new JButton();
sixthButton.addActionListener(this);
seventhButton = new JButton();
seventhButton.addActionListener(this);
eighthButton = new JButton();
eighthButton.addActionListener(this);
ninthButton = new JButton();
ninthButton.addActionListener(this);
pane.add(firstButton, 0); // second parameter is the index on pane
pane.add(secondButton, 1);
pane.add(thirdButton, 2);
pane.add(fourthButton, 3);
pane.add(fithButton, 4);
pane.add(sixthButton, 5);
pane.add(seventhButton, 6);
pane.add(eighthButton, 7);
pane.add(ninthButton, 8);
pane.add(scoreP1);
pane.add(blank);
pane.add(scoreP2);
gamesPlayed++;
setContentPane(pane);
} // init method
// checks for mouse clicks here.
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
if (e.getSource() == firstButton)
revealButton(firstButton, 0);
else if (e.getSource() == secondButton)
revealButton(secondButton, 1);
else if (e.getSource() == thirdButton)
revealButton(thirdButton, 2);
else if (e.getSource() == fourthButton)
revealButton(fourthButton, 3);
else if (e.getSource() == fithButton)
revealButton(fithButton, 4);
else if (e.getSource() == sixthButton)
revealButton(sixthButton, 5);
else if (e.getSource() == seventhButton)
revealButton(seventhButton, 6);
else if (e.getSource() == eighthButton)
revealButton(eighthButton, 7);
else if (e.getSource() == ninthButton)
revealButton(ninthButton, 8);
}
} // actionPerformed method
// respond to button pushed
public void revealButton(JButton button, int index) {
// removes the indicated button from pane
pane.remove(button);
// creates a new text field to take the place of the button, makes it
// uneditable and sets background colour
JTextField textField = new JTextField();
textField.setEditable(false);
textField.setBackground(Color.WHITE);
// sets the alignment for the text in the field
textField.setHorizontalAlignment(SwingConstants.CENTER);
// adds the new textfield to the pane at the location of the old button
pane.add(textField, index);
posCheck[index] = symbol[(turnCounter % 2)];
prevWinner = "Player (" + symbol[(turnCounter % 2)]
+ ") is the winner!";
// re-creates pane with new information
setContentPane(pane);
// this sets the text field to either X or O depending who placed last.
textField.setText(symbol[(turnCounter) % 2]);
button.setEnabled(false);
// this is a counter to check if it is a cats game.
tieChecker++;
// check for winner X here
if (((posCheck[0] == "X") && (posCheck[1] == "X") && (posCheck[2] == "X"))
|| ((posCheck[3] == "X") && (posCheck[4] == "X") && (posCheck[5] == "X"))
|| ((posCheck[6] == "X") && (posCheck[7] == "X") && (posCheck[8] == "X"))
|| ((posCheck[0] == "X") && (posCheck[3] == "X") && (posCheck[6] == "X"))
|| ((posCheck[1] == "X") && (posCheck[4] == "X") && (posCheck[7] == "X"))
|| ((posCheck[2] == "X") && (posCheck[5] == "X") && (posCheck[8] == "X"))
|| ((posCheck[0] == "X") && (posCheck[4] == "X") && (posCheck[8] == "X"))
|| ((posCheck[2] == "X") && (posCheck[4] == "X") && (posCheck[6] == "X"))) {
// this part updates the winner score then refreshes the text field
// to reflect the change
scoreCount1++;
scoreP1.setText("Player (X) score: " + String.valueOf(scoreCount1));
blank.setText("Player (X) Is the winner.");
gameDone = true;
turnCounter++;
}
// this checks if O has won the game.
else if (((posCheck[0] == "O") && (posCheck[1] == "O") && (posCheck[2] == "O"))
|| ((posCheck[3] == "O") && (posCheck[4] == "O") && (posCheck[5] == "O"))
|| ((posCheck[6] == "O") && (posCheck[7] == "O") && (posCheck[8] == "O"))
|| ((posCheck[0] == "O") && (posCheck[3] == "O") && (posCheck[6] == "O"))
|| ((posCheck[1] == "O") && (posCheck[4] == "O") && (posCheck[7] == "O"))
|| ((posCheck[2] == "O") && (posCheck[5] == "O") && (posCheck[8] == "O"))
|| ((posCheck[0] == "O") && (posCheck[4] == "O") && (posCheck[8] == "O"))
|| ((posCheck[2] == "O") && (posCheck[4] == "O") && (posCheck[6] == "O"))) {
// this part updates the winner score then refreshes the text field
// to reflect the change
scoreCount2++;
scoreP2.setText("Player (O) score: " + String.valueOf(scoreCount2));
blank.setText("Player (O) Is the winner.");
gameDone = true;
turnCounter++;
}
// this checks if there has been a tie in the game
else if (tieChecker >= 9) {
prevWinner = ("It's a cat's game!");
tieChecker = 0;
gameDone = true;
turnCounter++;
}
// this makes sure the game doesnt end prematurely
else {
gameDone = false;
}
// this if statement is engaged when the game is done and resets the
// board
if (gameDone == true) {
pane.removeAll();
textField.removeAll();
tieChecker = 0;
init();
posCheck[0] = "";
posCheck[1] = "";
posCheck[2] = "";
posCheck[3] = "";
posCheck[4] = "";
posCheck[5] = "";
posCheck[6] = "";
posCheck[7] = "";
posCheck[8] = "";
posCheck[9] = "";
}// end of reset sequence
turnCounter++;
blank.setText("Games Played: " + gamesPlayed);
}
public static void main(String[] args) {
JFrame baseFrame = new JFrame();
Game gameObject = new Game();
gameObject.init();
baseFrame.setVisible(true);
baseFrame.getContentPane().add(gameObject);
}
}
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.
I have the following code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class TTT extends JFrame implements ActionListener { //DO NOT TOUCH!!!
//makes the array for the buttons
JButton spots[ ] = new JButton[ 9];
//keeps track of who's turn it is
int turn = 0;
//lets it go again
boolean go = true;
//gets the images for the X's and O's
ImageIcon red = new ImageIcon("x.PNG");
ImageIcon blue = new ImageIcon("o.PNG");
ImageIcon blank = new ImageIcon("blank.PNG");
public static void main (String []args ) {
TTT frame = new TTT(); //DO NOT TOUCH!!!
frame.setVisible(true);
}
public TTT( ) { //DO NOT TOUCH!!!
//set the frame default properties
setTitle ("Tic Tac Toe");
setSize ( 308, 308 );
setLocationRelativeTo ( null );
setResizable(false);
//register 'Exit upon closing' as a default close operation
setDefaultCloseOperation( EXIT_ON_CLOSE );
changeBkColor( );
}
private void changeBkColor() {
while (go) {
//declares some variables that we will use later
int newLine = 0;
int lineCount = 0;
//change background color to white
Container contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
//puts the buttons on the screen
for (int i = 0; i < spots.length; i++) {
//make it first appear as a blank image
spots[ i] = new JButton (blank);
//checks if it needs a new row
if (i == 3 || i == 6) {
newLine++;
lineCount = 0;
}
//sets the positions of the buttons
spots[ i].setBounds(lineCount*100, newLine*100, 100, 100);
//add it to the container
contentPane.add(spots[ i]);
spots[ i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*** Line 62 ***/ public void run() {
//check button pressed
for (int i = 0; i < spots.length; i++) {
if(e.getSource()==spots[ i]) {
//check turn
if (turn%2==0) {
spots[ i].setIcon(red);
} else {
spots[ i].setIcon(blue);
}
//disable the button so it can't be re-pressed
spots[ i].removeActionListener(this);
}
}
turn++;
//checks for wins
for(int i = 0; i < 3; i++) {
if (spots[ i].getIcon()==red && //checks for verticle x win
spots[ i+3].getIcon()==red &&
spots[ i+6].getIcon()==red) {
int again1 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
if (again1 == JOptionPane.YES_OPTION) {
go = true;
} if (again1 == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Okay then. Bye!");
go = false;
}
}else if (spots[ i].getIcon()==blue && //checks for verticle o win
spots[ i+3].getIcon()==blue &&
spots[ i+6].getIcon()==blue) {
int again2 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
if (again2 == JOptionPane.YES_OPTION) {
go = true;
} if (again2 == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Okay then. Bye!");
go = false;
}
}else if (spots[ i*3].getIcon()==red && //checks for horizontal x win
spots[ (i*3)+1].getIcon()==red &&
spots[ (i*3)+2].getIcon()==red) {
int again3 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
if (again3 == JOptionPane.YES_OPTION) {
go = true;
} if (again3 == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Okay then. Bye!");
go = false;
}
}else if (spots[ i*3].getIcon()==blue && //checks for horizontal o win
spots[ (i*3)+1].getIcon()==blue &&
spots[ (i*3)+2].getIcon()==blue) {
int again4 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
if (again4 == JOptionPane.YES_OPTION) {
go = true;
} if (again4 == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Okay then. Bye!");
go = false;
}
}else if (spots[ i].getIcon()==red && //checks for diagnol x win
spots[ 4].getIcon()==red &&
spots[ 8-i].getIcon()==red) {
int again5 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
if (again5 == JOptionPane.YES_OPTION) {
go = true;
} if (again5 == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Okay then. Bye!");
go = false;
}
}else if (spots[ i].getIcon()==blue && //checks for diagnol o win
spots[ 4].getIcon()==blue &&
spots[ 8-i].getIcon()==blue) {
int again6 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
if (again6 == JOptionPane.YES_OPTION) {
go = true;
} if (again6 == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Okay then. Bye!");
go = false;
}
}
}
lineCount++;
}
}
});
}
} if (!go) {
System.exit(0);
}
}
}
my compile hates line 62 - public void run() { - and I need help fixing it. I copied and pasted the line for an already-working program, so I don't know how it doesn't work.
EDIT
sorry guys, here's my errors:
TTT.java:62: error: illegal start of expression
public void run() {
^
TTT.java:62: error: illegal start of expression
public void run() {
^
TTT.java:62: error: ';' expected
public void run() {
^
It appears that you have defined a method within another method declaration, namely run() within actionPerformed(ActionEvent e).
This is not allowed in Java.
It also appears that you have a misunderstanding about the declaration of static void run() this is not a constructor; it is a method declaration with a return type of void.
changeBkColor is an infinite loop so after the constructor is called frame.setVisible(true) is never called. Might be an issue.