-
Notifications
You must be signed in to change notification settings - Fork 1
/
Piece.java
128 lines (116 loc) · 4.29 KB
/
Piece.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Write a description of class Piece here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Piece extends Actor
{
private GreenfootImage image;
public final int PIECE_SIZE = GameWorld.WORLD_SIZE / 10;
public Color color;
private MouseInfo mouse;
private boolean hovering = false;
private boolean clicked = false;
private ArrayList<Tile> availableTiles;
public Piece(Color color){
this.color = color;
image = new GreenfootImage(PIECE_SIZE + 1, PIECE_SIZE + 1);
drawPiece(color);
}
public Color getColor() {return color;}
/**
* Act - do whatever the Pieces wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
mouse = Greenfoot.getMouseInfo();
// try
// {
// System.out.println(mouse.getX() + " " + mouse.getY()); // debug
// }catch(Exception NullPointerException){}
Tile tile = (Tile)getOneIntersectingObject(Tile.class);
if(Greenfoot.mouseClicked(this)){
hovering = false;
clicked = true;
availableTiles = new ArrayList<Tile>();
check();
}
else if(clicked) {
for(Tile availableTile: availableTiles){
if(Greenfoot.mouseClicked(availableTile)){
this.setLocation(availableTile.getX(), availableTile.getY());
createBoard();
}
clicked = false;
}
}
else if(!clicked){
if(Greenfoot.mouseMoved(this)){
hovering = true;
// if(!soundPlayed){ //adds sound
// moveSound.play();
// soundPlayed = true;
// }
tile.drawTile(tile.getColor(), hovering);
}
if(Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this)){
hovering = false;
// soundPlayed = false;
tile.drawTile(tile.getColor(), hovering);
}
}
}
private void drawPiece(Color color){
image.clear();
image.setColor(color);
image.fillOval(1, 1, PIECE_SIZE, PIECE_SIZE);
setImage(image);
}
private void check(){
char[][] middle = createBoard();
int newX = getX()/GameWorld.TILE_SIZE;
int newY = getY()/GameWorld.TILE_SIZE;
if(GameWorld.game.check(middle,newX,newY,newX-1,newY+1)) {
List tilesPresent = getWorld().getObjectsAt(getX() - GameWorld.TILE_SIZE, getY() + GameWorld.TILE_SIZE, Tile.class);
if(tilesPresent.size() > 0) availableTiles.add((Tile)tilesPresent.get(0));
}
if(GameWorld.game.check(middle,newX,newY,newX+1,newY+1))
{
List tilesPresent = getWorld().getObjectsAt(getX() + GameWorld.TILE_SIZE, getY() + GameWorld.TILE_SIZE, Tile.class);
if(tilesPresent.size() > 0) availableTiles.add((Tile)tilesPresent.get(0));
}
for(Tile tile: availableTiles) tile.drawTile(tile.getColor(), true);
}
private void printBoard(char[][] board) //debugging
{
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++) System.out.print(board[i][j]);
System.out.println();
}
}
private char[][] createBoard()
{
char[][] board = GameWorld.game.getBoard();
for(int row = 0; row < 8; row++)
{
for(int col = 0; col < 8; col++)
{
int offset = (col + 1) % 2 == 0 ? 0:1;
if((row + 1 + offset) % 2 == 1)
{
List pieceAt = getWorld().getObjectsAt(50 * (col) + 25,50 * (row) + 25, Piece.class);
if(pieceAt.size() == 0) {board[row][col] = 'n';}
else if(((Piece)pieceAt.get(0)).getColor().equals(GameWorld.pieceColor1)) {board[row][col] = 'r';}
else board[row][col] = 'w';
}
else board[row][col] = 'n';
}
}
return board;
}
}