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

Posted by Zopyrus on Stack Overflow See other posts from Stack Overflow or by Zopyrus
Published on 2010-04-21T23:55:33Z Indexed on 2010/04/22 0:33 UTC
Read the original article Hit count: 405

Filed under:
|

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

}

© Stack Overflow or respective owner

Related posts about java

Related posts about mvc