最新【组合模式 实战(1)——实现 五子棋(1),多线程面试题高级程序开发

感受:

其实我投简历的时候,都不太敢投递阿里。因为在阿里一面前已经过了字节的三次面试,投阿里的简历一直没被捞,所以以为简历就挂了。

特别感谢一面的面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。

也祝愿各位同学,都能找到自己心动的offer。

分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档

拿到字节跳动offer后,简历被阿里捞了起来,二面迎来了P9"盘问"

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

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

public void setPosition(int x, int y){

position[0] = x;

position[1] = y;

}

public String showInfo(){

return “This is black piece.”;

}

public int getColor(){

return BLACK;

}

}

//Board.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.JPanel;

import java.util.Iterator;

// Chess board for Wu Zi Qi

class Board extends JPanel implements MouseListener{

final static int BLACK = 1, WHITE = -1, R=13;

private String blackPlayer = null, whitePlayer = null;

private Composite arrPieces = new Composite();

private GChessPiece graphicPiece = null;

private ChessComponent conceptPiece = null;

int[][] whitePieces = new int[15][15];

int[][] blackPieces = new int[15][15];

boolean isGameOver = false;

public static int LENGTH = 15;

int inset=10, leftEdge, rightEdge, top, bottom;

int width, height, unit;

int x = -1, y = -1, chessColor = BLACK;

int m=0, n=0;

int count =0;

public Board(){

setSize(420,420);

width = getWidth()- 2*inset;

height = getHeight()- 2*inset;

unit = width/14;

top = inset;

bottom = height+inset;

leftEdge = inset;

rightEdge = width+inset;

setBackground(Color.orange);

addMouseListener(this);

}

// Paint the Wuzi Qi board

public void paint(Graphics g){

drawBoard(g);

drawPlayers();

}

public void mousePressed(MouseEvent e){

if(e.getModifiers()==InputEvent.BUTTON1_MASK){

if( (isGameOver == false) && (blackPlayer != null) && (whitePlayer != null) ) {

x = (int)e.getX();

y = (int)e.getY();

addPieceToBoard(x, y);

}

}

}

private void addPieceToBoard(int x, int y){

convertToGrid(x, y);

if ( x>= leftEdge-5 && x<= rightEdge+5 && y>=top-5 && y<=bottom +5) {

if(chessColor == BLACK) {

conceptPiece = new BlackPiece();

blackPieces[m][n]=1;

graphicPiece = new GChessPiece(Color.black);

}

else if(chessColor == WHITE){

conceptPiece = new WhitePiece();

whitePieces[m][n]=1;

graphicPiece = new GChessPiece(Color.white);

}

this.add(graphicPiece);

graphicPiece.setBounds(munit+inset-R, nunit+inset-R, 2R, 2R);

conceptPiece.setPosition(x, y);

arrPieces.attach(conceptPiece); /// save to array

evaluateGame();

chessColor = chessColor*(-1);

}

}

private void convertToGrid(int x, int y){

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

int k=(inset+i*unit)/unit;

if( (x >=kunit -unit/2) && (x <kunit+ unit/2 ) )

m=k;

}

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

int p =(inset+j*unit)/unit;

if( (y >=punit -unit/2) && (y <punit+ unit/2 ) )

n=p;

}

}

private void evaluateGame(){

GameOperations g = new GameOperations(this);

if(chessColor == WHITE){

g.checkMatixLines(whitePieces);

g.checkLeftSlantDiagonal(whitePieces);

g.checkRightSlantDiagonal(whitePieces);

if(isGameOver == true)

anounceWinner();

}

if(chessColor == BLACK){

g.checkMatixLines(blackPieces);

g.checkLeftSlantDiagonal(blackPieces);

g.checkRightSlantDiagonal(blackPieces);

if(isGameOver == true)

anounceWinner();

}

}

// This replay method will recover the whole game step by step

// One call of this method will do only one step

public void replay(){

if(count ==0)

this.removeAll();

if (count >=0 && count < arrPieces.getSize()){

GChessPiece gBlackPiece = new GChessPiece(Color.black);

GChessPiece gWhitePiece = new GChessPiece(Color.white);

ChessComponent element = arrPieces.getElement(count);

int[] coordinates = element.getPosition();

int c = element.getColor();

int x=coordinates[0];

int y=coordinates[1];

convertToGrid(x, y);

if(c == BLACK ){

this.add(gBlackPiece);

gBlackPiece.setBounds(munit+inset-R, nunit+inset-R, 2R, 2R);

}

else if(c == WHITE ){

this.add(gWhitePiece);

gWhitePiece.setBounds(munit+inset-R, nunit+inset-R, 2R, 2R);

}

count++;

}

drawPlayers();

}

private void drawBoard(Graphics g){

for(int m=leftEdge; m<=rightEdge; m=m+unit) // draw ||||

g.drawLine(m, top, m, bottom-10);

for(int n=top; n<=bottom; n=n+unit) //draw -----

g.drawLine(leftEdge, n, rightEdge-10, n);

}

public void setPlayers(String blackPlayer, String whitePlayer){

this.blackPlayer = blackPlayer;

this.whitePlayer = whitePlayer;

drawPlayers();

}

private void drawPlayers() {

Graphics2D g2 = (Graphics2D)getGraphics();

g2.drawString("Black Player: "+blackPlayer, 55, bottom+20);

g2.drawString("White Player: "+whitePlayer, 250, bottom+20);

}

private void anounceWinner() {

Graphics2D g2 = (Graphics2D)getGraphics();

if(chessColor == BLACK)

g2.drawString("Black player won the game: ", 55, bottom+30);

else if(chessColor == WHITE)

g2.drawString("White player won the game: ", 250 , bottom+30);

}

public void resetGame(){

isGameOver = false;

whitePieces = new int[15][15];

blackPieces = new int[15][15];

arrPieces.removeAllElements();

count=0;

}

public void setGameOver(boolean b){

isGameOver = b;

}

public void mouseReleased(MouseEvent e){}

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();

}

1200页Java架构面试专题及答案

小编整理不易,对这份1200页Java架构面试专题及答案感兴趣劳烦帮忙转发/点赞

百度、字节、美团等大厂常见面试题

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

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

omposite extends ChessComponent{

private int[] position = null;

private ArrayList arrGoStone = null;

private ChessComponent currentPiece = null;

public Composite(){

arrGoStone = new ArrayList();

}

1200页Java架构面试专题及答案

小编整理不易,对这份1200页Java架构面试专题及答案感兴趣劳烦帮忙转发/点赞

[外链图片转存中…(img-OsoN0yzC-1715657134500)]

[外链图片转存中…(img-o9cLbhH1-1715657134501)]

百度、字节、美团等大厂常见面试题

[外链图片转存中…(img-zmHRiGYl-1715657134501)]

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

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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值