NoughtsAndCrossesGame play button doesn't work
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author 000621812
*/
public class NoughtsAndCrossesGamev2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
NoughtsAndCrossesGameFrame noughtsAndCrossesGame;
noughtsAndCrossesGame = new NoughtsAndCrossesGameFrame();
noughtsAndCrossesGame.setVisible(true);
}
}
class NoughtsAndCrossesGameFrame extends JFrame {
NoughtsAndCrossesGamePanel gamePanel;
NoughtsAndCrossesButtonPanel buttonPanel;
private ButtonPlayActionHandle playHandler;
private ButtonExitActionHandler exitHandler;
private JButton playButton;
private JButton exitButton;
public NoughtsAndCrossesGameFrame() {
gamePanel = new NoughtsAndCrossesGamePanel();
buttonPanel = new NoughtsAndCrossesButtonPanel();
this.setLayout(new GridLayout(3, 3));
this.setLayout(new FlowLayout());
this.setVisible(true);
this.setTitle("Noughts and Crosses");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(playButton );
this.add(exitButton );
this.add(gamePanel, FlowLayout.LEFT);
this.add(buttonPanel,FlowLayout.LEFT);
this.setSize(145, 145);
this.setVisible(true);
}
/**
* Asks the panel containing the game to reset the game.
*/
private void resetGame() {
//**
//* The button panel
//*/
}
class NoughtsAndCrossesButtonPanel extends JPanel {
/**
* The game panel
*
*/
public NoughtsAndCrossesButtonPanel() {
playButton = new JButton("Play");
playButton.addActionListener(playHandler);
playHandler = new ButtonPlayActionHandle();
exitButton = new JButton("Exit");
exitHandler = new ButtonExitActionHandler();
exitButton.addActionListener(exitHandler);
}
}
}
class NoughtsAndCrossesGamePanel extends JPanel {
private int clickCount = 0;
private JButton[] btnKeypad = new JButton[9];
public NoughtsAndCrossesGamePanel() {
/**
* Resets the buttons and clicjCount ready for a new game
*/
HandlePlayerShot shotHandler = new HandlePlayerShot();
// Set some JFrame details
// NOTE - In grid layout you provide the number of
// rows and columns but columns are ignored and always set equal to rows
// unless the rows are zero.
//TODO
this.setLayout(new BorderLayout());
this.setLayout(new GridLayout(3, 3));
this.setSize(110, 110);
this.setVisible(true);
// Create the buttons and add them to the JFrame
for (int i = 0; i < 9; i++) {
btnKeypad[i] = new JButton(" ");
this.add(btnKeypad[i]);
btnKeypad[i].addActionListener(shotHandler);
//TODO
}
// Register the same listener for each button as the action needed is always the same.
// Note - this could have been done in the above loop but is shown separately here to make
// it clearer.
//shotHandler = new HandlePlayerShot();
this.setSize(200, 200);
}
private void resetGame() {
/**
* Disables all the buttons when the game ends
*
*/
for(int i=0; i<9; i++)
{
char ch=(char)('0'+i+1);
btnKeypad[i].setText(""+ch);
}
}
private void gameFinished() {
/**
* Inner class of NoughtsAndCrossesGamePanel to handle a players shot
* ie when a game button is pressed.
*
*/
for (int i = 0; i < 9; i++) {
btnKeypad[i].setEnabled(false);
}
//this.dispose(gamePanel);
}
class HandlePlayerShot implements ActionListener { //TODO implements ????
/**
* Inner class of NoughtsAndCrossesGameFrame to respond to the Exit Button being
* pressed. Dispose of the frame and exits the application
*/
@Override
/**
* Responds to the button press - ie player shot
*/
public void actionPerformed(ActionEvent e) {
JButton currentButton;
String winner;
clickCount++;
currentButton = (JButton) (e.getSource());
// If it is an odd click count (button press count) then it is "X"
// shot as "X" player always goes first
if (clickCount % 2 == 1) {
currentButton.setText("X");
} else {
currentButton.setText("0");
}
currentButton.setEnabled(false);
winner = checkForWinner();
if (winner != null) {
//Display a message showing who the winner is
//TODO
JOptionPane.showMessageDialog(null, "The winner is " + winner);
//NoughtsAndCrossesGamePanel.this.dispose();
} else {
// check that all possible shots have been had
if (clickCount == 9) {
//Display a message saying it is a drawn game
//TODO
JOptionPane.showMessageDialog(null, "Draw!");
}
}
}
/**
* Checks if the button in btnKeypad array in positions i, j, k
* all have the same text.
* @param i - button number
* @param j - button number
* @param k - button number
* @return true if button i,j,k
*/
private boolean isAWinner(int i, int j, int k) {
return !btnKeypad[i].getText().equals(" ")
&& btnKeypad[i].getText().equals(btnKeypad[j].getText())
&& btnKeypad[i].getText().equals(btnKeypad[k].getText());
}
/**
* Checks for all possible winning scenarios
* @return null if there is no winner or the text on the button of the
* winner
*/
public String checkForWinner() {
/* Assumed mapping of button indexes to position in game is -
* 012
* 345
* 678
*/
int winner = -1; // -1 value means there was no winner
if (isAWinner(0, 1, 2)) {// Horizontal row 1
winner = 0;
} else if (isAWinner(3, 4, 5)) { // Horizontal row 2
winner = 3;
} else if (isAWinner(6, 7, 8)) { // Horizontal row 3
winner = 6;
} else if (isAWinner(0, 3, 6)) { // Vertical row 1
winner = 0;
} else if (isAWinner(1, 4, 7)) { // Vertical row 2
winner = 1;
} else if (isAWinner(2, 5, 8)) { // Vertical row 3
winner = 2;
} else if (isAWinner(0, 4, 8)) { // Diagonal 1
winner = 0;
} else if (isAWinner(2, 4, 6)) { // Diagonal 2
winner = 2;
}
if (winner != -1) {
return btnKeypad[winner].getText();
} else {
return null;
}
}
}
}
class ButtonExitActionHandler implements ActionListener {
/**
* Inner class of NoughtsAndCrossesGameFrame to respond to the Play Button being
* pressed. Asks the frame to reset the game by calling the frames resetGame
* method
*/
public void actionPerformed(ActionEvent e) {
//this.dispose(gamePanel);
System.exit(0);
}
}
class ButtonPlayActionHandle implements ActionListener {
public void actionPerformed(ActionEvent e) {
//resetGame();
}
}
I have problem with NoughtsAndCrossesGame which the play button doesn't work which i couldn't write the play button function code. The rest works but the play button doesn't work used to restart the button. i will good if someone give me a solution so that i could understand. thank you for kindness
View Answers
Related Tutorials/Questions & Answers:
NoughtsAndCrossesGame play button doesn't work NoughtsAndCrossesGame play button doesn't work /*
* To change...
NoughtsAndCrossesGameFrame
noughtsAndCrossesGame;
noughtsAndCrossesGame = new...() {
//**
//* The
button panel
//*/
}
class NoughtsAndCrossesButtonPanel
NoughtsAndCrossesGame play button doesn't work NoughtsAndCrossesGame play button doesn't work /*
* To change...
NoughtsAndCrossesGameFrame
noughtsAndCrossesGame;
noughtsAndCrossesGame = new...() {
//**
//* The
button panel
//*/
}
class NoughtsAndCrossesButtonPanel
Advertisements
After Logout Back Button Should not work againAfter Logout Back
Button Should not
work again Hi deepak,,
I am doing Login Page,..
IN That If Logout Then When I Click Logout
Button It Should not
work..
For That what is the logic And what Concept I have To Use..
Please
Running problem with NoughtsAndCrossesGame in blank of NoughtsAndCrossesGameFrame to respond to the
Play Button being
* pressed...Running problem with
NoughtsAndCrossesGame in blank Hi i was having problem created
NoughtsAndCrossesGame in end the it works but i runs the gui
JFrame Button click to start playJFrame
Button click to start play i made a game and and i add a
button in new jframe and i want when i click it it start to
play...can anyone help me
ButtonButton How to use image for
button in JSF
iPhone Button Click Sound coding to
play the sound. In my application i am using a
button to give the action to
play a sound. You can add and switch the
button with action in Interface...... you can
play the sound on clicking the
button.
ADS_TO_REPLACE_6
Download
buttonbutton can i give multiple commands on click of a
button? if yes how can i do that??
multiple commands can we retriving dat from database, capturing data, moving to next page.. etc
buttonbutton can i give multiple commands on click of a
button? if yes how can i do that?? multiple commands can be retriving dat from database, capturing data, moving to next page.. etc
Create a input button inside js function this but
doesn't seems to
work. Lets show is the function being called
function show... called function. I used something like this but
doesn't seems to
work. Lets show...Create a input
button inside js function I call my js function from
iPhone play, pause and stop playing music
iPhone
play, pause and stop playing music
This is the small iPhone SDK example that will show you how to
play, pause and stop the music on
button... you are going to
play on
button click.
Once you done, just open your
jvm workjvm work wht is the
work of jvm? deaply.
Hi Friend,
Java Virtual Machine or JVM for short is a software execution engine to run the java programs. Java Virtual Machine is also known as "Java Interpreter" which
why set doesn't allow duplicate valuewhy set
doesn't allow duplicate value HI Everyone
Any one can help me why set not allowed duplicate value in java.
Please post ur comment, its urgent.
Thanks in advance
hello .. still doesn't run - Java Beginnershello .. still
doesn't run Iam still having a prblem in running this problem
errors are:
can not resolve symbol
import.util.Scanner
class... stupid alittle bit
but this is my home
work ModuleNotFoundError: No module named 'Work'ModuleNotFoundError: No module named '
Work' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
Work'
How to remove the ModuleNotFoundError: No module named '
Work' error
Hibernate Envers - REVINFO table doesn't existHibernate Envers - REVINFO table
doesn't exist Hi,
In my project I have to save the data change log. For this we are using the Hibernate Envers...:
Hibernate Envers - REVINFO table
doesn't exist
How to resolve this?
Thanks
work - JSP-ServletJSP
Work Directory What is the absolute path for the JSP working directory
collection frame workcollection frame work explain the hierarchy of one dimensional collection frame
work classes
collection frame workcollection frame work could you please tell me detail the concept of collection frame
work How to work with Ajax in springHow to
work with Ajax in spring give some sample code for ajax with spring (example like if i select a state from one drop down in another drop down related districts should come
ios work with nsdictionaryios
work with nsdictionary How to
work with NSDictionary in Objective C?
Working with NSDictionary in Objective C
Basically, in Objective C NSDictionary is used to create and retrive the Key values for objectecs
JFreeChart dosn't workJFreeChart dosn't work Hello everybody
I'm trying to make a line chart from JFreechart, but I just can't get it to
work. The graph get its data... it, an error occurs (i tried an example from here, with XY, and it didn't
work either)
I
wnidows button wnidows
button How to handle windows back and forward
button in struts applications, without using java script
ModuleNotFoundError: No module named 'har-work'ModuleNotFoundError: No module named 'har-
work' Hi,
My Python...-
work'
How to remove the ModuleNotFoundError: No module named 'har-
work... to install padas library.
You can install har-
work python with following
ModuleNotFoundError: No module named 'work_h'ModuleNotFoundError: No module named '
work_h' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
work_h'
How to remove the ModuleNotFoundError: No module named '
work_h'
ModuleNotFoundError: No module named 'work_h'ModuleNotFoundError: No module named '
work_h' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
work_h'
How to remove the ModuleNotFoundError: No module named '
work_h'
ModuleNotFoundError: No module named 'work-queue'ModuleNotFoundError: No module named '
work-queue' Hi,
My Python... '
work-queue'
How to remove the ModuleNotFoundError: No module named '
work... have to install padas library.
You can install
work-queue python
ModuleNotFoundError: No module named 'work-toto'ModuleNotFoundError: No module named '
work-toto' Hi,
My Python... '
work-toto'
How to remove the ModuleNotFoundError: No module named '
work... have to install padas library.
You can install
work-toto python with following
ModuleNotFoundError: No module named 'yzs-work'ModuleNotFoundError: No module named 'yzs-
work' Hi,
My Python...-
work'
How to remove the ModuleNotFoundError: No module named 'yzs-
work... to install padas library.
You can install yzs-
work python with following
ModuleNotFoundError: No module named 'har-work'ModuleNotFoundError: No module named 'har-
work' Hi,
My Python...-
work'
How to remove the ModuleNotFoundError: No module named 'har-
work... to install padas library.
You can install har-
work python with following
ModuleNotFoundError: No module named 'work_h'ModuleNotFoundError: No module named '
work_h' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
work_h'
How to remove the ModuleNotFoundError: No module named '
work_h'
ModuleNotFoundError: No module named 'work-queue'ModuleNotFoundError: No module named '
work-queue' Hi,
My Python... '
work-queue'
How to remove the ModuleNotFoundError: No module named '
work... have to install padas library.
You can install
work-queue python