日期:2014-05-20  浏览次数:20761 次

求java版的终端版的五子棋游戏
求java版的终端版的五子棋游戏

------解决方案--------------------
写过一个雏形,剩下的楼主写一下吧,也不是很难
Java code


import java.io.*;
/**
 * @author  yeeku.H.lee kongyeeku@163.com
 * @version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date: 
 */
public class Gobang
{
    //定义一个二维数组来充当棋盘
    private String[][] board;
    //定义棋盘的大小
    private static int BOARD_SIZE = 15;
    public void initBoard()
    {
        //初始化棋盘数组
        board = new String[BOARD_SIZE][BOARD_SIZE];
        //把每个元素赋为"╋",用于在控制台画出棋盘
        for (int i = 0 ; i < BOARD_SIZE ; i++)
        {
            for ( int j = 0 ; j < BOARD_SIZE ; j++)
            {
                board[i][j] = "╋";
            }
        }
    }
    //在控制台输出棋盘的方法
    public void printBoard()
    {
        //打印每个数组元素
        for (int i = 0 ; i < BOARD_SIZE ; i++)
        {
            for ( int j = 0 ; j < BOARD_SIZE ; j++)
            {
                //打印数组元素后不换行
                System.out.print(board[i][j]);
            }
            //每打印完一行数组元素后输出一个换行符
            System.out.print("\n");
        }
    }
    public static void main(String[] args)throws Exception
    {
        Gobang gb = new Gobang();
        gb.initBoard();
        gb.printBoard();
        //这是用于获取键盘输入的方法
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String inputStr = null;
        //br.readLine():每当在键盘上输入一行内容按回车,刚输入的内容将被br读取到。
        while ((inputStr = br.readLine()) != null)
        {
            //将用户输入的字符串以逗号(,)作为分隔符,分隔成2个字符串
            String[] posStrArr = inputStr.split(",");
            //将2个字符串转换成用户下棋的座标
            int xPos = Integer.parseInt(posStrArr[0]);
            int yPos = Integer.parseInt(posStrArr[1]);
            //把对应的数组元素赋为"●"。
            gb.board[xPos - 1][yPos - 1] = "●";                
            /*
             电脑随机生成2个整数,作为电脑下棋的座标,赋给board数组。
             还涉及
                1.座标的有效性,只能是数字,不能超出棋盘范围
                2.如果下的棋的点,不能重复下棋。
                3.每次下棋后,需要扫描谁赢了
             */
            gb.printBoard();
            System.out.println("请输入您下棋的座标,应以x,y的格式:");
        }
    }
}

------解决方案--------------------
Java code
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/*
 *main方法创建了ChessFrame类的一个实例对象(cf),
 *并启动屏幕显示显示该实例对象。
 **/
public class FiveChessAppletDemo {
    public static void main(String args[]){
        ChessFrame cf = new ChessFrame();
        cf.setVisible(true);
    }
}

/*
 *类ChessFrame主要功能是创建五子棋游戏主窗体和菜单
 **/
class ChessFrame extends JFrame implements ActionListener {
    /**
     * 
     */
    private static final long serialVersionUID = 2183726320279905885L;
    private String[] strsize={"20x15","30x20","40x30"};
    private String[] strmode={"人机对弈","人人对弈"};
    public static boolean iscomputer=true;
    public static boolean checkcomputer=true;
    private int width,height;
    private ChessModel cm;
    private MainPanel mp;
    
    //构造五子棋游戏的主窗体
    public ChessFrame() {
        this.setTitle("五子棋游戏");
        cm=new ChessModel(1);
        mp=new MainPanel(cm);
        Container con=this.getContentPane();
        con.add(mp,"Center");
        this.setResizable(false);
        this.addWindowListener(new ChessWindowEvent());
        MapSize(20,15);
        JMenuBar mbar = new JMenuBar();
        this.setJMenuBar(mbar);
        JMenu gameMenu = new JMenu("游戏");
        mbar.add(makeMenu(gameMenu, new Object[] {
            "开局", "棋盘","模式", null, "退出"
            }, this));
        JMenu lookMenu =new JMenu("视图");
        mbar.add(makeMenu(lookMenu,new Object[] {
            "Metal","Motif","Windows"
            },this));
        JMenu helpMenu = new JMenu("帮助");
        mbar.add(makeMenu(helpMenu, new Object[] {
            "关于"
        }, this));
    }

    //构造五子棋游戏的主菜单
    public  JMenu makeMenu(Object parent, Object items[], Object target){
        JMenu m = null;
        if(parent instanceof JMenu)
            m = (JMenu)parent;
        else if(parent instanceof String)
            m = new JMenu((String)parent);
        else
            return null;
        for(int i = 0; i < items.length; i++)
            if(items[i] == null)
                m.addSeparator();
            else if(items[i] == "棋盘"){
                JMenu jm = new JMenu("棋盘");
                ButtonGroup group=new ButtonGroup();
                JRadioButtonMenuItem rmenu;
                for (int j=0;j<strsize.length;j++){
                    rmenu=makeRadioButtonMenuItem(strsize[j],target);
                    if (j==0)
                        rmenu.setSelected(true);
                    jm.add(rmenu);
                    group.add(rmenu);
                }
                m.add(jm);
            }else if(items[i] == "模式"){
                JMenu jm = new JMenu("模式");
                ButtonGroup group=new ButtonGroup();
                JRadioButtonMenuItem rmenu;
                for (int h=0;h<strmode.length;h++){
                    rmenu=makeRadioButtonMenuItem(strmode[h],target);
                    if(h==0)
                        rmenu.setSelected(true);
                    jm.add(rmenu);
                    group.add(rmenu);
                }
                m.add(jm);
            }else
                m.add(makeMenuItem(items[i], target));
        return m;
    }
    
    //构造五子棋游戏的菜单项
    public  JMenuItem makeMenuItem(Object item, Object target){
        JMenuItem r = null;
        if(item instanceof String)
            r = new JMenuItem((String)item);
        else if(item instanceof JMenuItem)
            r = (JMenuItem)item;
        else
            return null;
        if(target instanceof ActionListener)
            r.addActionListener((ActionListener)target);
        return r;
    }
    
    //构造五子棋游戏的单选按钮式菜单项
    public  JRadioButtonMenuItem makeRadioButtonMenuItem(
        Object item, Object target){
        JRadioButtonMenuItem r = null;
        if(item instanceof String)
            r = new JRadioButtonMenuItem((String)item);
        else if(item instanceof JRadioButtonMenuItem)
            r = (JRadioButtonMenuItem)item;
        else
            return null;
        if(target instanceof ActionListener)
            r.addActionListener((ActionListener)target);
        return r;
    }
    
    public void MapSize(int w,int h){
        setSize(w * 20+50 , h * 20+100 );
        if(!ChessFrame.checkcomputer) {
            ChessFrame.iscomputer=false;
        } else {
            ChessFrame.iscomputer=true;
        }
        mp.setModel(cm);
        mp.repaint();
    }
    
    public boolean getiscomputer(){
        return ChessFrame.iscomputer;
    }
    
    public void restart(){
        int modeChess = cm.getModeChess();
        if(modeChess <= 3 && modeChess >= 1){
            cm = new ChessModel(modeChess);
            MapSize(cm.getWidth(),cm.getHeight());
        }else{
            System.out.println("\u81EA\u5B9A\u4E49");
        }
    }
    
    public void actionPerformed(ActionEvent e){
        String arg=e.getActionCommand();
        try{
            if (arg.equals("Windows"))
                UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            else if(arg.equals("Motif"))
                UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            else
                UIManager.setLookAndFeel(
                    "javax.swing.plaf.metal.MetalLookAndFeel" );
            SwingUtilities.updateComponentTreeUI(this);
        }catch(Exception ee){}
        if(arg.equals("20x15")){
            this.width=20;
            this.height=15;
            cm=new ChessModel(1);
            MapSize(this.width,this.height);
            SwingUtilities.updateComponentTreeUI(this);
        }
        if(arg.equals("30x20")){
            this.width=30;
            this.height=20;
            cm=new ChessModel(2);
            MapSize(this.width,this.height);
            SwingUtilities.updateComponentTreeUI(this);
        }
        if(arg.equals("40x30")){
            this.width=40;
            this.height=30;
            cm=new ChessModel(3);
            MapSize(this.width,this.height);
            SwingUtilities.updateComponentTreeUI(this);
        }
        if(arg.equals("人机对弈")){
            this.checkcomputer=true;
            this.iscomputer=true;
            cm=new ChessModel(cm.getModeChess());
            MapSize(cm.getWidth(),cm.getHeight());
            SwingUtilities.updateComponentTreeUI(this);
        }
        if(arg.equals("人人对弈")){
            this.checkcomputer=false;
            this.iscomputer=false;
            cm=new ChessModel(cm.getModeChess());
            MapSize(cm.getWidth(),cm.getHeight());
            SwingUtilities.updateComponentTreeUI(this);
        }
        if(arg.equals("开局")){
            restart();
        }
        if(arg.equals("关于"))
            JOptionPane.showMessageDialog(this, "五子棋游戏测试版本", "关于", 0);
        if(arg.equals("退出"))
            System.exit(0);
    }
}