-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwixtCell.java
executable file
·137 lines (126 loc) · 3.18 KB
/
TwixtCell.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
129
130
131
132
133
134
135
136
137
/**
* TwixtCell.java
*
* CSE Fall 2007
* Game Project
* @author Steven Speicher
* @version 1.0
*/
//-------------------
// Import Statements
//-------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TwixtCell extends JLabel
{
//-----------
// Constants
//-----------
public static final int BLANK = 0;
public static final int RED = 1;
public static final int BLUE = 2;
//----------------------
// Private data members
//----------------------
private JLabel content;
private ImageIcon redImageIcon; //Red peg image
private ImageIcon blueImageIcon; //Blue peg image
private ImageIcon blankImageIcon; //Blank peg image
private int row; //Row of cell
private int col; //Column of cell
//-------------
// Constructor
//-------------
public TwixtCell(int row, int col)
{
this(null);
this.row = row + 1;
this.col = col + 1;
}
public TwixtCell(Point point)
{
redImageIcon = new ImageIcon("redPlayerPeg.gif");
blueImageIcon = new ImageIcon("bluePlayerPeg.gif");
blankImageIcon = new ImageIcon("blankPeg.gif");
setLayout(new BorderLayout());
setBackground(Color.white);
setBorder(BorderFactory.createLineBorder(Color.gray));
setBlank();
//content = new JLabel(blankImageIcon);
//add(content);
// location = point;
}
//---------
//Set Blank
//---------
public void setBlank()
{
content = new JLabel(blankImageIcon);
add(content);
}
//--------
//Get Row
//--------
public int getRow()
{
return this.row;
}
//-----------
//Get Column
//-----------
public int getCol()
{
return this.col;
}
//-----------
//Set Content
//-----------
public void setContent(int image)
{
switch (image){
case RED: content.setIcon(redImageIcon);
break;
case BLUE: content.setIcon(blueImageIcon);
break;
case BLANK: content.setIcon(blankImageIcon);
break;
default: //nothing
break;
}
}
//-----------------
//Check Free Space
//-----------------
public boolean checkFreeSpace()
{
boolean takenSpace = false;
boolean freeSpace = true;
if (content.getIcon().equals(redImageIcon)){
return takenSpace;
}else if(content.getIcon().equals(blueImageIcon)){
return takenSpace;
}else{
return freeSpace;
}
}
//------------
//Check Border
//------------
public boolean checkBorder(boolean redPlayerTurn)
{
boolean borderTrespass = false;
if(redPlayerTurn){
if(getRow() == 1 || getRow() == 24){
JOptionPane.showMessageDialog(null,"Red cannont move here.");
borderTrespass = true;
}
}else{
if(getCol() == 1 || getCol() == 24){
JOptionPane.showMessageDialog(null,"Blue cannont move here.");
borderTrespass = true;
}
}
return borderTrespass;
}
}