【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!)

基础语法篇

day01:

  • 开发前的准备工作,主要包括:常用DOS命令;工具的安装;快捷键介绍;Java语言特性;

day02:

  • 搭建Java开发环境,主要包括:path、JAVA_HOME、classpath等环境变量的配置;第一个程序的编写;Java中的注释;public class与class的区别;

day03:

  • Java基础语法,主要包括:标识符;关键字;字面量;变量;数据类型;

day04:

  • Java基础语法,主要包括:数据类型;二进制转换;原码反码补码;基本类型转换规则;运算符;

day05:

  • Java基础语法,主要包括:运算符;控制语句if和switch;

day06:

  • Java基础语法,主要包括:循环控制语句;转向语句;

day07:

  • Java基础语法,主要包括:方法的定义;方法的调用;栈数据结构;方法执行的内存变化;

day08:

  • Java基础语法,主要包括:方法重载;方法递归调用;

视频观看请点击下方链接:


面向对象篇

day09:

  • 面向对象,主要包括:面向对象的认识;类和对象;JVM内存结构;

day10:

  • 面向对象,主要包括:构造方法;封装;static关键字;

day11:

  • 面向对象,主要包括:static关键字;this关键字;之前的回顾总结;

day12:

  • 面向对象,主要包括:继承;方法覆盖;多态;

day13:

  • 面向对象,主要包括:多态在开发中的作用;super关键字;

视频观看请点击下方链接:


数组篇

day14:

面向对象,主要包括:final关键字;抽象类;接口;

day15:

面向对象,主要包括:接口在开发中的作用;package与import;访问权限修饰符;Object类的toString方法;

day16:

面向对象与数组,主要包括:Object类的常用方法;内部类;一维数组初步;

day17:

数组,主要包括:一维数组;二维数组;数组实现酒店管理系统;数组工具类;冒泡排序算法;

day18:

数组与常用类,主要包括:选择排序;二分法查找;String对象内存原理;String的常用方法;包装类的自动装箱和拆箱;

视频观看请点击下方链接:


常用类、异常、集合

day19:

  • 常用类与异常,主要包括:包装类;日期处理;数字处理;随机数;枚举;异常继承结构;

day20:

  • 异常,主要包括:异常怎么处理;异常在开发中的实际应用;

day21:

  • 集合,主要包括:Collection继承结构;Collection常用方法;List接口特有方法;

day22:

  • 集合,主要包括:链表数据结构;泛型;foreach;Map接口常用方法;Map集合遍历;哈希表数据结构;

视频观看请点击下方链接:


IO、线程与反射机制

day23:

  • 集合与IO流,主要包括:Map集合;可排序集合;使用字节流读文件;

day24:

  • IO流中常用的流的使用;

day25:

  • 线程与线程安全;

day26:

  • 线程与反射机制,主要包括:线程安全;生产者和消费者模式;反射机制初步;

day27:

  • 反射机制学习完了javase这里有十来个javaSE的小项目供你练练手,巩固一下学习的基础,以便后面javaweb的更好学习注解;

视频观看请点击下方链接:


视频学完了,来看看杜老师的直播总结吧

直播视频

Javase零基础课程总结课件

直播总结

初学者常用200练习单词

【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!)插图15
java初学者常用单词.xls
41K
·
百度网盘

直播问题解答和精选问题解答思维导图

【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!)插图17
【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!)插图19

JavaSE小项目

学习完了javase不要慌,这里有十来个javaSE的小项目供你练练手,巩固一下学习的基础,以便后面javaweb的更好学

以五子棋为例:

该项目 为 Java 版五子棋小游戏,为控制台小游戏,主要功能有 人机大战,左右互战,可以悔棋可以提示,

人机大战采用 五元贡献法,代码简单 实况效果好 ,相比 一般 博弈论 判断局面的方式,比正真的AI 还是有一定的差距。还有更多可扩展 可以自行摸索

源码:https://github.com/Five-great/FiveChess

importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.util.*;//游戏运行入口publicclassFiveChess{publicstaticvoidmain(String[]args){newFiveChessMenu();}}//界面类,这是游戏主体框架classFiveChessWindowsextendsJFrame{//五子棋窗口privateChessboardchessboard=newChessboard();//五子棋盘【关键】privateChesschess=newChess();//五子棋业务逻辑【关键】privatePaneltoolbar1,toolbar2,toolbar3;privatebooleanflag;//privateJButtonstartButton;privateJTextFieldtext1,text2,text3,text4;privateJButtonprompt1,prompt2;privateJButtonbackButton1,backButton2;privateJButtonexitButton;privateintowner_flag=1;//用于左右互搏privatebooleanprompt_flag=false;privateStringutf_hz=null,utf_bz=null;////完成五子棋游戏界面publicvoidinit(Booleanflag,intres_flag){this.flag=flag;Chess.FIRST=res_flag;setTitle("java版五子棋-five");//创建游戏界面窗口toolbar1=newPanel();toolbar2=newPanel();toolbar3=newPanel();toolbar1.setLayout(newGridLayout(3,1,20,20));toolbar3.setLayout(newGridLayout(3,1,20,20));text1=newJTextField();text1.setPreferredSize(newDimension(60,60));// 设置大小text1.setFont(newFont(null,Font.BOLD,50));text1.setEditable(false);//设置不可编辑text2=newJTextField();text2.setPreferredSize(newDimension(60,60));// 设置大小text2.setFont(newFont(null,Font.BOLD,50));text2.setEditable(false);//设置不可编辑text3=newJTextField();text3.setPreferredSize(newDimension(60,60));// 设置大小text3.setFont(newFont(null,Font.BOLD,50));text3.setEditable(false);//设置不可编辑text4=newJTextField();text4.setPreferredSize(newDimension(60,60));// 设置大小text4.setFont(newFont(null,Font.BOLD,50));text4.setEditable(false);//设置不可编辑//把选项菜单加入到菜单栏//MyButton startButton= new MyButton("       重   新    开     始           ");startButton=newMyButton("       重     新    开     始           ");backButton1=newMyButton("       悔      棋         ");backButton2=newMyButton("       悔      棋         ");prompt1=newMyButton("   提        ?      示       ");prompt2=newMyButton("   提        ?      示       ");exitButton=newMyButton("        返     回    菜    单             ");toolbar1.setBackground(Color.cyan);toolbar2.setBackground(Color.green);toolbar3.setBackground(Color.cyan);if(!flag)toolbar1.add(newJLabel(newImageIcon("./src/玩家.png")));elsetoolbar1.add(newJLabel(newImageIcon("./src/电脑1.png")));toolbar3.add(newJLabel(newImageIcon("./src/玩家.png")));JPanelj3=newJPanel(newGridLayout(2,1,5,5));j3.add(text1);j3.add(text3);toolbar1.add(j3);if(!flag)text1.setText("—玩 家—");elsetext1.setText("—电 脑—");JPanelj4=newJPanel(newGridLayout(2,1,5,5));j4.add(text2);j4.add(text4);toolbar3.add(j4);text2.setText("—玩 家—");JPanelj1=newJPanel();j1.setLayout(newGridLayout(3,1,20,20));j1.setBackground(Color.cyan);j1.add(prompt1);j1.add(backButton1);toolbar1.add(j1);JPanelj2=newJPanel();j2.setLayout(newGridLayout(3,1,20,20));//网格布局j2.setBackground(Color.cyan);j2.add(prompt2);j2.add(backButton2);toolbar3.add(j2);exitButton.setSize(180,60);toolbar2.add(startButton);toolbar2.add(exitButton);Gbk_Utf1();textInit();ButtonInit();//按钮事件绑定初始化add(toolbar1,BorderLayout.WEST);add(toolbar2,BorderLayout.SOUTH);add(toolbar3,BorderLayout.EAST);add(chessboard,BorderLayout.CENTER);//把五子棋盘加入到frame//绑定鼠标事件,要下棋了,为了避免写无用的抽象方法的实现,chessboard.addMouseListener(newMouseAdapter(){publicvoidmouseClicked(MouseEvente){play(e);//鼠标点击引发下棋事件,处理下棋事件比较繁琐,为此开一个方法}});//设置frame窗口左上角图标setIconImage(this.getToolkit().getImage("./src/favicon.png"));setLocation(225,100);setSize(1198,800);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setResizable(false);setVisible(true);AIFirstOptionInit();//}publicvoidButtonInit(){backButton1.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){intxy=chessboard.goback();if(xy>0)chess.goback(xy);}});backButton2.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){// goback();intxy=chessboard.goback();if(xy>0)chess.goback(xy);}});prompt1.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){prompt_flag=!prompt_flag;chessboard.setPrompt(prompt_flag);if(prompt_flag)chessboard.setScores(chess.getScores());chessboard.repaint();}});prompt2.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){prompt_flag=!prompt_flag;chessboard.setPrompt(prompt_flag);if(prompt_flag){chessboard.setScores(chess.getScores());}chessboard.repaint();}});exitButton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){newFiveChessMenu();dispose();}});startButton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){FiveChessWindowsfive=newFiveChessWindows();five.init(flag,1);newRockPaperScissors(flag,five);dispose();}});}//先后手处理事件publicvoidAIFirstOptionInit(){//棋盘还没有落子的时候可以选择机器先手,一旦有落子,选择机器先手失效if(chessboard.isEmpty()){if(Chess.FIRST==-1){//机器先手,则先在中间位置下一个棋子chessboard.addChessman(7,7,-1);chess.addChessman(7,7,-1);}}}publicvoidGbk_Utf1(){try{utf_hz=newString("⚫".getBytes("UTF-8"),"UTF-8");utf_bz=newString("⚪".getBytes("UTF-8"),"UTF-8");}catch(UnsupportedEncodingExceptione1){e1.printStackTrace();}}publicvoidtextInit(){if(Chess.FIRST==-1){text3.setText(" 先手"+utf_hz);text4.setText(" 后手 "+utf_bz);}else{text3.setText(" 后手 "+utf_bz);text4.setText(" 先手 "+utf_hz);}}//核心业务逻辑  处理鼠标落子事件publicvoidplay(MouseEvente){intcellSize=chessboard.getCellSize();//每个格子的边长intx=(e.getX()-5)/cellSize;//像素值转换成棋盘坐标inty=(e.getY()-5)/cellSize;//像素值转换成棋盘坐标//判断落子是否合法booleanisLegal=chess.isLegal(x,y);//如果落子合法if(isLegal){chessboard.addChessman(x,y,owner_flag);//界面方面加一个棋子chess.addChessman(x,y,owner_flag);//逻辑业务方面加一个棋子//判断人类是否胜利if(chess.isWin(x,y,owner_flag)){chessboard.setIsWin(chess.getisWinChess());JOptionPane.showMessageDialog(this,"                   己方获胜","您赢了!",JOptionPane.PLAIN_MESSAGE);chessboard.init();//初始化chess.init();textInit();return;}Locationloc=chess.searchLocation();//根据五元贡献的方法确定if(flag){//机器落子//Location loc = chess.searchLocation();//根据五元贡献的方法确定chessboard.addChessman(loc);chess.addChessman(loc.getX(),loc.getY(),loc.getOwner());//判断机器是否胜利if(chess.isWin(loc.getX(),loc.getY(),-1)){chessboard.setIsWin(chess.getisWinChess());JOptionPane.showMessageDialog(this,"             对方获胜","您输了!",JOptionPane.PLAIN_MESSAGE);chessboard.init();chess.init();textInit();return;}}elseowner_flag=-owner_flag;chessboard.setPrompt(prompt_flag);//更行提示分数和棋子chess.searchLocation();chessboard.setScores(chess.getScores());}}}classChessboardextendsJPanel{//棋盘类 继承了 JPanelprivatestaticfinallongserialVersionUID=1L;publicstaticfinalintCHESSBOARD_SIZE=15;//棋盘大小15X15privateArrayList<Location>locationList=newArrayList<>();//棋盘上所有可以落子的位置坐标等信息privateColorbackgroundColor=newColor(255,245,186);//棋盘背景色privateColorlineColor=newColor(66,66,66);//棋盘线条颜色privateintmargin=30;//棋盘边缘长度privateint[][]scores;//获取胜利棋子;privateint[]iswinxy;privatebooleanprompt_flag=false;publicvoidinit(){locationList.clear();repaint();}//初始化棋盘publicvoidsetPrompt(booleanprompt_flag2){this.prompt_flag=prompt_flag2;}//获取是否提示状态publicvoidsetScores(int[][]scores){this.scores=scores;}//获取分数数据publicvoidsetIsWin(int[]iswinxy){this.iswinxy=iswinxy;}publicvoidpaint(Graphicsg){//覆盖paint方法super.paint(g);drawChessboard(g);//画棋盘drawScores(g);//画分数drawiswin(g);//画胜利棋子drawChessman(g);//画棋子}publicvoiddrawChessboard(Graphicsg){//画棋盘g.setColor(backgroundColor);//先画背景g.fillRect(0,0,this.getWidth(),this.getHeight());g.setColor(lineColor);//画线intcellSize=(this.getWidth()-2*margin)/(CHESSBOARD_SIZE-1);//每个格子的边长for(inti=0;i<CHESSBOARD_SIZE;i++){g.drawLine(margin,margin+i*cellSize,this.getWidth()-margin,margin+i*cellSize);//画横线g.drawLine(margin+i*cellSize,margin,margin+i*cellSize,this.getHeight()-margin);//画纵线}}publicvoiddrawChessman(Graphicsg){//画棋子for(inti=0;i<locationList.size();i++){Locationloc=locationList.get(i);intcellSize=(this.getWidth()-2*margin)/(CHESSBOARD_SIZE-1);//每个格子的边长if(i==locationList.size()-1){g.setColor(newColor(207,13,62));// 标记最后一个棋子四周为红色g.fillRoundRect(margin+cellSize*loc.getX()-cellSize/2-5,margin+cellSize*loc.getY()-cellSize/2-5,cellSize+8,cellSize+8,100,100);g.setColor(backgroundColor);g.fillRoundRect(margin+cellSize*loc.getX()-cellSize/2-2,margin+cellSize*loc.getY()-cellSize/2-2,cellSize+2,cellSize+2,100,100);}//根据先后手设置棋子为黑色和白色if(loc.getOwner()==Chess.FIRST)g.setColor(Color.BLACK);elseg.setColor(Color.WHITE);//画棋子g.fillOval(margin+cellSize*loc.getX()-cellSize/2+3,margin+cellSize*loc.getY()-cellSize/2+3,cellSize-6,cellSize-6);}}//划分数publicvoiddrawScores(Graphicsg){if(prompt_flag){intMaxscore=0,Maxx=-1,Maxy=-1;g.setColor(Color.darkGray);intcellSize=(this.getWidth()-2*margin)/(CHESSBOARD_SIZE-1);//每个格子的边长for(inti=0;i<CHESSBOARD_SIZE;i++)for(intj=0;j<CHESSBOARD_SIZE;j++){if(Maxscore<=scores[i][j]){Maxscore=scores[i][j];Maxx=i;Maxy=j;}g.drawString(""+scores[i][j],margin+i*cellSize,margin+j*cellSize);}g.setColor(Color.green);//画提示棋子。g.fillOval(margin+cellSize*Maxx-cellSize/2+5,margin+cellSize*Maxy-cellSize/2+5,cellSize-8,cellSize-8);}}publicvoiddrawiswin(Graphicsg){//画胜利棋子if(iswinxy!=null&&iswinxy.length>3){g.setColor(Color.darkGray);System.out.println(iswinxy.length);intcellSize=(this.getWidth()-2*margin)/(CHESSBOARD_SIZE-1);//每个格子的边长intx1,y1;for(inti=0;i<5;i++){x1=iswinxy[i]/100;y1=iswinxy[i]%100;g.setColor(newColor(207,13,62));//g.fillRoundRect(margin+cellSize*x1-cellSize/2-5,margin+cellSize*y1-cellSize/2-5,cellSize+8,cellSize+8,100,100);g.setColor(backgroundColor);g.fillRoundRect(margin+cellSize*x1-cellSize/2-2,margin+cellSize*y1-cellSize/2-2,cellSize+2,cellSize+2,100,100);}iswinxy=null;}}publicintgoback(){if(locationList.size()==0)return-1;intx=locationList.get(locationList.size()-1).getX();inty=locationList.get(locationList.size()-1).getY();locationList.remove(locationList.size()-1);repaint();returnx*100+y;}publicvoidaddChessman(intx,inty,intowner){locationList.add(newLocation(x,y,owner));repaint();}//落子publicvoidaddChessman(Locationloc){locationList.add(loc);repaint();}publicintgetCellSize(){return(this.getWidth()-2*margin)/(CHESSBOARD_SIZE-1);}//计算棋盘每个小格子的大小publicbooleanisEmpty(){returnlocationList.size()==0?true:false;}//判断棋盘是否还没有棋子}classLocation{privateintx;//某个棋盘位置横坐标,0-14privateinty;//某个棋盘位置纵坐标,0-14privateintowner;//占据该位置的棋手方,1是人类,-1是机器,0是空privateintscore;//对该位置的打的分数publicLocation(){}//构造方法publicLocation(intx,inty,intowner){this.x=x;this.y=y;this.owner=owner;}publicLocation(intx,inty,intowner,intscore){this(x,y,owner);this.score=score;}publicintgetX(){returnthis.x;}//x 坐标publicvoidsetX(intx){this.x=x;}publicintgetY(){returnthis.y;}//y坐标publicvoidsetY(inty){this.y=y;}publicintgetOwner(){returnthis.owner;}// 棋盘点 状态坐标publicvoidsetOwner(intowner){this.owner=owner;}publicintgetScore(){returnthis.score;}// 该点分数publicvoidsetScore(intscore){this.score=score;}}//下棋业务核心类,与界面棋盘对应,业务放在这里,可以和界面代码分离classChess{publicstaticfinalintCHESSBOARD_SIZE=15;publicstaticintFIRST=1;//先手,-1表示机器,1表示人类,与Location类中的对应privateint[][]chessboard=newint[CHESSBOARD_SIZE][CHESSBOARD_SIZE];//与界面棋盘对应,0代表空,-1代表机器,1代表人类privateint[][]score=newint[CHESSBOARD_SIZE][CHESSBOARD_SIZE];//每个位置得分privateint[]iswinxy=newint[6];//胜利棋子坐标。publicChess(){}publicvoidgoback(intxy){chessboard[xy/100][xy%100]=0;}publicvoidinit(){FIRST=1;//默认人类先手for(inti=0;i<CHESSBOARD_SIZE;i++)for(intj=0;j<CHESSBOARD_SIZE;j++){chessboard[i][j]=0;score[i][j]=0;}//初始化棋盘 和分数表}publicvoidaddChessman(intx,inty,intowner){chessboard[x][y]=owner;}//落子publicbooleanisLegal(intx,inty){//棋盘边界判断 以及是否落子	//判断落子位置是否合法if(x>=0&&x<CHESSBOARD_SIZE&&y>=0&&y<CHESSBOARD_SIZE&&chessboard[x][y]==0)returntrue;returnfalse;}publicbooleanisWin(intx,inty,intowner){//判断哪方赢了(必定有刚落的子引发,因此只需判断刚落子的周围),owner为-1代表机器,owner为1代表人类intsum=0,k=1;for(inti=0;i<6;i++)iswinxy[i]=0;iswinxy[0]=x*100+y;//判断纵向上下方向for(inti=x-1;i>=0;i--)if(chessboard[i][y]==owner){sum++;iswinxy[k++]=i*100+y;}elsebreak;for(inti=x+1;i<CHESSBOARD_SIZE;i++)if(chessboard[i][y]==owner){sum++;iswinxy[k++]=i*100+y;}elsebreak;if(sum>=4){returntrue;}//判断横向向左右边sum=0;k=1;for(inti=y-1;i>=0;i--)if(chessboard[x][i]==owner){sum++;iswinxy[k++]=x*100+i;}elsebreak;for(inti=y+1;i<CHESSBOARD_SIZE;i++)if(chessboard[x][i]==owner){sum++;iswinxy[k++]=x*100+i;}elsebreak;if(sum>=4){returntrue;}//判断左上角到右下角方向上侧和左上角到右下角方向下侧(正斜线)sum=0;k=1;for(inti=x-1,j=y-1;i>=0&&j>=0;i--,j--)if(chessboard[i][j]==owner){sum++;iswinxy[k++]=i*100+j;}elsebreak;for(inti=x+1,j=y+1;i<CHESSBOARD_SIZE&&j<CHESSBOARD_SIZE;i++,j++)if(chessboard[i][j]==owner){sum++;iswinxy[k++]=i*100+j;}elsebreak;if(sum>=4){returntrue;}//判断右上角到左下角方向上侧和右上角到左下角方向下侧(反斜线)sum=0;k=1;for(inti=x+1,j=y-1;i<CHESSBOARD_SIZE&&j>=0;i++,j--)if(chessboard[i][j]==owner){sum++;iswinxy[k++]=i*100+j;}elsebreak;for(inti=x-1,j=y+1;i>=0&&j<CHESSBOARD_SIZE;i--,j++)if(chessboard[i][j]==owner){sum++;iswinxy[k++]=i*100+j;}elsebreak;if(sum>=4){returntrue;}returnfalse;}publicint[]getisWinChess(){returniswinxy;}//获取胜利棋子publicint[][]getchessborad(){returnchessboard;}//获取胜利棋子//确定机器落子位置//使用五元贡献评分算法//算法思路:对15X15的572个五元组分别评分,一个五元组的得分就是该五元组为其中每个位置贡献的分数,  一个位置的分数就是其所在所有五元组分数之和。所有空位置中分数最高的那个位置就是落子位置。publicLocationsearchLocation(){inthumanChessmanNum=0;//五元组中的黑棋数量intmachineChessmanNum=0;//五元组中的白棋数量inttupleScoreTmp=0;//五元组得分临时变量intgoalX=-1;//目标位置x坐标intgoalY=-1;//目标位置y坐标intmaxScore=-1;//最大分数for(inti=0;i<CHESSBOARD_SIZE;i++)//每次都初始化下score评分数组(全部置零)////每次机器找寻落子位置,评分都重新算一遍for(intj=0;j<CHESSBOARD_SIZE;j++)score[i][j]=0;//1.扫描横向和横向的15个行for(inti=0;i<CHESSBOARD_SIZE;i++){for(intj=0;j<CHESSBOARD_SIZE-4;j++){for(intk=j;k<j+5;k++)//每次加五个状态if(chessboard[i][k]==-1)machineChessmanNum++;elseif(chessboard[i][k]==1)humanChessmanNum++;tupleScoreTmp=tupleScore(humanChessmanNum,machineChessmanNum);//为该五元组的每个位置添加分数for(intk=j;k<j+5;k++)score[i][k]+=tupleScoreTmp;humanChessmanNum=tupleScoreTmp=machineChessmanNum=0;//清零for(intk=j;k<j+5;k++){//每次加五个状态if(chessboard[k][i]==-1)machineChessmanNum++;elseif(chessboard[k][i]==1)humanChessmanNum++;}tupleScoreTmp=tupleScore(humanChessmanNum,machineChessmanNum);for(intk=j;k<j+5;k++)score[k][i]+=tupleScoreTmp;humanChessmanNum=tupleScoreTmp=machineChessmanNum=0;//清零}}//3.扫描右上角到左下角上侧部分for(inti=CHESSBOARD_SIZE-1;i>=4;i--){for(intk=i,j=0;j<CHESSBOARD_SIZE&&k>=0;j++,k--){intm=k,n=j;while(m>k-5&&k-5>=-1){if(chessboard[m][n]==-1)machineChessmanNum++;elseif(chessboard[m][n]==1)humanChessmanNum++;m--;n++;}if(m==k-5){//注意斜向判断的时候,可能构不成五元组(靠近四个角落),遇到这种情况要忽略掉tupleScoreTmp=tupleScore(humanChessmanNum,machineChessmanNum);for(m=k,n=j;m>k-5;m--,n++)score[m][n]+=tupleScoreTmp;//为该五元组的每个位置添加分数}humanChessmanNum=tupleScoreTmp=machineChessmanNum=0;//清零}}//4.扫描右上角到左下角下侧部分for(inti=1;i<CHESSBOARD_SIZE;i++){for(intk=i,j=CHESSBOARD_SIZE-1;j>=0&&k<CHESSBOARD_SIZE;j--,k++){intm=k,n=j;while(m<k+5&&k+5<=CHESSBOARD_SIZE){if(chessboard[n][m]==-1)machineChessmanNum++;elseif(chessboard[n][m]==1)humanChessmanNum++;m++;n--;}if(m==k+5){//注意斜向判断的时候,可能构不成五元组(靠近四个角落),遇到这种情况要忽略掉tupleScoreTmp=tupleScore(humanChessmanNum,machineChessmanNum);for(m=k,n=j;m<k+5;m++,n--)score[n][m]+=tupleScoreTmp;//为该五元组的每个位置添加分数}humanChessmanNum=tupleScoreTmp=machineChessmanNum=0;//清零}}//5.扫描左上角到右下角部分for(inti=0;i<CHESSBOARD_SIZE-4;i++){for(intk=i,j=0;j<CHESSBOARD_SIZE&&k<CHESSBOARD_SIZE;j++,k++){intm,n;for(m=k,n=j;m<k+5&&k+5<=CHESSBOARD_SIZE;m++,n++){if(chessboard[m][n]==-1)machineChessmanNum++;elseif(chessboard[m][n]==1)humanChessmanNum++;}if(m==k+5){//注意斜向判断的时候,可能构不成五元组(靠近四个角落),遇到这种情况要忽略掉tupleScoreTmp=tupleScore(humanChessmanNum,machineChessmanNum);for(m=k,n=j;m<k+5;m++,n++)score[m][n]+=tupleScoreTmp;//为该五元组的每个位置添加分数}humanChessmanNum=tupleScoreTmp=machineChessmanNum=0;//清零for(m=k,n=j;m<k+5&&k+5<=CHESSBOARD_SIZE;m++,n++){if(chessboard[n][m]==-1)machineChessmanNum++;elseif(chessboard[n][m]==1)humanChessmanNum++;}if(m==k+5){//注意斜向判断的时候,可能构不成五元组(靠近四个角落),遇到这种情况要忽略掉tupleScoreTmp=tupleScore(humanChessmanNum,machineChessmanNum);//为该五元组的每个位置添加分数for(m=k,n=j;m<k+5;m++,n++)score[n][m]+=tupleScoreTmp;}humanChessmanNum=tupleScoreTmp=machineChessmanNum=0;//清零}}for(inti=0;i<CHESSBOARD_SIZE;i++)//从空位置中找到得分最大的位置(确定落子位置)for(intj=0;j<CHESSBOARD_SIZE;j++)if(chessboard[i][j]==0&&score[i][j]>maxScore){goalX=i;goalY=j;maxScore=score[i][j];}elseif(chessboard[i][j]!=0)score[i][j]=0;if(goalX!=-1&&goalY!=-1)returnnewLocation(goalX,goalY,-1);returnnewLocation(-1,-1,-1);//没找到坐标说明平局了}publicint[][]getScores(){returnscore;}//各种五元组情况评分表publicinttupleScore(inthumanChessmanNum,intmachineChessmanNum){int[]hb={0,15,400,1800,100000};//人类落子 1,2,3,4 分别对应 15 400 1800,100000int[]mb={0,35,800,15000,800000};//机器落子 1,2,3,4分别对应 35 800 15000 800000//1.既有人类落子,又有机器落子,判分为0if(humanChessmanNum>0&&machineChessmanNum>0)return0;if(humanChessmanNum==0&&machineChessmanNum==0)return7;if(machineChessmanNum>0)returnmb[machineChessmanNum];if(humanChessmanNum>0)returnhb[humanChessmanNum];return-1;//若是其他结果肯定出错了。这行代码根本不可能执行}}//自定义按钮classMyButtonextendsJButton{privateColorquit=newColor(205,255,205);// 离开时颜色publicstaticfinalColorBUTTON_COLOR1=newColor(205,255,205);publicstaticfinalColorBUTTON_COLOR2=newColor(51,154,47);publicMyButton(Strings){super(s);setFont(newjava.awt.Font(null,1,22));//setBorder(BorderFactory.createRaisedBevelBorder());//凸出setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));setContentAreaFilled(false);// 是否显示外围矩形区域 选否}publicvoidpaintComponent(Graphicsg){g.setColor(quit);g.fillRoundRect(0,0,getSize().width-1,getSize().height-1,20,20);super.paintComponent(g);}publicvoidpaintBorder(Graphicsg){g.drawRoundRect(0,0,getSize().width-1,getSize().height-1,20,20);}}

页面展示

1 主菜单页面展示

【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!)插图21

2 猜拳决定落子展示

【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!)插图23

3.下棋展示(当前落子圈红)

【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!)插图25

4.获胜突出现实展示

【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!)插图27

5.开启提示展示(绿色提示落子)

【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!)插图29

五子棋内容出处注明

作者:five-菜鸟级

出自:博客园

原文:https://www.cnblogs.com/five-great/p/12796647.html

原创文章 【超强干货!】学完这篇彻底入门Java零基础(文末附Java全套资源教程!),版权所有
如若转载,请注明出处:https://www.itxiaozhan.cn/20227306.html

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注