Skip to content
Snippets Groups Projects
Commit bcb04412 authored by dsjohns2's avatar dsjohns2
Browse files

everything working

parent c2e3df45
No related branches found
No related tags found
No related merge requests found
......@@ -22,23 +22,93 @@ public class Controller extends Applet {
public JButton second_clicked;
public int second_clicked_x;
public int second_clicked_y;
public String player1;
public String player2;
public boolean draw_offered = false;
public void init(){
try {
my_game = new Game();
my_game.initialize_board();
player1 = JOptionPane.showInputDialog("What is Player 1's name?");
player2 = JOptionPane.showInputDialog("What is Player 2's name?");
my_view = new View(this);
my_view.undo_press(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0; i<8; i++) {
for (int j = 0; j < 8; j++) {
my_game.board[i][j] = new Square(my_game.last_position[i][j]);
}
}
my_game.white_turn = !my_game.white_turn;
try {
my_view.update_view(my_game);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
my_view.white_draws(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(draw_offered){
JOptionPane.showMessageDialog(my_view.f, "Draw");
my_game.initialize_board();
draw_offered = false;
}
else{
draw_offered = true;
}
try {
my_view.update_view(my_game);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
my_view.black_draws(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(draw_offered){
JOptionPane.showMessageDialog(my_view.f, "Draw");
my_game.initialize_board();
draw_offered = false;
}
else{
draw_offered = true;
}
try {
my_view.update_view(my_game);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
my_view.white_forfeits(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
my_game.initialize_board();
try {
my_view.update_view(my_game);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
my_view.black_forfeits(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
my_game.initialize_board();
try {
my_view.update_view(my_game);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
my_view.square_move(new ActionListener() {
......@@ -69,26 +139,37 @@ public class Controller extends Applet {
}
}
if(move_found){
for(int i=0; i<8; i++) {
for (int j = 0; j < 8; j++) {
my_game.last_position[i][j] = new Square(my_game.board[i][j]);
}
}
my_game.add_piece_to_location(second_clicked_x, second_clicked_y, my_game.board[first_clicked_x][first_clicked_y].name, my_game.white_turn, true);
my_game.add_piece_to_location(first_clicked_x, first_clicked_y, "", true, false);
my_game.white_turn = !my_game.white_turn;
}
else{
System.out.println("Illegal");
JOptionPane.showMessageDialog(my_view.f, "Illegal");
}
try {
my_view.update_view(my_game);
legal_moves = my_game.get_all_moves(my_game.white_turn, my_game.board);
int end_val = my_game.check_for_end_of_game(legal_moves);
if(end_val == 0){
System.out.println("Stalemate");
JOptionPane.showMessageDialog(my_view.f, "Stalemate");
my_game.initialize_board();
}
else if(end_val == 1){
System.out.println("White Wins");
JOptionPane.showMessageDialog(my_view.f, "White Wins");
my_game.white_score += 1;
my_game.initialize_board();
}
else if(end_val == 2){
System.out.println("Black Wins");
JOptionPane.showMessageDialog(my_view.f, "Black Wins");
my_game.black_score += 1;
my_game.initialize_board();
}
my_view.update_view(my_game);
} catch (IOException e1) {
e1.printStackTrace();
}
......
......@@ -10,9 +10,12 @@ import java.util.concurrent.TimeUnit;
* The class that contains the entire chess game
*/
public class Game {
public int white_score = 0;
public int black_score = 0;
public int x_dim = 8;
public int y_dim = 8;
public Square[][] board = new Square[x_dim][y_dim];
public Square[][] last_position = new Square[x_dim][y_dim];
public boolean white_turn = true;
/**
......@@ -32,6 +35,7 @@ public class Game {
for(int i=0; i<x_dim; i++){
for(int j=0; j<y_dim; j++){
this.board[i][j] = new Square(other.board[i][j]);
this.last_position[i][j] = new Square(other.last_position[i][j]);
}
}
}
......@@ -40,6 +44,7 @@ public class Game {
* This method sets up the game board at the beginning of the game.
*/
public void initialize_board(){
this.white_turn = true;
for(int i=0; i<x_dim; i++){
for(int j=0; j<y_dim; j++){
Square new_square = new Square();
......@@ -59,14 +64,13 @@ public class Game {
new_square.occupied = false;
}
board[i][j] = new_square;
last_position[i][j] = new Square(new_square);
}
}
/*
add_piece_to_location(2, 3, "A", false, true);
add_piece_to_location(2, 4, "Z", false, true);
add_piece_to_location(5, 3, "A", true, true);
add_piece_to_location(5, 4, "Z", true, true);
*/
}
/**
......
......@@ -17,14 +17,17 @@ import javax.swing.*;
* This is the View class in the MCV model
*/
public class View {
public JFrame f;
private JButton buttons[][];
private JPanel panel;
private JPanel control_panel;
private JPanel name_panel;
private JButton white_forfeit;
private JButton black_forfeit;
private JButton white_draw;
private JButton black_draw;
private JButton score;
private JButton undo;
/**
* This is the default constructor that takes in the controller
* @param controller this is the controller of the view
......@@ -32,7 +35,7 @@ public class View {
*/
View(Controller controller) throws IOException {
Game game = controller.my_game;
JFrame f= new JFrame("Chess");
f= new JFrame("Chess");
panel=new JPanel();
panel.setLayout(new GridLayout(8, 8, 0, 0));
panel.setBounds(0,0,500,500);
......@@ -108,36 +111,43 @@ public class View {
buttons[i][j] = button;
}
}
name_panel = new JPanel();
name_panel.setLayout(new GridLayout(1, 3, 0, 0));
name_panel.setBounds(0,500,500,20);
name_panel.setBackground(Color.gray);
JButton white_name = new JButton(controller.player1);
JButton black_name = new JButton(controller.player2);
score = new JButton(game.white_score + " - " + game.black_score);
name_panel.add(white_name);
name_panel.add(score);
name_panel.add(black_name);
control_panel=new JPanel();
control_panel.setLayout(new GridLayout(1, 5, 0, 0));
control_panel.setBounds(0,500,500,50);
control_panel.setBounds(0,520,500,40);
control_panel.setBackground(Color.gray);
white_forfeit = new JButton("Forfeit");
black_forfeit = new JButton("Forfeit");
white_draw = new JButton("Draw");
black_draw = new JButton("Draw");
score = new JButton("0 - 0");
white_forfeit.setSize(40, 40);
black_forfeit.setSize(40, 40);
white_draw.setSize(40, 40);
black_draw.setSize(40, 40);
score.setSize(40, 40);
undo = new JButton("Undo");
white_forfeit.setBackground(Color.white);
white_draw.setBackground(Color.white);
black_draw.setBackground(Color.darkGray);
black_forfeit.setBackground(Color.darkGray);
control_panel.add(white_forfeit);
control_panel.add(white_draw);
control_panel.add(score);
control_panel.add(undo);
control_panel.add(black_draw);
control_panel.add(black_forfeit);
f.add(panel);
f.add(control_panel);
f.add(name_panel);
f.setSize(600,600);
f.setLayout(null);
f.setVisible(true);
}
public void update_buttons(Game game, JPanel panel) throws IOException {
score.setText(game.white_score + " - " + game.black_score);
for(int i=0; i<8; i++) {
for (int j = 0; j<8; j++) {
JButton button = this.buttons[i][j];
......@@ -206,8 +216,8 @@ public class View {
public void update_view(Game my_game) throws IOException {
update_buttons(my_game, this.panel);
}
public void move_piece(ActionListener a){
white_forfeit.addActionListener(a);
public void undo_press(ActionListener a){
undo.addActionListener(a);
}
public void white_forfeits(ActionListener a){
white_forfeit.addActionListener(a);
......@@ -215,6 +225,12 @@ public class View {
public void black_forfeits(ActionListener a){
black_forfeit.addActionListener(a);
}
public void white_draws(ActionListener a){
white_draw.addActionListener(a);
}
public void black_draws(ActionListener a){
black_draw.addActionListener(a);
}
public void square_move(ActionListener a){
for(int i=0; i<8; i++) {
for (int j = 0; j<8; j++) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment