五子棋实训报告(电子版)

发布时间:2018-07-01 03:54:00

JAVA程序设计》实训报告

课程名称:JAVA程序设计

业:计算机应用技术

级:11计算机应用班

小组成员:巨敏 石丽涛 张娅雯 李延

尚文学 董丁喜 周致远

指导老师:武文廷

一.实训目的·································1

. 实训题目和要求

2.1实训题目描述·······························1

2.2实训要求··································1

三.实训报告内容

3.1五子棋主框架·································1

3.2棋盘、棋子及说明信息··························1

3.3对弈算法相关问题设计··························1

四.实训中的部分代码·····························2

五.五子棋源程序代码·····························3

. 总结··········································17

一、实训目的

本次实训,学生可以将理论知识与具体实践相结合,巩固对JAVA相关方法和概念的理解。通过实训单机版五子棋游戏的编程,掌握JAVA语言编程的基础知识并能熟练运用,熟悉累声明与对象的使用,运用JAVAswing编写单机版五子棋游戏,并实现其功能。通过本次实训,可以开拓思维,增强编程思想,为深入学习JAVA打下良好的基础。

二、实训题目描述和要求

2.1实训题目描述

实训题目:JAVA五子棋单机版游戏。描述:通过JAVAswing组件,实现五子棋简单的双人对弈,并通过内部条件判断实现输赢的双方的下棋过程。

2.2实训要求

1)五子棋游戏的主窗口也就是游戏界面的实现

2)棋子黑白色的设置及判定

3)完成判断某一横行是否练成五子及所有方向是否练成五子的功能

4)几个简单按钮的实现,“重新开始”“悔棋”“退出”按钮

5)菜单栏的实现,“重新开始”“悔棋”“退出”菜单项

三、实训报告内容

3.1主框架

编写一个startCheesJFrame类,主要用来显行主窗体界面,包括工具条面板、菜单栏项。设置界面关闭事件。并编写一个内部类MyItemListener来监听按钮和菜单栏的单机事件。

3.2棋盘、棋子

1)编写point类,包括棋子的X/Y索引,颜色。定义构造函数和相应的get方法。

2)编写ChessBoard类,设置棋盘背景颜色为橘黄色

3)在主框架类中创建ChessBoard对象,并添加到主框架中

4)编写mousePressed方法来进行绘制棋盘和棋子

3.3对弈算法相关问题设计

1)编写mousePressed方法的内容,预定义isBlack表示下的是黑棋还是白棋。PointCount表示当前棋子的个数。

(2)添加相应的判断:不能画到棋盘外,下过的地方不能再下(需要辅助方法find point)。

3)添加胜利的判断iswin,添加标记变量gameOver。在mousePressed方法的最前面调用加入gameOver的判断,在mousePressed方法的最后调用iswin,返回true则给出消息提示,gameOver设置为tuer

四、实训中的部分代码

1.ChessBoard.java

Private ChessBoard chessboard;

Private JPanel toolbar;

Private JButton shartButton;

Private JButton backButton;

Private JButton exiButton;

2.point.java

Public class point{

Private int x;

Private int y;

Private color color;

Public static final int DIAMETER=30;

Public point lint x, int y, color color{

This. X=x;

This.y=y;

This . color=color;

}

3.startChessJFrame.java

Public startChess JFrame(){

Set Title(“单机版五子棋”);

chessboard=new chessboard();

menuBar=new JMenuBar();

sysMenu=new JMenu(“系统”);

startMenuItem=new JMenuItem(“重新开始”);

exitMenuItem=new JMenuItem(“退出”)

backMenuItem=new JMenuItem(“悔棋”)

sysMenu.add(startMenuItem);

五、五子棋源程序代码

//StartChessFrame

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class StartChessFrame extends JFrame {

private ChessBoard chessBoard;

private JPanel toolbar

private JButton startButton, backButton, exitButton;

private JMenuBar menuBar;

private JMenu sysMenu;

private JMenuItem startMenuItem, exitMenuItem, backMenuItem;

public StartChessFrame() {

setTitle("单机版五子棋");

chessBoard = new ChessBoard();

menuBar = new JMenuBar();

sysMenu = new JMenu("系统");

startMenuItem = new JMenuItem("重新开始");

exitMenuItem = new JMenuItem("退出");

backMenuItem = new JMenuItem("悔棋");

sysMenu.add(startMenuItem);

sysMenu.add(backMenuItem);

sysMenu.add(exitMenuItem);

MyItemListener lis = new MyItemListener();

this.startMenuItem.addActionListener(lis);

backMenuItem.addActionListener(lis);

exitMenuItem.addActionListener(lis);

menuBar.add(sysMenu);

setJMenuBar(menuBar);

import java.awt.Color;

public class Point {

private int x;

private int y;

private Color color;

public static final int DIAMETER = 30;

public Point(int x, int y, Color color) {

this.x = x;

this.y = y;

this.color = color;

}

public int getX() {

return x;

}

public int getY() {

return y;

}

public Color getColor() {

return color;

}

}

toolbar = new JPanel();

startButton = new JButton("重新开始");

backButton = new JButton("悔棋");

exitButton = new JButton("退出");

toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

toolbar.add(startButton);

toolbar.add(backButton);

toolbar.add(exitButton);

startButton.addActionListener(lis);

backButton.addActionListener(lis);

exitButton.addActionListener(lis);

add(toolbar, BorderLayout.SOUTH);

add(chessBoard);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//setSize(800,800);

pack();

}

private class MyItemListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

Object obj = e.getSource();

if (obj == StartChessFrame.this.startMenuItem || obj == startButton)

{

System.out.println("重新开始...");

chessBoard.restartGame();

} else if (obj == exitMenuItem || obj == exitButton) {

System.exit(0);

} else if (obj == backMenuItem || obj == backButton) {

System.out.println("悔棋...");

chessBoard.goback();

}

}

}

public static void main(String[] args) {

StartChessFrame f = new StartChessFrame();

f.setVisible(true);

}

}

//ChessBoard

import javax.swing.*;

import java.awt.*;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.event.MouseEvent;

public class ChessBoard extends JPanel implements MouseListener {

public static final int MARGIN = 30;

public static final int GRID_SPAN = 35;

public static final int ROWS = 10;

public static final int COLS = 10;

Point[] chessList = new Point[(ROWS + 1) * (COLS + 1)];

boolean isBlack = true;

boolean gameOver = false;

int chessCount;

int xIndex, yIndex;

public ChessBoard()

{

setBackground(Color.ORANGE);

addMouseListener(this);

addMouseMotionListener(new MouseMotionListener() {

public void mouseDragged(MouseEvent e) {

}

public void mouseMoved(MouseEvent e) {

int x1 = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;

int y1 = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;

if (x1 < 0 || x1 > ROWS || y1 < 0 || y1 > COLS || gameOver

|| findChess(x1, y1))

setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

else

setCursor(new Cursor(Cursor.HAND_CURSOR));

}

});

}

public void paintComponent(Graphics g) {

super.paintComponent(g);

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

g.drawLine(MARGIN, MARGIN + i * GRID_SPAN, MARGIN + COLS

* GRID_SPAN, MARGIN + i * GRID_SPAN);

}

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

g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN,

MARGIN + ROWS * GRID_SPAN);

}

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

int xPos = chessList[i].getX() * GRID_SPAN + MARGIN;

int yPos = chessList[i].getY() * GRID_SPAN + MARGIN; g.setColor(chessList[i].getColor());

g.fillOval(xPos - Point.DIAMETER / 2, yPos - Point.DIAMETER / 2,

Point.DIAMETER, Point.DIAMETER);

if (i == chessCount - 1) {

g.setColor(Color.red);

g.drawRect(xPos - Point.DIAMETER / 2,

yPos - Point.DIAMETER / 2, Point.DIAMETER,

Point.DIAMETER);

}

}

}

public void mousePressed(MouseEvent e) {

if (gameOver)

return;

String colorName = isBlack ? "黑棋" : "白棋";

xIndex = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;

yIndex = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;

if (xIndex < 0 || xIndex > ROWS || yIndex < 0 || yIndex > COLS)

return;

if (findChess(xIndex, yIndex))

return;

Point ch = new Point(xIndex, yIndex, isBlack ? Color.black: Color.white);

chessList[chessCount++] = ch;

repaint();

if (isWin()) {

String msg = String.format("恭喜,%s赢了!", colorName);

JOptionPane.showMessageDialog(this, msg);

gameOver = true;

}

isBlack = !isBlack;

}

public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

private boolean findChess(int x, int y) {

for (Point c : chessList) {

if (c != null && c.getX() == x && c.getY() == y)

return true;

}

return false;

}

private boolean isWin() {

int continueCount = 1;

for (int x = xIndex - 1; x >= 0; x--) {

Color c = isBlack ? Color.black : Color.white;

if (getChess(x, yIndex, c) != null) {

continueCount++;

} else

break;

}

for (int x = xIndex + 1; x <= ROWS; x++) {

Color c = isBlack ? Color.black : Color.white;

if (getChess(x, yIndex, c) != null) {

continueCount++;

} else

break;

}

if (continueCount >= 5) {

return true;

} else

continueCount = 1;

for (int y = yIndex - 1; y >= 0; y--) {

Color c = isBlack ? Color.black : Color.white;

if (getChess(xIndex, y, c) != null) {

continueCount++;

} else

break;

}

for (int y = yIndex + 1; y <= ROWS; y++) {

Color c = isBlack ? Color.black : Color.white;

if (getChess(xIndex, y, c) != null) {

continueCount++;

} else

break;

}

if (continueCount >= 5) {

return true;

} else

continueCount = 1;

for (int x = xIndex + 1, y = yIndex - 1; y >= 0 && x <= COLS; x++, y--) {

Color c = isBlack ? Color.black : Color.white;

if (getChess(x, y, c) != null) {

continueCount++;

} else

break;

}

for (int x = xIndex - 1, y = yIndex + 1; y <= ROWS && x >= 0; x--, y++) {

Color c = isBlack ? Color.black : Color.white;

if (getChess(x, y, c) != null) {

continueCount++;

} else

break;

}

if (continueCount >= 5) {

return true;

} else

continueCount = 1;

for (int x = xIndex - 1, y = yIndex - 1; y >= 0 && x >= 0; x--, y--) {

Color c = isBlack ? Color.black : Color.white;

if (getChess(x, y, c) != null) {

continueCount++;

} else

break;

}

for (int x = xIndex + 1, y = yIndex + 1; y <= ROWS && x <= COLS; x++, y++) {

Color c = isBlack ? Color.black : Color.white;

if (getChess(x, y, c) != null) {

continueCount++;

} else

break;

}

if (continueCount >= 5) {

return true;

} else

continueCount = 1;

return false;

}

private Point getChess(int xIndex, int yIndex, Color color) {

for (Point c : chessList) {

if (c != null && c.getX() == xIndex && c.getY() == yIndex

&& c.getColor() == color)

return c;

}

return null;

}

public void restartGame() {

for (int i = 0; i < chessList.length; i++)

chessList[i] = null;

isBlack = true;

gameOver = false;

chessCount = 0;

repaint();

}

public void goback() {

if (chessCount == 0)

return;

chessList[chessCount - 1] = null;

chessCount--;

if (chessCount > 0) {

xIndex = chessList[chessCount - 1].getX();

yIndex = chessList[chessCount - 1].getY();

}

isBlack = !isBlack;

repaint();

}

public Dimension getPreferredSize() {

return new Dimension(MARGIN * 2 + GRID_SPAN * COLS, MARGIN * 2

+ GRID_SPAN * ROWS);

}

}

六、总结

通过本次实训,我们小组成员可以将理论知识与具体实践相结合,巩固了对java相关方法与概念的理解。通过单机版五子棋的编程,我们开拓了思路,增强了编程思路思想,此游戏的编写也激发了同学们学好java的兴趣。因此,此次实训是成功的。

五子棋实训报告(电子版)

相关推荐