Commit 7bc51122 authored by kimia's avatar kimia

GUI

parent 633367f9
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/src.iml" filepath="$PROJECT_DIR$/.idea/src.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="7f2b8b21-3095-45eb-bb8e-52b707f4640b" name="Default" comment="" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="TRACKING_ENABLED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="GradleLocalSettings">
<option name="externalProjectsViewState">
<projects_view />
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="-11" />
<option name="y" value="-11" />
<option name="width" value="697" />
<option name="height" value="735" />
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" proportions="" version="1">
<flattenPackages />
<showMembers />
<showModules />
<showLibraryContents />
<hideEmptyPackages />
<abbreviatePackageNames />
<autoscrollToSource />
<autoscrollFromSource />
<sortByType />
<manualOrder />
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="PackagesPane" />
<pane id="ProjectPane" />
<pane id="AndroidView" />
<pane id="Scope" />
<pane id="Scratches" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="settings.editor.selected.configurable" value="preferences.externalResources" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration default="true" type="Applet" factoryName="Applet">
<option name="HTML_USED" value="false" />
<option name="WIDTH" value="400" />
<option name="HEIGHT" value="300" />
<option name="POLICY_FILE" value="$APPLICATION_HOME_DIR$/bin/appletviewer.policy" />
<module />
</configuration>
<configuration default="true" type="Application" factoryName="Application">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" />
<option name="PROGRAM_PARAMETERS" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="ENABLE_SWING_INSPECTOR" value="false" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<module name="" />
<envs />
</configuration>
<configuration default="true" type="JUnit" factoryName="JUnit">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<module name="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PACKAGE_NAME" />
<option name="MAIN_CLASS_NAME" />
<option name="METHOD_NAME" />
<option name="TEST_OBJECT" value="class" />
<option name="VM_PARAMETERS" value="-ea" />
<option name="PARAMETERS" />
<option name="WORKING_DIRECTORY" value="%MODULE_WORKING_DIR%" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="singleModule" />
</option>
<envs />
<patterns />
</configuration>
<configuration default="true" type="Remote" factoryName="Remote">
<option name="USE_SOCKET_TRANSPORT" value="true" />
<option name="SERVER_MODE" value="false" />
<option name="SHMEM_ADDRESS" value="javadebug" />
<option name="HOST" value="localhost" />
<option name="PORT" value="5005" />
</configuration>
<configuration default="true" type="TestNG" factoryName="TestNG">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<module name="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="SUITE_NAME" />
<option name="PACKAGE_NAME" />
<option name="MAIN_CLASS_NAME" />
<option name="METHOD_NAME" />
<option name="GROUP_NAME" />
<option name="TEST_OBJECT" value="CLASS" />
<option name="VM_PARAMETERS" value="-ea" />
<option name="PARAMETERS" />
<option name="WORKING_DIRECTORY" value="%MODULE_WORKING_DIR%" />
<option name="OUTPUT_DIRECTORY" />
<option name="ANNOTATION_TYPE" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="singleModule" />
</option>
<option name="USE_DEFAULT_REPORTERS" value="false" />
<option name="PROPERTIES_FILE" />
<envs />
<properties />
<listeners />
</configuration>
<configuration default="true" type="#org.jetbrains.idea.devkit.run.PluginConfigurationType" factoryName="Plugin">
<module name="" />
<option name="VM_PARAMETERS" value="-Xmx512m -Xms256m -XX:MaxPermSize=250m -ea" />
<option name="PROGRAM_PARAMETERS" />
<predefined_log_file id="idea.log" enabled="true" />
</configuration>
</component>
<component name="ShelveChangesManager" show_recycled="false">
<option name="remove_strategy" value="false" />
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="7f2b8b21-3095-45eb-bb8e-52b707f4640b" name="Default" comment="" />
<created>1558206801393</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1558206801393</updated>
</task>
<servers />
</component>
<component name="ToolWindowManager">
<frame x="-11" y="-11" width="1942" height="1042" extended-state="6" />
<layout>
<window_info id="Palette" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Palette&#9;" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Image Layers" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Capture Analysis" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Capture Tool" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Designer" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="true" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="UI Designer" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Theme Preview" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
</layout>
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager />
<watches-manager />
</component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.8</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>
\ No newline at end of file
public class Bishop extends Piece {
public Bishop(Cell cell, Color color) {
super(cell, color) ;
public Bishop(Cell cell, PieceColor pieceColor) {
super(cell, pieceColor) ;
}
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
......@@ -7,6 +7,12 @@ class Cell {
this.col = col ;
this.empty = true ;
}
public Cell(Cell c){
if(c == null) return;
this.row = c.getRow();
this.col = c.col;
this.empty = c.empty;
}
public int getRow() {
return row ;
......@@ -34,6 +40,6 @@ class Cell {
}
enum Color {
enum PieceColor {
BLACK, WHITE
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class ChessBoardGUI extends JPanel {
public JButton[][] button = new JButton[8][8];
JPanel removedPiecePanel;
JButton removedPieces[][] = new JButton[8][4];
private int wORb = 0;
private Piece pw[];
private Piece pb[];
private Player white;
private Player black;
private Cell board[][];
private Cell source;
private Cell dest;
private boolean[][] moveBoard = null;
public ArrayList<Piece> deletedList = new ArrayList<>();
public ChessBoardGUI(JPanel removedPiecePanel){
super();
this.removedPiecePanel=removedPiecePanel;
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,600);
initlizeGame();
inilizeRemovedPiecePanel();
setLayout(new GridLayout(8,8));
Piece pieceBoard[][] = getUpdatedBoard();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
button[i][j] = new JButton();
int finalI = i;
int finalJ = j;
if(pieceBoard[i][j]!=null)
button[i][j].setIcon(new ImageIcon(pieceBoard[i][j].getPieceImage()));
button[i][j].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println(finalJ +","+ finalI);
source = new Cell(dest);
dest = new Cell(finalI,finalJ);
if(moveBoard!=null){
if(moveBoard[finalI][finalJ] && source!=null && dest!=null){
movePiece(source,dest);
// System.out.println("Moves");
buttonRepaint();
updateDeletedPanel();
//----------------
int condition = checkCondition(board,pw,pb);
if(condition==2){
JOptionPane.showMessageDialog(removedPiecePanel,"Condition White");
}
else if(condition==1){
JOptionPane.showMessageDialog(removedPiecePanel,"Condition Black");
}
//----------------
}
}
moveBoard = moves(dest);
if(moveBoard !=null){
updateGUIBoard(moveBoard);
}
}
});
add(button[i][j]);
if((i+j)%2==0)
button[i][j].setBackground(Color.CYAN);
else {
button[i][j].setBackground(Color.WHITE);
}
}
}
// setVisible(true);
}
private void inilizeRemovedPiecePanel(){
for (int i = 0 ; i < 8 ;i++){
for (int j = 0 ; j < 4 ;j++) {
removedPieces[i][j] = new JButton();
removedPieces[i][j].setBackground(Color.orange);
// added.setMinimumSize(new Dimension(50,50));
removedPiecePanel.add( removedPieces[i][j]);
}
}
}
private void updateDeletedPanel(){
int wCount = 0;
int bCount = 0;
for (Piece p : deletedList){
if(p.getPieceColor()==PieceColor.WHITE) {
int i = wCount % 8;
int j = wCount / 8;
wCount++;
removedPieces[i][j].setIcon(new ImageIcon(p.getPieceImage()));
}else {
int i = bCount % 8;
int j = bCount / 8;
bCount++;
removedPieces[i][3-j].setIcon(new ImageIcon(p.getPieceImage()));
}
}
}
private void buttonRepaint(){
Piece[][] pieceBoard = getUpdatedBoard();
for (int i = 0 ;i<8 ;i++){
for (int j = 0 ; j < 8 ;j++){
if(pieceBoard[i][j]!=null) {
button[i][j].setIcon(new ImageIcon(pieceBoard[i][j].getPieceImage()));
}else {
button[i][j].setIcon(new ImageIcon());
}
if((i+j)%2==0)
button[i][j].setBackground(Color.lightGray);
else {
button[i][j].setBackground(Color.WHITE);
}
}
}
}
private void updateGUIBoard(boolean [][] moveBoard){
if(moveBoard ==null) return;;
for (int i = 0 ; i < 8 ;i++){
for (int j =0 ; j < 8 ;j++){
if(moveBoard[i][j]){
button[i][j].setBackground(Color.RED);
// System.out.println("Reach Thereeee...");
} else {
if((i+j)%2==0)
button[i][j].setBackground(Color.lightGray);
else {
button[i][j].setBackground(Color.WHITE);
}
}
}
}
}
public void initlizeGame(){
white = new Player(PieceColor.WHITE) ;
black = new Player(PieceColor.BLACK) ;
pw = white.getPieces() ;
pb = black.getPieces() ;
board = new Cell[8][8] ;
for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) {
board[i][j] = new Cell (i,j) ;
}
}
for (Piece p:pw) {
board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ;
}
for (Piece p:pb) {
board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ;
}
}
public boolean[][] moves(Cell source){
boolean[][] binaryBoard = new boolean[8][8];
if (wORb%2==0) {
for (Piece p : pw){
if(p.getCell().getRow() == source.getRow() && p.getCell().getCol()==source.getCol()){
for (int i = 0 ; i < 8;i++){
for (int j = 0 ; j < 8 ;j++){
Cell dest = new Cell(i,j);
if(p.isValidMove(dest,board,pw,pb)){
binaryBoard[i][j] = true;
}
}
}
return binaryBoard;
}
}
}else {
for (Piece p : pb){
if(p.getCell().getRow() == source.getRow() && p.getCell().getCol()==source.getCol()){
for (int i = 0 ; i < 8;i++){
for (int j = 0 ; j < 8 ;j++){
Cell dest = new Cell(i,j);
if(p.isValidMove(dest,board,pw,pb)){
binaryBoard[i][j] = true;
}
}
}
return binaryBoard;
}
}
}
return null;
}
public boolean movePiece(Cell source, Cell dest){
//--------------
if (wORb%2==0) {
for (Piece p : pw) {
if (p.getCell().getRow() == source.getRow() &&
p.getCell().getCol() == source.getCol()) {
if (p.isValidMove(dest, board, pw, pb)) {
wORb++;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true);
p.setCell(dest);
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false);
for (Piece p1 : pb) {
if (p1.getCell().getRow() == dest.getRow() && p1.getCell().getCol() == dest.getCol()) {
p1.setDeleted(true);
}
}
if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p;
myPawn.setOnce(false);
}
for (Piece blackPiece : pb) {
if (blackPiece.getCell().getRow() == dest.getRow()
&& blackPiece.getCell().getCol() == dest.getCol()) {
blackPiece.setDeleted(true);
deletedList.add(blackPiece);
}
}
return true;
}
}
}
}else {
for (Piece p:pb) {
if (p.getCell().getRow()==source.getRow() &&
p.getCell().getCol() == source.getCol())
if (p.isValidMove(dest, board, pw, pb) ) {
wORb++ ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
p.setCell(dest) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
for (Piece p1:pw) {
if (p1.getCell().getRow()==dest.getRow() && p1.getCell().getCol()==dest.getCol()) {
p1.setDeleted(true) ;
}
}
if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p;
myPawn.setOnce(false) ;
}
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==dest.getRow()
&& whitePiece.getCell().getCol()==dest.getCol()) {
whitePiece.setDeleted(true);
deletedList.add(whitePiece);
}
}
return true;
}
}
}
//--------------
return false;
}
public Piece[][] getUpdatedBoard(){
Piece[][] pieceboard = new Piece[8][8];
Piece pw[] = white.getPieces() ;
Piece pb[] = black.getPieces() ;
for (Piece p : pw){
if(!p.isDeleted())
pieceboard[p.getCell().getRow()][p.getCell().getCol()] = p;
}
for (Piece p : pb){
if(!p.isDeleted())
pieceboard[p.getCell().getRow()][p.getCell().getCol()] = p;
}
return pieceboard;
}
private int checkCondition(Cell board[][], Piece pw[], Piece pb[]) {
for (Piece p:pw) {
if (p instanceof King) {
int sw1=0 ;
for (Piece p1:pb) {
if (p1.isValidMove(p.getCell(), board, pw, pb)) {
sw1=1 ;
}
}
if(sw1==1){
System.out.println("Check condition for White player!!");
return 1;
}
}
}
for (Piece p:pb) {
if (p instanceof King) {
int sw1=0 ;
for (Piece p1:pw) {
if (p1.isValidMove(p.getCell(), board, pw, pb)) {
sw1=1 ;
}
}
if(sw1==1) {
System.out.println("Check condition for Balck player!!") ;
return 2;
}
}
}
return 0;
}
}
import javax.swing.*;
import java.awt.*;
public class ChessGameGUI extends JFrame {
public static void main(String args[]){
new ChessGameGUI();
}
public ChessGameGUI(){
super();
JPanel parent = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1300,610);
parent.setSize(1300,610);
setResizable(false);
parent.setLayout(new BoxLayout(parent,BoxLayout.X_AXIS));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(8,4));
p1.setSize(500,600);
JPanel gameBoard = new ChessBoardGUI(p1);
gameBoard.setSize(800,600);
parent.add(p1);
parent.add(gameBoard);
add(parent);
setVisible(true);
}
}
public class King extends Piece {
public King(Cell cell, Color color) {
super(cell, color) ;
public King(Cell cell, PieceColor pieceColor) {
super(cell, pieceColor) ;
}
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
......@@ -8,15 +8,15 @@
public class Knight extends Piece {
public Knight(Cell cell, Color color) {
super(cell, color) ;
public Knight(Cell cell, PieceColor pieceColor) {
super(cell, pieceColor) ;
}
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
import java.util.Scanner;
public class Main{
/**
/**
*prints board and control the moves of the pieces
*@author kimiadorani
*@version 1.0
*@since 2019-5-7
*/
private static char boardChar[][] = new char [8][8] ;
private static char boardChar[][] = new char [8][8] ;
private static void printBoard() {
for (int i=1 ; i<9 ; ++i) {
......@@ -82,8 +83,8 @@ private static char boardChar[][] = new char [8][8] ;
public static void main(String[] args) {
Scanner in = new Scanner(System.in) ;
Player white = new Player(Color.WHITE) ;
Player black = new Player(Color.BLACK) ;
Player white = new Player(PieceColor.WHITE) ;
Player black = new Player(PieceColor.BLACK) ;
Piece pw[] = white.getPieces() ;
Piece pb[] = black.getPieces() ;
Cell board[][] = new Cell[8][8] ;
......@@ -141,7 +142,7 @@ private static char boardChar[][] = new char [8][8] ;
for (Piece p:pw) {
if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0') {
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb)) {
if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb)) {
wORb++ ;
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
......@@ -175,7 +176,7 @@ private static char boardChar[][] = new char [8][8] ;
for (Piece p:pb) {
if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0')
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb) ) {
if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb) ) {
wORb++ ;
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
......@@ -212,4 +213,3 @@ private static char boardChar[][] = new char [8][8] ;
}
}
\ No newline at end of file
public class Pawn extends Piece{
private boolean once, end ;
public Pawn(Cell cell, Color color) {
super(cell, color) ;
public Pawn(Cell cell, PieceColor pieceColor) {
super(cell, pieceColor) ;
this.once = true ;
this.end = false ;
}
......@@ -18,11 +18,11 @@ public class Pawn extends Piece{
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
if (super.getColor()==Color.BLACK && super.getCell().getRow()==0) end = true ;
if (super.getColor()==Color.WHITE && super.getCell().getRow()==7) end = true ;
if (super.getPieceColor()== PieceColor.BLACK && super.getCell().getRow()==0) end = true ;
if (super.getPieceColor()== PieceColor.WHITE && super.getCell().getRow()==7) end = true ;
int sw=0 ;
if (super.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......@@ -56,12 +56,12 @@ public class Pawn extends Piece{
else {
if(
(this.getCell().getCol() == c.getCol() &&
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE)) )
((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()== PieceColor.BLACK) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()== PieceColor.WHITE)) )
||
(Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK && sw==1) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE && sw==1)) )
((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()== PieceColor.BLACK && sw==1) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()== PieceColor.WHITE && sw==1)) )
) return true ;
else return false ;
}
......
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
public abstract class Piece {
private Cell cell ;
private Color color ;
private PieceColor pieceColor;
private boolean deleted ;
public Piece (Cell cell, Color color) {
public Piece (Cell cell, PieceColor pieceColor) {
this.cell = cell ;
this.cell.setEmpty(false) ;
this.color = color ;
this.pieceColor = pieceColor;
this.deleted = false ;
}
......@@ -16,9 +20,62 @@ public abstract class Piece {
public Cell getCell() {
return cell;
}
public Image getPieceImage(){
Image image = null;
try {
if (this instanceof Bishop) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Bishop.png"));
} else {
image = ImageIO.read(new File("./pics/Black-Bishop.png"));
}
} else if (this instanceof King) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-King.png"));
} else {
image = ImageIO.read(new File("./pics/Black-King.png"));
}
} else if (this instanceof Knight) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Knight.png"));
} else {
image = ImageIO.read(new File("./pics/Black-Knight.png"));
}
} else if (this instanceof Pawn) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Pawn.png"));
} else {
image = ImageIO.read(new File("./pics/Black-Pawn.png"));
}
} else if (this instanceof Queen) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Queen.png"));
public Color getColor() {
return color;
} else {
image = ImageIO.read(new File("./pics/Black-Queen.png"));
}
} else if (this instanceof Rook) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Rook.png"));
} else {
image = ImageIO.read(new File("./pics/Black-Rook.png"));
}
}
}catch (Exception exception){
}
return image;
}
public PieceColor getPieceColor() {
return pieceColor;
}
public boolean isDeleted() {
......@@ -29,8 +86,8 @@ public abstract class Piece {
this.cell = cell;
}
public void setColor(Color color) {
this.color = color;
public void setPieceColor(PieceColor pieceColor) {
this.pieceColor = pieceColor;
}
public void setDeleted(boolean deleted) {
......
public class Player {
private Piece[] pieces = new Piece[16] ;
private Color color ;
private PieceColor pieceColor;
public Piece[] getPieces() {
return pieces ;
}
public Color getColor() {
return color ;
public PieceColor getPieceColor() {
return pieceColor;
}
public void setPieces(Piece[] pieces) {
this.pieces = pieces ;
}
public void setColor(Color color) {
this.color = color ;
public void setPieceColor(PieceColor pieceColor) {
this.pieceColor = pieceColor;
}
public Player(Color color) {
this.color = color ;
if (color == Color.WHITE) {
public Player(PieceColor pieceColor) {
this.pieceColor = pieceColor;
if (pieceColor == PieceColor.WHITE) {
for(int i=0 ; i<8 ; i++) {
pieces[i] = new Pawn(new Cell(1,i), color) ;
}
pieces[8] = new Bishop(new Cell(0,2), color) ;
pieces[9] = new Bishop(new Cell(0,5), color) ;
pieces[10] = new Knight(new Cell(0,1), color) ;
pieces[11] = new Knight(new Cell(0,6), color) ;
pieces[12] = new Rook(new Cell(0,0), color) ;
pieces[13] = new Rook(new Cell(0,7), color) ;
pieces[14] = new King(new Cell(0,4), color) ;
pieces[15] = new Queen(new Cell(0,3), color) ;
pieces[i] = new Pawn(new Cell(1,i), pieceColor) ;
}
pieces[8] = new Bishop(new Cell(0,2), pieceColor) ;
pieces[9] = new Bishop(new Cell(0,5), pieceColor) ;
pieces[10] = new Knight(new Cell(0,1), pieceColor) ;
pieces[11] = new Knight(new Cell(0,6), pieceColor) ;
pieces[12] = new Rook(new Cell(0,0), pieceColor) ;
pieces[13] = new Rook(new Cell(0,7), pieceColor) ;
pieces[14] = new King(new Cell(0,4), pieceColor) ;
pieces[15] = new Queen(new Cell(0,3), pieceColor) ;
}
else {
for(int i=0 ; i<8 ; i++){
pieces[i] = new Pawn(new Cell(6,i), color) ;
}
pieces[8] = new Bishop(new Cell(7,2), color) ;
pieces[9] = new Bishop(new Cell(7,5), color) ;
pieces[10] = new Knight(new Cell(7,1), color) ;
pieces[11] = new Knight(new Cell(7,6), color) ;
pieces[12] = new Rook(new Cell(7,0), color) ;
pieces[13] = new Rook(new Cell(7,7), color) ;
pieces[14] = new King(new Cell(7,4), color) ;
pieces[15] = new Queen(new Cell(7,3), color) ;
pieces[i] = new Pawn(new Cell(6,i), pieceColor) ;
}
pieces[8] = new Bishop(new Cell(7,2), pieceColor) ;
pieces[9] = new Bishop(new Cell(7,5), pieceColor) ;
pieces[10] = new Knight(new Cell(7,1), pieceColor) ;
pieces[11] = new Knight(new Cell(7,6), pieceColor) ;
pieces[12] = new Rook(new Cell(7,0), pieceColor) ;
pieces[13] = new Rook(new Cell(7,7), pieceColor) ;
pieces[14] = new King(new Cell(7,4), pieceColor) ;
pieces[15] = new Queen(new Cell(7,3), pieceColor) ;
}
}
}
\ No newline at end of file
......@@ -2,15 +2,15 @@
public class Queen extends Piece {
public Queen(Cell cell, Color color) {
super(cell, color) ;
public Queen(Cell cell, PieceColor pieceColor) {
super(cell, pieceColor) ;
}
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
......@@ -7,15 +7,15 @@
public class Rook extends Piece {
public Rook(Cell cell, Color color) {
super(cell, color) ;
public Rook(Cell cell, PieceColor pieceColor) {
super(cell, pieceColor) ;
}
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment