Java最全【组合模式 实战(1)——实现 五子棋,Java进阶学习资料

Spring全套教学资料

Spring是Java程序员的《葵花宝典》,其中提供的各种大招,能简化我们的开发,大大提升开发效率!目前99%的公司使用了Spring,大家可以去各大招聘网站看一下,Spring算是必备技能,所以一定要掌握。

目录:

部分内容:

Spring源码

  • 第一部分 Spring 概述
  • 第二部分 核心思想
  • 第三部分 手写实现 IoC 和 AOP(自定义Spring框架)
  • 第四部分 Spring IOC 高级应用
    基础特性
    高级特性
  • 第五部分 Spring IOC源码深度剖析
    设计优雅
    设计模式
    注意:原则、方法和技巧
  • 第六部分 Spring AOP 应用
    声明事务控制
  • 第七部分 Spring AOP源码深度剖析
    必要的笔记、必要的图、通俗易懂的语言化解知识难点

脚手框架:SpringBoot技术

它的目标是简化Spring应用和服务的创建、开发与部署,简化了配置文件,使用嵌入式web服务器,含有诸多开箱即用的微服务功能,可以和spring cloud联合部署。

Spring Boot的核心思想是约定大于配置,应用只需要很少的配置即可,简化了应用开发模式。

  • SpringBoot入门
  • 配置文件
  • 日志
  • Web开发
  • Docker
  • SpringBoot与数据访问
  • 启动配置原理
  • 自定义starter

微服务架构:Spring Cloud Alibaba

同 Spring Cloud 一样,Spring Cloud Alibaba 也是一套微服务解决方案,包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。

  • 微服务架构介绍
  • Spring Cloud Alibaba介绍
  • 微服务环境搭建
  • 服务治理
  • 服务容错
  • 服务网关
  • 链路追踪
  • ZipKin集成及数据持久化
  • 消息驱动
  • 短信服务
  • Nacos Confifig—服务配置
  • Seata—分布式事务
  • Dubbo—rpc通信

Spring MVC

目录:

部分内容:

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){ }

}

//ClientUI.java

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class ClientUI extends JFrame {

private JSplitPane bigSplitPane;

private JScrollPane showInfoPane;

private JPanel btnPanel;

private JLabel lblPlayers;

private JTextField txtPlayerBlack, txtPlayerWhite;

private Dimension minimumSize;

private Board board;

public static final String PLAY = “Start Game”;

public static final String EXIT = “Exit”;

public static final String REPLAY = “Replay”;

public static final String NEXTGAME = “Next Game”;

public ClientUI() {

super("Composite Pattern- Wuzi Qi ");

minimumSize = new Dimension(130, 100);

board = new Board();

setUpChoicePanel();

setUpScrollPanes();

}

private void setUpChoicePanel() {

lblPlayers = new JLabel(“Enter players before Playing”);

txtPlayerBlack = new JTextField(“player-black”);

txtPlayerWhite = new JTextField(“player-white”);

ButtonListener btnListener = new ButtonListener();

//Create button objects

JButton playButton = new JButton(PLAY);

JButton nextButton = new JButton(NEXTGAME);

JButton exitButton = new JButton(EXIT);

JButton replayButton = new JButton(REPLAY);

playButton.setMnemonic(KeyEvent.VK_S);

nextButton.setMnemonic(KeyEvent.VK_S);

exitButton.setMnemonic(KeyEvent.VK_X);

replayButton.setMnemonic(KeyEvent.VK_X);

playButton.addActionListener(btnListener);

exitButton.addActionListener(btnListener);

replayButton.addActionListener(btnListener);

nextButton.addActionListener(btnListener);

btnPanel = new JPanel();

btnPanel.setBackground(Color.gray);

GridBagLayout gridbag = new GridBagLayout();

btnPanel.setLayout(gridbag);

GridBagConstraints gbc = new GridBagConstraints();

btnPanel.add(lblPlayers);

btnPanel.add(txtPlayerBlack);

btnPanel.add(txtPlayerWhite);

btnPanel.add(playButton);

btnPanel.add(nextButton);

btnPanel.add(exitButton);

btnPanel.add(replayButton);

gbc.insets.top = 5;

gbc.insets.bottom = 5;

gbc.insets.left = 5;

gbc.insets.right = 5;

gbc.gridx = 0;

gbc.gridy = 1;

gridbag.setConstraints(lblPlayers, gbc);

gbc.gridx = 1;

gbc.gridy = 1;

gridbag.setConstraints(txtPlayerBlack, gbc);

gbc.gridx = 2;

gbc.gridy = 1;

gridbag.setConstraints(txtPlayerWhite, gbc);

gbc.insets.left = 2;

gbc.insets.right = 2;

gbc.insets.top = 15;

gbc.gridx = 0;

gbc.gridy = 5;

gridbag.setConstraints(playButton, gbc);

gbc.gridx = 1;

gbc.gridy = 5;

gridbag.setConstraints(nextButton, gbc);

gbc.gridx = 2;

gbc.gridy = 5;

gridbag.setConstraints(exitButton, gbc);

gbc.gridx = 3;

gbc.gridy = 5;

gridbag.setConstraints(replayButton, gbc);

}

private void setUpScrollPanes() {

showInfoPane = new JScrollPane( board);

showInfoPane.setBackground(Color.orange);

bigSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, showInfoPane, btnPanel);

bigSplitPane.setDividerLocation(450);

getContentPane().add(bigSplitPane);

setSize(new Dimension(450, 600));

setVisible(true);

}

class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent ae) {

if (ae.getActionCommand().equals(EXIT)) { System.exit(1); }

if (ae.getActionCommand().equals(PLAY)) {

String blackPlayer = txtPlayerBlack.getText();

String whitePlayer = txtPlayerWhite.getText();

if(blackPlayer.length() != 0 && whitePlayer.length() !=0){

board.setPlayers(blackPlayer, whitePlayer);

board.resetGame();

}

}

if (ae.getActionCommand().equals(NEXTGAME)) {

board.removeAll();

String blackPlayer = txtPlayerBlack.getText();

String whitePlayer = txtPlayerWhite.getText();

if(blackPlayer.length() != 0 && whitePlayer.length() !=0)

board.setPlayers(blackPlayer, whitePlayer);

board.resetGame();

}

if (ae.getActionCommand().equals(REPLAY)) {

board.replay();

}

}

}

public static void main(String args[]) {

try { UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel”);

} catch (Exception evt) {}

ClientUI frame = new ClientUI();

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) { System.exit(0); }} );

frame.setSize(450, 600);

frame.setVisible(true);

}

}

//Composite.java

import java.util.ArrayList;

import java.util.Iterator;

// Chess board for Game of Go

class Composite extends ChessComponent{

private int[] position = null;

private ArrayList arrGoStone = null;

private ChessComponent currentPiece = null;

public Composite(){

arrGoStone = new ArrayList();

}

public void attach(ChessComponent piece){

if(piece != null)

arrGoStone.add(piece);

}

public void detach(ChessComponent piece){

if(piece != null)

arrGoStone.remove(piece);

}

public void removeAllElements(){

arrGoStone.clear();

}

public Iterator elements(){

return arrGoStone.iterator();

}

public ChessComponent getElement(int k){

return arrGoStone.get(k);

}

public void setPosition(int x, int y){ }

public int[ ] getPosition(){

return position;

}

public String showInfo(){

return “This is composite.”;

}

public int getSize(){

return arrGoStone.size();

}

public int getColor(){

return 0;

}

}

//GameOperations.java

// This class encapsulates all the operations

// needed by the game logic. All the methods

// here are to called by the class Board

public class GameOperations{

private int LEN = Board.LENGTH;

private Board b = null;

public GameOperations(Board b){

this.b = b;

}

// Get a right slant line from a 2D integer array, 0 <= k < 29

public int[] getALeftSlantLine(int[][] intArr, int k){

int n=0;

int[] temp = new int[LEN];

for(int i=0; i<LEN; i++) {

for(int j=0; j<LEN; j++) {

if(i + j == k){

temp[n] = intArr[i][j];

} // if

} // for j ended, only assign term n of temp

n++;

} // for i ended, temp is filled with a left matrix diagonal line

return temp;

}

// Get a right slant line from a 2D integer array, k=-14 to 14 inclusive

public int[] getARightSlantLine(int[][] intArr, int k){

int n=0;

int[] temp = new int[LEN];

for(int i=0; i<LEN; i++) {

for(int j=0; j<LEN; j++) {

if(i == j+ k){

temp[n] = intArr[i][j];

} // if

} // for j ended, only assign term n of temp

n++;

} // for i ended, temp is filled with a right matrix diagonal line

return temp;

}

// Check a line to decide if a player wins or not

public void checkAline(int[] a){

int len = a.length;

for(int m=3; m< len-2; m++){

if( (a[m-2]==1)&&(a[m-1]==1)&&(a[m]==1)&&(a[m+1]==1)&&(a[m+2]==1) ){

b.setGameOver(true);

}

}

}

// Check all horizental and vertical lines of the matrix to

// see if the black player or white player wins or not

public void checkMatixLines(int[][] intArr){

for(int n=0; n<LEN; n++){

int[] b = new int[LEN];

for(int i=0; i<LEN; i++)

b[i] = intArr[i][n];

checkAline(b);

}

for(int m=0; m<LEN; m++){

int[] c = new int[LEN];

for(int j=0; j<LEN; j++)

c[j] = intArr[m][j];

checkAline©;

}

}

// Check all left slant lines of the matrix to see if

// the black player or white player wins or not

public void checkLeftSlantDiagonal(int[][] board){

for (int k=0; k<2*LEN-1; k++) {

int[] temp = new int[LEN];

temp=getALeftSlantLine(board, k);

checkAline(temp);

}

}

// Check all right slant lines of the matrix to see if

// the black player or white player wins or not

public void checkRightSlantDiagonal(int[][] board){

for (int k=-(LEN-1); k<LEN; k++) {

int[] temp = new int[LEN];

temp=getARightSlantLine(board, k);

checkAline(temp);

}

}

}

//GChessPiece.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.JOptionPane;

import java.awt.geom.Ellipse2D;

// Represents a Piece of the Wuzi Qi Game

class GChessPiece extends Canvas {

Color color = null;

Ellipse2D circle = new Ellipse2D.Double(0.0d, 0.0d, 26.0d, 26.0d);

GChessPiece(Color color) {

this.color=color;

setSize(26, 26);

}

public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D)g;

g2.setColor(color);

g2.fill(circle);

}

}

//WhitePiece.java

import java.awt.*;

// White Stone of Game of Go

// Used to encapsulate the data needed for a

// white chess piece: position and color

class WhitePiece extends ChessComponent {

int[] position = new int[2];

private static final int WHITE = -1;

WhitePiece() {

position = new int[2];

}

public int[ ] getPosition(){

return position;

}

public void setPosition(int x, int y){

position[0] = x;

position[1] = y;

}

public String showInfo(){

return “This is white piece.”;

最后

在面试前我整理归纳了一些面试学习资料,文中结合我的朋友同学面试美团滴滴这类大厂的资料及案例

MyBatis答案解析
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

大家看完有什么不懂的可以在下方留言讨论也可以关注。

觉得文章对你有帮助的话记得关注我点个赞支持一下!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

te static final int WHITE = -1;

WhitePiece() {

position = new int[2];

}

public int[ ] getPosition(){

return position;

}

public void setPosition(int x, int y){

position[0] = x;

position[1] = y;

}

public String showInfo(){

return “This is white piece.”;

最后

在面试前我整理归纳了一些面试学习资料,文中结合我的朋友同学面试美团滴滴这类大厂的资料及案例
[外链图片转存中…(img-yUy5eBpX-1715333312768)]

[外链图片转存中…(img-GWIScZM4-1715333312768)]
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

大家看完有什么不懂的可以在下方留言讨论也可以关注。

觉得文章对你有帮助的话记得关注我点个赞支持一下!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的Java GUI五子棋游戏实现。我们使用Swing库来构建GUI界面。 ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Gobang extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; private final int ROWS = 15; private final int COLS = 15; private final int GAP = 30; private final int WIDTH = COLS * GAP + 50; private final int HEIGHT = ROWS * GAP + 70; private JButton[][] board; private int[][] data; private int currentPlayer = 1; public Gobang() { setTitle("五子棋"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); initBoard(); setVisible(true); } private void initBoard() { JPanel p = new JPanel(); p.setLayout(new GridLayout(ROWS, COLS)); board = new JButton[ROWS][COLS]; data = new int[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i][j] = new JButton(); board[i][j].setBackground(Color.WHITE); board[i][j].addActionListener(this); p.add(board[i][j]); data[i][j] = 0; } } add(p, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (e.getSource() == board[i][j]) { if (data[i][j] == 0) { board[i][j].setBackground(currentPlayer == 1 ? Color.BLACK : Color.WHITE); data[i][j] = currentPlayer; if (checkWin(i, j)) { JOptionPane.showMessageDialog(this, "玩家 " + currentPlayer + " 获胜!"); System.exit(0); } currentPlayer = currentPlayer == 1 ? 2 : 1; } } } } } private boolean checkWin(int row, int col) { int count = 1; int r, c; //检查横向 for (int i = col - 1; i >= 0; i--) { if (data[row][i] == currentPlayer) { count++; } else { break; } } for (int i = col + 1; i < COLS; i++) { if (data[row][i] == currentPlayer) { count++; } else { break; } } if (count >= 5) { return true; } //检查纵向 count = 1; for (int i = row - 1; i >= 0; i--) { if (data[i][col] == currentPlayer) { count++; } else { break; } } for (int i = row + 1; i < ROWS; i++) { if (data[i][col] == currentPlayer) { count++; } else { break; } } if (count >= 5) { return true; } //检查左上-右下斜线 count = 1; r = row - 1; c = col - 1; while (r >= 0 && c >= 0) { if (data[r][c] == currentPlayer) { count++; } else { break; } r--; c--; } r = row + 1; c = col + 1; while (r < ROWS && c < COLS) { if (data[r][c] == currentPlayer) { count++; } else { break; } r++; c++; } if (count >= 5) { return true; } //检查左下-右上斜线 count = 1; r = row + 1; c = col - 1; while (r < ROWS && c >= 0) { if (data[r][c] == currentPlayer) { count++; } else { break; } r++; c--; } r = row - 1; c = col + 1; while (r >= 0 && c < COLS) { if (data[r][c] == currentPlayer) { count++; } else { break; } r--; c++; } if (count >= 5) { return true; } return false; } public static void main(String[] args) { new Gobang(); } } ``` 我们首先定义了一个15x15的棋盘,使用了一个二维数组来存储每个位置的状态。玩家可以通过点击棋盘上的按钮来下棋。每次点击后,我们会检查当前玩家是否获胜,如果是,我们会弹出一个提示框。 在checkWin()方法中,我们会检查是否有5个同色棋子连成一线,如果是,返回true,否则返回false。 这只是一个简单的五子棋游戏实现,你可以根据自己的需求对其行扩展和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值