I am trying to add a twist to the game of chess, where the game is decided by a taking the most pieces. Each piece is worth 1 point, and the game ends when one player has 10 points.
When there is a "kill"/the piece is removed, a message needs to update so as to show the score. This code below shows the part where the "kill" code is. I just coded a "System.out.println" in the method to test if an output will show if a piece is "killed".
import Menus.GameUI;
import java.util.LinkedList;
public class Piece {
public int xPos;
public int yPos;
public int x;
public int y;
public boolean isWhite;
LinkedList<Piece> ps;
public String type;
public Piece(int xPos, int yPos, boolean isWhite, String type, LinkedList<Piece> ps) {
this.xPos = xPos;
this.yPos = yPos;
x = xPos*64;
y = yPos*64;
this.isWhite = isWhite;
this.ps = ps;
this.type = type;
ps.add(this);
}//constructor
public boolean move(int xPos, int yPos){//allows the piece to move
if(GameUI.getPiece(xPos*64, yPos*64) != null){
if(GameUI.getPiece(xPos*64, yPos*64).isWhite != isWhite){
GameUI.getPiece(xPos*64, yPos*64).kill();
//check that a piece is not the same colour, so it can be "killed"/removed
}else{
x = this.xPos*64;
y = this.yPos*64;
return true;
//ensures a piece cannot move to the same square if a piece of the same colour is already on
}//end of if
}//end of if
this.xPos = xPos;
this.yPos = yPos;
x = xPos*64;
y = yPos*64;
return false;
}//move
public int kill(){//removes a piece if a piece of the other colour is on the same square
int score = 1;
ps.remove(this);
System.out.println(score);
return score;
}//kill
This is the main class which would show the score:
import Pieces.Pawn;
import Pieces.Piece;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
/**
*
* @author V
*/
public class GameUI {
public static LinkedList<Piece> ps = new LinkedList<>();
public static Piece selPiece = null;
private static JLabel wMess = new JLabel(" ScoreW: ");
private static JLabel bMess = new JLabel(" ScoreB: ");
//public static final int MAX_HEALTH = 10;
public static void main(String[] args) throws IOException{
//extracts the images
BufferedImage all = ImageIO.read(new File("chess.png"));
Image img[] = new Image[12];
int ind = 0;
for(int y = 0; y < 400; y = 200){
for(int x = 0; x < 1200; x = 200){
img[ind] = all.getSubimage(x, y, 200, 200).getScaledInstance(64, 64, BufferedImage.SCALE_SMOOTH);
ind ;
}//end of for
}//end of for
//Displays the white pieces on the board
Piece wKing = new Piece(4, 7, true, "King", ps) {};
Piece wQueen = new Piece(3, 7, true, "Queen", ps) {};
Piece wBishop = new Piece(2, 7, true, "Bishop", ps) {};
Piece wBishop2 = new Piece(5, 7, true, "Bishop", ps) {};
Piece wKinght = new Piece(1, 7, true, "Knight", ps) {};
Piece wKnight2 = new Piece(6, 7, true, "Knight", ps) {};
Piece wRook = new Piece(0, 7, true, "Rook", ps) {};
Piece wRook2 = new Piece(7, 7, true, "Rook", ps) {};
for(int i = 0; i < 8; i ){
//displays the pawns across the board
//"i" is equal to the horizontal tile position on board
Piece wPawn = new Pawn(i, 6, true, "Pawn", ps) {};
}//for
//Displays the black pieces
Piece bKing = new Piece(4, 0, false, "King", ps) {};
Piece bQueen = new Piece(3, 0, false, "Queen", ps) {};
Piece bBishop = new Piece(2, 0, false, "Bishop", ps) {};
Piece bBishop2 = new Piece(5, 0, false, "Bishop", ps) {};
Piece bKinght = new Piece(1, 0, false, "Knight", ps) {};
Piece bKnight2 = new Piece(6, 0, false, "Knight", ps) {};
Piece bRook = new Piece(0, 0, false, "Rook", ps) {};
Piece bRook2 = new Piece(7, 0, false, "Rook", ps) {};
for(int i = 0; i < 8; i ){
//displays the pawns across the board
//"i" is equal to the horizontal tile position on board
Piece bPawn = new Piece(i, 1, false, "Pawn", ps) {};
}//for
JFrame frame = new JFrame();
frame.setBounds(10, 10, 1024, 512);
JPanel panel = new JPanel(new BorderLayout());
JToolBar tools = new JToolBar();
JButton exit = new JButton("exit");
tools.setFloatable(false);
frame.add(tools,BorderLayout.AFTER_LINE_ENDS);
tools.add(wMess);//displays the health of the white side
tools.addSeparator();
tools.add(bMess);//displays the helath of the black side
tools.addSeparator();
tools.add(exit);
//exit.addActionListener(this);
frame.setUndecorated(true);//removes border so board is seen in full
JPanel pn = new JPanel(){
@Override
public void paint(Graphics g){
boolean white = true;
for(int y = 0; y < 8; y ){
for(int x = 0; x < 8; x ){
if(white){
g.setColor(new Color(0, 0, 255));
}else{
g.setColor(new Color(255, 0, 0));
}//end of if
g.fillRect(x*64, y*64, 64,64);
white =! white;//check value of white, and reverses it
}//end of for
white =! white;
}//end of for
for(Piece p: ps){
int imagePos = 0;
if(p.type.equalsIgnoreCase("King")){
imagePos = 0;
}
if(p.type.equalsIgnoreCase("Queen")){
imagePos = 1;
}
if(p.type.equalsIgnoreCase("Bishop")){
imagePos = 2;
}
if(p.type.equalsIgnoreCase("Knight")){
imagePos = 3;
}
if(p.type.equalsIgnoreCase("Rook")){
imagePos = 4;
}
if(p.type.equalsIgnoreCase("Pawn")){
imagePos = 5;
}
if(!p.isWhite){
imagePos = 6;
}//if
//checks the position of the piece in the image
g.drawImage(img[imagePos], p.xPos*64, p.yPos*64, this);
}//end of for
}//paint
};
frame.add(pn);
frame.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
//allows the pieces to be dragged to desired square
if(selPiece != null){
selPiece.x = e.getX()-32;
selPiece.y = e.getY()-32;
frame.repaint();
}//end of if
}
@Override
public void mouseMoved(MouseEvent e) {
}
});
frame.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
//selects the piece when mouse is pressed
selPiece = getPiece(e.getX(), e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
if(selPiece == null){
return;
//if tile is selected with nothing on it, nothing happens
}//end of if
selPiece.move((e.getX()/64), (e.getY()/64));
frame.repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
}//end of main
public static Piece getPiece(int x, int y){
int xPos = x/64;
int yPos = y/64;
for(Piece p: ps){
if(p.xPos == xPos && p.yPos == yPos){
return p;
}//end of if
}//end of for
return null;
}//getPiece
}//end of class
Now how would I be able to show this "score" in the main class under the "wMess" or "bMess" JLabel which would hold the score and continuously update when a piece is "killed"? The other problem is that it will not be able to determine which colour "killed" which, so it will not know which score to update. How can I fix these things?
CodePudding user response:
Most likely not close to the best solution but:
I think you can get the "killed" counter by using an ActionListener together with a timer.
I used something similar to regularly check how many Files were selected in the explorer:
JLabel label = new JLabel();
int delay = 5; //time to wait between updates in milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int row = table.getSelectedRowCount(); //this was for me to get the amount of selected rows
String selected = String.valueOf(row); //change int to String
label.setText(selected " Files selected"); //set text shown
}
};
new Timer (delay, taskPerformer).start();
obviously you would need to find another way to get the amount of kills. However, I assume you could implement a counter basically, which checks for The "kill" action.
Regarding your other question. Could you not simply put the Label in the center of a frame, while you have the others put to north and south? Maybe I misunderstood something.
hope it's somewhat useful.
CodePudding user response:
In my opinion, the way you have structured your code is poor. You have a