//Java Checkers Applet // Written by Dr. Roger Webster import java.applet.*; import java.awt.*; import java.util.*; import java.net.*; import java.applet.Applet; public class javawin extends Applet { Frame window; TextArea textarea = new TextArea(5, 80); checkers mycheckers; boolean mousedragging = false; boolean trytomakemove = false; public void init() { window = new Frame ("Webster's Java Checkers Window"); window.setSize (800, 800); textarea.setFont(new Font("Helvetica", Font.PLAIN, 12)); textarea.setEditable(false); window.add("North", textarea); textarea.appendText("hello Java Checkers Players\n"); mycheckers = new checkers(); window.add("Center", mycheckers); window.show(); } public void stop() { window.dispose(); } class checkers extends Canvas { int grid = 8; int currentx, currenty; Color grey = new Color(165,165,165);//Board colors Color tan = new Color(255,255,204); //Board colors int[][] BoardInfo = new int[8][8]; //Data structure for pieces int ActivePiece; int w, h; int FromX, FromY; int ToX, ToY; boolean you_go_first=true; boolean MyTurn; boolean AmIBlack; int nextx; int nexty; Graphics og; Image offScrImg; int imgWidth; int imgHeight; int width; int height; int checkerwidth; int checkerheight; int upcurrentx; int upcurrenty; int lastx; int lasty; // Initialization function checkers() { super(); textarea.appendText("You are Black and you go first !\n"); this.setup(you_go_first); width = size().width/grid; height = size().height/grid; imgWidth = width/7; imgHeight = height/7; w = width; h = height; checkerwidth = (int)(w/1.5); checkerheight = (int)(h/1.5); } public void setup(boolean mefirst) { int i,j; ActivePiece = 0; if(mefirst){ MyTurn = true; AmIBlack = true; }else{ MyTurn = false; AmIBlack = false; } for(i = 0;i < 8;i++) for (j = 0;j < 8;j++){ if ((i == 3) || (i == 4)){ // No pieces on BoardInfo[j][i] = 0; }else if((i == 0) || (i == 2)){ if ((j == 1) || (j == 3) || (j ==5) || (j == 7)){ BoardInfo[j][i] = 2; } }else if(i == 1){ if ((j == 0) || (j == 2) || (j ==4) || (j == 6)){ BoardInfo[j][i] = 2; } }else if(i == 6){ if ((j == 1) || (j == 3) || (j ==5) || (j == 7)){ BoardInfo[j][i] = 1; } }else if((i == 5) || (i == 7)){ if ((j == 0) || (j == 2) || (j ==4) || (j == 6)){ BoardInfo[j][i] = 1; } } } } public void DrawPiece(Graphics g, int x, int y, int Type) { switch (Type) { case 1: g.setColor(Color.red); g.fillOval(x,y, checkerwidth,checkerheight); break; case 2: g.setColor(Color.black); g.fillOval(x,y, checkerwidth,checkerheight); break; case 3: g.setColor(Color.red); g.fillOval(x,y, checkerwidth,checkerheight); g.setColor(Color.white); g.setFont(new Font("Helvetica", Font.PLAIN, 18)); g.drawString("K",x + checkerwidth/2, y + checkerheight/2); break; case 4: g.setColor(Color.black); g.fillOval(x,y, checkerwidth,checkerheight); g.setColor(Color.white); g.setFont(new Font("Helvetica", Font.PLAIN, 18)); g.drawString("K",x + checkerwidth/2, y + checkerheight/2); break; } } void cliptoaffectedarea (Graphics g, int oldx, int oldy, int newx, int newy, int width, int height) { int x = Math.min(oldx, newx); int y = Math.min(oldy, newy); int w = ( Math.max(oldx, newx) + width) - x; int h = ( Math.max(oldy, newy) + height) - y; g.clipRect(x,y,w,h); } public void update( Graphics g ) { if (offScrImg == null) offScrImg = createImage(size().width, size().height); og = offScrImg.getGraphics(); lastx = upcurrentx-checkerwidth/2; lasty = upcurrenty-checkerheight/2; if (lastx > nextx) upcurrentx = nextx-checkerwidth; else upcurrentx = nextx +checkerwidth; if (lasty > nexty) upcurrenty = nexty-checkerwidth; else upcurrenty = nexty+checkerwidth; // textarea.appendText("clipping lastx is" + lastx + " lasty is "+ lasty +"!\n"); // textarea.appendText("clipping upcurrentx is" + upcurrentx + " upcurrenty is "+ upcurrenty +"!\n"); if (mousedragging) { cliptoaffectedarea (og, lastx, lasty, upcurrentx, upcurrenty, imgWidth, imgHeight); cliptoaffectedarea (g, lastx, lasty, upcurrentx, upcurrenty, imgWidth, imgHeight); } else { cliptoaffectedarea (og, 0, 0, size().width, size().height, size().width, size().height); cliptoaffectedarea (g, 0, 0, size().width, size().height, size().width, size().height); } paint(og); g.drawImage(offScrImg, 0, 0, this); og.dispose(); } // display the image public void paint( Graphics g ) { boolean shade = false; int PieceType; width = size().width/grid; height = size().height/grid; w = width; h = height; checkerwidth = (int)(w/1.5); checkerheight = (int)(h/1.5); imgWidth = checkerwidth; imgHeight = checkerheight; for (int y=0;y<=grid;y++) for (int x=0; x <= grid; x++) { g.setColor ( (shade = !shade) ? tan :grey); g.fillRect ( x * w, y * h, w, h); } for (int y = 0; y < grid; y++) // Draw pieces on game board for (int x = 0; x < grid; x++) { if (BoardInfo[x][y] == 1) { PieceType = 1; DrawPiece(g, x * w + (w/7), y * h +(w/7), PieceType); } if (BoardInfo[x][y] == 2) { PieceType = 2; DrawPiece(g, x * w + (w/7), y * h +(w/7), PieceType); } if (BoardInfo[x][y] == 3) { PieceType = 3; DrawPiece(g, x * w + (w/7), y * h +(w/7), PieceType); } if (BoardInfo[x][y] == 4) { PieceType = 4; DrawPiece(g, x * w + (w/7), y * h +(w/7), PieceType); } } if (mousedragging) { DrawPiece(g, currentx, currenty, ActivePiece); } } //end of paint() public boolean mouseDown (Event e, int x, int y) { trytomakemove = false; FromX = FindXSquare(x); FromY = FindYSquare(y); // textarea.appendText("FromX " + FromX + " FromY is "+ FromY +"!\n"); if((AmIBlack) && ((BoardInfo[FromX][FromY] == 2)||(BoardInfo[FromX][FromY] == 4))) { ActivePiece =BoardInfo[FromX][FromY]; BoardInfo[FromX][FromY] = 0; trytomakemove = true; nextx = x; nexty = y; currentx = x; currenty = y; mousedragging = true; repaint(); } if((!AmIBlack) && ((BoardInfo[FromX][FromY]== 1)||(BoardInfo[FromX][FromY]== 3))) { ActivePiece =BoardInfo[FromX][FromY]; BoardInfo[FromX][FromY] = 0; trytomakemove = true; nextx = x; nexty = y; currentx = x; currenty = y; mousedragging = true; repaint(); } return true; } public boolean mouseUp (Event e, int x, int y) { Graphics g; ToX = FindXSquare(x); ToY = FindYSquare(y); mousedragging = false; nextx = x; nexty = y; if (trytomakemove) { // textarea.appendText("FromX " + FromX + " FromY is "+ FromY +"!\n"); // textarea.appendText("ToX " + ToX + " ToY is "+ ToY +"!\n"); MakeMove(FromX, FromY, ToX, ToY); repaint(); } trytomakemove = false; return true; } public boolean mouseDrag(Event e, int x, int y) { nextx = x; nexty = y; if (trytomakemove) { currentx = x; currenty = y; mousedragging = true; repaint(); } return true; } //Checks if a piece will become a king on this move //Also Checks if piece is already a king //this method written by Andrea Olson public boolean IsKing(int x, int y) { int ThePiece = ActivePiece; if ((ThePiece == 3) || (ThePiece == 4)) //piece is already king return true; else if (((ThePiece == 1) && (y == 0)) && ((x == 1) || (x == 3) || (x== 5) || (x == 7))) { ActivePiece = 3; return true; } else if ((ThePiece == 2) && (y == 7) && //piece is black and moving ((x == 0) || (x == 2) || (x == 4) || (x == 6))) { ActivePiece = 4; return true; } else //piece is not and will not become return false; } // Check valid move //this method written by Andrea Olson public boolean ValidMove() { int MoveDifX = FromX - ToX; // Difference of old X spot int MoveDifY = FromY - ToY; // Difference of old Y spot if(BoardInfo[ToX][ToY] != 0) //Piece already there return false; if ((ActivePiece == 3) || (ActivePiece == 4)) { // If King if (((MoveDifX == 1) || (MoveDifX == -1)) && ((MoveDifY == 1) || (MoveDifY == -1))) return true; else return false; }else if (ActivePiece == 1){ // If red player if (((MoveDifX == 1) || (MoveDifX == -1)) && (MoveDifY == 1)) return true; else return false; }else if (ActivePiece == 2){ // If black player if (((MoveDifX == 1) || (MoveDifX == -1)) && (MoveDifY == -1)) return true; else return false; }else{ return false; } } //this method written by Andrea Olson public boolean ValidJump() { int MoveDifX = FromX - ToX; // Difference of old X spotand new X spot int MoveDifY = FromY - ToY; // Difference of old Y spotand new Y spot int JumpedPieceX = MoveDifX / -2 + FromX; // Calculate where Xjumped piece is int JumpedPieceY = MoveDifY / -2 + FromY; // Calculate where Yjumped piece is int JumpOverSpot = BoardInfo[JumpedPieceX][JumpedPieceY]; // Spot ofpiece to jump //Spot jumped is not empty and is not your own piece if ((JumpOverSpot != 0) && (JumpOverSpot % 2 != ActivePiece % 2)) { BoardInfo[JumpedPieceX][JumpedPieceY] = 0; //Removejumped piece if ((ActivePiece == 3) || (ActivePiece == 4)) { // If King if (((MoveDifX == 2) || (MoveDifX == -2)) && ((MoveDifY == 2) ||(MoveDifY == -2))) return true; else return false; }else if (ActivePiece == 1) { // If red player if (((MoveDifX == 2) || (MoveDifX == -2)) && (MoveDifY == 2)) return true; else return false; }else if(ActivePiece == 2) { // If black player if (((MoveDifX == 2) || (MoveDifX == -2)) && (MoveDifY == -2)) return true; else return false; }else return false; }else return false; } public int FindXSquare(int mousex) { int found = -1; int width = size().width/grid; int height = size().height/grid; found = mousex / width; return found; } public int FindYSquare(int mousey) { int found = -1; int width = size().width/grid; int height = size().height/grid; found = mousey / height; return found; } public void MakeMove(int fx, int fy, int tx, int ty) { if( ValidMove() || ValidJump() ) { if( (IsKing(ToX, ToY)) || (!IsKing(ToX, ToY)) ) { BoardInfo[ToX][ToY] = ActivePiece; MyTurn = !MyTurn; AmIBlack = !AmIBlack; if (MyTurn) textarea.appendText("It is now Black's turn !\n"); else textarea.appendText("It is now Red's turn !\n"); } } else BoardInfo[FromX][FromY] = ActivePiece; ActivePiece = 0; } } }