我已经制作了连接四的图形版本,但是当我运行该程序时会出现很多错误,有人可以告诉我我的代码有什么问题吗?
现在这段代码没有读任何东西,我不知道为什么。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class four extends JPanel implements ActionListener,KeyListener {
private static final long serialVersionUID = 1L;
static int x=0, y=0;
static int cx=0, cy=0;
static JFrame f;
static char [][]a = new char[7][7];
static int ccolumn=0;
static int bottom=6;
int p;
boolean Player1=false;
boolean Player2=false;
boolean Draw=false;
boolean Gamefinished=false;
public void init (){
this.addKeyListener(this);
setFocusable(true);
for(int c = 0;c<7;c++){ //Initialize for connect four to "0"
for(int d = 0;d<7;d++){
a[c][d] = '0';
}
}
p = 1; //switch the chips
bottom = 6; // "bottom" is the empty slot above an chip
}
public void paintComponent (Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
g.drawRect(x,100,700,100);
g.drawRect(x,200,700,100);
g.drawRect(x,300,700,100);
g.drawRect(x,400,700,100);
g.drawRect(x,500,700,100);
g.drawRect(x,600,700,100);
g.drawRect(x,700,700,100);
g.drawRect(100,100,100,700);
g.drawRect(200,100,0,700);
g.drawRect(300,100,0,700);
g.drawRect(400,100,0,700);
g.drawRect(500,100,0,700);
g.drawRect(600,100,0,700);
g.setColor(Color.black);
g.drawString("1",50,50);
g.setColor(Color.black);
g.drawString("2",150,50);
g.setColor(Color.black);
g.drawString("3",250,50);
g.setColor(Color.black);
g.drawString("4",350,50);
g.setColor(Color.black);
g.drawString("5",450,50);
g.setColor(Color.black);
g.drawString("6",550,50);
g.setColor(Color.black);
g.drawString("7",650,50);
if (Player1==true) {
g.setColor(Color.RED);
g.fillOval(cx,cy,50,50);
}
if (Player2==true) {
g.setColor(Color.YELLOW);
g.fillOval(cx,cy,50,50);
}
if (Gamefinished==true){
g.setColor(Color.black);
g.fillRect(1,1,1000,2000);
g.setColor(Color.white);
if (Player1)
g.drawString("red player win!",190,300);
if (Player2)
g.drawString("yellow player win!",190,300);
if (Draw==true)
g.drawString("Draw!",190,300);
}
}
public void actionPerformed(ActionEvent e) {
if(Player1==true){ //p is switch the chips
bottom=6;
for (int b=0 ; b<7; b++){ //calculate bottom for red
if (a[b][ccolumn]!='0'){
bottom=b-1;
// if(bottom<0) {
// Gamefinished = true;
// Draw = true;
// Player1 = false;
// Player2 = false;
// }
// break;
}
}
a[bottom][ccolumn] = 'R';
}
if (Player2==true) {
// System.out.println("Drop a yellow disk at column(0-7): ");
// ecol = input.nextInt();
bottom=6;
for (int b=0 ; b<7; b++){ //calculate bottom for yellow
if (a[b][ccolumn]!='0'){
bottom=b-1;
// if(bottom<0) {
// Gamefinished = true;
// Draw = true;
// Player1 = false;
// Player2 = false;
// }
}
}
a[bottom][ccolumn] = 'Y';
}
cy=bottom*100+50;
cx=ccolumn*100+150;
//if (isfour()==true) {
// Gamefiniched = true;
// Draw = false;
// }
f.repaint();
}
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
if (c == KeyEvent.VK_1){
ccolumn=0;
p++;
}
if (c == KeyEvent.VK_2){
ccolumn=1;
p++;
}
if (c == KeyEvent.VK_3){
ccolumn=2;
p++;
}
if (c == KeyEvent.VK_4){
ccolumn=3;
p++;
}
if (c == KeyEvent.VK_5){
ccolumn=4;
p++;
}
if (c == KeyEvent.VK_6){
ccolumn=5;
p++;
}
if (c == KeyEvent.VK_7){
ccolumn=6;
p++;
}
if(p%2==0){ //p is switch the chips
Player1 = true;
Player2 = false;
}
else {
Player2 = true;
Player1 = false;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args){
four tt = new four();
JFrame f = new JFrame();
tt.init();
f.setTitle("hi");
f.setSize(800,700);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(tt);
Timer t = new Timer(100,tt);
t.start();
}
public static boolean isfour(){
int c = 0;
int d=0;
int k, u;
//System.out.println("row"+row+" column"+column);
k=bottom;
u=ccolumn;
//Check for right diagonal (4 elements)
if((k<=7-4) && (u<=7-4)) {// Check if it is out of bound
if ((a[k][u]==a[k+1][u+1]) && (a[k][u]==a[k+2][u+2]) && (a[k][u]==a[k+3][u+3]) &&(a[k][u]!='0'))
return true;
}
//Check for left diagonal (4 elements)
if ((k<=7-4)&&(u>7-4)) {
if ((a[k][u]==a[k+1][u-1]) && (a[k][u]==a[k+2][u-2]) && (a[k][u]==a[k+3][u-3]) &&(a[k][u]!='0'))
return true;
}
//Check for vertical (4 elements)
if (k<=7-4) {
if ((a[k][u]==a[k+1][u]) && (a[k][u]==a[k+2][u]) && (a[k][u]==a[k+3][u]) &&(a[k][u]!='0'))
return true;
}
//Check for horizontal
for (c=0;c<6; c++){
if ((a[k][c]==a[k][c+1]) &&(a[k][c]!='0')) {
d++;
if (d==3) return true;
}
else d=0;
}
return false; // no 4 connections
}
}