Java MVC - 如何将完成的文本游戏划分为MVC? [英] Java MVC - How to divide a done text game into MVC?

查看:130
本文介绍了Java MVC - 如何将完成的文本游戏划分为MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在坐在这里几个小时试图解决这个问题,所以对这个大问题有点同情。 :)

Been sitting here for hours now trying to figure this out, so a bit sympathy for this large question. :)

目标:我只想将完成的代码分成MVC(模型视图控制器)部分。我已完成游戏逻辑并基于文本 - 代码工作正常。

The Goal: I simply want to divide my done code into MVC (Model View Controller) parts. I have the game logics done and text based - the code works fine.

问题:嗯,我想将此代码实现到MVC中,但在哪里解释它应该使用基于文本的MODEL ?因为VIEW仅用于布局(图形)正确吗?我真的很难找到从哪里开始。任何指针都会很棒!

The Problem: Well, I want to implement this code into MVC, but where do explain for the MODEL that it should use text-based? Because the VIEW is only for the layout (graphically) correct? I am having a REALLY hard time figuring out where to begin at all. Any pointers would be so nice!

这是我的游戏逻辑代码:

Here is my game logics code:

import mind.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;
import java.awt.*;

public class Drive {
String[] mellan;
boolean gameEnd, checkempty, checkempty2, enemy, enemy2;
String gr,rd,tom;
int digits;

public Drive() {
    // Gamepieces in textform
    gr="G"; rd="R"; tom=" ";


    mellan = new String[7];
    String[] begin = {gr,gr,gr,tom,rd,rd,rd};
    String[] end = {rd,rd,rd,tom,gr,gr,gr};

    //input
    Scanner in = new Scanner(System.in);

    mellan=begin;
    gameEnd=false;
    while (gameEnd == false) {
        for(int i=0; i<mellan.length; i++) {
            System.out.print(mellan[i]);
        }
        System.out.print("        Choose 0-6: ");

        digits = in.nextInt();
        move();
        checkWin();
    }
}

void move() {
    //BOOLEAN for gameruls!!!
    checkempty = digits<6 && mellan[digits+1]==tom;
    checkempty2 = digits>0 && mellan[digits-1]==tom;
    enemy = (mellan[digits]==gr && mellan[digits+1]==rd &&     mellan[digits+2]==tom);
    enemy2 = (mellan[digits]==rd && mellan[digits-1]==gr && mellan[digits-2]==tom);

    if(checkempty) {
        mellan[digits+1]=mellan[digits];
        mellan[digits]=tom;
    } else if (checkempty2) {
        mellan[digits-1]=mellan[digits];
        mellan[digits]=tom;
    } else if (enemy) {
        mellan[digits+2]=mellan[digits];
        mellan[digits]=tom;
    } else if (enemy2) {
        mellan[digits-2]=mellan[digits];
        mellan[digits]=tom;
    }
}

void checkWin() {
    String[] end = {rd,rd,rd,tom,gr,gr,gr};
    for (int i=0; i<mellan.length; i++){
    }
    if (Arrays.equals(mellan,end)) {
        for (int j=0; j<mellan.length; j++) {
            System.out.print(mellan[j]);
        }
        displayWin();
    }
}

void displayWin() {
    gameEnd = true;
    System.out.println("\nNicely Done!");
    return;
}

// Kör Drive!
public static void main(String args[]) {
    new Drive();
}
}

这是我到目前为止定义DriveView的方式:(只是想让一个按钮工作)

Here is how I defined my DriveView thus far: (just trying to make one button to work)

import mind.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;

public class DriveView extends JFrame {
JButton ruta1 = new JButton("Green");
JButton ruta2 = new JButton("Green");
JButton rutatom = new JButton("");
JButton ruta6 = new JButton("Red");
private DriveModel m_model;

public DriveView(DriveModel model) {
    m_model = model;

    //Layout for View
    JPanel myPanel = new JPanel();
    myPanel.setLayout(new FlowLayout());
    myPanel.add(ruta1);
    myPanel.add(ruta2);
    myPanel.add(rutatom);
    myPanel.add(ruta6);
    this.setContentPane(myPanel);
    this.pack();
    this.setTitle("Drive");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

void addMouseListener(ActionListener mol) {
    ruta2.addActionListener(mol);
}

}

和DriveController在编译时给出错误

And DriveController which gives me error at compile

import mind.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;

public class DriveController {
private DriveModel m_model;
private DriveView m_view;

public DriveController(DriveModel model, DriveView view) {
    m_model = model;
    m_view = view;

    view.addMouseListener(new MouseListener());
}

class MouseListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String mening;
        mening = e.getActionCommand();
        if (mening.equals("Green")) {
            setForeground(Color.red);
        }
    }
}

}

推荐答案

您的游戏模型可以有多个视图:GUI视图,控制台视图,状态视图等。通常每个视图都安排收听对于模型中的更改,然后查询模型以获取其呈现特定视图所需的信息。这个简单的游戏专门用于说明概念。名为设计的部分详细阐述。

Your game model can have more than one view: a GUI view, a console view, a status view, etc. Typically each view arranges to listen for changes in the model, and it then queries the model for the information it needs to render it's particular view. This simple game was designed specifically to illustrate the concepts. The section named "Design" elaborates in more detail.

附录:此大纲大致对应于此 architecture ,如下所示。

Addendum: This outline corresponds roughly to this architecture, symbolized below.

public class MVCOutline {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            //@Override
            public void run() {
                new MVCOutline().create();
            }
        });
    }

    private void create() {
        JFrame f = new JFrame("MVC Outline");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MainPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class MainPanel extends JPanel {

    public MainPanel() {
        super(new BorderLayout());
        Model model = new Model();
        View view = new View(model);
        Control  control = new Control(model, view);
        this.add(view, BorderLayout.CENTER);
        this.add(control, BorderLayout.WEST);
    }
}

class Control extends JPanel implements ... {

    private Model model;
    private View view;

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

class View extends JPanel implements Observer {

    private Model model;

    public View(Model model) {
        this.model = model;
        model.addObserver(this);
    }

    public void update(Observable o, Object arg) {
        // update GUI based on model
    }
}

class Model extends Observable {

    public void next() {
        this.notifyObservers(...);
    }
}

这篇关于Java MVC - 如何将完成的文本游戏划分为MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆