Edit Article

  • 34 views
  • 3 Editors

  • Edited

Eight Methods:Create the main class and call it TacProjectCreate a called CheckTurns.Create Nine Button on ScreenCreate ShowResult MethodCreate findHorzWin() methodCreate findVertWin() methodCreate findDiaWin() methodCreate Reset() method

Tic-tac-toe (or Noughts and crosses, Xs and Os) is a fun game that requires a paper-and-pencil and an opponent. Its a two players game i.e X and O, where each player attempts to place three of their markers (usually an X or an O) in a line on a 3x3 grid.The player who succeeds in placing three respective marks first in a horizontal, vertical, or diagonal row wins the game.

EditMethod 1 of 8: Create the main class and call it TacProject

  1. 1
    public class TacProject  extends JFrame Implements ActionListener { public TacProject() { initComponents(); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TicProject().setVisible(true); } }); } }
  2. 2
    Initialize Variable private int turns=0;   // to count no of turnsprivateString letter=" ";       // to Hold X & Oprivate boolean win=false;   // to Manage the user status

EditMethod 2 of 8: Create a called CheckTurns.

  1. 1
    This method will  be use to figure out the  Symbols i.e X and O . alternativelyvoid checkTurns(){    turns++; if (turns%2==0)        letter="O";        else        letter="X";}

EditMethod 3 of 8: Create Nine Button on Screen

  1. 1
    Add action listener  on Buttons Object
  2. 2
    Add the following code on actionPerformed method of
  3. 3
    private void ActionPerformed(java.awt.event.ActionEvent evt) {                                       if(evt.getSource()==btn_1){       checkTurns();                       // check whos turn is this        btn_1.setText(letter);          // set the  appropriate letter on the Button e.g X or O        btn_1.setEnabled(false);      // disable button afetr press           ShowResults();              // to Compute Result}    else if(evt.getSource()==btn_2){       checkTurns();                       // check whos turn is this        btn_2.setText(letter);          // set the  appropriate letter on the Button e.g X or O        btn_2.setEnabled(false);   // disable button afetr press           ShowResults();              // to Compute Result} else if(evt.getSource()==btn_3){       checkTurns();                       // check whos turn is this        btn_3.setText(letter);          // set the  appropriate letter on the Button e.g X or O        btn_3.setEnabled(false);   // disable button afetr press           ShowResults();              // to Compute Result}   }

EditMethod 4 of 8: Create ShowResult Method

  1. 1
    public  void ShowResults(){    if(turns>=5){    findHorzWin();    findVertWin();    findDiaWin();    }               if(win==true){        JOptionPane.showMessageDialog(this, letter+" is the winner  Mabrook");   reset();    }    else if (turns==9 && win==false){        JOptionPane.showMessageDialog(this, "The Game is a Tie");        }                       }

EditMethod 5 of 8: Create findHorzWin() method

  1. 1
    public  void  findHorzWin(){        if(btn_1.getText().equals(btn_2.getText()) &&btn_2.getText().equals(btn_3.getText())&& btn_1.getText().equals("")==false )    win=true;   else if(btn_4.getText().equals(btn_5.getText()) &&btn_5.getText().equals(btn_6.getText())&& btn_4.getText().equals("")==false )    win=true;   else if(btn_7.getText().equals(btn_8.getText()) &&btn_8.getText().equals(btn_9.getText())&& btn_7.getText().equals("")==false )    win=true;                }

EditMethod 6 of 8: Create findVertWin() method

  1. 1
    public  void  findVertWin(){        if(btn_1.getText().equals(btn_4.getText()) &&btn_4.getText().equals(btn_7.getText())&& btn_1.getText().equals("")==false )    win=true;   else if(btn_2.getText().equals(btn_5.getText()) &&btn_5.getText().equals(btn_8.getText())&& btn_2.getText().equals("")==false )    win=true;   else if(btn_3.getText().equals(btn_6.getText()) &&btn_6.getText().equals(btn_9.getText())&& btn_3.getText().equals("")==false )    win=true;                }

EditMethod 7 of 8: Create findDiaWin() method

  1. 1
    public  void  findDiaWin(){        if(btn_1.getText().equals(btn_5.getText()) &&btn_5.getText().equals(btn_9.getText())&& btn_1.getText().equals("")==false )    win=true;   else if(btn_3.getText().equals(btn_5.getText()) &&btn_5.getText().equals(btn_7.getText())&& btn_3.getText().equals("")==false )    win=true;                   }

EditMethod 8 of 8: Create Reset() method

  1. 1
    public void reset(){              win=false;                                 turns=0;        btn_1.setText("");      btn_1.setEnabled(true);        btn_2.setText("");      btn_2.setEnabled(true);        btn_3.setText("");      btn_3.setEnabled(true);        btn_4.setText("");      btn_4.setEnabled(true);        btn_5.setText("");      btn_5.setEnabled(true);        btn_6.setText("");      btn_6.setEnabled(true);        btn_7.setText("");      btn_7.setEnabled(true);        btn_8.setText("");      btn_8.setEnabled(true);        btn_9.setText("");      btn_9.setEnabled(true);

--Mindscapestechnologies (talk) 19:09, 7 November 2014 (UTC) Asma Rizvi Mindscapes Technologies


== # http://mstechnologies.org https://www.facebook.com/groups/1575453492679717/ https://www.facebook.com/mindscapestechnologies ==

We could really use your help!

Can you tell us about
organizing rooms?
Yes I can
organizing rooms
how to organize your bedroom
Can you tell us about
cleaning clothes?
Yes I can
cleaning clothes
how to remove mildew from fabric
Can you tell us about
managing employees?
Yes I can
managing employees
how to empower employees
Can you tell us about
bodybuilding?
Yes I can
bodybuilding
how to become a bodybuilder
Thanks for helping! Please tell us everything you know about
...
Tell us everything you know here. Remember, more detail is better.
Tips
Provide Details.
Please be as detailed as possible in your explanation. Don't worry about formatting! We'll take care of it. For example:
Don't say: Eat more fats.
Do say: Add fats with some nutritional value to the foods you already eat. Try olive oil, butter, avocado, and mayonnaise.

Article Info

Categories: Format | Games

Recent edits by: Cltk9unit, Mindscapestechnologies

Thanks to all authors for creating a page that has been read 34 times.

Did this article help you?
Yes No