-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
279 lines (243 loc) · 9.46 KB
/
GUI.java
File metadata and controls
279 lines (243 loc) · 9.46 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.awt.*;
import javax.swing.*;
import javax.swing.text.html.Option;
import java.util.ArrayList;
import java.util.Scanner;
class GUI extends JFrame{
public GUI(){
super("Upwords");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
add(mainPanel);
Game game = new Game(2);
JLabel Uppertext = new JLabel("Player 1s turn Score: "+game.getPlayers()[0].getScore(), SwingConstants.LEFT);
JLabel Bottomtext = new JLabel(" ", SwingConstants.LEFT);
Grid grid = new Grid(Uppertext,Bottomtext,game);
mainPanel.add(Uppertext,BorderLayout.PAGE_START);
mainPanel.add(grid, BorderLayout.CENTER);
mainPanel.add(Bottomtext, BorderLayout.PAGE_END);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new GUI();
}
}
class Grid extends JPanel {
Game game;
JButton[][] squares = new JButton[8][8];
JButton[] pieces = new JButton[7];
JButton[] options = new JButton[4];
JLabel upperText;
JLabel bottomText;
Board board;
Board cloneBoard;
ArrayList<String> CurrrentPlayerhand;
int turn;
String CurrentLetter;
String CurrentLocation;
int CurrentHeight;
public Grid(JLabel upperText, JLabel bottomText,Game games) {
this.game = games;
CurrrentPlayerhand = game.getPlayers()[turn].getHand();
this.game.setCurrrentPlayerhand(CurrrentPlayerhand);
this.upperText = upperText;
this.bottomText = bottomText;
setLayout(new BorderLayout());
board = game.getGameboard();
cloneBoard = new Board();
turn = game.getPlayerturn();
CurrentLetter ="";
CurrentLocation = "";
CurrentHeight = 0;
initializeBoard();
initializeBottomrow();
}
public void initializeBoard(){
System.out.println(board);
JPanel gridPanel = new JPanel(new GridLayout(8, 8));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
squares[i][j] = new JButton();
String letter = (((board.getBoard()).get(Integer.toString(j+1)+Integer.toString(i+1)))).getLetter();
if(board.getBoard().get(Integer.toString(j+1)+Integer.toString(i+1)).getHeight()>0){
squares[i][j].setText(letter+board.getBoard().get(Integer.toString(j+1)+Integer.toString(i+1)).getHeight());
}
else{
squares[i][j].setText("");
}
gridPanel.add(squares[i][j]);
addActionListenerBoard(i,j);
}
}
add(gridPanel, BorderLayout.CENTER);
}
public void initializeBottomrow(){
JPanel Bottomrow = new JPanel();
JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
for (int j = 0; j < 7; j++) {
pieces[j] = new JButton();
pieces[j].setPreferredSize(new Dimension(70, 50));
if(j<CurrrentPlayerhand.size()){
pieces[j].setText(CurrrentPlayerhand.get(j));
System.out.println(pieces[j].getText());
pieces[j].repaint();
}
else{
pieces[j].setText("?");
}
rowPanel.add(pieces[j]);
addActionListenerHand(j);
}
Bottomrow.add(rowPanel);
JPanel bowPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
String[] option = {"Skip","End Turn","Restart","Exchange a peice"};
for (int j = 0; j < 4; j++) {
options[j] = new JButton();
options[j].setPreferredSize(new Dimension(150, 50));
options[j].setText(option[j]);
bowPanel.add(options[j]);
addActionListenerOption(j);
}
Bottomrow.repaint();
Bottomrow.add(bowPanel);
add(Bottomrow, BorderLayout.PAGE_END);
}
public void addActionListenerBoard(int i, int j){
squares[i][j].addActionListener(e -> actionPerformedBoard(i, j));
}
public void actionPerformedBoard(int i, int j){
if(CurrentLetter.equals("")){
System.out.println(squares[i][j].getText());
bottomText.setText("Must click a letter before clicking a space");
}
else{
String location = numberToLetter(j+1)+Integer.toString(i+1);
game.setCurrentPlacedLetter(CurrentLetter);
game.setCurrentPlacedLocation(location);
try{
game.LetterinHand();
game.SurroundingisEmpty(cloneBoard);
game.checkplayedlocations();
cloneBoard.PlaceALetter(location, CurrentLetter);
location = game.gameboard.getnumber(location.substring(0,1))+location.substring(1);
game.TakenOverLetters+= game.gameboard.getBoard().get(location).getLetter();
System.out.println(cloneBoard);
}
catch(Exception a){
bottomText.setText(a.getMessage());
return;
}
CurrentHeight = cloneBoard.getBoard().get(location).getHeight();
squares[i][j].setText(CurrentLetter+CurrentHeight);
game.removeletter();
CurrentLetter = "";
CurrentLocation = "";
for(int a = 0;i<CurrrentPlayerhand.size();i++){
if(CurrrentPlayerhand.get(a).equals(CurrentLetter)){
CurrrentPlayerhand.remove(a);
break;
}
}
updateHand();
bottomText.setText(" ");
}
}
public void addActionListenerHand(int i){
pieces[i].addActionListener(e -> actionPerformedHand(i));
}
public void actionPerformedHand(int i){
CurrentLetter = pieces[i].getText();
bottomText.setText("Current letter is "+CurrentLetter);
}
public void addActionListenerOption(int i){
options[i].addActionListener(e -> actionPerformedOption(i));
}
public void actionPerformedOption(int i){
if(options[i].getText().equals("Restart")) {
playerOptionOne();
}
if(options[i].getText().equals("End Turn")) {
playerOptionTwo();
}
}
public void playerOptionOne() {
while (game.enteredLetters.size() > 0) {
game.CurrrentPlayerhand.add(game.enteredLetters.get(0));
game.enteredLetters.remove(0);
System.out.println(game.enteredLetters);
System.out.println(game.CurrrentPlayerhand);
}
resetvariables();
initializeBottomrow();
initializeBoard();
bottomText.setText("Restarted Board");
}
public void resetvariables(){
game.enteredLocations = new ArrayList<String>();
game.currentPlacedLetter ="";
game.currentPlacedLocation = "";
CurrrentPlayerhand = game.CurrrentPlayerhand;
game.enteredLetters = new ArrayList<String>();
cloneBoard = game.gameboard.clone();
board = game.getGameboard();
}
private void updateHand() {
for (int i = 0; i < 7; i++) {
pieces[i].setText("");
if (i < CurrrentPlayerhand.size()) {
pieces[i].setText(CurrrentPlayerhand.get(i));
}
}
}
public void playerOptionTwo(){
if(cloneBoard.equals(game.gameboard)){
bottomText.setText("No changes to the board have been made can't finish turn");
}
try{
game.BoardisValid(cloneBoard);
game.gameboard = cloneBoard.clone();
System.out.println(game.gameboard);
System.out.println(cloneBoard);
while(game.CurrrentPlayerhand.size()<7){
game.CurrrentPlayerhand.add(game.letters.Draw1Letter());
}
game.NumberofSkips = 0;
if(game.getPlayerturn() == 1){
game.setPlayerturn(0);
}
else if(game.getPlayerturn() == 0){
game.setPlayerturn(1);
}
turn = game.getPlayerturn();
game.CurrrentPlayerhand = game.players[turn].getHand();
resetvariables();
System.out.println(game.CurrrentPlayerhand);
System.out.println(CurrrentPlayerhand);
upperText.setText("Player "+(turn+1)+"s turn Score: "+game.getPlayers()[turn].getScore());
updateHand();
}
catch(Exception a){
bottomText.setText(a.getMessage());
}
}
public String numberToLetter(int num){
if(num == 1){
return "a";
}if(num == 2){
return "b";
}if(num == 3){
return "c";
}if(num == 4){
return "d";
}if(num == 5){
return "e";
}if(num == 6){
return "f";
}if(num == 7){
return "g";
}
return "h";
}
}