導航:首頁 > 編程語言 > java五子棋簡單

java五子棋簡單

發布時間:2022-07-18 12:15:22

『壹』 初學java寫的簡單五子棋代碼,大神幫看看哪兒錯了,用windows可以運行,但是邊界會出錯。

是的,數組下標越界。

『貳』 用JAVA設計游戲:五子棋游戲

下面的源代碼分為4個文件;
chessClient.java:客戶端主程序。
chessInterface.java:客戶端的界面。
chessPad.java:棋盤的繪制。
chessServer.java:伺服器端。
可同時容納50個人同時在線下棋,聊天。
沒有加上詳細注釋,不過絕對可以運行,j2sdk1.4下通過。

/*********************************************************************************************
1.chessClient.java
**********************************************************************************************/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

class clientThread extends Thread
{
chessClient chessclient;

clientThread(chessClient chessclient)
{
this.chessclient=chessclient;
}

public void acceptMessage(String recMessage)
{
if(recMessage.startsWith("/userlist "))
{
StringTokenizer userToken=new StringTokenizer(recMessage," ");
int userNumber=0;

chessclient.userpad.userList.removeAll();
chessclient.inputpad.userChoice.removeAll();
chessclient.inputpad.userChoice.addItem("所有人");
while(userToken.hasMoreTokens())
{
String user=(String)userToken.nextToken(" ");
if(userNumber>0 && !user.startsWith("[inchess]"))
{
chessclient.userpad.userList.add(user);
chessclient.inputpad.userChoice.addItem(user);
}

userNumber++;
}
chessclient.inputpad.userChoice.select("所有人");
}
else if(recMessage.startsWith("/yourname "))
{
chessclient.chessClientName=recMessage.substring(10);
chessclient.setTitle("Java五子棋客戶端 "+"用戶名:"+chessclient.chessClientName);
}
else if(recMessage.equals("/reject"))
{
try
{
chessclient.chesspad.statusText.setText("不能加入游戲");
chessclient.controlpad.cancelGameButton.setEnabled(false);
chessclient.controlpad.joinGameButton.setEnabled(true);
chessclient.controlpad.creatGameButton.setEnabled(true);
}
catch(Exception ef)
{
chessclient.chatpad.chatLineArea.setText("chessclient.chesspad.chessSocket.close無法關閉");
}
chessclient.controlpad.joinGameButton.setEnabled(true);
}
else if(recMessage.startsWith("/peer "))
{
chessclient.chesspad.chessPeerName=recMessage.substring(6);
if(chessclient.isServer)
{
chessclient.chesspad.chessColor=1;
chessclient.chesspad.isMouseEnabled=true;
chessclient.chesspad.statusText.setText("請黑棋下子");
}
else if(chessclient.isClient)
{
chessclient.chesspad.chessColor=-1;
chessclient.chesspad.statusText.setText("已加入游戲,等待對方下子...");
}

}
else if(recMessage.equals("/youwin"))
{
chessclient.isOnChess=false;
chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);
chessclient.chesspad.statusText.setText("對方退出,請點放棄游戲退出連接");
chessclient.chesspad.isMouseEnabled=false;
}
else if(recMessage.equals("/OK"))
{
chessclient.chesspad.statusText.setText("創建游戲成功,等待別人加入...");
}
else if(recMessage.equals("/error"))
{
chessclient.chatpad.chatLineArea.append("傳輸錯誤:請退出程序,重新加入 \n");
}
else
{
chessclient.chatpad.chatLineArea.append(recMessage+"\n");
chessclient.chatpad.chatLineArea.setCaretPosition(
chessclient.chatpad.chatLineArea.getText().length());
}
}

public void run()
{
String message="";
try
{
while(true)
{
message=chessclient.in.readUTF();
acceptMessage(message);
}
}
catch(IOException es)
{
}
}

}

public class chessClient extends Frame implements ActionListener,KeyListener
{
userPad userpad=new userPad();
chatPad chatpad=new chatPad();
controlPad controlpad=new controlPad();
chessPad chesspad=new chessPad();
inputPad inputpad=new inputPad();

Socket chatSocket;
DataInputStream in;
DataOutputStream out;
String chessClientName=null;
String host=null;
int port=4331;

boolean isOnChat=false; //在聊天?
boolean isOnChess=false; //在下棋?
boolean isGameConnected=false; //下棋的客戶端連接?
boolean isServer=false; //如果是下棋的主機
boolean isClient=false; //如果是下棋的客戶端

Panel southPanel=new Panel();
Panel northPanel=new Panel();
Panel centerPanel=new Panel();
Panel westPanel=new Panel();
Panel eastPanel=new Panel();

chessClient()
{
super("Java五子棋客戶端");
setLayout(new BorderLayout());
host=controlpad.inputIP.getText();

westPanel.setLayout(new BorderLayout());
westPanel.add(userpad,BorderLayout.NORTH);
westPanel.add(chatpad,BorderLayout.CENTER);
westPanel.setBackground(Color.pink);

inputpad.inputWords.addKeyListener(this);
chesspad.host=controlpad.inputIP.getText();

centerPanel.add(chesspad,BorderLayout.CENTER);
centerPanel.add(inputpad,BorderLayout.SOUTH);
centerPanel.setBackground(Color.pink);

controlpad.connectButton.addActionListener(this);
controlpad.creatGameButton.addActionListener(this);
controlpad.joinGameButton.addActionListener(this);
controlpad.cancelGameButton.addActionListener(this);
controlpad.exitGameButton.addActionListener(this);

controlpad.creatGameButton.setEnabled(false);
controlpad.joinGameButton.setEnabled(false);
controlpad.cancelGameButton.setEnabled(false);

southPanel.add(controlpad,BorderLayout.CENTER);
southPanel.setBackground(Color.pink);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if(isOnChat)
{
try
{
chatSocket.close();
}
catch(Exception ed)
{
}
}
if(isOnChess || isGameConnected)
{
try
{
chesspad.chessSocket.close();
}
catch(Exception ee)
{
}
}
System.exit(0);
}
public void windowActivated(WindowEvent ea)
{

}
});

add(westPanel,BorderLayout.WEST);
add(centerPanel,BorderLayout.CENTER);
add(southPanel,BorderLayout.SOUTH);

pack();
setSize(670,548);
setVisible(true);
setResizable(false);
validate();
}

public boolean connectServer(String serverIP,int serverPort) throws Exception
{
try
{
chatSocket=new Socket(serverIP,serverPort);
in=new DataInputStream(chatSocket.getInputStream());
out=new DataOutputStream(chatSocket.getOutputStream());

clientThread clientthread=new clientThread(this);
clientthread.start();
isOnChat=true;
return true;
}
catch(IOException ex)
{
chatpad.chatLineArea.setText("chessClient:connectServer:無法連接,建議重新啟動程序 \n");
}
return false;
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==controlpad.connectButton)
{
host=chesspad.host=controlpad.inputIP.getText();
try
{
if(connectServer(host,port))
{
chatpad.chatLineArea.setText("");
controlpad.connectButton.setEnabled(false);
controlpad.creatGameButton.setEnabled(true);
controlpad.joinGameButton.setEnabled(true);
chesspad.statusText.setText("連接成功,請創建游戲或加入游戲");
}

}
catch(Exception ei)
{
chatpad.chatLineArea.setText("controlpad.connectButton:無法連接,建議重新啟動程序 \n");
}
}
if(e.getSource()==controlpad.exitGameButton)
{
if(isOnChat)
{
try
{
chatSocket.close();
}
catch(Exception ed)
{
}
}
if(isOnChess || isGameConnected)
{
try
{
chesspad.chessSocket.close();
}
catch(Exception ee)
{
}
}
System.exit(0);

}
if(e.getSource()==controlpad.joinGameButton)
{
String selectedUser=userpad.userList.getSelectedItem();
if(selectedUser==null || selectedUser.startsWith("[inchess]") ||
selectedUser.equals(chessClientName))
{
chesspad.statusText.setText("必須先選定一個有效用戶");
}
else
{
try
{
if(!isGameConnected)
{
if(chesspad.connectServer(chesspad.host,chesspad.port))
{
isGameConnected=true;
isOnChess=true;
isClient=true;
controlpad.creatGameButton.setEnabled(false);
controlpad.joinGameButton.setEnabled(false);
controlpad.cancelGameButton.setEnabled(true);
chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName);
}
}
else
{
isOnChess=true;
isClient=true;
controlpad.creatGameButton.setEnabled(false);
controlpad.joinGameButton.setEnabled(false);
controlpad.cancelGameButton.setEnabled(true);
chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName);
}

}
catch(Exception ee)
{
isGameConnected=false;
isOnChess=false;
isClient=false;
controlpad.creatGameButton.setEnabled(true);
controlpad.joinGameButton.setEnabled(true);
controlpad.cancelGameButton.setEnabled(false);
chatpad.chatLineArea.setText("chesspad.connectServer無法連接 \n"+ee);
}

}
}
if(e.getSource()==controlpad.creatGameButton)
{
try
{
if(!isGameConnected)
{
if(chesspad.connectServer(chesspad.host,chesspad.port))
{
isGameConnected=true;
isOnChess=true;
isServer=true;
controlpad.creatGameButton.setEnabled(false);
controlpad.joinGameButton.setEnabled(false);
controlpad.cancelGameButton.setEnabled(true);
chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName);
}
}
else
{
isOnChess=true;
isServer=true;
controlpad.creatGameButton.setEnabled(false);
controlpad.joinGameButton.setEnabled(false);
controlpad.cancelGameButton.setEnabled(true);
chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName);
}
}
catch(Exception ec)
{
isGameConnected=false;
isOnChess=false;
isServer=false;
controlpad.creatGameButton.setEnabled(true);
controlpad.joinGameButton.setEnabled(true);
controlpad.cancelGameButton.setEnabled(false);
ec.printStackTrace();
chatpad.chatLineArea.setText("chesspad.connectServer無法連接 \n"+ec);
}

}
if(e.getSource()==controlpad.cancelGameButton)
{
if(isOnChess)
{
chesspad.chessthread.sendMessage("/giveup "+chessClientName);
chesspad.chessVictory(-1*chesspad.chessColor);
controlpad.creatGameButton.setEnabled(true);
controlpad.joinGameButton.setEnabled(true);
controlpad.cancelGameButton.setEnabled(false);
chesspad.statusText.setText("請建立游戲或者加入游戲");
}
if(!isOnChess)
{
controlpad.creatGameButton.setEnabled(true);
controlpad.joinGameButton.setEnabled(true);
controlpad.cancelGameButton.setEnabled(false);
chesspad.statusText.setText("請建立游戲或者加入游戲");
}
isClient=isServer=false;
}

}

public void keyPressed(KeyEvent e)
{
TextField inputWords=(TextField)e.getSource();

if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
if(inputpad.userChoice.getSelectedItem().equals("所有人"))
{
try
{
out.writeUTF(inputWords.getText());
inputWords.setText("");
}
catch(Exception ea)
{
chatpad.chatLineArea.setText("chessClient:KeyPressed無法連接,建議重新連接 \n");
userpad.userList.removeAll();
inputpad.userChoice.removeAll();
inputWords.setText("");
controlpad.connectButton.setEnabled(true);
}
}
else
{
try
{
out.writeUTF("/"+inputpad.userChoice.getSelectedItem()+" "+inputWords.getText());
inputWords.setText("");
}
catch(Exception ea)
{
chatpad.chatLineArea.setText("chessClient:KeyPressed無法連接,建議重新連接 \n");
userpad.userList.removeAll();
inputpad.userChoice.removeAll();
inputWords.setText("");
controlpad.connectButton.setEnabled(true);
}
}
}

}

public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}

public static void main(String args[])
{
chessClient chessClient=new chessClient();
}
}

/******************************************************************************************
下面是:chessInteface.java
******************************************************************************************/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

class userPad extends Panel
{
List userList=new List(10);

userPad()
{
setLayout(new BorderLayout());

for(int i=0;i<50;i++)
{
userList.add(i+"."+"沒有用戶");
}
add(userList,BorderLayout.CENTER);

}

}

class chatPad extends Panel
{
TextArea chatLineArea=new TextArea("",18,30,TextArea.SCROLLBARS_VERTICAL_ONLY);

chatPad()
{
setLayout(new BorderLayout());

add(chatLineArea,BorderLayout.CENTER);
}

}

class controlPad extends Panel
{
Label IPlabel=new Label("IP",Label.LEFT);
TextField inputIP=new TextField("localhost",10);
Button connectButton=new Button("連接主機");
Button creatGameButton=new Button("建立游戲");
Button joinGameButton=new Button("加入游戲");
Button cancelGameButton=new Button("放棄游戲");
Button exitGameButton=new Button("關閉程序");

controlPad()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
setBackground(Color.pink);

add(IPlabel);
add(inputIP);
add(connectButton);
add(creatGameButton);
add(joinGameButton);
add(cancelGameButton);
add(exitGameButton);
}

}

class inputPad extends Panel
{
TextField inputWords=new TextField("",40);
Choice userChoice=new Choice();

inputPad()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
for(int i=0;i<50;i++)
{
userChoice.addItem(i+"."+"沒有用戶");
}
userChoice.setSize(60,24);
add(userChoice);
add(inputWords);
}
}

/**********************************************************************************************
下面是:chessPad.java
**********************************************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

class chessThread extends Thread
{
chessPad chesspad;

chessThread(chessPad chesspad)
{
this.chesspad=chesspad;
}

public void sendMessage(String sndMessage)
{
try
{
chesspad.outData.writeUTF(sndMessage);
}
catch(Exception ea)
{
System.out.println("chessThread.sendMessage:"+ea);
}
}

public void acceptMessage(String recMessage)
{
if(recMessage.startsWith("/chess "))
{
StringTokenizer userToken=new StringTokenizer(recMessage," ");
String chessToken;
String[] chessOpt={"-1","-1","0"};
int chessOptNum=0;

while(userToken.hasMoreTokens())
{
chessToken=(String)userToken.nextToken(" ");
if(chessOptNum>=1 && chessOptNum<=3)
{
chessOpt[chessOptNum-1]=chessToken;

}
chessOptNum++;
}
chesspad.netChessPaint(Integer.parseInt(chessOpt[0]),Integer.parseInt(chessOpt[1]),Integer.parseInt(chessOpt[2]));

}
else if(recMessage.startsWith("/yourname "))
{
chesspad.chessSelfName=recMessage.substring(10);
}
else if(recMessage.equals("/error"))
{
chesspad.statusText.setText("錯誤:沒有這個用戶,請退出程序,重新加入");
}
else
{
//System.out.println(recMessage);
}
}

public void run()
{
String message="";
try
{
while(true)
{
message=chesspad.inData.readUTF();
acceptMessage(message);
}
}
catch(IOException es)
{
}
}

}

class chessPad extends Panel implements MouseListener,ActionListener
{
int chessPoint_x=-1,chessPoint_y=-1,chessColor=1;
int chessBlack_x[]=new int[200];
int chessBlack_y[]=new int[200];
int chessWhite_x[]=new int[200];
int chessWhite_y[]=new int[200];
int chessBlackCount=0,chessWhiteCount=0;
int chessBlackWin=0,chessWhiteWin=0;
boolean isMouseEnabled=false,isWin=false,isInGame=false;
TextField statusText=new TextField("請先連接伺服器");

Socket chessSocket;
DataInputStream inData;
DataOutputStream outData;

String chessSelfName=null;
String chessPeerName=null;
String host=null;
int port=4331;
chessThread chessthread=new chessThread(this);

chessPad()
{
setSize(440,440);
setLayout(null);
setBackground(Color.pink);
addMouseListener(this);
add(statusText);
statusText.setBounds(40,5,360,24);
statusText.setEditable(false);
}

public boolean connectServer(String ServerIP,int ServerPort) throws Exception
{
try
{
chessSocket=new Socket(ServerIP,ServerPort);
inData=new DataInputStream(chessSocket.getInputStream());
outData=new DataOutputStream(chessSocket.getOutputStream());
chessthread.start();
return true;
}
catch(IOException ex)
{
statusText.setText("chessPad:connectServer:無法連接 \n");
}
return false;
}

public void chessVictory(int chessColorWin)
{
this.removeAll();
for(int i=0;i<=chessBlackCount;i++)
{
chessBlack_x[i]=0;
chessBlack_y[i]=0;
}
for(int i=0;i<=chessWhiteCount;i++)
{
chessWhite_x[i]=0;
chessWhite_y[i]=0;
}
chessBlackCount=0;
chessWhiteCount=0;
add(statusText);
statusText.setBounds(40,5,360,24);

if(chessColorWin==1)
{ chessBlackWin++;
statusText.setText("黑棋勝,黑:白為"+chessBlackWin+":"+chessWhiteWin+",重新開局,等待白棋下子...");
}
else if(chessColorWin==-1)
{
chessWhiteWin++;
statusText.setText("白棋勝,黑:白為"+chessBlackWin+":"+chessWhiteWin+",重新開局,等待黑棋下子...");
}
}

public void getLocation(int a,int b,int color)
{

if(color==1)
{
chessBlack_x[chessBlackCount]=a*20;
chessBlack_y[chessBlackCount]=b*20;
chessBlackCount++;
}
else if(color==-1)
{
chessWhite_x[chessWhiteCount]=a*20;
chessWhite_y[chessWhiteCount]=b*20;
chessWhiteCount++;
}
}

public boolean checkWin(int a,int b,int checkColor)
{
int step=1,chessLink=1,chessLinkTest=1,chessCompare=0;
if(checkColor==1)
{
chessLink=1;
for(step=1;step<=4;step++)
{
for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)
{
if(((a+step)*20==chessBlack_x[chessCompare]) && ((b*20)==chessBlack_y[chessCompare]))
{
chessLink=chessLink+1;
if(chessLink==5)
{
return(true);
}
}
}
if(chessLink==(chessLinkTest+1))
chessLinkTest++;
else
break;
}
for(step=1;step<=4;step++)
{
for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)
{
if(((a-step)*20==chessBlack_x[chessCompare]) && (b*20==chessBlack_y[chessCompare]))
{
chessLink++;
if(chessLink==5)
{
return(true);
}
}
}
if(chessLink==(chessLinkTest+1))
chessLinkTest++;
else
break;
}
chessLink=1;
chessLinkTest=1;
for(step=1;step<=4;step++)
{
for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)
{
if((a*20==chessBlack_x[chessCompare]) && ((b+step)*20==chessBlack_y[chessCompare]))
{

『叄』 五子棋的Java演算法

五子棋的演算法是比較簡單的。

把棋盤當作一個 2 維數組。 用2維數組來當作棋盤的坐標系

當落子 之後。 把落子,插入到 數組中 獲得 棋盤 的數組, 循環剛才數組, 判斷,

剛才 數組元素 的 橫向坐標 -5 到剛才 數組元素坐標 + 5 是否都是 一個數字(黑子代表 1 ,白子代表0) 只要其中 有連續 5個 都是 黑子,或者白子, 則黑子或白子 贏了。

判斷,剛才元素 縱向坐標 -5 到 + 5 如上判斷。

判斷 右斜線。 判斷 橫向坐標 -5 y -5 到 橫向坐標 + 5 y + 5

判斷 y + 5 x + 5 到 y-5 x -5

簡單來說。

用2維數組 來代表 棋盤 , 每次在 界面上, 由白子,或黑子 落子之後, 在數組相應坐標,放入 1 或者0 。

然後循環數組判斷, 數組橫向 豎向 右斜線 左斜線 是否是 黑子或者白子 連續的。 如果是,則獲勝。

『肆』 最近用JAVA寫了一個簡單的五子棋,滑鼠點擊問題

repaint();

試試

『伍』 用簡單的java語言編寫五子棋

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

class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();

if(x < 25 || x > 330 + 25 ||y < 25 || y > 330+25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i <= 330;i += 30)
{
for(int j = 30;j <= 330; j+= 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330);
}
}

for(int j = 30;j <= 330;j += 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j);
}

}
void Drawchess(Graphics g)
{
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;

for(i = 0;i < 11;i++)//橫向判斷
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}

for(i = 0;i < 11;i++)//豎向判斷
{
for(int j = 0;j < 11;j++)
{
if(chess[j][i] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}

for(i = 0;i < 7;i++)//左向右斜判斷
{
for(int j = 0;j < 7;j++)
{
for(int k = 0;k < 5;k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i + k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}

for(i = 4;i < 11;i++)//右向左斜判斷
{
for(int j = 6;j >= 0;j--)
{
for(int k = 0;k < 5;k++)
{
if(chess[i - k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i - k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}

}
void Clear_Chess()
{
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}

}

class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panel.setBounds(0,23, 360, 360);
setTitle("單人版五子棋");
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);

}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe();
}
}

『陸』 求一個最簡單的JAVA五子棋程序。。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

;

public class 下棋 extends JFrame {
int x = 0, y = 0;

int a = 0;

int x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, x4 = 0, y4 = 0;;

boolean fa = true, huatu = true, yin = false;

int a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0, a9 = 0;

int s[] = new int[10], jj[] = new int[10];

JButton congxin, huanse;

JLabel jl;

public 下棋() {
Container c = getContentPane();
c.setLayout(new FlowLayout());
congxin = new JButton("重新開始紅隊先下");
c.add(congxin);
huanse = new JButton("重新開始藍隊先下");
c.add(huanse);
jl = new JLabel();
c.add(jl, BorderLayout.SOUTH);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
if (event.isAltDown()) {
repaint();
}
x = event.getPoint().x;
y = event.getPoint().y;
int a = mm(x, y);
if (yin) {
jl.setForeground(Color.RED);
jl
.setText(" 游戲已結束,請重新開始 ");

}

else {
if (s[a] == 1) {
jl.setForeground(Color.RED);
jl.setText(" 此位置有棋子,請另選位置重新下棋 ");
} else {
fa = false;
if (a == 1)
a1 = 1;
if (a == 2)
a2 = 1;
if (a == 3)
a3 = 1;
if (a == 4)
a4 = 1;
if (a == 5)
a5 = 1;
if (a == 6)
a6 = 1;
if (a == 7)
a7 = 1;
if (a == 8)
a8 = 1;
if (a == 9)
a9 = 1;
jl.setText("");
repaint();
}
}
s[a] = 1;
}
});

congxin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
for (int i = 0; i < s.length; i++) {
s[i] = 0;
jj[i] = 0;
}
fa = true;
repaint();
yin = false;
huatu = true;
jl.setForeground(Color.black);
jl
.setText(" 紅隊先下 ");
}
});

huanse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
for (int i = 0; i < s.length; i++) {
s[i] = 0;
jj[i] = 0;
}
fa = true;
repaint();
yin = false;
huatu = false;
jl.setForeground(Color.black);
jl
.setText(" 藍隊先下 ");
}
});

setSize(500, 500);
setVisible(true);
}

public void paint(Graphics g) {
if (fa) {
super.paint(g);
int x = 100, y = 100, c = 300, k = 300;
for (int i = 0; i < 300; i += 100)
g.drawRect(x, y + i, c, k - i);
for (int i = 0; i < 300; i += 100)
g.drawRect(x + i, y, c - i, k);
} else {
jl.setForeground(Color.black);
if (huatu) {
g.setColor(Color.red);
if (a1 == 1) {
g.drawLine(120, 120, 180, 180);
g.drawLine(180, 120, 120, 180);
a1 = 0;
jj[1] = 1;
}
if (a2 == 1) {
g.drawLine(220, 120, 280, 180);
g.drawLine(280, 120, 220, 180);
a2 = 0;
jj[2] = 1;
}

if (a3 == 1) {
g.drawLine(320, 120, 380, 180);
g.drawLine(380, 120, 320, 180);
a3 = 0;
jj[3] = 1;
}
if (a4 == 1) {
g.drawLine(120, 220, 180, 280);
g.drawLine(180, 220, 120, 280);
a4 = 0;
jj[4] = 1;
}
if (a5 == 1) {
g.drawLine(220, 220, 280, 280);
g.drawLine(280, 220, 220, 280);
a5 = 0;
jj[5] = 1;
}
if (a6 == 1) {
g.drawLine(320, 220, 380, 280);
g.drawLine(380, 220, 320, 280);
a6 = 0;
jj[6] = 1;
}
if (a7 == 1) {
g.drawLine(120, 320, 180, 380);
g.drawLine(180, 320, 120, 380);
a7 = 0;
jj[7] = 1;
}
if (a8 == 1) {
g.drawLine(220, 320, 280, 380);
g.drawLine(280, 320, 220, 380);
a8 = 0;
jj[8] = 1;
}
if (a9 == 1) {
g.drawLine(320, 320, 380, 380);
g.drawLine(380, 320, 320, 380);
a9 = 0;
jj[9] = 1;
}
huatu = false;
jl
.setText(" 藍隊下棋 ");
} else {
g.setColor(Color.blue);
if (a1 == 1) {
g.drawOval(125, 125, 50, 50);
a1 = 0;
jj[1] = 2;
}
if (a2 == 1) {
g.drawOval(225, 125, 50, 50);
a2 = 0;
jj[2] = 2;
}
if (a3 == 1) {
g.drawOval(325, 125, 50, 50);
a3 = 0;
jj[3] = 2;
}
if (a4 == 1) {
g.drawOval(125, 225, 50, 50);
a4 = 0;
jj[4] = 2;
}
if (a5 == 1) {
g.drawOval(225, 225, 50, 50);
a5 = 0;
jj[5] = 2;
}
if (a6 == 1) {
g.drawOval(325, 225, 50, 50);
a6 = 0;
jj[6] = 2;
}
if (a7 == 1) {
g.drawOval(125, 325, 50, 50);
a7 = 0;
jj[7] = 2;
}
if (a8 == 1) {
g.drawOval(225, 325, 50, 50);
a8 = 0;
jj[8] = 2;
}
if (a9 == 1) {
g.drawOval(325, 325, 50, 50);
a9 = 0;
jj[9] = 2;
}
huatu = true;
jl
.setText(" 紅隊下棋 ");
}
dd();
}
}

public int mm(int x, int y) {
if (x >= 100 && x < 200 && y >= 100 && y < 200)
a = 1;
else if (x >= 200 && x < 300 && y >= 100 && y < 200)
a = 2;
else if (x >= 300 && x < 400 && y >= 100 && y < 200)
a = 3;
else if (x >= 100 && x < 200 && y >= 200 && y < 300)
a = 4;
else if (x >= 200 && x < 300 && y >= 200 && y < 300)
a = 5;
else if (x >= 300 && x < 400 && y >= 200 && y < 300)
a = 6;
else if (x >= 100 && x < 200 && y >= 300 && y < 400)
a = 7;
else if (x >= 200 && x < 300 && y >= 300 && y < 400)
a = 8;
else if (x >= 300 && x < 400 && y >= 300 && y < 400)
a = 9;
else
a = 0;
return a;
}

public void dd() {

if (jj[1] == 1 && jj[2] == 1 && jj[3] == 1) {
jl.setForeground(Color.RED);
jl
.setText(" 紅方獲勝,連接線1,2,3 ");
yin = true;
}
if (jj[4] == 1 && jj[5] == 1 && jj[6] == 1) {
jl.setForeground(Color.RED);
jl
.setText(" 紅方獲勝,連接線4,5,6 ");
yin = true;
}
if (jj[7] == 1 && jj[8] == 1 && jj[9] == 1) {
jl.setForeground(Color.RED);
jl
.setText(" 紅方獲勝,連接線7,8,9 ");
yin = true;
}
if (jj[1] == 1 && jj[5] == 1 && jj[9] == 1) {
jl.setForeground(Color.RED);
jl
.setText(" 紅方獲獲勝,連接線1,5,9 ");
yin = true;
}
if (jj[3] == 1 && jj[5] == 1 && jj[7] == 1) {
jl.setForeground(Color.RED);
jl
.setText(" 紅方獲勝,連接線3,5,7 ");
yin = true;
}
if (jj[1] == 1 && jj[4] == 1 && jj[7] == 1) {
jl.setForeground(Color.RED);
jl
.setText(" 紅方獲勝,連接線1,4,7 ");
yin = true;
}
if (jj[2] == 1 && jj[5] == 1 && jj[8] == 1) {
jl.setForeground(Color.RED);
jl
.setText(" 紅方獲勝,連接線2,5,8 ");
yin = true;
}
if (jj[3] == 1 && jj[6] == 1 && jj[9] == 1) {
jl.setForeground(Color.RED);
jl
.setText(" 紅方獲勝,連接線3,6,9 ");
yin = true;
}

if (jj[1] == 2 && jj[2] == 2 && jj[3] == 2) {
jl.setForeground(Color.RED);
jl
.setText(" 藍方獲勝,連接線1,2,3 ");
yin = true;
}
if (jj[4] == 2 && jj[5] == 2 && jj[6] == 2) {
jl.setForeground(Color.RED);
jl
.setText(" 藍方獲勝,連接線4,5,6 ");
yin = true;
}
if (jj[7] == 2 && jj[8] == 2 && jj[9] == 2) {
jl.setForeground(Color.RED);
jl
.setText(" 藍方獲勝,連接線7,8,9 ");
yin = true;
}
if (jj[1] == 2 && jj[5] == 2 && jj[9] == 2) {
jl.setForeground(Color.RED);
jl
.setText(" 藍方獲勝,連接線1,5,9 ");
yin = true;
}
if (jj[3] == 2 && jj[5] == 2 && jj[7] == 2) {
jl.setForeground(Color.RED);
jl
.setText(" 藍方獲勝,連接線3,5,7 ");
yin = true;
}
if (jj[1] == 2 && jj[4] == 2 && jj[7] == 2) {
jl.setForeground(Color.RED);
jl.setText("藍方獲勝,連接線1,4,7 ");
yin = true;
}
if (jj[2] == 2 && jj[5] == 2 && jj[8] == 2) {
jl.setForeground(Color.RED);
jl
.setText(" 藍方獲勝,連接線2,5,8 ");
yin = true;
}
if (jj[3] == 2 && jj[6] == 2 && jj[9] == 2) {
jl.setForeground(Color.RED);
jl
.setText(" 藍方獲勝,連接線3,6,9 ");
yin = true;
}
if (s[1] == 1 && s[2] == 1 && s[3] == 1 && s[4] == 1 && s[5] == 1
&& s[6] == 1 && s[7] == 1 && s[8] == 1 && s[9] == 1) {
jl.setForeground(Color.RED);
jl
.setText(" 雙方都沒獲勝,游戲結束 ");
yin = true;
}
System.out.println(jj[2]);
}

public static void main(String[] args) {
下棋 s = new 下棋();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

『柒』 java五子棋問題

你好:
我給你講下思路吧。你可以用0表示沒有 棋子,1代表黑棋,2代表白棋。然後"int [][] allChess = new int[19][19]; ",這個二維數組剛好表示空棋盤。你每下一個白棋,把該位置的值改為2,黑棋改為1。輸贏判斷,橫向,如果allChess[x][y]的顏色跟allChess[x+i][y],i從1循環4,往左就減i。滿足一個count++,count初始為一,因為從你點擊的那個子開始算,至少有一個子連成一條線。關鍵來了,左上--右下方向 , 判斷allChess[x][y]==allChess[x+i][y+i]?count++:count。

『捌』 JAVA單機版五子棋怎麼寫

界面思路:
用按鈕數組模擬棋盤。

改變按鈕的背景圖片標志這個棋盤的格子上是黑棋、白棋、空。同時使用一個二維數組記錄棋盤棋子的分布,比如qipan[0][0]=1標示第1行第一列的棋子是黑棋子,乙烯類推。

循環檢測是否某行、某列、某斜線上是否已經有五個顏色相同的棋子。

簡單的演算法就是判斷某行、某列、某斜線的妻子數目那個最多。通過2個for循環遍歷棋盤。

復雜的演算法,你看看下面的參考資料,不過下面這段材料如果看不懂的話,用簡單的演算法實現了五子棋也是很好的了,^_^

五子棋演算法(AI)

任何一種棋類游戲其關鍵是對當前棋局是否有正確的評分,評分越准確則電腦的AI越高。五子棋游戲也是如此,但在打分之前,我們先掃描
整個棋盤,把每個空位從八個方向上的棋型填入數組gStyle(2, 15, 15, 8, 2),其中第一個下標為1時表示黑棋,為2時表示白棋,第二和第三
個下標表示(x,y),第四個下標表示8個方向,最後一個下標為1時表示棋子數,為2時表示空格數,如:

gStyle(1,2,2,1,1)=3表示與坐標(2,2)在第1個方向上相鄰的黑棋棋子數為3
gstyle(1,2,2,1,2)=4表示與坐標(2,2)在第1個方向上的最近的空格數為4
在定義方向時,也應該注意一定的技巧,表示兩個相反的方向的數應該差4,在程序中我是這樣定義的:
Const DIR_UP = 1
Const DIR_UPRIGHT = 2
Const DIR_RIGHT = 3
Const DIR_RIGHTDOWN = 4
Const DIR_DOWN = 5
Const DIR_DOWNLEFT = 6
Const DIR_LEFT = 7
Const DIR_LEFTUP = 8
這樣我們前四個方向可以通過加四得到另一個方向的值。如果你還是不太明白,請看下面的圖:
---------
---------
---oo----
-ox*xx---
---------
---------
圖中的*點從標為(4,4),(打*的位置是空位),則:
gStyle(2,4,4,1,1)=1在(4,4)點相鄰的上方白棋數為1
gStyle(2,4,4,1,2)=2在(4,4)點的上方距上方白棋最近的空格數為2
gStyle(1,4,4,3,1)=2在(4,4)點相鄰的右方黑棋數為2
gStyle(1,4,4,3,2)=1在(4,4)點的右方距右方黑棋最近的空格數為3
...

一旦把所有空點的棋型值填完,我們很容易地得出黑棋水平方向上點(4,4)的價值,由一個沖1(我把有界的棋稱為沖)和活2(兩邊無界的
棋稱為活)組成的。對於而白棋在垂直方向上點(4,4)的價值是一個活1,而在/方向也是活1所以,只要我們把該點的對於黑棋和白棋的價值算出
來,然後我們就取棋盤上各個空點的這兩個值的和的最大一點作為下棋的點。然而,對各種棋型應該取什麼值呢?我們可以先作如下假設:
Fn 表示先手n個棋子的活棋型,如:F4表示先手活四
Fn'表示先手n個棋子的沖棋型,如:F4'表示先手沖四
Ln 表示後手n個棋子的活棋型,如:L3表示後手活三
Ln'表示後手n個棋子的沖棋型,如:L3'表示後手沖三
.
.
.
根據在一行中的棋型分析,得到如下關系:
L1'<=F1'<L2'<=F2'<=L1<F1<L2<F2<L3'<=F3'<L4'<F4'=F4
從這個關系包含了進攻和防守的關系(當然,這個關系是由我定的,你可以自己定義這些關系)。對這些關系再進一步細化,如在一個可下
棋的點,其四個方向上都有活三,也比不上一個沖四,所以我們可以又得到4*F3<L4'這個關系,同樣,我們還可以得到其它的關系,如:4*F2<L3、4*L3<F3...,這些的關系由於你的定法和我的定法制可能不一樣,這樣計算機的AI也就不一樣,最後我們把分值最小的L1'值定為1,則我們就得
到了下面各種棋型的分值,由C語言表示為:
F[2][5]={{0,2,5,50,16000},{0,10,30,750,16000}};
L[2][5]={{0,1,5,50,3750},{0,10,30,150,4000}};
F數組表示先手,第一個下標為0時表示沖型,第二個下標表示棋子數,則F2'對應F[0][2]L數組表示後手,第一個下標為0時表示沖型,第二
個下標表示棋子數,則L2對應F[1][2]Ok,棋型的分值關系確定好了以後,我們把每一個可下點的四個方向的棋型值相加(包括先手和後手的分
值),最後選擇一個最大值,並把這一點作為計算機要下的點就OK了:)。

『玖』 求一個簡單的JAVA五子棋代碼!! 網上復制的別來了!

我有一個,剛剛做的。可以實現人機對戰,人人對戰,悔棋,禁手等操作。機器方主要採用的是a-b剪枝演算法。功能很強大,代碼很多。

閱讀全文

與java五子棋簡單相關的資料

熱點內容
app易語言post怎麼學 瀏覽:963
地梁的箍筋加密區位置 瀏覽:300
二分法排序程序及編譯結果 瀏覽:677
日語命令形和禁止型 瀏覽:283
安裝軟體用管理員解壓 瀏覽:503
編譯原理代碼塊 瀏覽:398
小孩可以用壓縮面膜嗎 瀏覽:12
錐形倒角怎麼計演算法 瀏覽:880
java合並鏈表 瀏覽:505
pic單片機編譯器 瀏覽:803
麗水四軸加工中心編程 瀏覽:689
國產系統怎麼解壓 瀏覽:552
戰雙程序員 瀏覽:483
him觸摸編程軟體 瀏覽:931
植物大戰僵屍存檔怎麼轉移安卓 瀏覽:852
java棧的元素 瀏覽:737
程序員與籃球事件 瀏覽:676
app反編譯不完整 瀏覽:788
電腦上的文件夾怎麼調整 瀏覽:8
伺服器無響應是什麼原因呀 瀏覽:985