enable users to enter names

enable users to enter names

this is the code for tic tac toe game, i want users can save their name as player X and player O, but I do not know how? can you give me some tips?




package threeTsGame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TicTacToe implements ActionListener {

private int[][] winCombinations = new int[][] {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}
};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[9];
private int count, xWins, oWins = 0;
private String letter = "";
private boolean win = false;


public TicTacToe(){

window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
window.setLocationRelativeTo(null);


for(int i=0; i<=8; i++){
buttons[i] = new JButton();
window.add(buttons[i]);
buttons[i].addActionListener(this);
}

window.setVisible(true);

}


public void actionPerformed(ActionEvent a) {
count++;

if(count % 2 == 0){
letter = "O";
} else {
letter = "X";
}


JButton pressedButton = (JButton)a.getSource();
pressedButton.setText(letter);
pressedButton.setEnabled(false);


for(int i=0; i<=7; i++){
if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
buttons[winCombinations[i][0]].getText() != ""){
win = true;
}
}


if(win == true){
if (count % 2 == 0){
JOptionPane.showMessageDialog(null, "Player O WINS the game!");
playAgainDialog();
}else
JOptionPane.showMessageDialog(null, "Player X WINS the game!");
playAgainDialog();
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "The game was tie!");
playAgainDialog();
}
}

public void playAgainDialog() {
if(letter.equals("X")) xWins++;
else oWins++;

int response = JOptionPane.showConfirmDialog(null, "Do you want to play again?","Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if(response == JOptionPane.YES_OPTION) reset();
else window.hide();
}

public void reset() {
for (int i=0; i<=8; i++){
buttons[i].setText("");
buttons[i].setEnabled(true);
}
win = false;
count = 0;
}

public static void main(String[] args){
new TicTacToe();
}
}
View Answers

April 1, 2010 at 11:38 AM

Hi Friend,

We have modified your code:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TicTacToe implements ActionListener {
private int[][] winCombinations = new int[][] {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[9];
private int count, xWins, oWins = 0;
private String letter = "";
private boolean win = false;
public TicTacToe(){
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
window.setLocationRelativeTo(null);
for(int i=0; i<=8; i++){
buttons[i] = new JButton();
window.add(buttons[i]);
buttons[i].addActionListener(this);
}
window.setVisible(true);
}
public void actionPerformed(ActionEvent a) {
count++;
if(count % 2 == 0){
letter = "O";
} else {
letter = "X";
}
JButton pressedButton = (JButton)a.getSource();
pressedButton.setText(letter);
pressedButton.setEnabled(false);
for(int i=0; i<=7; i++){
if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
buttons[winCombinations[i][0]].getText() != ""){
win = true;
}
}
if(win == true){
if (count % 2 == 0){
JOptionPane.showMessageDialog(null, "Player O WINS the game!");
saveName();
playAgainDialog();
}else
JOptionPane.showMessageDialog(null, "Player X WINS the game!");
saveName();
playAgainDialog();
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "The game was tie!");
playAgainDialog();
}
}
public void playAgainDialog() {
if(letter.equals("X")) xWins++;
else oWins++;
int response = JOptionPane.showConfirmDialog(null, "Do you want to play again?","Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(response == JOptionPane.YES_OPTION) reset();
else window.hide();
}
public void reset() {
for (int i=0; i<=8; i++){
buttons[i].setText("");
buttons[i].setEnabled(true);
}
win = false;
count = 0;
}
public void saveName(){
int response = JOptionPane.showConfirmDialog(null, "Do you want to save your name?","Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(response == JOptionPane.YES_OPTION){
String name= JOptionPane.showInputDialog(null,"Enter Name (Player X or Player O)");
try{
File file = new File("TicTacToe.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(name);
out.newLine();
out.close();
}
catch(Exception e){}
}
}
public static void main(String[] args){
new TicTacToe();
}
}

Thanks









Related Tutorials/Questions & Answers:
enable users to enter names - Java Beginners
enable users to enter names  this is the code for tic tac toe game, i want users can save their name as player X and player O, but I do not know how...= JOptionPane.showInputDialog(null,"Enter Name (Player X or Player O)"); try{ File file = new File
Names
=JOptionPane.showInputDialog(null,"Enter any character"); switch(ch){ case 1... (String arg[]){ String st=JOptionPane.showInputDialog(null,"Enter any
Advertisements
enable hyperlink
enable hyperlink  How to enable hyperlink
count the users?
count the users?  how to get the number of users logged in any application
Names
JNDI names in netbeans
JNDI names in netbeans  how to create a JNDI names in netbeans
it can be submitted by hitting ENTER
it can be submitted by hitting ENTER  How do I make a form so it can be submitted by hitting ENTER
Program to Ignore Space and Enter ?!
Program to Ignore Space and Enter ?!  Hi, dear friend i wont to write program to enter many statements, if i use Space and Enter in the Run Time must be delete the space and ignore the Enter ..for example if i entered
enable disable checkbox in javascript
enable disable checkbox in javascript  How to enable disable checkbox in javascript
ModuleNotFoundError: No module named 'enable'
ModuleNotFoundError: No module named 'enable'  Hi, My Python... 'enable' How to remove the ModuleNotFoundError: No module named 'enable'... to install padas library. You can install enable python with following command
enable text box
enable text box  Dear all I would like to enable a textbox by clicking on a button. pleas ehelp me out how can i do this regards JV
Enable Browser's back button
Enable Browser's back button  how to redirect to a home page when browser's back button is pressed in jsp instead of displaying the previous page
enable disable table
enable disable table  hi I have table with 3 columns if i click one column the other 2 column should disable pls can anyboby help me
disable and enable the submit buttons
disable and enable the submit buttons  i have two summit button in my form. one is disabled and other is navigating to the other page. after 3 submissions of the submit button the disabled one must be enabled and the enabled one
Inform user to enable JavaScript
Inform user to enable JavaScript   Hi sir How can we inform user to turn on JavaScript ? Because if we have implemented validation etc with JavaScript , then it will not work. What option we have when this situation occurs
ModuleNotFoundError: No module named 'names'
ModuleNotFoundError: No module named 'names'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'names' How to remove the ModuleNotFoundError: No module named 'names'
Display set names
Display set names  If i enter the First letter of a name it will display the list of names starting with that letter in command prompt using java...); System.out.print("Enter letter: "); String letter=input.next
Sorting Country names alphabetically
Sorting Country names alphabetically  Hello, I have a list of country names in an array. I need a jsp code which will sort me the country names in an alphaberical order. It would be more useful when I get the coding using
ModuleNotFoundError: No module named 'enter-leave'
ModuleNotFoundError: No module named 'enter-leave'  Hi, My Python... 'enter-leave' How to remove the ModuleNotFoundError: No module named 'enter-leave' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'wechat_enter'
ModuleNotFoundError: No module named 'wechat_enter'  Hi, My Python... 'wechat_enter' How to remove the ModuleNotFoundError: No module named 'wechat_enter' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'enter-leave'
ModuleNotFoundError: No module named 'enter-leave'  Hi, My Python... 'enter-leave' How to remove the ModuleNotFoundError: No module named 'enter-leave' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'users'
ModuleNotFoundError: No module named 'users'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'users' How to remove the ModuleNotFoundError: No module named 'users'
ModuleNotFoundError: No module named 'users'
ModuleNotFoundError: No module named 'users'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'users' How to remove the ModuleNotFoundError: No module named 'users'
To restrict users using LDAP
To restrict users using LDAP  Hi, I am trying to restrict users for an application I developed in java. Can i get the code to autheticate users using LDAP and Active directory. can anyone help
PHP find online users
PHP find online users  How to find the online users in PHP
pass parameter names and values
pass parameter names and values  What is the <jsp:param> standard action?   The <jsp:param> standard action is used with <jsp:include> or <jsp:forward> to pass parameter names and values
ask a user to enter 5 integer
ask a user to enter 5 integer  make a program that enter 5 numbers then identify the largest and the smallest number sample program 2 4 3 5 6 the smallest number: 2 the largest number: is 6 66
Allow to Enter only numeric data.
Allow to Enter only numeric data.  hi....... I want to ask that in my project i have a textfield where user is going to enter the data so i want that the user should allow to enter only numeric data. I mean to say that if user
How to handle enter key in javascript
How to handle enter key in javascript  Can any one tell me how to handle the enter key event on a Button in HTML. Like in my simple HTML page i want to submit the form even if user hits the enter key from keyboard instead
ipad default image names
ipad default image names  i have a problem while launching application in iPad. The Default image that i have set is not displaying in landscape mode correctly. please suggest. Thanks.   ipad Default image names
how to enable nszombie in xcode 4
how to enable nszombie in xcode 4  how to enable nszombie in xcode 4
ModuleNotFoundError: No module named 'oxasl-enable'
ModuleNotFoundError: No module named 'oxasl-enable'  Hi, My Python... 'oxasl-enable' How to remove the ModuleNotFoundError: No module named 'oxasl-enable' error? Thanks   Hi, In your python
enable text box and label on selection
enable text box and label on selection  hello, Please tell me how to enable label and text box on selection of drop down list box. in drop down list box all values come from database. please reply
ModuleNotFoundError: No module named 'names_comparator'
ModuleNotFoundError: No module named 'names_comparator'  Hi, My... named 'names_comparator' How to remove the ModuleNotFoundError: No module named 'names_comparator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'names_comparator'
ModuleNotFoundError: No module named 'names_comparator'  Hi, My... named 'names_comparator' How to remove the ModuleNotFoundError: No module named 'names_comparator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'names-dataset'
ModuleNotFoundError: No module named 'names-dataset'  Hi, My... 'names-dataset' How to remove the ModuleNotFoundError: No module named 'names-dataset' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'names-dataset'
ModuleNotFoundError: No module named 'names-dataset'  Hi, My... 'names-dataset' How to remove the ModuleNotFoundError: No module named 'names-dataset' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'names-gen'
ModuleNotFoundError: No module named 'names-gen'  Hi, My Python... 'names-gen' How to remove the ModuleNotFoundError: No module named 'names... have to install padas library. You can install names-gen python with following
ModuleNotFoundError: No module named 'names-dataset'
ModuleNotFoundError: No module named 'names-dataset'  Hi, My... 'names-dataset' How to remove the ModuleNotFoundError: No module named 'names-dataset' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'names_translator'
ModuleNotFoundError: No module named 'names_translator'  Hi, My... named 'names_translator' How to remove the ModuleNotFoundError: No module named 'names_translator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'classification-names'
ModuleNotFoundError: No module named 'classification-names'  Hi...: No module named 'classification-names' How to remove the ModuleNotFoundError: No module named 'classification-names' error? Thanks   Hi
ModuleNotFoundError: No module named 'grill-names'
ModuleNotFoundError: No module named 'grill-names'  Hi, My Python... 'grill-names' How to remove the ModuleNotFoundError: No module named 'grill-names' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'human-names'
ModuleNotFoundError: No module named 'human-names'  Hi, My Python... 'human-names' How to remove the ModuleNotFoundError: No module named 'human-names' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'flake8-variables-names'
ModuleNotFoundError: No module named 'flake8-variables-names'  Hi...: No module named 'flake8-variables-names' How to remove the ModuleNotFoundError: No module named 'flake8-variables-names' error? Thanks   Hi
ModuleNotFoundError: No module named 'names_comparator'
ModuleNotFoundError: No module named 'names_comparator'  Hi, My... named 'names_comparator' How to remove the ModuleNotFoundError: No module named 'names_comparator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'names_comparator'
ModuleNotFoundError: No module named 'names_comparator'  Hi, My... named 'names_comparator' How to remove the ModuleNotFoundError: No module named 'names_comparator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'names_comparator'
ModuleNotFoundError: No module named 'names_comparator'  Hi, My... named 'names_comparator' How to remove the ModuleNotFoundError: No module named 'names_comparator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'names_comparator'
ModuleNotFoundError: No module named 'names_comparator'  Hi, My... named 'names_comparator' How to remove the ModuleNotFoundError: No module named 'names_comparator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'names_comparator'
ModuleNotFoundError: No module named 'names_comparator'  Hi, My... named 'names_comparator' How to remove the ModuleNotFoundError: No module named 'names_comparator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'names-dataset'
ModuleNotFoundError: No module named 'names-dataset'  Hi, My... 'names-dataset' How to remove the ModuleNotFoundError: No module named 'names-dataset' error? Thanks   Hi, In your python

Ads