1

I'm building a board game for a cs course, I've started with the engine - its backbone - and I've perfected it, I'm still stuck on the GUI part though. In the engine, I have a double array that initialises the board with the pieces, and it has a getWinner and move methods. In the GUI part, I created my JFrame and set its layout to gridlayout, and I've another double array there that checks the engine's array and prints the board with the board pieces accordingly.. however, whenever I move, the engine's array registers the move but it doesn't get displayed on my JFrame.. I've no idea on how to do it.. Here's my Main class in the GUI package:

package eg.edu.guc.loa.gui;

import java.awt.*;
import java.util.ArrayList;

import javax.swing.*;

import eg.edu.guc.loa.engine.*;
import eg.edu.guc.loa.engine.Point;

@SuppressWarnings("serial")
public class LOA extends JFrame{

    static Tiles[][] Jboard;
    static Board b = new Board();
    static Color temp;
    static Color col1 = Color.DARK_GRAY;
    static Color col2 = Color.LIGHT_GRAY;
    static JFrame LOA = new JFrame();
    JButton b1, b2;

    public LOA(){
        Jboard = new Tiles[8][8];
        LOA.setLayout(new GridLayout(8, 8));
        initBoard();
        LOA.setSize(600, 600);
        LOA.setVisible(true);
        LOA.setLocationRelativeTo(null);
        LOA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b.printBoard();
    }

    public static void initBoard(){
        remove();
        b.printBoard();

        for(int i = 0; i<8; i++){
            if (i%2 == 0){
                temp = col1;
            }
            else{
                temp = col2;
            }
            for(int j = 0; j<8; j++){
                Jboard[i][j] = new Tiles(temp, i, j);
                LOA.getContentPane().add(Jboard[i][j]);
                if (temp.equals(col1)){
                    temp = col2;
                }
                else{
                    temp = col1;
                }

                if(b.getPiece(new Point(j, i)).getPlayer() == BoardCell.PLAYER_1_PIECE){
                    Jboard[i][j].hasChecker(true);
                    Jboard[i][j].setWhite(true);
                    Jboard[i][j] = new Tiles(temp, i, j);

                }
                if(b.getPiece(new Point(j, i)).getPlayer() == BoardCell.PLAYER_2_PIECE){
                    Jboard[i][j].hasChecker(true);
                    Jboard[i][j].setWhite(false);
                    Jboard[i][j] = new Tiles(temp, i, j);
                }

            }
        }
    }

    public static void remove(){
        for(int i = 0; i<8;i++){
            for(int j = 0; j<8; j++){
                if(Jboard[i][j] != null)
                    LOA.remove(Jboard[i][j]);
            }
        }
    }



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

        }

}

Any help will be much appreciated.. Thanks in advance.

EDIT: b.print() prints the Engine's array on the console, and remove() is just my attempt on removing the old frame and building a new one based on the new updated array (with moved piece), which obviously failed.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
mhalabi
  • 31
  • 6

1 Answers1

1

As shown in this much simpler MVCGame, you can arrange for your view to register as a listener to your model using the observer pattern. User gestures should update the model; when notified by the model, the view should simply render the current state of the model. There should be no drawing in the model and no game logic in the view.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045