Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

model view controller - Java MVC - How to divide a done text game into MVC?

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

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.

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("
Nicely Done!");
    return;
}

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

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

}

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

}

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

MVC diagram

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...