Ⅰ 用java實現五子棋悔棋代碼
具體嘛.我覺得可以讓一個線程來做這個東西,單獨用一個線程式控制制!
Ⅱ 用eclipse編寫一個基於java的五子棋(與電腦對弈)
基於java的五子棋:
主要功能代碼如下:
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
{
=1L;
JButton[][]point=newJButton[40][40];
int[][]state=newint[40][40];
int[][]value=newint[40][40];
int[]place=newint[2];
publicMain(){
this.setTitle("五子棋");
this.setBounds(100,10,1000,1000);
this.setLayout(newGridLayout(30,30));
inti,j;
for(i=0;i<=39;i++){
for(j=0;j<=39;j++){
state[i][j]=0;
value[i][j]=0;
}
}
for(i=5;i<=34;i++){
for(j=5;j<=34;j++){
point[i][j]=newJButton("");
this.add(point[i][j]);
point[i][j].addActionListener(this);
}
}
this.setVisible(true);
}
publicvoidactionPerformed(ActionEvente)
{
inti,j;
for(i=5;i<=34;i++){
for(j=5;j<=34;j++){
if(e.getSource()==point[i][j]){
point[i][j].setBackground(Color.RED);
state[i][j]=1;
point[i][j].setEnabled(false);
value[i][j]=0;
}
}
}
publicstaticvoidmain(String[]args)throwsNullPointerException{
newMain();
}
elseif(((state[i][j]==0)&&(state[i][j+1]==1)&&(state[i][j+2]==0))||
((state[i][j]==0)&&(state[i][j-1]==1)&&(state[i][j-2]==0))||
((state[i][j]==0)&&(state[i+1][j]==1)&&(state[i+2][j]==0))||
((state[i][j]==0)&&(state[i-1][j]==1)&&(state[i-2][j]==0))||//空紅空
((state[i][j]==0)&&(state[i+1][j+1]==1)&&(state[i+2][j+2]==0))||
((state[i][j]==0)&&(state[i+1][j-1]==1)&&(state[i+2][j-2]==0))||
((state[i][j]==0)&&(state[i-1][j+1]==1)&&(state[i-2][j+2]==0))||
((state[i][j]==0)&&(state[i-1][j-1]==1)&&(state[i-2][j-2]==0))||
((state[i][j]==0)&&(state[i][j+1]==1)&&(state[i][j+2]==1)&&(state[i][j+3]==2))||
((state[i][j]==0)&&(state[i][j-1]==1)&&(state[i][j-2]==1)&&(state[i][j-3]==2))||
((state[i][j]==0)&&(state[i+1][j]==1)&&(state[i+2][j]==1)&&(state[i+3][j]==2))||
((state[i][j]==0)&&(state[i-1][j]==1)&&(state[i-2][j]==1)&&(state[i-3][j]==2))||//空紅紅藍型
((state[i][j]==0)&&(state[i+1][j+1]==1)&&(state[i+2][j+2]==1)&&(state[i+3][j+3]==2))||
((state[i][j]==0)&&(state[i+1][j-1]==1)&&(state[i+2][j-2]==1)&&(state[i+3][j-3]==2))||
((state[i][j]==0)&&(state[i-1][j+1]==1)&&(state[i-2][j+2]==1)&&(state[i-3][j+3]==2))||
((state[i][j]==0)&&(state[i-1][j-1]==1)&&(state[i-2][j-2]==1)&&(state[i-3][j-3]==2))){
value[i][j]=2;
returnvalue[i][j];
}
elseif(((state[i][j]==0)&&(state[i][j+1]==2)&&(state[i][j+2]==0))||
((state[i][j]==0)&&(state[i][j-1]==2)&&(state[i][j-2]==0))||
((state[i][j]==0)&&(state[i+1][j]==2)&&(state[i+2][j]==0))||
((state[i][j]==0)&&(state[i-1][j]==2)&&(state[i-2][j]==0))||
((state[i][j]==0)&&(state[i+1][j+1]==2)&&(state[i+2][j+2]==0))||
((state[i][j]==0)&&(state[i+1][j-1]==2)&&(state[i+2][j-2]==0))||
((state[i][j]==0)&&(state[i-1][j+1]==2)&&(state[i-2][j+2]==0))||
((state[i][j]==0)&&(state[i-1][j-1]==2)&&(state[i-2][j-2]==0))||
((state[i][j]==0)&&(state[i][j+1]==2)&&(state[i][j+2]==2)&&(state[i][j+3]==1))||
((state[i][j]==0)&&(state[i][j-1]==2)&&(state[i][j-2]==2)&&(state[i][j-3]==1))||
((state[i][j]==0)&&(state[i+1][j]==2)&&(state[i+2][j]==2)&&(state[i+3][j]==1))||
((state[i][j]==0)&&(state[i-1][j]==2)&&(state[i-2][j]==2)&&(state[i-3][j]==1))||
((state[i][j]==0)&&(state[i+1][j+1]==2)&&(state[i+2][j+2]==2)&&(state[i+3][j+3]==1))||
((state[i][j]==0)&&(state[i+1][j-1]==2)&&(state[i+2][j-2]==2)&&(state[i+3][j-3]==1))||
((state[i][j]==0)&&(state[i-1][j+1]==2)&&(state[i-2][j+2]==2)&&(state[i-3][j+3]==1))||
((state[i][j]==0)&&(state[i-1][j-1]==2)&&(state[i-2][j-2]==2)&&(state[i-3][j-3]==1))){
value[i][j]=1;
returnvalue[i][j];
}
else{
value[i][j]=0;
returnvalue[i][j];
}
}
publicint[]largestValue(int[][]value){
intmax,i,j,l,k;
l=0;k=0;
max=0;
for(i=5;i<34;i++){
for(j=5;j<34;j++){
if(max<value[i][j]){
max=value[i][j];
place[0]=i;
place[1]=j;
}
}
}
returnplace;
}
}
運行結果如下圖所示:
Ⅲ 做五子棋JAVA所需要的知識點
不知道你是想做web 瀏覽器界面 還是用java swing界面。
界面繪制
落子後的演算法,要計算當前落子坐標上下左右包括兩個斜對角線上是否有連續的五個相同顏色的棋子,來判斷是否贏棋。
落子後界面變化的同步,也就是說 A玩家落子後,B玩家要可以看到棋盤上的變更。
落子坐標控制, 已落子的坐標無法再次落子等
以上基本上就可以完成游戲的主功能。
其他的考慮:
游戲房間創建
旗子顏色選擇
悔棋功能
Ⅳ 五子棋的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語言編寫五子棋
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編寫的五子棋代碼,要有電腦AI的
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]))
{
chessLink++;
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*20==chessBlack_x[chessCompare]) && ((b-step)*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-step)*20==chessBlack_x[chessCompare]) && ((b+step)*20==chessBlack_y[chessCompare]))
{
chessLink++;
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-step)*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+step)*20==chessBlack_x[chessCompare]) && ((b+step)*20==chessBlack_y[chessCompare]))
{
chessLink++;
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-step)*20==chessBlack_y[chessCompare]))
{
chessLink++;
if(chessLink==5)
{
return(true);
}
}
Ⅶ java游戲五子棋
http://hi..com/caesarsword/blog/item/d3e08044af7a01056b63e5c4.html
Ⅷ java五子棋源代碼
我這有演算法 不過沒做swing界面 DOS下可以直接運行 要不你拿去改改
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
* 五子棋源碼
* 所用的符號標識 ○ ● ┼
* 在dos界面下運行效果最佳
* 黑白雙方交叉輸入落子點坐標 以逗號隔開如 1,1
* 輸入空 或者一方勝出 程序停止
*/
public class Chess {
// 定義棋盤大小
private static int SIZE = 15;
private String[][] board;
public static void main(String[] args) throws Exception {
Chess chess = new Chess();
// 初始化棋盤
chess.initBoard();
// 畫出棋盤
chess.paintBoard();
// 根據who的奇偶性 判斷該誰落子
int who = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while ((str = br.readLine()) != null) {
// 提取輸入的 以","分開的數 分別對應x y坐標
String[] posStr = str.split(",");
int x = Integer.parseInt(posStr[0]);
int y = Integer.parseInt(posStr[1]);
// 判斷落子點是否合法
if (!"┼".equals(chess.board[x][y])) {
System.out.println("這里不允許落子,請重下..");
continue;
}
if (who % 2 == 0) {
chess.board[x][y] = "○";
chess.paintBoard();
// 判斷是否勝出
if (chess.isWin("○")) {
System.out.println("○獲勝");
return;
}
} else {
chess.board[x][y] = "●";
chess.paintBoard();
// 判斷是否勝出
if (chess.isWin("●")) {
System.out.println("●獲勝");
return;
}
}
who++;
}
}
// 以 "┼" 初始化棋盤
public void initBoard() {
board = new String[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = "┼";
}
}
}
// 描繪出當前棋盤
public void paintBoard() {
// 以下代碼 這里為了使得棋盤坐標看的清楚 加入了坐標值
System.out.print(" ");
for (int i = 0; i < SIZE; i++) {
if (i < 10) {
System.out.print(i + " ");
} else {
System.out.print((i - 10) + " ");
}
}
System.out.println();
// 以上代碼 這里為了使得棋盤坐標看的清楚 加入了坐標值
for (int i = 0; i < SIZE; i++) {
if (i < 10) {
System.out.print(" " + i);
} else {
System.out.print(i);
}
for (int j = 0; j < SIZE; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
// 判斷是否獲勝
public boolean isWin(String sign) {
int count = 0;
// 橫向掃描各行
// 有一個sign的子 計數器+1
// 碰到不是sign的子 計數器置零
// 計數器到達5時 返回true 勝出
for (int i = 0; i < SIZE; i++) {
count = 0;
for (int j = 0; j < SIZE; j++) {
if (board[i][j].equals(sign)) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
// 縱向掃描各列
// 方法同上
for (int i = 0; i < SIZE; i++) {
count = 0;
for (int j = 0; j < SIZE; j++) {
if (board[j][i].equals(sign)) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
// 掃描斜右下
// 在橫向掃描基礎上 外層套一個循環 以k為標識
// 坐標x-y的范圍在-SIZE+1到SIZE-1之間
// 當x-y的值相等時 在同一右下斜線上
for (int k = -SIZE + 1; k <= SIZE - 1; k++) {
count = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (i - j == k) {
if (board[i][j].equals(sign)) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
}
}
// 掃描斜左邊上
// 方法同上 坐標x+y的值相等時 在同一左上斜線上
for (int k = -SIZE + 1; k <= SIZE - 1; k++) {
count = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (i + j == k) {
if (board[i][j].equals(sign)) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
}
}
return false;
}
}
Ⅸ 五子棋棋盤java實現
其實我也有用JAVA做五子棋呢~,棋盤都是用畫的,我把代碼發下,你自己試下,也不定合你一意.事件代碼我都去啦,因為是簡單的麻煩事.~!
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class ChessBoard extends JPanel{
/*
* 製作棋盤的寬高;
*/
public static final int BOARD_WIDTH=515;
/*
* 計算棋盤表格坐標(單元格寬高相等)
*/
public static int [] location=new int[22];
static{
for(int i=0,WIDTH=30;i<location.length;i++,WIDTH+=22){
location[i]=WIDTH;
}
}
public ChessBoard(int x,int y){
super(null);
this.setBounds(x, y, BOARD_WIDTH, BOARD_WIDTH);
this.setBackground(new Color(255, 164, 85));
}
/**
* 重寫方法,繪制棋盤表格圖;
*/
public void paintComponent(Graphics g){
super.paintComponent(g);
char ch='A';
g.setFont(new Font("宋體",Font.BOLD,12));
//畫橫線
for(int i=0,width=30+22*21;i<location.length;i++,ch++){
g.setColor(Color.black);
g.drawLine(30,location[i],width,location[i]);
g.setColor(Color.blue);
g.drawString(""+ch,5,location[i]+3);
}
//畫豎線
for(int i=0,width=30+22*21;i<location.length;i++){
g.setColor(Color.black);
g.drawLine(location[i],30,location[i],width);
g.setColor(Color.blue);
g.drawString(""+(i+1),location[i]-3,13);
}
}
}