nixufix 0 Newbie Poster

Hi,

To start with I'm new to Java and this is my second project: to create a graphical TicTacToe game with MVC pattern. View and Control will be in the same class for this project. The goal is to learn about interfaces and to split your problems into seperate parts. I know the basics of MVC and where to put things but it keeps getting confusing.

For now I have four classes (see below) and the graphics seems to be in place. I can also put X's down and I seem to be able to find each button's position (for later win/loose checks). This is where it gets tricky. What I need is some pointers or examples how to move on. I need to get the player's turn method to really work and change the "X" to "O" and I think I have some stuff in the wrong class, so the structures itself confuses me, and don't really function as the MVC should. And then ofcourse I will implement the logic of the game and win conditions etc.

Any pointers or help would be appreciated. Here comes my four classes:

Interface class

public interface Boardgame {
   boolean move(int i, int j);
   String getStatus(int i, int j);
   String getMessage(int i, int k);
}

Model class for game logics

import javax.swing.JLabel;

public class TicTacToe implements Boardgame {

    int counter;
    JLabel spelare;

    public boolean move(int i, int j){
        //return true if success otherwise false
        //if bla bla...
        return false;
    } 
    public String getStatus(int i, int j){
        return null;
    }

    public String getMessage(int i, int k) {
        System.out.println("button "+ i+","+k);
        return null;
        //reports whos turn it is
    }

    public TicTacToe(){

    }
}

Model class for the buttons

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

@SuppressWarnings("serial")
public class Square extends JButton implements ActionListener {
    String strx; String stro; String state; TicTacToe theGame; int i; int counter; int k; ViewControl spe;

    public Square(String st, TicTacToe game, int ii, int kk, ViewControl s, String marker) {
        strx = "X"; stro = "O"; state = st; theGame = game; i=ii; k=kk; spe=s;
        setPreferredSize(new Dimension(40,40));
        addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        //Start on "whos turn" check.
        if (state.equals("")){
            theGame.getMessage(i, k);
            counter=counter+1;
            this.setText(strx);     
        }
        else if (state.equals("O")){
            this.setText(stro);         
        }
        else if (state.equals("X")){
            this.setText(strx);
        }
    }
}

View-Control class

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

@SuppressWarnings("serial")
class ViewControl extends JFrame implements ActionListener {

    //private Boardgame game;
    //private int size;
    private Square[][] board;
    private JLabel player = new JLabel();
    int width; int height;
    int counter;

    public ViewControl(){

        board = new Square[3][3];
        width = 500;
        height = 500;
        int n = 3;
        int i = 0;
        TicTacToe spelet = new TicTacToe();

        //mainpanel and buttonpanel
        JPanel mainpanel = new JPanel();
        mainpanel.setLayout(new BorderLayout());
        add(mainpanel);
        mainpanel.setBackground(Color.LIGHT_GRAY);
        JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new GridLayout(3,3));
        mainpanel.add(buttonpanel, BorderLayout.CENTER);

        //Player text
        player = new JLabel("Welcome to TicTacToe, player 1: make your move");
        mainpanel.add(player, BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(width, height);

        setVisible(true);

        //Create buttons
        for(i=0; i<n; i++){
            for(int k = 0; k<3; k++){
                Square button = new Square("", spelet, i, k, this, "");
                board[i][k] = button;
                buttonpanel.add(button, BorderLayout.CENTER);
                button.addActionListener(this);

            }   
        }

    }

    public void updateView(){

    }

    public void position(int i, int j){

    }


    public String vemsTur(int counter, Square aktknapp){
        if (counter%2 == 0) {
            player.setText("Player 1's turn");
            String marker = "X";
            return marker;

        } else {
            player.setText("Player 2's turn");
            String marker = "O";
            return marker;

        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Square currentButton = (Square)e.getSource();
        counter=counter+1;
        this.vemsTur(counter, currentButton);

    }
    public static void main(String[] args){
        new ViewControl();

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.