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

almost everything working

parent 07a4bb36
No related branches found
No related tags found
No related merge requests found
......@@ -3,17 +3,124 @@ package com.company;
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* This is the Controller class in the MCV model
*/
public class Controller extends Applet {
public void init(Game game){
public Game my_game;
public View my_view;
public boolean piece_selected = false;
public JButton first_clicked;
public int first_clicked_x;
public int first_clicked_y;
public Color first_clicked_color;
public JButton second_clicked;
public int second_clicked_x;
public int second_clicked_y;
public void init(){
try {
new View(game);
my_game = new Game();
my_game.initialize_board();
my_view = new View(this);
my_view.white_forfeits(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
my_game.initialize_board();
}
});
my_view.black_forfeits(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
my_game.initialize_board();
}
});
my_view.square_move(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!piece_selected) {
first_clicked = (JButton) e.getSource();
first_clicked_y = (first_clicked.getBounds().x-2)/62;
first_clicked_x = (first_clicked.getBounds().y-2)/62;
first_clicked_color = first_clicked.getBackground();
first_clicked.setBackground(Color.yellow);
piece_selected = true;
}
else {
first_clicked.setBackground(first_clicked_color);
second_clicked = (JButton) e.getSource();
second_clicked_y = (second_clicked.getBounds().x-2)/62;
second_clicked_x = (second_clicked.getBounds().y-2)/62;
Game potential_move = new Game(my_game);
potential_move.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);
potential_move.add_piece_to_location(first_clicked_x, first_clicked_y, "", true, false);
boolean move_found = false;
Square[][][] legal_moves = my_game.get_all_moves(my_game.white_turn, my_game.board);
for(int i=0; i<legal_moves.length; i++){
if(my_game.equal_boards(legal_moves[i], potential_move.board)){
move_found = true;
break;
}
}
if(move_found){
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");
}
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");
}
else if(end_val == 1){
System.out.println("White Wins");
}
else if(end_val == 2){
System.out.println("Black Wins");
}
} catch (IOException e1) {
e1.printStackTrace();
}
piece_selected = false;
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* A method to run through the game by taking turns
* @return End game state integer
*/
public int run_game() throws InterruptedException, IOException {
int end_of_game = -1;
while(end_of_game == -1){
end_of_game = my_game.make_move();
my_view.update_view(my_game);
TimeUnit.SECONDS.sleep(2);
}
if(end_of_game == 0){
System.out.println("Stalemate");
}
else if(end_of_game == 1){
System.out.println("White Wins");
}
else{
System.out.println("Black Wins");
}
return end_of_game;
}
}
package com.company;
import org.junit.Test;
import static org.junit.Assert.*;
public class ControllerTest {
@Test
public void init() {
}
}
\ No newline at end of file
......@@ -15,6 +15,27 @@ public class Game {
public Square[][] board = new Square[x_dim][y_dim];
public boolean white_turn = true;
/**
* A default constructor
*/
public Game(){
}
/**
* A copy constructor
* @param other the other game
*/
public Game(Game other){
this.x_dim = other.x_dim;
this.y_dim = other.y_dim;
this.white_turn = other.white_turn;
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 method sets up the game board at the beginning of the game.
*/
......@@ -40,10 +61,12 @@ public class Game {
board[i][j] = 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);
*/
}
/**
......@@ -91,38 +114,6 @@ public class Game {
}
}
/**
* A method to print out the current state of the game
* @param board the current state of the game
*/
public void print_board(Square[][] board){
/* color vars from https://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println */
String ANSI_RESET = "\u001B[0m";
String ANSI_GREEN = "\u001B[32m";
String ANSI_BLUE = "\u001B[34m";
System.out.println();
System.out.println("~~~~~~~~~~~~~~~~~");
for(int i=0; i<x_dim; i++){
for(int j=0; j<y_dim; j++){
Square cur_square = board[i][j];
System.out.print("|");
if(cur_square.occupied && cur_square.white) {
System.out.print(ANSI_GREEN + cur_square.name + ANSI_RESET);
}
else if(cur_square.occupied && !cur_square.white) {
System.out.print(ANSI_BLUE + cur_square.name + ANSI_RESET);
}
else{
System.out.print(" ");
}
}
System.out.println("|");
System.out.println("~~~~~~~~~~~~~~~~~");
}
System.out.println();
}
/**
* A method to change board states through a legal move
* @return -1 if the game is not over
......@@ -191,6 +182,25 @@ public class Game {
return(legal_moves[n]);
}
/**
* test if two boards are equal
* @param board1 first board
* @param board2 second board
* @return true if equal, false if not
*/
public boolean equal_boards(Square[][] board1, Square[][] board2){
boolean is_equal = true;
for(int i=0; i<x_dim; i++) {
for (int j = 0; j < y_dim; j++) {
if(!board1[i][j].equal_squares(board1[i][j], board2[i][j])){
is_equal = false;
break;
}
}
}
return is_equal;
}
/**
* A method to get the legal moves and then remove the moves that end with the play still in check
* @param white whose turn
......
......@@ -20,7 +20,7 @@ public class GameTest {
test_game.initialize_board();
assertEquals (test_game.make_move(), -1);
}
/*
@org.junit.Test
public void initialize_board() {
......@@ -52,5 +52,5 @@ public class GameTest {
@org.junit.Test
public void remove_moves_into_check() {
}
*/
}
\ No newline at end of file
package com.company;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* The class that runs the game
*/
public class Main {
public static void main(String[] args) throws InterruptedException {
Game my_game = new Game();
my_game.initialize_board();
public static void main(String[] args) throws InterruptedException, IOException {
Controller control = new Controller();
control.init(my_game);
my_game.print_board(my_game.board);
int end_of_game = -1;
while(end_of_game == -1){
end_of_game = my_game.make_move();
TimeUnit.SECONDS.sleep(1);
my_game.print_board(my_game.board);
}
if(end_of_game == 0){
System.out.println("Stalemate");
}
else if(end_of_game == 1){
System.out.println("White Wins");
}
else{
System.out.println("Black Wins");
}
control.init();
}
}
package com.company;
import org.junit.Test;
import static org.junit.Assert.*;
public class MainTest {
@Test
public void main() {
}
}
\ No newline at end of file
......@@ -37,6 +37,29 @@ public class Square {
this.y = other.y;
}
/**
* test if two squares are equal
* @param square1 first board
* @param square2 second board
* @return true if equal, false if not
*/
public boolean equal_squares(Square square1, Square square2){
if(square1.occupied && square2.occupied){
if((square1.name == square2.name) && (square1.white == square2.white)){
return true;
}
else{
return false;
}
}
else if(!square1.occupied && !square2.occupied){
return true;
}
else{
return false;
}
}
/**
* A method to return the possible locations the current piece can reach given its current location
* @param board the current state of the board
......
package com.company;
import org.junit.Test;
import static org.junit.Assert.*;
public class SquareTest {
@Test
public void squares_that_can_be_taken() {
}
@Test
public void white_pawn_squares() {
}
@Test
public void legal_square_addition() {
}
@Test
public void black_pawn_squares() {
}
@Test
public void line() {
}
@Test
public void rook_squares() {
}
@Test
public void bishop_squares() {
}
@Test
public void square_checker() {
}
@Test
public void knight_squares() {
}
@Test
public void queen_squares() {
}
@Test
public void a_squares() {
}
@Test
public void z_squares() {
}
@Test
public void king_squares() {
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ import java.awt.*;
//based off of code at https://www.javatpoint.com/java-jpanel
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
......@@ -16,17 +17,27 @@ import javax.swing.*;
* This is the View class in the MCV model
*/
public class View {
private JButton buttons[][];
private JPanel panel;
private JPanel control_panel;
private JButton white_forfeit;
private JButton black_forfeit;
private JButton white_draw;
private JButton black_draw;
private JButton score;
/**
* This is the default constructor that takes in the model
* @param game this is the model to be displayed
* This is the default constructor that takes in the controller
* @param controller this is the controller of the view
* @throws IOException
*/
View(Game game) throws IOException {
View(Controller controller) throws IOException {
Game game = controller.my_game;
JFrame f= new JFrame("Chess");
JPanel panel=new JPanel();
panel=new JPanel();
panel.setLayout(new GridLayout(8, 8, 0, 0));
panel.setBounds(0,0,500,500);
panel.setBackground(Color.gray);
buttons = new JButton[8][8];
for(int i=0; i<8; i++) {
for (int j = 0; j<8; j++) {
JButton button = new JButton("");
......@@ -94,11 +105,121 @@ public class View {
button.setIcon(new ImageIcon(newimage));
}
panel.add(button);
buttons[i][j] = button;
}
}
control_panel=new JPanel();
control_panel.setLayout(new GridLayout(1, 5, 0, 0));
control_panel.setBounds(0,500,500,50);
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);
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(black_draw);
control_panel.add(black_forfeit);
f.add(panel);
f.add(control_panel);
f.setSize(600,600);
f.setLayout(null);
f.setVisible(true);
}
public void update_buttons(Game game, JPanel panel) throws IOException {
for(int i=0; i<8; i++) {
for (int j = 0; j<8; j++) {
JButton button = this.buttons[i][j];
boolean occupied = game.board[i][j].occupied;
String cur_piece = game.board[i][j].name;
boolean white = game.board[i][j].white;
if(occupied) {
Image image;
if (cur_piece == "P" && !white) {
image = ImageIO.read(getClass().getResource("b_pawn.png"));
}
else if (cur_piece == "R" && !white) {
image = ImageIO.read(getClass().getResource("b_rook.png"));
}
else if (cur_piece == "N" && !white) {
image = ImageIO.read(getClass().getResource("b_knight.png"));
}
else if (cur_piece == "B" && !white) {
image = ImageIO.read(getClass().getResource("b_bishop.png"));
}
else if (cur_piece == "Q" && !white) {
image = ImageIO.read(getClass().getResource("b_queen.png"));
}
else if (cur_piece == "K" && !white) {
image = ImageIO.read(getClass().getResource("b_king.png"));
}
else if (cur_piece == "A" && !white) {
image = ImageIO.read(getClass().getResource("b_spy.png"));
}
else if (cur_piece == "Z" && !white) {
image = ImageIO.read(getClass().getResource("b_joker.png"));
}
else if (cur_piece == "P" && white) {
image = ImageIO.read(getClass().getResource("w_pawn.png"));
}
else if (cur_piece == "R" && white) {
image = ImageIO.read(getClass().getResource("w_rook.png"));
}
else if (cur_piece == "N" && white) {
image = ImageIO.read(getClass().getResource("w_knight.png"));
}
else if (cur_piece == "B" && white) {
image = ImageIO.read(getClass().getResource("w_bishop.png"));
}
else if (cur_piece == "Q" && white) {
image = ImageIO.read(getClass().getResource("w_queen.png"));
}
else if (cur_piece == "A" && white) {
image = ImageIO.read(getClass().getResource("w_spy.png"));
}
else if (cur_piece == "Z" && white) {
image = ImageIO.read(getClass().getResource("w_joker.png"));
}
else {
image = ImageIO.read(getClass().getResource("w_king.png"));
}
Image newimage = image.getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH);
button.setIcon(new ImageIcon(newimage));
}
else{
button.setIcon(new ImageIcon());
}
}
}
}
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 white_forfeits(ActionListener a){
white_forfeit.addActionListener(a);
}
public void black_forfeits(ActionListener a){
black_forfeit.addActionListener(a);
}
public void square_move(ActionListener a){
for(int i=0; i<8; i++) {
for (int j = 0; j<8; j++) {
buttons[i][j].addActionListener(a);
}
}
}
}
package com.company;
import static org.junit.Assert.*;
public class ViewTest {
}
\ No newline at end of file
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