[英]Can someone please tell me why my winning conditions aren't working?
我正在使游戲連接四個,但是每當我運行該程序時,它都會掉圈並且除獲勝條件之外的所有內容均不起作用。 我不太確定我是否正確地給他們打電話,或者只是沒有在正確的位置打電話給他們。 請指教。 感謝您即將收到的答復。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ConnectFourJan {
static boolean winCheck;
static drawBoard drawConnectFourBoard = new drawBoard();
static int [][] spotOnBoard = new int [6][7];
static int maxRow=6;
static int maxCol=7;
static boolean endOfGame = false;
static boolean gameStart;
static JPanel boardPanel;
static JButton firstOption = new JButton ("Drop");
static JButton secondOption = new JButton ("Drop");
static JButton thirdOption = new JButton ("Drop");
static JButton fourthOption = new JButton ("Drop");
static JButton fifthOption = new JButton ("Drop");
static JButton sixthOption = new JButton ("Drop");
static JButton seventhOption = new JButton ("Drop");
static JButton playAgain = new JButton ("New");
static JButton reset = new JButton ("Reset");
static JButton exit = new JButton ("Exit");
static int blank =0;
static int red=1;
static int yellow=2;
static int firstColour= red;
public static void board() {
spotOnBoard = new int [maxRow][maxCol];
for (int row=0; row < maxRow; row++){
for (int col=0; col< maxCol; col++){
spotOnBoard[row][col]=blank;
}
}
}
public static class drawBoard extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
drawConnectFourBoard(g);
}//end of paintComponent.
void drawConnectFourBoard(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, 100+100*maxCol, 100+100*maxRow);
for (int row=0; row<maxRow; row++)
for (int col=0; col<maxCol; col++) {
if (spotOnBoard[row][col]==blank) g.setColor(Color.white);
if (spotOnBoard[row][col]==red) g.setColor(Color.red);
if (spotOnBoard[row][col]==yellow) g.setColor(Color.yellow);
g.fillOval(100*col, 100*row, 100, 100);
}
}
}
public static boolean winCheck(int colour) {
int box;
if (colour==red)
box = red;
else
box = yellow;
for(int j = 0; j < 6; j++) {
for (int k = 0; k < 7; k++) {
if(spotOnBoard[j][k]==(box)) {
if(((j -3) >= 0) && spotOnBoard[j -1][k]==(box) && spotOnBoard[j -2][k]==(box) && spotOnBoard[j -3][k]==(box)) {
return true;
}
else if(((k -3) >= 0) && spotOnBoard[j][k -1]==(box) && spotOnBoard[j][k -2]==(box) && spotOnBoard[j][k -3]==(box)) {
return true;
}
else if(((j+3)<= 5) && spotOnBoard[j+1][k]==(box) && spotOnBoard[j+2][k]==(box) && spotOnBoard[j+3][k]==(box)){
return true;
}
else if(((k +3) <= 6) && spotOnBoard[j][k +1]==(box) && spotOnBoard[j][k +2]==(box) && spotOnBoard[j][k +3]==(box)) {
return true;
}
else if(((j -3)>= 0) && ((k +3)<=6) && spotOnBoard[j-1][k+1]==(box) && spotOnBoard[j-2][k+2]==(box) && spotOnBoard[j-3][k+3]==(box)){
return true;
}
else if(((j +3) <=5) && ((k -3) >=0) && spotOnBoard[j +1][k -1]==(box) && spotOnBoard[j +2][k -2]==(box) && spotOnBoard[j +3][k -3]==(box)){
return true;
}
else if(((j -3)>=0) && ((k -3)>= 0) && spotOnBoard[j -1][k -1]==(box) && spotOnBoard[j -2][k -2]==(box) && spotOnBoard[j -3][k -3]==(box)){
return true;
}
else if(((j +3) <=5) && ((k +3) <=6) && spotOnBoard[j +1][k +1]==(box) && spotOnBoard[j +2][k +2]==(box) && spotOnBoard[j +3][k +3]==(box)){
return true;
}
}
}
}
return false;
}
public static void displayWinner(int n) {
if (n==red) {
JOptionPane.showMessageDialog(null, "red wins! Congratulations!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
}
else
JOptionPane.showMessageDialog(null, "Yellow wins! Congratulations!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
}
public static void main (String [] args) {
board();
ButtonHandler listen = new ButtonHandler();
firstOption.addActionListener(listen);
secondOption.addActionListener(listen);
thirdOption.addActionListener(listen);
fourthOption.addActionListener(listen);
fifthOption.addActionListener(listen);
sixthOption.addActionListener(listen);
seventhOption.addActionListener(listen);
playAgain.addActionListener(listen);
reset.addActionListener(listen);
exit.addActionListener(listen);
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1,7));
topPanel.setBackground(new Color(0,0,0));
topPanel.add(firstOption);
topPanel.add(secondOption);
topPanel.add(thirdOption);
topPanel.add(fourthOption);
topPanel.add(fifthOption);
topPanel.add(sixthOption);
topPanel.add(seventhOption);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1,4));
bottomPanel.add(playAgain);
bottomPanel.add(reset);
bottomPanel.add(exit);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(new Color(0,214,154));
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(drawConnectFourBoard);
board();
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
JFrame window = new JFrame ("Connect4");
window.setContentPane(mainPanel);
window.setSize(720,700);
window.setLocation(500,100);
window.setResizable(true);
window.setVisible(true);
}
private static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent a) {
if (a.getSource()== firstOption)
dropCircle(0);
else if (a.getSource()== secondOption)
dropCircle(1);
else if (a.getSource()== thirdOption)
dropCircle(2);
else if (a.getSource()== fourthOption)
dropCircle(3);
else if (a.getSource()== fifthOption)
dropCircle(4);
else if (a.getSource()== sixthOption)
dropCircle(5);
else if (a.getSource()==seventhOption)
dropCircle(6);
if (a.getSource() == playAgain) {
gameStart=true;
}
if (a.getSource() == exit) {
System.exit(0);
}
if (a.getSource() == reset) {
for (int row=0; row < maxRow; row++){
for (int col=0; col< maxCol; col++){
spotOnBoard[row][col]=blank;
}
}
drawConnectFourBoard.repaint();
}
}
public void dropCircle(int n) {
if (endOfGame) return;
gameStart=true;
int row;
for (row=0; row<maxRow; row++)
if (spotOnBoard[row][n]>0) break;
if (row>0) {
if(winCheck(firstColour)){
displayWinner(firstColour);
}
spotOnBoard[--row][n]=firstColour;
if (firstColour==red)
firstColour=yellow;
else
firstColour=red;
}
drawConnectFourBoard.repaint();
}
}
}
您的問題是在dropCircle
,您正在填充boardArray
,但是在winCheck
,當您檢查勝利時,您正在尋找的是spotOnBoard
,這是一個完全不同的數組。 也就是說,您要在一個數組中設置值,但要在其他地方查找它們。 這永遠不會起作用。
除了其他人所說的,您的for循環似乎還有一個錯誤:
for(int j = 0; j < 6; j++) {
for(int k = 0; k < 7; k++) {
if(/* condition */) {
return true;
} else if(/* condition */) {
return true;
} else if(/* condition */) {
return true;
} else { // bug
return false; // here
}
}
}
return false;
這只會檢查第一次迭代,因此您需要刪除else。
似乎您也沒有檢查winCheck的結果:
winCheck(firstColour);
displayWinner(firstColour);
我認為您的意思是這樣的:
if(winCheck(firstColour)) {
displayWinner(firstColour);
}
當您對程序進行修訂時,答案可能會改變,但是您似乎缺少了如何使用為游戲定義的數組的核心概念。
字符串,整數,布爾值,雙精度數等。無論您決定使用哪種對象/基元對游戲進行建模,都應在整個程序中始終使用一致。 (即:應該使用.equals()來比較字符串,正如已經多次提到的那樣),並且您提供的數據結構也應起到有意義的作用(即:不要分配新的數據結構以在一個地方使用)如果您可以在兩個地方合法地共享相同的數據,則在另一個地方分配相似目的的另一個)。
到現在為止,您的dropCircle方法似乎有點問題。
public void dropCircle(int n) {
if (endOfGame) return;
gameStart=true;
int row;
for (row=0; row<maxRow; row++)
if (spotOnBoard[row][n]>0) break;
if (row>0) {
if(winCheck(firstColour)){ // checking for a win here will result in
// false because the winning position has
// yet to be assigned.
displayWinner(firstColour);
}
spotOnBoard[--row][n] = firstcolour; // move this line above the if statement above.
if (firstColour==red)
firstColour=yellow;
else
firstColour=red;
}
drawConnectFourBoard.repaint();
}
要注意的另一點是您始終使用字符串和整數表示板狀態。 可以想象,您可以使用相同的數據類型來實現整個游戲,並且可以消除大量的邏輯錯誤,這些錯誤很可能是由於對數據類型的處理不當所致。
我可能會建議的一件事是,重命名並合並一些數據結構。 spotOnBoard表示板上有一個空格,而實際上,此陣列就是您的板上。 板上的點是[row,col]處的元素
其次,我將在Javadocs中查看“對象相等”。 特別是String等於。 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)
祝好運
編輯:修改以反映OP更新和建議采取的措施。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.