Old Game - Implement logic

2

Well, my final work of the semester is to put together a game of the old one in java, I made all the look of it, but the problem comes from there: I need to make every click of the mouse change the button icon (first click an X, second click an O), and that when repeating horizontally, vertically or diagonally, a JOptionPane appears with the message "you won" or the name of the winner, such thing ... how to do these actions? I can not, could anyone help? Here is the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Jogo extends JFrame{
    private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
    private JLabel l1;
    private Dimension dim;
    private Font f;

        public Jogo(){
            dim = new Dimension (90,90);
            setTitle("Jogo da Velha");
            setBounds(300,160,400,450);
            setResizable(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            getContentPane().setBackground(new Color (197,197,197));
            setLayout(null);

            f = new Font("Tahoma",Font.ITALIC,17);
            setFont(f);

            JMenuBar mbar = new JMenuBar();
            JMenu opcoes = new JMenu("Opções");
            JMenu sair = new JMenu("Sair");

            JMenuItem sMim = new JMenuItem("Sobre mim");
            JMenuItem sprogram = new JMenuItem("Sobre o Programa");

            opcoes.add(sMim);
            opcoes.add(sprogram);

            mbar.add(opcoes);
            mbar.add(sair);

            setJMenuBar(mbar);

            l1 = new JLabel();
            l1.setText("Gabriel Ozzy Santos");
            l1.setBounds(130,10,200,30);
            l1.setFont(f);
            this.add(l1);

                b1 = new JButton();
                b1.setSize(dim);
                b1.setLocation(30,50);
                this.add(b1);

                b2 = new JButton();
                b2.setSize(dim);
                b2.setLocation(150,50);
                this.add(b2);

                b3 = new JButton();
                b3.setSize(dim);
                b3.setLocation(270,50);
                this.add(b3);

                b4 = new JButton();
                b4.setSize(dim);
                b4.setLocation(30,170);
                this.add(b4);

                b5 = new JButton();
                b5.setSize(dim);
                b5.setLocation(150,170);
                this.add(b5);

                b6 = new JButton();
                b6.setSize(dim);
                b6.setLocation(270,170);
                this.add(b6);

                b7 = new JButton();
                b7.setSize(dim);
                b7.setLocation(30,290);
                this.add(b7);

                b8 = new JButton();
                b8.setSize(dim);
                b8.setLocation(150,290);
                this.add(b8);

                b9 = new JButton();
                b9.setSize(dim);
                b9.setLocation(270,290);
                this.add(b9);
        }
        public static void main (String [] args){
            new Jogo().setVisible(true);
        }
}
    
asked by anonymous 31.05.2014 / 07:22

2 answers

6

Now what is a school work, there is no more opportune moment to learn.

Some suggestions regarding your code:

1. Separation of Responsibilities

I noticed that there is still no business logic in your code, which is a good sign. I understand that there is a natural willingness to put the business code mixed with the vision, but avoid it at all costs.

In other words, you must assemble the old game logic out of the Game class. Assemble a class called OldGame and include the attributes and methods necessary for an old game to play from start to finish. Once you have the class ready and tested, then yes you will worry about the vision.

Separating vision from business is of paramount importance. Basically, it can be said that it is one of the pillars that support the really great software that are made today. In this context, study the MVC (Model-Controller-Vision) standard, because it is something meant to separate those responsibilities.

Always think about responsibilities. What is the role of your GameDaVelha class? Do you agree that this class should simply control the instance of a particular game? In other words, this class does not want to know if the game is played over the Web, the Desktop, or even the Console. It does not matter to her. In fact, she is not even "aware" of where she is being played.

2. Vision

I will not go into the controller's merit here, let's go straight to the vision to make it easier for you to understand how everything connects.

In the Game class above, one of the attributes will be of the type of your GameGame class. You should instantiate this object and work with it on each event generated by its buttons. Working with it means making calls to methods and changing their state (changing the value of attributes) as the game happens.

In order for you to take action when the user clicks a button, you must implement an ActionListener. Basically, it is the most common way of making an event handler. It is an event because it is triggered by an external entity, in this case your user will click on the button and this will generate an event.

Example:

b1 = new JButton();
b1.setSize(dim);
b1.setLocation(30,50);
this.add(b1);
b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       System.out.println("You clicked the button");
    });

The code above, although verbose, is not complicated to understand. What is being done is the inclusion of an ActionListener in the list of ActionListeners of the b1 button. So when the button is clicked, it will notify the objects that are in this list, calling, for each, the actionPerformed method.

See an example with two ActionListeners:

    b1 = new JButton();
    b1.setSize(dim);
    b1.setLocation(30,50);
    this.add(b1);
    b1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("OI");
        }
    });

    b1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("OI2");
        }
    });

So when you click, the two actionPerformed above will be executed.

Notice that the objects of type ActionListener created are anonymous. Anonymous, because the object does not have a variable from which you can reference it in the "outside world".

In the above case, we are only printing a message on the console. For the old woman's game, the idea is to do two things: Change the state of your old game and check if the game is over. Changing the state means marking in the game's internal data structure that a certain cell has been marked with X or O.

Of course at the time of implementing difficulties will appear and you can take another path, but the idea of separating responsibilities should always be your biggest concern.

    
31.05.2014 / 14:49
1

About the button: In the Click event you check the text. If the text is "" set to "X", if it is "X" set it to "O", if it is "O" set it to "". Multiple if or switch resolves this.

About victory: With each completed move you check all the buttons, checking their contents and seeing if each of the winning chances are met. And then it displays the message. For this, you can check in the button text or use variables that are also defined in the backend to control that data.

    
17.09.2015 / 22:49