我說下我的想法:
10年積累的網(wǎng)站設(shè)計、網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有會澤免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
看坐標(biāo): 橫坐標(biāo)一致
或縱坐標(biāo)一致
還有就是/.\兩種情況下 固定的位置你事先固定好
當(dāng)三個點都有了棋子就一直線
以前寫過一個java的井字棋 ,
其中的重點是要判斷每走一步后,是否有比賽的結(jié)果(輸,贏,平)
可以使用swing 來作為外觀進(jìn)行顯示.
表示棋盤如下
0 1 2
3 4 5
6 7 8
定義一個二維數(shù)組,每次走完后,匹配該數(shù)組, 如果匹配成功就贏了
int[][] WIN = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 },
{ 2, 4, 6 } };
效果圖
當(dāng)然了,因為井字棋比較簡單, 可以寫一個比較簡單的判斷局勢,然后自動下棋的AI .
(AI使用了很多的if else判斷, 比如人現(xiàn)在的情況是什么樣的,有幾個棋子連在一起了,電腦自己的情況是怎么樣的)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JApplet
{
private JTextField rows=new JTextField("3");
private JTextField cols=new JTextField("3");
private static final int BLANK=0,XX=1,OO=2;
class ToeDialog extends JDialog
{
private int turn=XX;
ToeDialog(int cellsWide,int cellsHigh)
{
setTitle("The game itself");
Container cp=getContentPane();
cp.setLayout(new GridLayout(cellsWide,cellsHigh));
for(int i=0;icellsWide*cellsHigh;i++)
cp.add(new ToeButton());
setSize(cellsWide*50,cellsHigh*50);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
class ToeButton extends JPanel
{
private int state=BLANK;
public ToeButton()
{
addMouseListener(new ML());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int x1=0,y1=0;
int x2=getSize().width-1;
int y2=getSize().height-1;
g.drawRect(x1,y1,x2,y2);
x1=x2/4;
y1=y2/4;
int wide=x2/2,high=y2/2;
if(state==XX)
{
g.drawLine(x1,y1,x1+wide,y1+high);
g.drawLine(x1,y1+high,x1+wide,y1);
}
if(state==OO)
g.drawOval(x1,y1,x1+wide/2,y1+high/2);
}
class ML extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if(state==BLANK)
{
state=turn;
turn=(turn==XX?OO:XX);
}
else
state=(state==XX?OO:XX);
repaint();
}
}
}
}
class BL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JDialog d=new ToeDialog(Integer.parseInt(rows.getText()),Integer.parseInt(cols.getText()));
d.setVisible(true);
}
}
public void init()
{
JPanel p=new JPanel();
p.setLayout(new GridLayout(2,2));
p.add(new JLabel("Rows",JLabel.CENTER));
p.add(rows);
p.add(new JLabel("Columns",JLabel.CENTER));
p.add(cols);
Container cp=getContentPane();
cp.add(p,BorderLayout.NORTH);
JButton b=new JButton("go");
b.addActionListener(new BL());
cp.add(b,BorderLayout.SOUTH);
}
public static void main(String[] args)
{
TicTacToe test=new TicTacToe();
JFrame frame=new JFrame("TicTacToe");
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.getContentPane().add(test);
frame.setSize(100,100);
test.init();
test.start();
frame.setVisible(true);
}
}
新聞標(biāo)題:井字棋java代碼實現(xiàn),井字棋java程序設(shè)計
當(dāng)前網(wǎng)址:http://chinadenli.net/article26/hecpjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、外貿(mào)網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)、小程序開發(fā)、域名注冊、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)