繁体   English   中英

井字游戏 MVC 游戏

[英]Tic Tac Toe MVC GAME

我正在尝试根据 MVC 模式创建井字游戏。 我所理解的是,在视图中是图形内容。 在 model 中有游戏的所有逻辑思维,例如,轮到谁,谁赢了等等......而 controller 中有监听器 function。

但我不明白的是我如何正确连接它们。

我已经装箱了图形内容,它看起来像在此处输入图像描述

如何将这 3 个类相互连接? 我做得正确还是遗漏了什么? 当我按下游戏面板时如何获得响应? 我确实相信我正确地调用了函数,但由于它不起作用,我猜不是。

这是视图:

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

这是 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();
    */
    
}

}

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

        
    }

不,你做错了首先,游戏的所有逻辑应该在 controller 中,model 应该只将棋盘存储为数组,所以如果我接近这个,我会创建一个名为 class 的 GameBoard 扩展 java observable

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

然后我要做的是将这个 class 作为 JFrame 的构造函数的参数传递,并使 JFrame 实现观察者接口

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM