~~~~~100分求一个非智能的两人对战的简单五子棋程序~~~~~~?想做一个这样的程序,不过只是想做,动手起来问题太多,谁有这样的程序吗?或者帮做一下,让我这样的新手感觉一下编程的乐趣!

解决方案 »

  1.   

    写的比较简单,在eclipse3.1,jdk5.0下调试通过:/*
     * Created on 2005-10-6
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    /**
     * @author yanhai
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */class Grid // 格子类 
    {
    private int x,y;//x,y坐标
    private Polygon po ;
    static public final int he = 40;//格子大小
    public boolean flag = false;//是否刷新的标志
    private int had ;
    public Grid(int x,int y,int had)
    {
    this.x = x;
    this.y = y;
    this.had = had;
    po = new Polygon();
    po.addPoint(x,y);
    po.addPoint(x+he,y);
    po.addPoint(x,y +he);
    po.addPoint(x + he,y + he);
    }
    public int getX()
    {
    return x;
    }
    public int getY()
    {
    return y;
    }

    public Polygon getPo()
    {
    return po;
    }
    public int getHad()
    {
    return had;
    }
    public void setHad(int had)
    {
    this.had = had;
    }
    }class MainFrame extends JFrame implements MouseListener
    {
    Grid [][] pan = new Grid[9][9];//9x9的盘
    boolean alt = false;//标志轮流
    int startX = 20,startY = 60;//起始点20,20
    ArrayList al = new ArrayList();
    Grid last;
    public MainFrame()
    {
    for(int i =0; i < 9;i ++)
    for(int j = 0;j < 9;j ++)
    pan[i][j] = new Grid(i *Grid.he + startX, j *Grid.he +startY,0);
    this.setSize(400,500);
    this.addMouseListener(this);
    this.setResizable(false);
    this.setVisible(true);
    }

    private boolean isInside(int x,int y,Grid gr)
    {
    if(x > gr.getX() && x <gr.getX() + Grid.he)
    if(y> gr.getY() && y< gr.getY() + Grid.he)
    return true;
    return false;
    }

    public  void mouseClicked(MouseEvent e)  
    {
    int xPoint,yPoint;
    xPoint = e.getX();
    yPoint = e.getY();
    for(int i = 0  ;i < 9 ;i ++)
    for(int j = 0 ;j < 9; j++)
    {

    if(pan[i][j].getHad() == 0 && isInside(xPoint,yPoint,pan[i][j])  )//在范围内而且没有子
    {
    pan[i][j].flag = true;
    if(alt == false )
    {
    pan[i][j].setHad(1);
    alt = true;
    }
    else
    {
    pan[i][j].setHad(2);
    alt = false;
    }
    al.add(pan[i][j]);
    last = pan[i][j];
    repaint();
    if(isWin(i,j))
    {
    JOptionPane.showMessageDialog(null,"win");
    System.exit(1);
    }
    return;
    }

    } }

    private boolean isWin(int i,int j)//判断是否胜利
    {
    int t = last.getHad();
    int count = 0;
    int m,n;
    for(m = i,n = j;m > 0 &&pan[m][n].getHad() == t;m --);//向上走到不一样的点
    for(m ++;m < 9 &&pan[m][n].getHad() == t ;m ++,count ++);
    if(count == 5)
    return true;
    count = 0;
    for(m = i, n =j;n > 0 && pan[m][n].getHad() == t;n --);//向左走
    for(n ++;n < 9 && pan[m][n].getHad() == t;n++,count ++);
    if(count == 5)
    return true;
    count = 0;
    for(m = i,n = j;m > 0 && n > 0 && pan[m][n].getHad() == t;m --,n --);//向左上走
    for(m ++,n ++;m < 9 && n < 9 && pan[m][n].getHad() == t; m ++,n ++,count ++);
    if(count == 5)
    return true;
    count = 0;
    for(m = i,n = j; m >0 && n < 9 && pan[m][n].getHad() == t;m --,n ++);//向右上走
    for(m ++,n --;m <9 && n > 0 && pan[m][n].getHad() == t; m ++,n --,count ++);
    if(count == 5)
    return true;
    return false;

    }

    public void mouseEntered(MouseEvent e) { }

    public void mouseExited(MouseEvent e){ }

    public void mousePressed(MouseEvent e){}

    public void mouseReleased(MouseEvent e){}  

    public void paint(Graphics g)
    {
    super.paint(g);

    g.setColor(Color.BLACK);
    for(int i = 0;i <= 9;i ++)
    {
    g.drawLine( startX,i * Grid.he + startY,Grid.he * 9 + startX, i * Grid.he + startY);
    }

    for(int i = 0;i <= 9;i ++)
    {
    g.drawLine( i * Grid.he + startX,startY,i * Grid.he + startX,Grid.he * 9 + startY );
    }
    ListIterator it = al.listIterator();
    while(it.hasNext())
    {
    Grid temp = (Grid)it.next();
    if(temp.getHad() == 1)
    g.setColor(Color.blue);
    else
    g.setColor(Color.black);
    g.fillOval(temp.getX(),temp.getY(),Grid.he,Grid.he);
    }

    }
    }public class MainClass { public static void main(String[] args) {
    MainFrame mf = new MainFrame();
    }
    }
      

  2.   

    isWin函数有点问题,改过了:
    private boolean isWin(int i,int j)//判断是否胜利
    {
    int t = last.getHad();
    int count = 0;
    int m,n;
    for(m = i,n = j;m >= 0 &&pan[m][n].getHad() == t;m --);//向上走到不一样的点
    for(m ++;m < 9 &&pan[m][n].getHad() == t ;m ++,count ++);
    if(count == 5)
    return true;
    count = 0;
    for(m = i, n =j;n >= 0 && pan[m][n].getHad() == t;n --);//向左走
    for(n ++;n < 9 && pan[m][n].getHad() == t;n++,count ++);
    if(count == 5)
    return true;
    count = 0;
    for(m = i,n = j;m >= 0 && n >= 0 && pan[m][n].getHad() == t;m --,n --);//向左上走
    for(m ++,n ++;m < 9 && n < 9 && pan[m][n].getHad() == t; m ++,n ++,count ++);
    if(count == 5)
    return true;
    count = 0;
    for(m = i,n = j; m >= 0 && n < 9 && pan[m][n].getHad() == t;m --,n ++);//向右上走
    for(m ++,n --;m <9 && n >= 0 && pan[m][n].getHad() == t; m ++,n --,count ++);
    if(count == 5)
    return true;
    return false;

    }
      

  3.   

    书再多看看吧,感觉上不难,找本java实践的书,上面有很多类似的例子.看看别人的思路就行了!
    自己做的时候无从下手,看了别人的,肯定觉得非常简单啦!
      

  4.   

    to:Free_Wind22(自由的风)给你50分!不过现在还不能结这个帖子