trabajo dos

8
Trabajo Numero 2: Movimiento del Caballo en el Ajedrez: Cuando al Ingresar la posición del caballo en un tablero de ajedrez este programa nos mostrara sus posibles movimientos, como también la posibilidad de comer las fichas de otros jugadores. Codigo: FRAME: import javax.swing.*; public class Frame extends JFrame { // Metodo constructor Frame(JPanel panel){ super("Movimientos del Caballo en el Tablero de Ajedrez"); this.setContentPane(panel); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setBounds(0,0,800,800); this.setVisible(true); } } MAIN: public class Main { public static void main(String[] args) { Panel panel = new Panel(); Frame frame = new Frame(panel); } } PANEL: import java.awt.Color; import java.awt.event.*;

Upload: paromarye1

Post on 11-Dec-2015

213 views

Category:

Documents


0 download

DESCRIPTION

Trabajo 2

TRANSCRIPT

Page 1: Trabajo Dos

Trabajo Numero 2:Movimiento del Caballo en el Ajedrez:Cuando al Ingresar la posición del caballo en un tablero de ajedrez este programa nos mostrara sus posibles movimientos, como también la posibilidad de comer las fichas de otros jugadores.

Codigo:

FRAME:

import javax.swing.*;

public class Frame extends JFrame {

// Metodo constructorFrame(JPanel panel){

super("Movimientos del Caballo en el Tablero de Ajedrez");this.setContentPane(panel);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setBounds(0,0,800,800);this.setVisible(true);

}

}

MAIN:

public class Main {

public static void main(String[] args) {Panel panel = new Panel();Frame frame = new Frame(panel);

}}

PANEL:

import java.awt.Color;import java.awt.event.*;import javax.swing.*;

public class Panel extends JPanel implements ActionListener {

JButton[][] btn = new JButton[8][8]; int longitud = 75; ImageIcon caballoBlanco = new ImageIcon("C:/Users/lucki/Documents/NetBeansProjects/CCIII-Trabajo02/src/caballo.png");

Page 2: Trabajo Dos

ImageIcon caballoNegro = new ImageIcon("C:/Users/lucki/Documents/NetBeansProjects/CCIII-Trabajo02/src/caballoNegro.png"); ImageIcon negro = new ImageIcon("C:/Users/lucki/Documents/NetBeansProjects/CCIII-Trabajo02/src/negro.png"); ImageIcon blanco = new ImageIcon("C:/Users/lucki/Documents/NetBeansProjects/CCIII-Trabajo02/src/blanco.png"); ImageIcon tentativos = new ImageIcon("C:/Users/lucki/Documents/NetBeansProjects/CCIII-Trabajo02/src/MIDDLE.gif"); int[][] ubicaciones = new int[8][8]; int[][] temporal = new int[8][8]; int[] sw = new int[10];

Panel() { sw[0] = 0; sw[1] = 1; sw[2] = 2; for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { ubicaciones[x][y] = 0; temporal[x][y] = 0; } }

ubicaciones[1][0] = 3; ubicaciones[6][0] = 4; ubicaciones[0][0] = 1; ubicaciones[6][7] = 2;

for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { btn[x][y] = new JButton(""); btn[x][y].addActionListener(this); btn[x][y].setBounds(x * longitud, y * longitud, longitud, longitud); this.setLayout(null); this.add(btn[x][y]); if (x % 2 == 0) { if (y % 2 == 0) { btn[x][y].setBackground(new Color(255, 255, 255)); btn[x][y].setForeground(new Color(0, 0, 0)); } else { btn[x][y].setBackground(new Color(0, 0, 0)); btn[x][y].setForeground(new Color(255, 255, 255)); } } else if (y % 2 != 0) { btn[x][y].setBackground(new Color(255, 255, 255)); btn[x][y].setForeground(new Color(0, 0, 0));

Page 3: Trabajo Dos

} else { btn[x][y].setBackground(new Color(0, 0, 0)); btn[x][y].setForeground(new Color(255, 255, 255)); } } } limpiar(); ubicarCaballos(); }

boolean validar(int x, int y) { if (ubicaciones[x][y] == 0) { JOptionPane.showMessageDialog(this, "Movimiento Incorrecto"); return false; } else { if (sw[0] == 0) { if (ubicaciones[x][y] == 3 || ubicaciones[x][y] == 4) { JOptionPane.showMessageDialog(this, "Color Incorrecto"); return false; } } if (sw[0] == 1) { if (ubicaciones[x][y] == 1 || ubicaciones[x][y] == 2) { JOptionPane.showMessageDialog(this, "Color Incorrecto"); } return false; } } return true; }

void botonPulsado(int x, int y) { sw[7] = ubicaciones[x][y]; sw[8] = x; sw[9] = y; if (validar(x, y)) { for (int i = Math.max(x-2, 0); i <= Math.min(x + 2,7); i++) { for (int j = Math.max(y-2, 0); j <= Math.min(y + 2,7); j++) { //if ( i <= 7 && i >= 0 && j <= 7 && j >= 0 ) {

if ( Math.pow(x - i,2) + Math.pow(y - j,2) == 5 ) { if( ubicaciones[i][j]!=1&&ubicaciones[i][j]!=2){ btn[i][j].setIcon(tentativos); temporal[i][j] = 1; } // } }

Page 4: Trabajo Dos

} }

} }

void limpiar() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { temporal[i][j] = 0; btn[i][j].setText(""); if (i % 2 == 0) { if (j % 2 == 0) { btn[i][j].setIcon(blanco); } else { btn[i][j].setIcon(negro); } } else if (j % 2 != 0) { btn[i][j].setIcon(blanco); } else { btn[i][j].setIcon(negro); } } } } void ubicarCaballos() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { switch (ubicaciones[i][j]) { case 1: btn[i][j].setIcon(caballoBlanco); break; case 2: btn[i][j].setIcon(caballoBlanco); break; case 3: btn[i][j].setIcon(caballoNegro); break; case 4: btn[i][j].setIcon(caballoNegro); break; } } } } public void actionPerformed(ActionEvent e) { boolean swt = false ; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if(temporal[i][j]==1)

Page 5: Trabajo Dos

swt = true; } } if (!swt) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (e.getSource() == btn[i][j]) botonPulsado(i, j); } } } if (swt) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (e.getSource() == btn[i][j]){ botonPulsado2(i, j); swt = false; } } } } }private void botonPulsado2(int x, int y) {

if (ubicaciones[x][y] == 2 || ubicaciones[x][y] == 1) { JOptionPane.showMessageDialog(this, "Error"); } else { for (int i = 0; i < btn.length; i++) { for (int j = 0; j < btn.length; j++) { if (temporal[x][y] == 1) { ubicaciones[x][y] = sw[7]; ubicaciones[sw[8]][sw[9]] = 0; } } } limpiar(); ubicarCaballos(); } }}

Page 6: Trabajo Dos

RESULTADOS: