'Tic Tac Toe MVC GAME

I am trying to create a tic tac toe game according to MVC pattern. What I do understand is that in view is the grafic content. In model has all the logic thinking of the game, so for example, whos turn it is, who the winner is and etc... While controller has the listener function inside.

But What I don't understand is how I do connect them correctly.

I have crated the grafic content and it looks like enter image description here

How do I connect the 3 classes with each other? Have I done it correctly or missing something? And how do I get a respons when I am pressing the game panel?? I do believe I am calling the functions correctly, but since it's not working I guess not.

This is View:

    package test;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;

    public class View  extends JFrame{
    
    Random random = new Random(); 
    JFrame frame = new JFrame(); 
    JPanel panel = new JPanel();
    JPanel gamePanel = new JPanel(); 
    JLabel txt = new JLabel();
    JButton[] btns = new JButton[9]; 
    // this is going to be true, if player 1 is false than player 2 starts 
     boolean player; 

         Controller controller; 
         Model  model;

    
        public View (Controller controller, Model model){
            this.model = model;
            this.controller = controller;

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(700,700);
        //  frame.getContentPane().setBackground(new Color(50,50,50));
            frame.setLayout(new BorderLayout());
            
            // the img at the buttom of the frame 
            Icon userIcon = new ImageIcon("tttlogo.png");
            JLabel userLabel = new JLabel(userIcon, JLabel.CENTER);
            frame.add(userLabel, BorderLayout.SOUTH);
            
        // this is the menu deisgn 
    //      txt.setBackground(Color.RED);
//          txt.setForeground(Color.GRAY);
//          txt.setFont(new Font("Ink Free",Font.BOLD,18));
            txt.setHorizontalAlignment(JLabel.CENTER);
//          txt.setText("Tic-Tac-Toe");  

            txt.setText("Player O Score:   It's " +  player + "Turn Player X Score: " ); 
            txt.setOpaque(true);

        // black background on the menu bar thing 
            panel.setLayout(new BorderLayout());
            panel.setBounds(0,0,800,800);

        // make sure that the panels are places correctly 
            gamePanel.setLayout(new GridLayout(3,3));
        // this is just the color. 
            gamePanel.setBackground(new Color(150,150,150));
        
            panel.add(txt); 
            frame.add(panel,BorderLayout.NORTH); 
            frame.add(gamePanel);
            
            frame.setVisible(true);

                for(int i=0; i<9;i++) {
                    btns[i] = new JButton();
                    gamePanel.add(btns[i]);
                    btns[i].setFont(new Font("MV Boli", Font.BOLD,120));
                    btns[i].setFocusable(false);
                    btns[i].setActionCommand("X");
                    btns[i].addActionListener(controller);
                }
                model.firstTurn(); 
                model.check();
            }
    }

This is Model:

package test;

import java.awt.*;
import javax.swing.*;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JLabel;

public class Model{
    
    // textfield the mark if it's X or O that are placeing it 
        private JButton btns = new JButton();
        private JLabel txt = new JLabel();
        private Random random = new Random();

        Boolean player = true; 
        int size;
        int [][] board;


    public  void firstTurn() {

        System.out.println("MEEEEEEE ");
        if(random.nextInt(2)==0) {
            player=true;
            txt.setText("X turn");
        }
        else {
            player=false;
            txt.setText("O turn");
        }
    }

    public boolean check(){
    System.out.println("Check ");
        boolean leftWin = true;
        boolean rightWin = true;


        for(int j = 1; j < this.size; j++){
            boolean colWin = true;
            boolean rowWin = true;
            for(int i =0; i < this.size; i++){
                colWin = colWin && (board[i][j] == board[i][j-1]);
                rowWin = rowWin && (board[j][i] == board[j-1][i]);
            }
            if ((rowWin && board[j][0] != -1) || (colWin && board[0][j] != -1)){
                return true;
            }
            leftWin = leftWin  && (board[j][j] == board[j-1][j-1]);
            rightWin = leftWin  && (board[this.size - j][j] == board[this.size - j+1][j-1]);
        }
        return (rightWin && board[0][0] != -1) || (leftWin && board[this.size-1][0] != -1);
    }
  
public static void main(String []args){ 

    Model model = new Model(); 
   Controller controller = new Controller(model); 
  View view = new View(controller , model); 
    //View view = new View (controller); 
    controller.setView(view); 
//  Model view = new Model ();
//  new View();
    /*
    Model model = new Model(); 
    Controller controller = new Controller(model); 
    View view = new view( controller , model ); 
    new v();
    */
    
}

}

This is the Controller:

package test;

import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Controller extends WindowAdapter implements ActionListener{

    private View view; 
    private Model model;

        JPanel btnPanel = new JPanel(); 
        JLabel textfield = new JLabel();
        JButton[] btns = new JButton[9]; 
        boolean player; 
        Controller controller; 

    public Controller(View view, Model model) {
        this.view = view;
        this.model = model;

    }

    public void setView(View view) {
        this.view = view; 
}

public Controller(Controller controller) {
    this.controller = controller;
}



public Controller(Model model) {
    this.model = model; 

}

    @Override
    public void actionPerformed(ActionEvent event) {

   System.out.println("Pretyt"); 
        for(int i=0; i<9; i++) {
            System.out.println("pj"); 
        if(event.getActionCommand().equals(btns[i]))
            {
                    System.out.println("coffe"); 
                if(player) {
                    if(btns[i].getText()=="") {
                        btns[i].setText("X");
                        player=false; 
                        textfield.setText("O turn");
                        model.check();
                    }
                }
                else
                {
                    if(btns[i].getText()=="") {
                        btns[i].setText("O");
                        player=true; 
                        textfield.setText("X turn");
                        model.check();  
                }
            }
        }
        
    }
    
}

        
    }


Solution 1:[1]

No you are doing it wrong first of all all the logic of the game should be in the controller the model should only have the board stored as a array,so if i would approach this i would create a class called GameBoard that extends java observable

  public class GameBoard extends observable{
char[] board;
    
GameBoard(){
    this.board = new char[9];
}
// all of the logic as methods
}

then what i would do is pass this class as argument of the constructor of JFrame and would make the JFrame implement the observer interface

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ziad Howayek