您好,欢迎来到微智科技网。
搜索
您的当前位置:首页ChessGame

ChessGame

来源:微智科技网


import java.awt.Color;

import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D;

import java.awt.HeadlessException; import java.awt.Point; import java.awt.Toolkit;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*;

public class ChessGame {

public static void main(String[] args) { }

class GameFrame extends JFrame { private static final int Width = 430; private static final int Height = 470; GameFrame() { setTitle(\"五子棋游戏\");

CenteredFrame();

}

GameFrame GFrame = new GameFrame();

GFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GFrame.setVisible(true);

setSize(Width, Height); GamePanel Gpanel = new GamePanel(); Gpanel.setBackground(Color.ORANGE );

}

add(Gpanel);

void CenteredFrame() { Toolkit kit = Toolkit.getDefaultToolkit(); }

Dimension screenSize = kit.getScreenSize(); int screenHeight = screenSize.height; int screenWidth = screenSize.width; int Xposition = (screenWidth - Width) / 2; int Yposition = (screenHeight - Height) / 2; setLocation(Xposition, Yposition);

}

class GamePanel extends JPanel { private Point cursor = new Point(40, 60); // 棋盘坐标

private int[][] ChessState = new int[18][18];// 棋盘状态

private int i = 0;// 横坐标 private int j = 0;// 纵坐标

private final static int testnum = 5;// 五子棋的规定棋子数

private Player Black = new Player(1, Color.BLACK, \"黑方\");// 黑方棋子 private Player White = new Player(2, Color.WHITE, \"白方\");// 白方棋子 private Player Cplayer = null;// 当前用户的引用

private JTextField textBlack = new JTextField(\"\黑方文本提示框对象和文本长度设置

private JTextField textWhite = new JTextField(\"\白方文本提示框对象和文本长private String Nothing = \"\";

private String textblack = \"请黑方下子\";// 黑方提示文本 private String textwhite = \"请白方下子\";// 白方提示文本 GamePanel() {

setLayout(null); Initialization(); setFocusable(true);

JButton Rutton = new JButton(\"重新开局\"); Rutton.setBounds(20, 14, 100, 26);

RestartListener restart = new RestartListener(); Rutton.addActionListener(restart); add(Rutton);

textBlack.setHorizontalAlignment(JTextField.CENTER); textBlack.setBounds(150, 14, 110, 26); textBlack.setEditable(false);

add(textBlack);

textWhite.setHorizontalAlignment(JTextField.CENTER); textWhite.setBounds(290, 14, 110, 26); textWhite.setEditable(false); add(textWhite);

ChessManMouseListener mlistener = new ChessManMouseListener(); addMouseListener(mlistener);

度设置

}

// 设置第一个JTextField输出\"请黑方下棋\坐标为(40,60) void Initialization() {

for (int i = 0, j; i < 18; i++)

for (j = 0; j < 18; j++) { ChessState[i][j] = 0; }

cursor.setLocation(40, 60);

}

Cplayer = Black;

textBlack.setText(textblack); textWhite.setText(Nothing);

// 画棋盘和初始化棋局状态

protected void paintComponent(Graphics g) { }

// 判断棋盘的当前位置是否已有棋子 boolean isChessState() { }

// 记录落子后棋盘的当前位置的状态 void RecordChessState() {

this.j = (cursor.x - 40) / 20; this.i = (cursor.y - 60) / 20; if (ChessState[this.i][this.j] != 0) return true; else

return false;

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g; for (int i = 60; i <= 400; i += 20) { g2.drawLine(40, i, 380, i); }

for (int j = 40; j <= 380; j += 20) { }

g2.drawLine(j, 60, j, 400);

for (i = 0; i < 18; i++) for (j = 0; j < 18; j++) { if (ChessState[i][j] != 0) {

}

}

if (ChessState[i][j] == 1) {

g2.setPaint(Black.getplayerChessManColor()); }

if (ChessState[i][j] == 2) { g2.setPaint(White.getplayerChessManColor()); }

g2.fillOval(j * 20 + 40 - 10, i * 20 + 60 - 10, ChessMan.getChessManSize(), ChessMan

.getChessManSize());

}

this.j = (cursor.x - 40) / 20;

this.i = (cursor.y - 60) / 20;

ChessState[this.i][this.j] = Cplayer.getCurrentIdentify();

// 判断当前玩家落子后是否赢了 void JudgeWin() { }

for (int i = 0; i < 4; i++) if (JudgeWinLine(i)) { // 提示当前玩家已经获得胜利

try { String Ginformation = \"GameInformation\"; String Message = \"恭喜玩家\" + Cplayer.getsIdentify() + \"获胜!\" + \"\\n\" + \"继续游戏还是退出游戏?\";

String[] options = { \"继续\退出\" };

int selection = JOptionPane.showOptionDialog(null, }

Message, Ginformation,

JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);// throws HeadlessException

if (selection == JOptionPane.OK_OPTION) {

Initialization(); repaint(); return;

if (selection == JOptionPane.NO_OPTION) { System.exit(0);// 退出程序 }

} catch (HeadlessException e) { }

e.printStackTrace();

}

// 如果当前方没有赢棋则双方轮换 ChangeCurrentPlayer();

// 在当前方向上是否有连续的五只棋子 boolean JudgeWinLine(int direction) {

int i, j, di, dj, count; i = j = di = dj = count = 0; switch (direction) { case 0: j = this.j - (testnum - 1);

}

i = this.i;

dj = 1; di = 0; break; case 1:

j = this.j;

i = this.i - (testnum - 1); dj = 0; di = 1; break;

case 2: j = this.j - (testnum - 1);

i = this.i + (testnum - 1); dj = 1; di = -1; break;

case 3: j = this.j - (testnum - 1); i = this.i - (testnum - 1); }

dj = 1; di = 1; break;

for (int k = 0; k < testnum * 2 + 1; k++) { }

if (j >= 0 && j < 18 && i >= 0 && i < 18) { if (ChessState[i][j] == Cplayer.getCurrentIdentify()) {

count++;

if (count >= testnum) return true;

} else count = 0;

}

j += dj; i += di;

return false;

// 更换当前玩家

void ChangeCurrentPlayer() {

if (Cplayer == Black) { Cplayer = White; textBlack.setText(Nothing);

}

textWhite.setText(textwhite);

} else { Cplayer = Black; textBlack.setText(textblack); textWhite.setText(Nothing); }

// 重新开局

private class RestartListener implements ActionListener { }

public void actionPerformed(ActionEvent arg0) { }

Initialization(); repaint();

requestFocus();

private class ChessManMouseListener implements MouseListener {

public void mouseClicked(MouseEvent event) { int x = (int)(Math.round((event.getX() / 20.0))*20); int y = (int)(Math.round((event.getY() / 20.0))*20); if (x >= 40 && x <= 380 && y >= 60 && y <= 400) { }

}

cursor.x = x; cursor.y = y;

if (!isChessState()) { Cplayer.PerformChessMan(); RecordChessState(); }

repaint();

JudgeWin();// 判定当前落子后是否赢棋

repaint();

public void mouseEntered(MouseEvent arg0) { }

public void mouseExited(MouseEvent arg0) { }

public void mousePressed(MouseEvent arg0) {

}

}

}

public void mouseReleased(MouseEvent arg0) { }

class ChessMan { }

private static final int ChessManSize = 20; private Color ChessManColor; ChessMan(Color c) { }

ChessManColor = c;

static int getChessManSize() { return ChessManSize; }

Color getChessManColor() { return ChessManColor; }

class Player {

private int identify;

private ChessMan pChessMan; private String PlayerStringIdentify;

Player(int identify, Color c, String sIdentify) { this.identify = identify; }

pChessMan = new ChessMan(c); this.PlayerStringIdentify = sIdentify;

int getCurrentIdentify() { }

return identify;

String getsIdentify() { }

return PlayerStringIdentify;

void PerformChessMan() { }

Color getplayerChessManColor() { return pChessMan.getChessManColor(); }

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务