reorganized code

This commit is contained in:
CT
2026-04-21 22:43:13 -05:00
parent 94e4377364
commit 7f9cdfea63
4 changed files with 26 additions and 15 deletions

BIN
Board.class Normal file

Binary file not shown.

23
Board.java Normal file
View File

@@ -0,0 +1,23 @@
import java.awt.*;
public class Board {
Piece[][] board;
public Board() {
board = new Piece[8][8];
for (int i = 0; i <= 7; i++) {
board[i][2] = new Pawn(i + 1, 2, "Black");
}
for (int i = 0; i <= 7; i++) {
board[i][7] = new Pawn(i + 1, 7, "Black");
}
}
public void draw(Graphics g) {
for (Piece[] row : board) {
for (Piece p : row) {
if (p != null) p.draw(g);
}
}
}
}

Binary file not shown.

View File

@@ -1,15 +1,12 @@
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
public class Chess extends JPanel implements ActionListener {
// pieces stuff
ArrayList<Piece> white;
ArrayList<Piece> black;
Board board;
// game vars
int boardWidth, boardHeight;
@@ -25,15 +22,7 @@ public class Chess extends JPanel implements ActionListener {
setBackground(Color.WHITE);
setFocusable(true);
white = new ArrayList<>();
black = new ArrayList<>();
for (int i = 0; i <= 7; i++) {
white.add(new Pawn(i + 1, 2, "White"));
}
for (int i = 0; i <= 7; i++) {
black.add(new Pawn(i + 1, 7, "Black"));
}
board = new Board();
gameTimer = new Timer(200, this);
@@ -54,8 +43,7 @@ public class Chess extends JPanel implements ActionListener {
}
// draw pieces
for (Piece p : white) p.draw(g);
for (Piece p : black) p.draw(g);
board.draw(g);
}
public void paintComponent(Graphics g) {