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