Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners enable users to enter names

Have Programming Question? Ask it here!
 
 
 


san
enable users to enter names
1 Answer(s)      4 years and 5 months ago
Posted in : Java Beginners


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();
}
}

Advertisement
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
enable disable checkbox in javascript
enable disable checkbox in javascript  How to enable disable checkbox in javascript
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
JNDI names in netbeans
JNDI names in netbeans  how to create a JNDI names in netbeans
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
PHP find online users
PHP find online users  How to find the online users in PHP
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
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
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
how to enable nszombie in xcode 4
how to enable nszombie in xcode 4  how to enable nszombie in xcode 4
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
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
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
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 Proxy Setting
In this section, you will learn to enable proxy setting for Maven using setting.xml file
How to enable proxy settings in Maven?
Maven 3 Tutorial: How to enable proxy settings in Maven? In this tutorial I will teach you how to enable proxy settings in maven? If your computer is behind... build tools is not able to access the remote repository. To enable the proxy
how to enable and disable a input type in jsp - JSP-Servlet
how to enable and disable a input type in jsp  how to disable the input type and enable based on some input in the input type   Hi Friend...; } else{ document.form.name.disabled=false; } } Enter Name
Task manager enable and disable thru java
Task manager enable and disable thru java  I would like to know, how to enable and disable task manager using java. Kindly, please Let me know
Authenticating Users Programmatically
This section contains detailed description on 'authenticating users programmatically' which was introduced in Servlet 3.0.
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
Enable and Disable Tab in Java
Enable and Disable Tab in Java       In this section you will learn how to enable... number. To enable or disable any tab you can use the setEnabledAt(int index
Display set of names from array
Display set of names from array In this section, you will learn how to display the set of names from array. In the given code, we have declared an array of string consisting of some countries name. We have prompted the user to enter
Baby names iPhone, Baby Names Application, Baby name iPhone app, iPhone Baby Names, iPhone baby apps,iPhone baby application
the users to search unique and versatile names for their cute and sweet babies...Baby names iPhone - Baby Names Applications for iPhone Baby names iPhone Baby name iPhone app - Search baby names from over 70,000 baby names
Class names don't identify a class - Java Tutorials
Class names don't identify a class In JDK 1.2 the Sum Microsystems have added a new feature that allows you to identify a class not only by its name but also by its context for which it was loaded. For this you need to set
Chaldean universal identifications names compiler on the application for Funtepoz
Chaldean universal identifications names compiler on the application for Funtepoz  I want to set up criteria on application table. It is chaldean numerology. I want 30 letters and 4 names in the identification and L and E should
how to make enable/disable textbox in while(rs.next)
how to make enable/disable textbox in while(rs.next)  Hi, I'm trying to enable/disable the textbox in the while loop. It works but when i want to update my data, the data are not updated correctly. When i remove the enable
how to make enable/disable textbox in while(rs.next)
how to make enable/disable textbox in while(rs.next)  Hi, I'm trying to enable/disable the textbox in the while loop. It works but when i want to update my data, the data are not updated correctly. When i remove the enable
DIFFERENT PAGES FOR DIFFERENT USERS ACCORDING TO THEIR ROLE
DIFFERENT PAGES FOR DIFFERENT USERS ACCORDING TO THEIR ROLE  I HAVE A PROBLEM IN PHP CODING PLS HELP ME PROBLEM IS THAT I WANT DIFFERENT PAGES TO VIEW FOR DIFFERENT USERS WHEN THEY LOGIN ACCORDING TO THEIR ROLE IN DATABASE
How do i enable java1.5 in netbeans5.5 - IDE Questions
How do i enable java1.5 in netbeans5.5  what is the steps to enable Java 1.5 in netbeans 5.5
sendin subscribe newstype to many users - JavaMail
sendin subscribe newstype to many users  Now i doing a project news portal.i want to send news to many subscribed users by using javamail.I using jsp and mysql.i want to send many type news to different users please help
Need to know struts1 all jar file names
Need to know struts1 all jar file names  Hi, My name is Neeraj, I want to know all the jar file names for struts1 and plz provide me the link to download these files. Thanks & Regards Neeraj
java multiple users with single connection - JSP-Servlet
java multiple users with single connection  hi, my problem... created a home page of my website. this page consists of 4 types of users suppose A, B, C, D. now type A user consists of many users like A1,A2,A3 etc
SQL All Column Names
SQL All Column Names       SQL All Column Names is useful when you want to see the Field, Type, Null... on 'SQL All Column Names'. To understand and grasp the example we create a table
Chaldean universal identifications names compiler on the application for Funtepoz1
Chaldean universal identifications names compiler on the application for Funtepoz1  In which programme this VBA can be writen: Function Tryme(mycell) mylen = Len(mycell) For j = 1 To mylen mytest = UCase(Mid(mycell, j, 1
Count number of occurences and print names alphabetically in Java
Count number of occurences and print names alphabetically in Java  I have this code: public class Names { public static void main(String[] args) { String[] s = {"bob", "tom", "jim", "tom", "tom", "tim"}; printCount(s); } public
jsp page to add users to mysql database
jsp page to add users to mysql database  <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic
Jdbc Get Column Names
Jdbc Get Column Names       The JDBC Get Column Names return you the property of the retrieved... The Tutorial illustrates an example from JDBC Get Column Names The Program
Java enter Key problem - Java Beginners
Java enter Key problem  How we can use Enter Key to Click a Button(JButton) in Java?  hi, i guess u mean u want to make a button as a default button. if it is so, then u can do it by rootpane, getRootPane
Convert Euro to dollar, need to be able to enter decimal!
Convert Euro to dollar, need to be able to enter decimal!  Hi..., the program quits. My program does all that. The only problem is, that if you enter...) { Scanner input = new Scanner(System.in); System.out.print("Please enter
Convert Euro to dollar, need to be able to enter decimal!
Convert Euro to dollar, need to be able to enter decimal!  Hi..., the program quits. My program does all that. The only problem is, that if you enter...) { Scanner input = new Scanner(System.in); System.out.print("Please enter
Blackboard Inc. Develops Seamless Learning for iPhone Users
Blackboard Inc. Develops Seamless Learning for iPhone Users... developed free application for the iPhone users. With the use of this application iPhone users can access their course information online. This application
Advertisements
 

 

 

DMCA.com