-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMancalaView.java
More file actions
184 lines (152 loc) · 7.14 KB
/
Copy pathMancalaView.java
File metadata and controls
184 lines (152 loc) · 7.14 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
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* The class that represents the View of the MVC design pattern. Paints GUI components and listens for changes.
* @author: Hung Phan
* @author: Lien Cao
* @author: Wendy Ta
* @version: 1.0 11/28/25
*/
public class MancalaView extends JPanel implements ChangeListener {
private final MancalaModel model;
private ArrayList<Pit> pits;
private final int offset = 80;
private boolean initialRun = true;
/**
* Constructs the view of the game
* @param model the specific model of the game
*/
public MancalaView(MancalaModel model) {
this.model = model;
model.addChangeListener(this);
createBoard();
pits = new ArrayList<>();
pits.addAll(model.getNameToPit().values());
MouseListeners listeners = new MouseListeners();
addMouseListener(listeners);
addMouseMotionListener(listeners);
}
/**
* This class handles custom behavior when the mouse interacts
*/
private class MouseListeners extends MouseInputAdapter {
@Override
public void mousePressed(MouseEvent event) {
double x = event.getX() + offset; //get the x and y coordinates of mouse click
double y = event.getY();
for(Pit pit : model.getNameToPit().values()) { //iterate through each
if(pit.getName().equals("leftStore") || pit.getName().equals("rightStore")) {
continue; // skip the stores
}
if (pit.getPit().contains(x, y)) { //find the pit that was clicked on
if (model.getCurrentPlayer().equals(pit.getName().substring(0, 1))) { //check if selected pit is valid based on the player
model.gameRun(pit); //proceed with the game rules
}
else {
System.out.println("Player " + model.getCurrentPlayer() + " clicked on invalid pit.");
}
}
}
repaint();
}
}
/**
* Listens for a change and repaints the board.
*/
@Override
public void stateChanged(ChangeEvent e) {
repaint();
}
/**
* This method is to create the board game
*/
public void createBoard() { //update 11/6: update the board with the updated nuber of stones in each pit (# stones not always = inputNum)
JPanel boardPanel = new JPanel() { //store the main board
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle2D board = new Rectangle2D.Double(80, 45, 840, 300);
int leftNum = 0;
int rightNum = 0;
if (!initialRun) { //set to updated value if not initial run
leftNum = model.getNameToPit().get("leftStore").getNumStones();
rightNum = model.getNameToPit().get("rightStore").getNumStones();
}
Pit leftStore = new Pit("leftStore", 110.0, 80.0, 80.0, 230, leftNum);
Pit rightStore = new Pit("rightStore", 810.0, 80.0, 80.0, 230.0, rightNum);
model.addToNameMap("leftStore", leftStore);
model.addToNameMap("rightStore", rightStore);
model.addToAllPits(0, leftStore);
model.addToAllPits(7, rightStore);
BoardStyle boardStyle = model.getBoardStyle();
model.setBoardStyle(model.getBoardStyle());
boardStyle.drawBoard(g2, board);
boardStyle.drawPit(g2, leftStore);
boardStyle.drawPit(g2, rightStore);
g2.setStroke(new BasicStroke(3));
leftStore.drawPit(g2);
rightStore.drawPit(g2);
double x1 = leftStore.getY() + 30 + offset;
double y1 = leftStore.getY() + 20 + offset;
double x2 = leftStore.getY() + 30 + offset;
double y2 = leftStore.getY() + 130 + offset;
double w = 80;
Font smallFont = new Font("Ariel", Font.BOLD, 20);
int pitIdx = 1;
for (int j=1; j<=6; j++) { //bottom row, change y position to the next line
String pitName = "A" + j;
int numStones = model.getNumber(); //if first time, not all pits initialized yet
if (model.getNameToPit().size() == 14) { //all pits initialized --> use the specific number of stones, access from Pit
numStones = model.getNameToPit().get(pitName).getNumStones();
}
Pit pit = new Pit(pitName, x2+=100, y2, w, w, numStones);
model.addToNameMap(pitName, pit);
boardStyle.drawPit(g2, pit);
pit.drawPit(g2);
g2.setFont(smallFont);
g2.drawString(pitName, (int)(x2+30), (int)y2+110);
model.addToAllPits(pitIdx, pit);
pitIdx++;
}
pitIdx = 13;
for (int i=6; i>0; i--) { //top row
String pitName = "B" + i;
int numStones = model.getNumber(); //if first time, not all pits initialized yet
if (model.getNameToPit().size() == 14) { //all pits initialized --> use the specific number of stones, access from Pit
numStones = model.getNameToPit().get(pitName).getNumStones();
}
Pit pit = new Pit(pitName, x1+=100, y1, w, w, numStones);
model.addToNameMap(pitName, pit);
boardStyle.drawPit(g2, pit);
pit.drawPit(g2);
g2.setFont(smallFont);
g2.drawString(pitName, (int)(x1+30), (int)y1-15);
model.addToAllPits(pitIdx, pit);
pitIdx--;
}
String turnText = "Current Turn: Player " + model.getCurrentPlayer();
Font turnFont = new Font("Arial", Font.BOLD, 20);
g2.setFont(turnFont);
FontMetrics fm = g2.getFontMetrics();
int turnTextWidth = fm.stringWidth(turnText);
g2.drawString(turnText, getWidth() - turnTextWidth - 100, 30);
String winner = model.getWinner();
Font font = new Font("Arial", Font.BOLD, 30);
g2.setFont(font);
g2.setColor(Color.blue);
if (!winner.equals("None")) { //if there is a winner
String winnerText = "Player " + winner + " wins!";
g2.drawString(winnerText, getWidth() - turnTextWidth - 400, 20);
}
initialRun = false;
}
};
boardPanel.setPreferredSize(new Dimension(1000, 500));
add(boardPanel);
}
}