Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Board.java*/
- import java.lang.Math;
- /**
- * Represents a Board configuration of a game of Checkers61bl
- * @author Kyle Liang
- */
- public class Board {
- private Piece[][] board;
- private int[][] color;
- private int turn;
- private int count_click;
- private int count_move;
- private int x1,x2,y1,y2; /**
- * Define any variables associated with a Board object here. These
- * variables MUST be private.
- */
- /**
- * Constructs a new Board
- * @param shouldBeEmpty if true, add no pieces
- *///check
- public Board(boolean shouldBeEmpty) {
- int i,j;
- board= new Piece[8][8];
- color= new int[8][8];
- turn=0;
- count_click=0;
- count_move=0;
- x1=0;
- x2=0;
- y1=0;
- y2=0;
- // YOUR CODE HERE
- if(!shouldBeEmpty){
- for(i=0;i<8;i++){
- for(j=0;j<8;j++){
- if(j==7){
- if(i%2!=0){
- this.board[i][j]=new Piece(1);
- }
- }
- else if(j==5){
- if(i%2!=0){
- this.board[i][j]=new BombPiece(1);
- }
- }
- else if(j==6){
- if(i%2==0){
- this.board[i][j]=new ShieldPiece(1);
- }
- }
- if(j==1){
- if(i%2!=0){
- this.board[i][j]=new ShieldPiece(0);
- }
- }
- else if(j==2){
- if(i%2==0){
- this.board[i][j]=new BombPiece(0);
- }
- }
- else if(j==0){
- if(i%2==0){
- this.board[i][j]=new Piece(0);
- }
- }
- }
- }
- }
- }
- /**
- * gets the Piece at coordinates (x, y)
- * @param x X-coordinate of Piece to get
- * @param y Y-coordinate of Piece to get
- * @return the Piece at (x, y)
- *///check
- public Piece pieceAt(int x, int y) {
- if(x>=0&&y>=0&&x<=7&&y<=7)
- return board[x][y];
- else return null;
- // YOUR CODE HERE
- }
- /**
- * Places a Piece at coordinate (x, y)
- * @param p Piece to place
- * @param x X coordinate of Piece to place
- * @param y Y coordinate of Piece to place
- *///check
- public void place(Piece p, int x, int y) {
- if(x>=0&&y>=0&&x<=7&&y<=7)
- if(p!=null)
- board[x][y]=p;
- }
- /**
- * Removes a Piece at coordinate (x, y)
- * @param x X coordinate of Piece to remove
- * @param y Y coordinate of Piece to remove
- * @return Piece that was removed
- *///check
- public Piece remove(int x, int y) {
- Piece temp= new Piece(0);
- if(x>=0&&y>=0&&x<=7&&y<=7){
- if(board[x][y]!=null){
- temp=board[x][y];
- board[x][y]=null;
- return temp;
- }
- else{
- System.out.println("There is no piece.");
- return null;
- }
- }
- else{
- System.out.println("It's out of boundary.");
- return null;
- }
- // YOUR CODE HERE
- }
- /**
- * Determines if a Piece can be selected
- * @param x X coordinate of Piece
- * @param y Y coordinate of Piece to select
- * @return true if the Piece can be selected
- */
- public boolean canSelect(int x, int y) {
- if(x>=0&&x<=7&&y>=0&&y<=7){
- if(board[x][y]==null){
- if(this.count_move==0&&this.count_click==1) return false;
- else{
- if(board[this.x1][this.y1].isKing()){
- if((Math.abs(this.y1-y)==1&&Math.abs(this.x1-x)==1)||(Math.abs(this.y1-y)==2)&&(Math.abs(this.x1-x)==2)){
- if(Math.abs(this.y1-y)==1){
- if(count_move==0)
- return true;
- else return false;
- }
- else{
- if(board[x+(this.x1-x)/2][y+(this.y1-y)/2]!=null){
- if(board[x+(this.x1-x)/2][y+(this.y1-y)/2].side()!=turn){
- return true;
- }
- else{
- return false;
- }
- }
- else return false;
- }//remark
- }
- else{
- return false;
- }
- }
- //mark
- else{
- if(this.turn==0){
- if((y-this.y1==1&&Math.abs(x-this.x1)==1)||(y-this.y1==2&&Math.abs(x-this.x1)==2)){
- if(y-this.y1==1&&Math.abs(x-this.x1)==1){
- if(count_move==0)
- return true;
- else return false;
- }
- else{
- if(board[x+(this.x1-x)/2][y+(this.y1-y)/2]!=null){
- if(board[x+(this.x1-x)/2][y+(this.y1-y)/2].side()!=turn){
- return true;
- }
- else{
- return false;
- }
- }
- else return false;
- }//remark
- }
- else{
- return false;
- }
- }
- //marker
- else {
- if((this.y1-y==1&&Math.abs(x-this.x1)==1)||(this.y1-y==2&&Math.abs(x-this.x1)==2)){
- if(this.y1-y==1&&Math.abs(x-this.x1)==1){
- if(count_move==0)
- return true;
- else
- return false;
- }
- else{
- if(board[x+(this.x1-x)/2][y+(this.y1-y)/2]!=null){
- if(board[x+(this.x1-x)/2][y+(this.y1-y)/2].side()!=turn){
- return true;
- }
- else{
- return false;
- }
- }
- else return false;
- }//marker
- }
- else{
- return false;
- }
- }
- //marker
- }
- //mark
- }
- }
- else{
- if(this.count_move==0&&this.count_click==1){
- if(board[x][y].side()==turn) return true;
- else return false;
- }
- else return false;
- }
- }
- else return false;
- }
- /**
- * Selects a square. If no Piece is active, selects the Piece and
- * makes it active. If a Piece is active, performs a move if an empty
- * place is selected. Else, allows you to reselect Pieces
- * @param x X coordinate of place to select
- * @param y Y coordinate of place to select
- */
- public void select(int x, int y) {
- for(int i=0;i<8;i++){
- for(int j=0;j<8;j++){
- color[i][j]=0;
- }
- }
- if(x>=0&&y>=0&&x<=7&&y<=7){
- color[x][y]=1;
- }
- }
- /**
- * Moves the active piece to coordinate (x, y)
- * @param p Piece to move
- * @param x1 Original X coordinate of p
- * @param y1 Origin Y coordinate of p
- * @param x X coordinate to move to
- * @param y Y coordinate to move to
- */
- public void move(int x1, int y1, int x2, int y2) {
- Piece temp= new Piece(-1);
- temp= board[x1][y1];
- board[x1][y1]=board[x2][y2];
- board[x2][y2]=temp;
- if(Math.abs(x2-x1)==2){
- if(this.board[x1+(x2-x1)/2][y1+(y2-y1)/2].isking=true)
- board[x2][y2].isking=true;
- this.board[x1+(x2-x1)/2][y1+(y2-y1)/2]=null;
- if(board[x2][y2] instanceof BombPiece){
- board[x2][y2]=null;
- if(y2+1<=7){
- if(!(board[x2][y2+1] instanceof ShieldPiece))
- board[x2][y2+1]=null;
- }
- if(y2-1>=0){
- if(!(board[x2][y2-1] instanceof ShieldPiece))
- board[x2][y2-1]=null;
- }
- if(x2+1<=7){
- if(!(board[x2+1][y2] instanceof ShieldPiece))
- board[x2+1][y2]=null;
- if(y2+1<=7){
- if(!(board[x2+1][y2+1] instanceof ShieldPiece))
- board[x2+1][y2+1]=null;
- }
- if(y2-1>=0){
- if(!(board[x2+1][y2-1] instanceof ShieldPiece))
- board[x2+1][y2-1]=null;
- }
- }
- else if(x2-1>=0){
- if(!(board[x2-1][y2] instanceof ShieldPiece))
- board[x2-1][y2]=null;
- if(y2+1<=7){
- if(!(board[x2-1][y2+1] instanceof ShieldPiece))
- board[x2-1][y2+1]=null;
- }
- if(y2-1>=0){
- if(!(board[x2-1][y2-1] instanceof ShieldPiece))
- board[x2-1][y2-1]=null;
- }
- }
- }
- // YOUR CODE HERE
- }
- }
- /**
- * Determines if the turn can end
- * @return true if the turn can end
- */
- public boolean canEndTurn() {
- if(count_move>0)
- return true;
- else
- return false;
- // YOUR CODE HERE
- }
- /**
- * Ends the current turn. Changes the player.
- */
- public void endTurn() {
- if (StdDrawPlus.isSpacePressed()&&this.canEndTurn()) {
- this.turn = 1 - this.turn;
- this.count_move=0;
- this.count_click=0;
- this.x1=0;
- this.x2=0;
- this.y1=0;
- this.y2=0;
- }
- }
- /**
- * Returns the winner of the game
- * @return The winner of this game
- */
- public String winner() {
- int num_fire=0;
- int num_water=0;
- for(int i=0;i<8;i++){
- for(int j=0;j<8;j++){
- if(this.board[i][j]!=null){
- if(this.board[i][j].side()==0){
- num_fire++;
- }
- else{
- num_water++;
- }
- }
- }
- }
- if(num_fire!=0&&num_water==0) return "fire";
- else if(num_fire==0&&num_water!=0) return "water";
- else if(num_fire==0&&num_water==0) return "tie";
- else return null;
- }
- private void drawBoard(int[][] color) {
- for (int i = 0; i < board.length; i++) {
- for (int j = 0; j < board[0].length; j++) {
- if ((i + j) % 2 == 0) {
- if(color[i][j]==0){
- StdDrawPlus.setPenColor(StdDrawPlus.GRAY);
- }
- else{
- StdDrawPlus.setPenColor(StdDrawPlus.WHITE);
- }
- } else {
- if(color[i][j]==0){
- StdDrawPlus.setPenColor(StdDrawPlus.RED);
- }
- else{
- StdDrawPlus.setPenColor(StdDrawPlus.WHITE);
- }
- }
- StdDrawPlus.filledSquare(i + .5, j + .5, .5);
- if(board[i][j]!=null){
- if(board[i][j].isKing()){
- if(board[i][j].side()==0){
- if(board[i][j] instanceof BombPiece){
- StdDrawPlus.picture(i + .5, j + .5, "img/bomb-fire-crowned.png", 1, 1);
- }
- else if(board[i][j] instanceof ShieldPiece){
- StdDrawPlus.picture(i + .5, j + .5, "img/shield-fire-crowned.png", 1, 1);
- }
- else if(board[i][j]!=null){
- StdDrawPlus.picture(i + .5, j + .5, "img/pawn-fire-crowned.png", 1, 1);
- }
- }
- else if(board[i][j].side()==1){
- if(board[i][j] instanceof BombPiece){
- StdDrawPlus.picture(i + .5, j + .5, "img/bomb-water-crowned.png", 1, 1);
- }
- else if(board[i][j] instanceof ShieldPiece){
- StdDrawPlus.picture(i + .5, j + .5, "img/shield-water-crowned.png", 1, 1);
- }
- else if(board[i][j]!=null){
- StdDrawPlus.picture(i + .5, j + .5, "img/pawn-water-crowned.png", 1, 1);
- }
- }
- }
- else{
- if(board[i][j].side()==0){
- if(board[i][j] instanceof BombPiece){
- StdDrawPlus.picture(i + .5, j + .5, "img/bomb-fire.png", 1, 1);
- }
- else if(board[i][j] instanceof ShieldPiece){
- StdDrawPlus.picture(i + .5, j + .5, "img/shield-fire.png", 1, 1);
- }
- else if(board[i][j]!=null){
- StdDrawPlus.picture(i + .5, j + .5, "img/pawn-fire.png", 1, 1);
- }
- }
- else if(board[i][j].side()==1){
- if(board[i][j] instanceof BombPiece){
- StdDrawPlus.picture(i + .5, j + .5, "img/bomb-water.png", 1, 1);
- }
- else if(board[i][j] instanceof ShieldPiece){
- StdDrawPlus.picture(i + .5, j + .5, "img/shield-water.png", 1, 1);
- }
- else if(board[i][j]!=null){
- StdDrawPlus.picture(i + .5, j + .5, "img/pawn-water.png", 1, 1);
- }
- }
- }
- }
- }
- }
- }
- public static void main(String[] args) {
- Board b=new Board(false);
- StdDrawPlus.setScale(0, 8);
- while(true){
- b.drawBoard(b.color);
- StdDrawPlus.show(10);
- if (StdDrawPlus.mousePressed()) {
- int x_click = (int) StdDrawPlus.mouseX();
- int y_click = (int) StdDrawPlus.mouseY();
- if(x_click>=0&&x_click<=7&&y_click<=7&&y_click>=0){ //mark
- b.count_click++;
- if(b.board[x_click][y_click]!=null&&b.board[b.x1][b.y1]!=null&&b.count_click==2&&b.count_move==0){
- b.count_click=1;
- x_click=8;
- y_click=8;
- }
- if(b.canSelect(x_click,y_click)){
- b.x2=x_click;
- b.y2=y_click;
- b.select(b.x2,b.y2);
- }
- else{
- b.count_click=0;
- }
- } //mark
- if(b.count_click>1){
- if(!(b.x1==b.x2&&b.y1==b.y2)){
- if(b.canSelect(b.x2,b.y2)){
- b.move(b.x1,b.y1,b.x2,b.y2);
- b.count_move++;
- b.count_click=0;
- }
- }
- }
- b.x1=b.x2;
- b.y1=b.y2;
- }
- System.out.println(b.count_click);
- // determine is king or not
- for(int i=0;i<8;i++){
- if(b.board[i][0]!=null){
- if(b.board[i][0].side()==1){
- b.board[i][0].isking=true;
- }
- }
- if(b.board[i][7]!=null){
- if(b.board[i][7].side()==0){
- b.board[i][7].isking=true;
- }
- }
- }
- if(b.winner()!=null){
- System.out.println(b.winner());
- }
- b.endTurn();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement