-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChessUI.java
197 lines (171 loc) · 5.17 KB
/
ChessUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package chessUtkarsh;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Image;
public class ChessUI extends JPanel implements MouseListener{
private static final long serialVersionUID = 1L;
public static int x=0,y=0,k=0,counter=1,playerTurn=1,temp1,temp2,R1=0,C1=0,R2=0,C2=0,KC=7,KR=3,kC=0,kR=3;
public Image img;
public static Color brown = new Color(150,76,0);
public static Color yellow = new Color(255,217,23);
Simple s;
public static boolean valid1,valid2,valid3,valid4 ;
@Override
public void paint(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.LIGHT_GRAY);
printBoard(g);
printPieces(g,s);
this.addMouseListener(this);
if((x<=450 && x>=50)&&(y<=450 && y>=50))
{
x/=50;
x*=50;
y/=50;
y*=50;
if(playerTurn%2==1)
playerTurn=1;
else
playerTurn=2;
if(counter%2==1)
counter=1;
else
counter=2;
if(playerTurn==1){
if(counter==1){
R1=(x-50)/50;
C1=(y-50)/50;
System.out.println(counter+" "+"Row "+R1+" Column "+C1+" "+"player 1 counter 1");
valid1=MouseClick.isSelectedPieceWhite(R1, C1);
if(valid1)
counter++;
}
else if(counter==2){
R2=(x-50)/50;
C2=(y-50)/50;
System.out.println(counter+" "+"Row "+R1+" Column "+C1+" "+"player 1 counter 2");
valid2 = MouseClick.isTargetPieceWhite(R2, C2);
if(!valid2){
R1=R2;
C1=C2;
}
if(valid2){
valid3= Simple.validateMove(R1, C1, R2, C2);
if(valid3){
counter++;
playerTurn++;
repaint();
printPieces(g,s);
king.Check(playerTurn,R2,C2);
}
}
}
}
if(playerTurn==2){
if(counter==1){
R1=(x-50)/50;
C1=(y-50)/50;
System.out.println(counter+" "+"Row "+R1+" Column "+C1+" "+"player 2 counter 1");
valid1=MouseClick.isSelectedPieceBlack(R1, C1);
if(valid1)
counter++;
}
else if(counter==2){
R2=(x-50)/50;
C2=(y-50)/50;
System.out.println(counter+" "+"Row "+R1+" Column "+C1+" "+"player 1 counter 2");
valid2 = MouseClick.isTargetPieceBlack(R2, C2);
if(!valid2){
R1=R2;
C1=C2;
}
// valid2 = Simple.validateMove(R1,C1,R2,C2);
// repaint();
// printPieces(g,s);
if(valid2){
valid3= Simple.validateMove(R1, C1, R2, C2);
if(valid3){
counter++;
playerTurn++;
repaint();
printPieces(g,s);
king.Check(playerTurn,R2,C2);
}
}
}
}
System.out.println();
}
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {
x=e.getX();
y=e.getY();
//if(counter==0)counter++;
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
void printBoard(Graphics g)
{
g.setColor(yellow);
for(int i = 50; i <= 400; i+=100){
for(int j = 50; j <= 400; j+=100){
g.fillRect(i, j, 50, 50);
}
}
for(int i = 100; i <= 450; i+=100){
for(int j = 100; j <= 450; j+=100){
g.fillRect(i, j, 50, 50);
}
}
g.setColor(brown);
for(int i = 100; i <= 400; i+=100){
for(int j = 50; j <= 400; j+=100){
g.fillRect(i, j, 50, 50);
}
}
for(int i = 50; i <= 400; i+=100){
for(int j = 100; j <= 450; j+=100){
g.fillRect(i, j, 50, 50);
}
}
}
void printPieces(Graphics g,Simple s)
{
for(int i=0;i<8;i++){
for(int j=0;j<8;j++)
{
if(Simple.chessBoard[i][j]<='z' && Simple.chessBoard[i][j]>='a')
{
img = new ImageIcon("b"+Simple.chessBoard[i][j]+".png").getImage();
g.drawImage(img, 50+(50*j),50+(50*i), 50,50, this);
}
if(Simple.chessBoard[i][j]<='Z' && Simple.chessBoard[i][j]>='A')
{
img = new ImageIcon("w"+Simple.chessBoard[i][j]+".png").getImage();
g.drawImage(img, 50+(50*j),50+(50*i), 50,50, this);
}
}
}
}
public static void main(String[] args) {
JFrame f=new JFrame("CHESS GAME");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChessUI ui=new ChessUI();
f.add(ui);
f.setSize(500, 500);
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setResizable(false);
}
}