-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFEN.java
116 lines (116 loc) · 2.84 KB
/
FEN.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
public class FEN {
private Piece[][] position;
private boolean activeColor;
private boolean[] castling;
private String enPassant;
private int halfMove;
private int fullMove;
//private Piece[] whitePieces,blackPieces;
//private int whiteCount,blackCount;
public FEN(String fenCode) {
String[] info = fenCode.split(" ");
this.position=parsePosition(info[0]);
if(info[1].equals("w")){
this.activeColor=true;
}
else{
this.activeColor=false;
}
this.castling=new boolean[4];
for (int i=0;i<4;i++){
this.castling[i]=false;
}
for (int i=0;i<info[2].length();i++){
if(info[2].charAt(i)=='-'){
break;
}
else if(info[2].charAt(i)=='K'){
this.castling[0]=true;
}
else if(info[2].charAt(i)=='Q'){
this.castling[1]=true;
}
else if(info[2].charAt(i)=='k'){
this.castling[2]=true;
}
else if(info[2].charAt(i)=='q'){
this.castling[3]=true;
}
}
this.enPassant=info[3];
this.halfMove=Integer.parseInt(info[4]);
this.fullMove=Integer.parseInt(info[5]);
/*this.whitePieces=new Piece[16];
this.blackPieces=new Piece[16];
this.whiteCount=0;
this.blackCount=0;*/
}
public Piece[][] getPos(){
return position;
}
public boolean getColor(){
return activeColor;
}
public String getEnPassant(){
return enPassant;
}
public boolean[] getCastling(){
return castling;
}
public Piece[][] parsePosition(String pos){
String[] rows = pos.split("/");
Piece [][] board=new Piece[8][8];
int x=0,y=0;
for (int i=0;i<8;i++){
//System.out.println("Rows: "+rows[i]);
for (int j=0;j<rows[i].length();j++){
int num=(int)(rows[i].charAt(j));
//System.out.println(rows[i].charAt(j));
//System.out.println(x+" "+y);
if(num>=48 && num<=57){
for(int k=0;k<num-48;k++){
//System.out.println(x+" "+y);
board[x][y]=null;
y++;
}
}
else{
boolean color;
char p=rows[i].charAt(j);
if (p>=65 && p<=90){
color=true;
}
else{
color=false;
}
Piece piece=new Piece(rows[i].charAt(j),color,true);
board[x][y]=piece;
/*if(color && whiteCount<16){
whitePieces[whiteCount]=piece;
whiteCount++;
}
else if(!color && blackCount<16){
blackPieces[blackCount]=piece;
blackCount++;
}*/
y++;
}
}
x++;
y=0;
}
return board;
}
/*public Piece[] getWhitePieces(){
return whitePieces;
}
public Piece[] getBlackPieces(){
return blackPieces;
}*/
}