/* Project: Email Chess Author: Matti Kantola maza@ee.oulu.fi http://www.angelfire.com/nd/maza Description: Chess game where moves are exchanged using email. This is the main program and logic. History: 26.7.2000 wrote initial version 27.7.2000 wrote rest, first release 2.8.2000 more features */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; public class Chess extends Applet implements MouseListener, ActionListener, ItemListener { String num="87654321"; String chr="ABCDEFGH"; final int RW=50,RH=50; final Color WCOLOR=Color.lightGray,BCOLOR=Color.gray; String move=null; Piece[] white; Piece[] black; VerLayout vl; TextField tfme; TextField tfopp; TextField tfemail; Label lmove; Button bmove; Button brot; TextArea ta; Choice chprom; Image wpawn; Image wrook; Image wknight; Image wbishop; Image wqueen; Image wking; Image bpawn; Image brook; Image bknight; Image bbishop; Image bqueen; Image bking; Image[] wbimages; boolean rotate=false; int lastmove=-1; int srcx=-1,srcy=-1; public void start() { URL u=getCodeBase(); white=new Piece[16]; black=new Piece[16]; wpawn=getImage(u,"images/wpawn.gif"); wrook=getImage(u,"images/wrook.gif"); wknight=getImage(u,"images/wknight.gif"); wbishop=getImage(u,"images/wbishop.gif"); wqueen=getImage(u,"images/wqueen.gif"); wking=getImage(u,"images/wking.gif"); bpawn=getImage(u,"images/bpawn.gif"); brook=getImage(u,"images/brook.gif"); bknight=getImage(u,"images/bknight.gif"); bbishop=getImage(u,"images/bbishop.gif"); bqueen=getImage(u,"images/bqueen.gif"); bking=getImage(u,"images/bking.gif"); wbimages=new Image[12]; wbimages[0]=wpawn; wbimages[1]=wknight; wbimages[2]=wbishop; wbimages[3]=wrook; wbimages[4]=wqueen; wbimages[5]=wking; wbimages[6]=bpawn; wbimages[7]=bknight; wbimages[8]=bbishop; wbimages[9]=brook; wbimages[10]=bqueen; wbimages[11]=bking; white[0]=new Piece(0,7,Piece.WHITE,wrook,"R"); white[1]=new Piece(1,7,Piece.WHITE,wknight,"N"); white[2]=new Piece(2,7,Piece.WHITE,wbishop,"B"); white[3]=new Piece(3,7,Piece.WHITE,wqueen,"Q"); white[4]=new Piece(4,7,Piece.WHITE,wking,"K"); white[5]=new Piece(5,7,Piece.WHITE,wbishop,"B"); white[6]=new Piece(6,7,Piece.WHITE,wknight,"N"); white[7]=new Piece(7,7,Piece.WHITE,wrook,"R"); white[8]=new Piece(0,6,Piece.WHITE,wpawn,"P"); white[9]=new Piece(1,6,Piece.WHITE,wpawn,"P"); white[10]=new Piece(2,6,Piece.WHITE,wpawn,"P"); white[11]=new Piece(3,6,Piece.WHITE,wpawn,"P"); white[12]=new Piece(4,6,Piece.WHITE,wpawn,"P"); white[13]=new Piece(5,6,Piece.WHITE,wpawn,"P"); white[14]=new Piece(6,6,Piece.WHITE,wpawn,"P"); white[15]=new Piece(7,6,Piece.WHITE,wpawn,"P"); black[0]=new Piece(0,0,Piece.BLACK,brook,"r"); black[1]=new Piece(1,0,Piece.BLACK,bknight,"n"); black[2]=new Piece(2,0,Piece.BLACK,bbishop,"b"); black[3]=new Piece(3,0,Piece.BLACK,bqueen,"q"); black[4]=new Piece(4,0,Piece.BLACK,bking,"k"); black[5]=new Piece(5,0,Piece.BLACK,bbishop,"b"); black[6]=new Piece(6,0,Piece.BLACK,bknight,"n"); black[7]=new Piece(7,0,Piece.BLACK,brook,"r"); black[8]=new Piece(0,1,Piece.BLACK,bpawn,"p"); black[9]=new Piece(1,1,Piece.BLACK,bpawn,"p"); black[10]=new Piece(2,1,Piece.BLACK,bpawn,"p"); black[11]=new Piece(3,1,Piece.BLACK,bpawn,"p"); black[12]=new Piece(4,1,Piece.BLACK,bpawn,"p"); black[13]=new Piece(5,1,Piece.BLACK,bpawn,"p"); black[14]=new Piece(6,1,Piece.BLACK,bpawn,"p"); black[15]=new Piece(7,1,Piece.BLACK,bpawn,"p"); vl=new VerLayout(430,100,22,20); setLayout(vl); tfme=new TextField(); tfopp=new TextField(); tfemail=new TextField(); bmove=new Button("Send move"); brot=new Button("Rotate board"); Button bin=new Button("Input board"); Button bout=new Button("Output board"); lmove=new Label(); chprom=new Choice(); chprom.add("Pawn"); chprom.add("Knight"); chprom.add("Bishop"); chprom.add("Rook"); chprom.add("Queen"); chprom.add("King"); ta=new TextArea("",8,8,TextArea.SCROLLBARS_NONE); vl.setSize(ta,100,100); bmove.setActionCommand("0"); brot.setActionCommand("1"); bin.setActionCommand("2"); bout.setActionCommand("3"); bmove.addActionListener(this); brot.addActionListener(this); bin.addActionListener(this); bout.addActionListener(this); chprom.addItemListener(this); add(new Label("Your name")); add(tfme); add(new Label("Opponent name")); add(tfopp); add(new Label("Opponent email")); add(tfemail); add(new Label("Current move")); add(lmove); add(bmove); add(brot); add(bout); add(bin); add(ta); add(new Label("Promote to")); add(chprom); addMouseListener(this); } public void paint(Graphics g) { int x,y; Color col; for(y=0;y<8;y++) { for(x=0;x<8;x++) { col=((x&1)^(y&1))==1?BCOLOR:WCOLOR; g.setColor(col); g.fillRect(x*RW,y*RH,RW,RH); } g.setColor(Color.black); g.drawString(""+num.charAt(y),8*RW+8,y*RH+(RH/2)); g.drawString(""+chr.charAt(y),y*RW+(RW/2),8*RH+12); } for(int i=0;i<16;i++) { drawPiece(g,white[i]); drawPiece(g,black[i]); } if(srcx!=-1) { g.setColor(Color.red); g.drawRect(srcx*RW,srcy*RH,RW-1,RH-1); g.drawRect(srcx*RW+1,srcy*RH+1,RW-3,RH-3); } } public void drawPiece(Graphics g,Piece p) { Color col; if(p.alive) { col=((p.x&1)^(p.y&1))==1?BCOLOR:WCOLOR; g.drawImage(p.image,p.x*RW,p.y*RH,col,this); } } public boolean move(int ox,int oy,int nx,int ny) { Piece src,dst; src=findAt(ox,oy); dst=findAt(nx,ny); if(src!=null) { if(dst!=null) { if(src.type==dst.type) { return false; } dst.alive=false; } src.x=nx; src.y=ny; return true; } return false; } public Piece findAt(int x,int y) { for(int i=0;i<16;i++) { if(white[i].isAt(x,y)) { return white[i]; } if(black[i].isAt(x,y)) { return black[i]; } } return null; } public void rotate180() { Piece[] p; if(rotate) rotate=false; else rotate=true; p=new Piece[64]; for(int i=0;i<64;i++) { p[i]=findAt(i%8,i/8); } for(int i=0;i<64;i++) { if(p[i]!=null) { p[i].x=7-i%8; p[i].y=7-i/8; } } if(rotate) { num="12345678"; chr="HGFEDCBA"; } else { num="87654321"; chr="ABCDEFGH"; } repaint(); } public void mousePressed(MouseEvent e) { if( (e.getX()>8*RW) || (e.getY()>8*RH) ) return; int oldx=-1,oldy=-1; int newmove=-1; if(srcx!=-1) { oldx=srcx; oldy=srcy; } srcx=e.getX()/RW; srcy=e.getY()/RH; repaint(srcx*RW,srcy*RH,RW,RH); if(oldx!=-1) { if((srcx==oldx) && (srcy==oldy)) { srcx=-1; } else { Piece p=findAt(oldx,oldy); if(p!=null) newmove=p.type; if(move(oldx,oldy,srcx,srcy)) { if( (move==null) || (newmove!=lastmove) ) { move=""; } lastmove=newmove; move+= ""+chr.charAt(oldx)+num.charAt(oldy)+"-"+ chr.charAt(srcx)+num.charAt(srcy)+" "; lmove.setText( ""+chr.charAt(oldx)+num.charAt(oldy)+"-"+ chr.charAt(srcx)+num.charAt(srcy)); srcx=-1; } } repaint(oldx*RW,oldy*RH,RW,RH); } } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void itemStateChanged(ItemEvent e) { if(e.getItemSelectable() instanceof Choice) { Choice c=(Choice)e.getItemSelectable(); Piece p=findAt(srcx,srcy); if(p!=null) { p.image=wbimages[p.type*6+c.getSelectedIndex()]; repaint(srcx*RW,srcy*RH,RW,RH); } } } public void actionPerformed(ActionEvent e) { switch(e.getActionCommand().charAt(0)) { case '0': if(move!=null) { try{ URL u=new URL( "mailto:"+tfemail.getText()+"?Subject="+ "Game "+tfme.getText()+" vs "+tfopp.getText()+" "+ move); getAppletContext().showDocument(u); move=null; } catch(Exception ee){} srcx=-1; } break; case '1': rotate180(); break; case '2': if(rotate) rotate180(); BoardIO.setup(ta.getText(),white,black); repaint(); break; case '3': if(rotate) rotate180(); Piece[] p=new Piece[64]; for(int i=0;i<64;i++) { p[i]=findAt(i%8,i/8); } try{ URL u=new URL( "mailto:"+tfemail.getText()+"?Subject="+ "Game "+tfme.getText()+" vs "+tfopp.getText()+" board situation&Body="+ BoardIO.getSetup(p)); getAppletContext().showDocument(u); } catch(Exception ee){} break; } } }