首页下载资源后端五子棋 Java代码

RAR五子棋 Java代码

cyazhang28.29KB需要积分:1
文件:五子棋.rar

资源介绍:

在编程世界中,Java是一种广泛使用的面向对象的编程语言,其强大的跨平台能力和丰富的库使得它成为开发各种应用的理想选择。本项目名为“五子棋 Java代码”,显然它是一个使用Java实现的五子棋游戏。五子棋是一种两人对弈的策略型棋类游戏,简单易学,但策略深邃,是初学者学习编程逻辑和算法的良好实践。 在这个项目中,我们可以期待学到以下几个关键知识点: 1. **基础Java语法**:项目会涉及基本的Java编程元素,如变量、数据类型、控制流(if语句、for循环、while循环)、方法定义和调用等。 2. **面向对象编程**:五子棋游戏涉及到对象的创建和交互,比如棋盘(Board)对象、棋子(Piece)对象,以及玩家(Player)对象。理解类的定义、对象的实例化、继承、封装和多态性等概念是非常重要的。 3. **图形用户界面(GUI)**:为了使游戏可视并交互,开发者可能会使用Java的Swing或JavaFX库来创建GUI。这将涵盖组件布局、事件处理(点击、拖动等)和GUI设计原则。 4. **游戏逻辑**:游戏的核心在于判断胜负条件和规则的实现。这包括检查是否有五子连珠、是否超时、是否有禁手规则等。这部分需要编写复杂的逻辑代码,可能涉及递归、数组操作和位运算。 5. **多线程**:为了实现玩家轮流下棋,可能需要使用到Java的线程机制。一个线程代表一个玩家的思考和行动,确保游戏的流畅进行。 6. **错误处理和调试**:在开发过程中,良好的错误处理机制是必不可少的。学会使用try-catch语句捕获异常,并通过日志或者控制台输出来调试代码,找出程序中的问题。 7. **版本控制**:如果项目中包含了版本控制文件,如.git,那么可以学习到如何使用Git来管理代码版本,协作开发,提交和回滚代码。 通过这个项目,不仅可以加深对Java编程的理解,还能提升问题解决能力,因为设计游戏需要考虑许多细节和边界情况。对于初学者来说,这是一个很好的实践项目,因为它结合了基础编程和特定应用领域的知识。同时,它也是进一步学习高级Java特性,如并发、网络编程和数据库连接等的起点。
import java.awt.*; import java.awt.event.*; class ChessPad extends Panel implements MouseListener,ActionListener{ int array[][]=new int[19][19]; Scan scanp=new Scan(); Scan scanc=new Scan(); AutoPlay autoPlay=new AutoPlay(); Evaluate evaluatep=new Evaluate(); Evaluate evaluatec=new Evaluate(); Sort sort=new Sort(); int i=0; int x=-1,y=-1,棋子颜色=1; Button button=new Button("重新开局"); TextField text_1=new TextField("请黑棋下子"), text_2=new TextField(), text_3=new TextField(); ChessPad(){ setSize(440,440); setLayout(null);setBackground(Color.pink); addMouseListener(this);add(button);button.setBounds(10,5,60,26); button.addActionListener(this); add(text_1); text_1.setBounds(90,5,90,24); add(text_2); text_2.setBounds(290,5,90,24); add(text_3); text_3.setBounds(200,5,80,24); for(int i=0;i<19;i++) for(int j=0;j<19;j++) {array[i][j]=0;} for(int i=0;i<19;i++) for(int j=0;j<19;j++) for(int h=0;h<5;h++) {scanp.shape[i][j][h]=0; scanc.shape[i][j][h]=0;} text_1.setEditable(false); text_2.setEditable(false); } public void paint(Graphics g){ for (int i=40;i<=400;i=i+20){ g.drawLine(40,i,400,i); } for(int j=40;j<=400;j=j+20){ g.drawLine(j,40,j,400); } g.fillOval(97,97,6,6); g.fillOval(337,97,6,6); g.fillOval(97,337,6,6); g.fillOval(337,337,6,6); g.fillOval(217,217,6,6); } public void mousePressed(MouseEvent e){ int a=0,b=0; if(e.getModifiers()==InputEvent.BUTTON1_MASK){ x=(int)e.getX();y=(int)e.getY(); ChessPoint_black chesspoint_black=new ChessPoint_black(this); ChessPoint_white chesspoint_white=new ChessPoint_white(this); i++; text_3.setText("这是第"+i+"步"); if((x+5)/20<2||(y+5)/20<2||(x-5)/20>19||(y-5)/20>19) {} else { a=(x+10)/20;b=(y+10)/20; if(array[b-2][a-2]==0&&棋子颜色==1){ this.add(chesspoint_black); chesspoint_black.setBounds(a*20-9,b*20-9,18,18); 棋子颜色=棋子颜色*(-1); array[b-2][a-2]=1; if (Judge.judge(array,1)){ text_1.setText("黑棋赢!");棋子颜色=2;removeMouseListener(this);} else{ text_1.setText(""); } } } if(i>2&&棋子颜色==-1){ scanp.scan(array,1); scanc.scan(array,-1); sort.sort(scanp.shape); sort.sort(scanc.shape); evaluatep.evaluate(scanp.shape); evaluatec.evaluate(scanc.shape); 棋子颜色=棋子颜色*(-1); this.add(chesspoint_white); if(evaluatep.max>evaluatec.max){ text_2.setText(evaluatep.max_x+" "+evaluatep.max_y+" "+evaluatep.max); chesspoint_white.setBounds((evaluatep.max_y+2)*20-9,(evaluatep.max_x+2)*20-9,18,18); array[evaluatep.max_x][evaluatep.max_y]=-1; text_1.setText("请黑棋下子"); for(int i=0;i<19;i++) for(int j=0;j<19;j++) for(int h=0;h<5;h++) {scanp.shape[i][j][h]=0; scanc.shape[i][j][h]=0;} } else { text_2.setText(evaluatec.max_x+" "+evaluatec.max_y+" "+evaluatec.max); chesspoint_white.setBounds((evaluatec.max_y+2)*20-9,(evaluatec.max_x+2)*20-9,18,18); array[evaluatec.max_x][evaluatec.max_y]=-1; if (Judge.judge(array,-1)) { text_2.setText("白棋赢!");棋子颜色=2;removeMouseListener(this);} else{ text_1.setText("请黑棋下子"); for(int i=0;i<19;i++) for(int j=0;j<19;j++) for(int h=0;h<5;h++) {scanp.shape[i][j][h]=0; scanc.shape[i][j][h]=0;} } } } if(i<=2&&棋子颜色==-1) { autoPlay.autoPlay(array,b-2,a-2); this.add(chesspoint_white); 棋子颜色=棋子颜色*(-1); chesspoint_white.setBounds((autoPlay.y+2)*20-9,(autoPlay.x+2)*20-9,18,18); array[autoPlay.x][autoPlay.y]=-1; if (Judge.judge(array,-1)) { text_2.setText("白棋赢!");棋子颜色=2;removeMouseListener(this);} else{ text_1.setText("请黑棋下子"); text_2.setText(autoPlay.x+" "+autoPlay.y); } } } } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void actionPerformed(ActionEvent e) {this.removeAll(); 棋子颜色=1; add(button);button.setBounds(10,5,60,26); add(text_1);text_1.setBounds(90,5,90,24); text_2.setText(""); text_1.setText("请黑棋下子"); add(text_2); text_2.setBounds(290,5,90,24); add(text_3); text_3.setBounds(200,5,80,24); i=0; text_3.setText("这是第"+i+"步"); for(int i=0;i<19;i++) for(int j=0;j<19;j++) {array[i][j]=0;} for(int i=0;i<19;i++) for(int j=0;j<19;j++) for(int h=0;h<5;h++) {scanp.shape[i][j][h]=0; scanc.shape[i][j][h]=0;} addMouseListener(this); } } class ChessPoint_black extends Canvas implements MouseListener{ ChessPad chesspad=null; ChessPoint_black(ChessPad p){ setSize(20,20); addMouseListener(this); chesspad=p; } public void paint(Graphics g){ g.setColor(Color.black);g.fillOval(0,0,18,18); } public void mousePressed(MouseEvent e){ /*if(e.getModifiers()==InputEvent.BUTTON3_MASK){ chesspad.remove(this); chesspad.棋子颜色=1; chesspad.text_2.setText(""); chesspad.text_1.setText("请黑棋下子"); }*/ } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){ } } class ChessPoint_white extends Canvas implements MouseListener{ ChessPad chesspad=null; ChessPoint_white(ChessPad p){ setSize(20,20);addMouseListener(this); chesspad=p; } public void paint(Graphics g){ g.setColor(Color.white);g.fillOval(0,0,18,18); } publi
100+评论
captcha