Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 29-06-2009
Member
 
Join Date: Jun 2009
Posts: 41
Implement Java tic tac toe

Hi
Recently i have done a course in java..and of am course am not that much expert in this,still running with mistakes,but am so enthusiastic to write a program,which actually for a project,a tic tac toe, using non swing java.so can any one help me out this in this regard,may be kind of input so that i can develop it by my own

thanks for any help...
Reply With Quote
  #2  
Old 29-06-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: Implement Java tic tac toe

yes i made a tic tac toe game in java
i will put down my codes here so that you will get an idea how to do that don't copy just understand the concept and write it by yourself


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

public class TicTacToe implements ActionListener {


private JFrame window = new JFrame("Tic-Tac-Toe");
private JPanel inputPanel = new JPanel();
private JPanel boardPanel = new JPanel();
private int boardSize=0;
private int count = -1;
private String letter = "";
private boolean win = false;

public TicTacToe(){
//Create Window
window.setSize(500,500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputPanel.add(label);
inputPanel.add(button3);
inputPanel.add(button4);
inputPanel.add(button5);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
inputPanel.setSize(500,200);
window.add(inputPanel,"North");

window.setVisible(true);
}

public void actionPerformed(ActionEvent a) {
if(count==-1){
letter="-";
count=0;
}
else{
count++;

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

if (a.getSource() == button3) {
boardSize=3;
}
else if (a.getSource()== button4){
boardSize=4;
}
else if (a.getSource() == button5){
boardSize=5;
}
if ((a.getSource() == button3)||(a.getSource()==button4)||(a.getSource()==button5)){
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
buttons = new JButton[boardSize][boardSize];
for(int i=0; i<boardSize; i++)
for (int j=0; j<boardSize; j++){
buttons[i][j] = new JButton();
boardPanel.add(buttons[i][j]);
buttons[i][j].addActionListener(this);
}

boardPanel.setSize(300,300);
boardPanel.setLayout(new GridLayout(boardSize,boardSize));
window.add(boardPanel,"Center");
}

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


if(win == true){
JOptionPane.showMessageDialog(null, letter + " wins the game!");
System.exit(0);
} else if(count == (boardSize*boardSize) && win == false){
JOptionPane.showMessageDialog(null, "The game was tie!");
System.exit(0);
}

}

public static void main(String[] args){
TicTacToe starter = new TicTacToe();
}
Reply With Quote
  #3  
Old 29-06-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Implement Java tic tac toe

Basically you have to make it your own way by utilizing enough time this is vital.it should be considered for each category of input and each state of the program. If you don't have a thorough idea of e structural level you won't make it in better way.

make use of data structure to keep the game board ,inputting formats, moves, resulting game board format and of course the final output. include like quitting and starting a new game options..so this is the planing level now write down the code level by level and then make it a single one this will increase the efficacy
good luck for you
Reply With Quote
  #4  
Old 29-06-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: Implement Java tic tac toe

Originally you have to start a project by deeply scrutinizing each variable the algorithm is very important in this concern .probing your problem will allow you to make a logically correct program bug free

begin by asking your self what is the game tic-tac-toe now you end up with an answer It?s a game consisting of a 3 by 3 grid. Each player takes alternating turns trying to earn three of their letters in a row horizontally, vertically, or diagonally. so you have an idea about the game and a grid layout 3 by 3,so here you go step by step..
any way best of luck...
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Implement Java tic tac toe"
Thread Thread Starter Forum Replies Last Post
Implement Simple Database using BST in Java maxmidg412 Software Development 2 31-07-2011 03:22 AM
Implement the return type in java Captain Carrot Software Development 4 25-03-2010 01:34 PM
How to implement IP Sec Enriquee Technology & Internet 5 12-01-2010 06:28 AM
How to implement Web conferencing Balamohan Technology & Internet 5 20-12-2009 05:14 AM
How to implement MultiDimensional Array in JAVA Nihar Khan Software Development 3 28-02-2009 01:25 PM


All times are GMT +5.5. The time now is 08:43 PM.