名字真好听的五子棋——成品展示

源代码

import java.awt.;
import java.awt.event.
;
import java.applet.;
import javax.swing.
;
import java.io.PrintStream;
import javax.swing.JComponent;
import javax.swing.JPanel;

class ChessFrame extends JFrame implements ActionListener {
private String[] strsize={"标准棋盘","改进棋盘","扩大棋盘"};
private String[] strmode={"人机对战","人人对战"};
public static boolean iscomputer=true,checkcomputer=true;
private int width,height;
private ChessModel cm;
private MainPanel mp;

public ChessFrame() {
this.setTitle("五子棋游戏");
cm=new ChessModel(1);
mp=new MainPanel(cm);
Container con=this.getContentPane();
con.add(mp,"Center");
this.setResizable(false);
this.addWindowListener(new ChessWindowEvent());
MapSize(14,14);
JMenuBar mbar = new JMenuBar();
this.setJMenuBar(mbar);
JMenu gameMenu = new JMenu("游戏");
mbar.add(makeMenu(gameMenu, new Object[] {
"开局", null,"棋盘",null,"模式", null, "退出"
}, this));
JMenu lookMenu =new JMenu("外观");
mbar.add(makeMenu(lookMenu,new Object[] {
"类型一","类型二","类型三"
},this));
JMenu helpMenu = new JMenu("版本");
mbar.add(makeMenu(helpMenu, new Object[] {
"关于"
}, this));
}

public JMenu makeMenu(Object parent, Object items[], Object target){
JMenu m = null;
if(parent instanceof JMenu)
m = (JMenu)parent;
else if(parent instanceof String)
m = new JMenu((String)parent);
else
return null;
for(int i = 0; i < items.length; i++)
if(items[i] == null)
m.addSeparator();
else if(items[i] == "棋盘"){
JMenu jm = new JMenu("棋盘");
ButtonGroup group=new ButtonGroup();
JRadioButtonMenuItem rmenu;
for (int j=0;j<strsize.length;j++){
rmenu=makeRadioButtonMenuItem(strsize[j],target);
if (j==0)
rmenu.setSelected(true);
jm.add(rmenu);
group.add(rmenu);
}
m.add(jm);
}else if(items[i] == "模式"){
JMenu jm = new JMenu("模式");
ButtonGroup group=new ButtonGroup();
JRadioButtonMenuItem rmenu;
for (int h=0;h<strmode.length;h++){
rmenu=makeRadioButtonMenuItem(strmode[h],target);
if(h==0)
rmenu.setSelected(true);
jm.add(rmenu);
group.add(rmenu);
}
m.add(jm);
}else
m.add(makeMenuItem(items[i], target));
return m;
}

public JMenuItem makeMenuItem(Object item, Object target){
JMenuItem r = null;
if(item instanceof String)
r = new JMenuItem((String)item);
else if(item instanceof JMenuItem)
r = (JMenuItem)item;
else
return null;
if(target instanceof ActionListener)
r.addActionListener((ActionListener)target);
return r;
}

public JRadioButtonMenuItem makeRadioButtonMenuItem(
Object item, Object target){
JRadioButtonMenuItem r = null;
if(item instanceof String)
r = new JRadioButtonMenuItem((String)item);
else if(item instanceof JRadioButtonMenuItem)
r = (JRadioButtonMenuItem)item;
else
return null;
if(target instanceof ActionListener)
r.addActionListener((ActionListener)target);
return r;
}

public void MapSize(int w,int h){
setSize(w * 24, h * 27);
if(this.checkcomputer)this.iscomputer=true;
elsethis.iscomputer=false;
mp.setModel(cm);
mp.repaint();
}public boolean getiscomputer(){
return this.iscomputer;
}public void restart(){
int modeChess = cm.getModeChess();
if(modeChess <= 3 && modeChess >= 0){cm = new ChessModel(modeChess);MapSize(cm.getWidth(),cm.getHeight());
}
}public void actionPerformed(ActionEvent e){
String arg=e.getActionCommand();
try{if (arg.equals("类型三"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");else if(arg.equals("类型二"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
elseUIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel" );
SwingUtilities.updateComponentTreeUI(this);

}catch(Exception ee){}
if(arg.equals("小棋盘")){
this.width=14;
this.height=14;
cm=new ChessModel(1);
MapSize(this.width,this.height);
SwingUtilities.updateComponentTreeUI(this);
}
if(arg.equals("中棋盘")){
this.width=18;
this.height=18;
cm=new ChessModel(2);
MapSize(this.width,this.height);
SwingUtilities.updateComponentTreeUI(this);
}
if(arg.equals("大棋盘")){
this.width=22;
this.height=22;
cm=new ChessModel(3);
MapSize(this.width,this.height);
SwingUtilities.updateComponentTreeUI(this);
}
if(arg.equals("人机对战")){
this.checkcomputer=true;
this.iscomputer=true;
cm=new ChessModel(cm.getModeChess());
MapSize(cm.getWidth(),cm.getHeight());
SwingUtilities.updateComponentTreeUI(this);
}
if(arg.equals("人人对战")){
this.checkcomputer=false;
this.iscomputer=false;
cm=new ChessModel(cm.getModeChess());
MapSize(cm.getWidth(),cm.getHeight());
SwingUtilities.updateComponentTreeUI(this);
}
if(arg.equals("开局")){
restart();
}
if(arg.equals("关于"))
JOptionPane.showMessageDialog(null, " 名字真好听的第一版 ", "版本",JOptionPane.PLAIN_MESSAGE );
if(arg.equals("退出"))
System.exit(0);
}
}

import java.awt.;
import java.awt.event.
;
import java.applet.;
import javax.swing.
;
import java.io.PrintStream;
import javax.swing.JComponent;
import javax.swing.JPanel;
class ChessModel {
private int width,height,modeChess;
private int x=0,y=0;
private int[][] arrMapShow;
private boolean isOdd,isExist;
public ChessModel() {}
public ChessModel(int modeChess){
this.isOdd=true;
if(modeChess == 1){
PanelInit(14, 14, modeChess);
}
if(modeChess == 2){
PanelInit(18, 18, modeChess);
}
if(modeChess == 3){
PanelInit(22, 22, modeChess);
}
}

private void PanelInit(int width, int height, int modeChess){
this.width = width;
this.height = height;
this.modeChess = modeChess;
arrMapShow = new int[width+1][height+1];
for(int i = 0; i <= width; i++){
for(int j = 0; j <= height; j++){
arrMapShow[i][j] = -1;
}
}
}

public boolean getisOdd(){
return this.isOdd;
}

public void setisOdd(boolean isodd){
if(isodd)
this.isOdd=true;
else
this.isOdd=false;
}

public boolean getisExist(){
return this.isExist;
}

public int getWidth(){
return this.width;
}

public int getHeight(){
return this.height;
}

public int getModeChess(){
return this.modeChess;
}

public int[][] getarrMapShow(){
return arrMapShow;
}

private boolean badxy(int x, int y){
if(x >= width+20 || x < 0)
return true;
return y >= height+20 || y < 0;
}

public boolean chessExist(int i,int j){
if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2)
return true;
return false;
}

public void readyplay(int x,int y){
if(badxy(x,y))
return;
if (chessExist(x,y))
return;
this.arrMapShow[x][y]=3;
}

public void play(int x,int y){
if(badxy(x,y))
return;
if(chessExist(x,y)){
this.isExist=true;
return;
}else
this.isExist=false;
if(getisOdd()){
setisOdd(false);
this.arrMapShow[x][y]=1;
}else{
setisOdd(true);
this.arrMapShow[x][y]=2;
}
}

public void computerDo(int width,int height){
int max_black,max_white,max_temp,max=0;
setisOdd(true);
System.out.println("计算机走棋 ...");
for(int i = 0; i <= width; i++){
for(int j = 0; j <= height; j++){
if(!chessExist(i,j)){
max_white=checkMax(i,j,2);
max_black=checkMax(i,j,1);
max_temp=Math.max(max_white,max_black);
if(max_temp>max){
max=max_temp;
this.x=i;
this.y=j;
}
}
}
}
setX(this.x);
setY(this.y);
this.arrMapShow[this.x][this.y]=2;
}

public void setX(int x){
this.x=x;
}

public void setY(int y){
this.y=y;
}

public int getX(){
return this.x;
}

public int getY(){
return this.y;
}

public int checkMax(int x, int y,int black_or_white){
int num=0,max_num,max_temp=0;
int x_temp=x,y_temp=y;
int x_temp1=x_temp,y_temp1=y_temp;

for(int i=1;i<5;i++){
x_temp1+=1;
if(x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
num++;
else
break;
}

x_temp1=x_temp;
for(int i=1;i<5;i++){
x_temp1-=1;
if(x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
num++;
else
break;
}
if(num<5)
max_temp=num;

x_temp1=x_temp;
y_temp1=y_temp;
num=0;
for(int i=1;i<5;i++){
y_temp1-=1;
if(y_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
num++;
else
break;
}

y_temp1=y_temp;
for(int i=1;i<5;i++){
y_temp1+=1;
if(y_temp1>this.height)
break;
if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
num++;
else
break;
}
if(num>max_temp&&num<5)
max_temp=num;

x_temp1=x_temp;
y_temp1=y_temp;
num=0;
for(int i=1;i<5;i++){
x_temp1-=1;
y_temp1-=1;
if(y_temp1<0 || x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
num++;
else
break;
}

x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<5;i++){
x_temp1+=1;
y_temp1+=1;
if(y_temp1>this.height || x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
num++;
else
break;
}
if(num>max_temp&&num<5)
max_temp=num;

x_temp1=x_temp;
y_temp1=y_temp;
num=0;
for(int i=1;i<5;i++){
x_temp1+=1;
y_temp1-=1;
if(y_temp1<0 || x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
num++;
else
break;
}

x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<5;i++){
x_temp1-=1;
y_temp1+=1;
if(y_temp1>this.height || x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
num++;
else
break;
}
if(num>max_temp&&num<5)
max_temp=num;
max_num=max_temp;
return max_num;
}

public boolean judgeSuccess(int x,int y,boolean isodd){
int num=1;
int arrvalue;
int x_temp=x,y_temp=y;
if(isodd)
arrvalue=2;
else
arrvalue=1;
int x_temp1=x_temp,y_temp1=y_temp;

for(int i=1;i<6;i++){
x_temp1+=1;
if(x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}

x_temp1=x_temp;
for(int i=1;i<6;i++){
x_temp1-=1;
if(x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;

x_temp1=x_temp;
y_temp1=y_temp;
num=1;
for(int i=1;i<6;i++){
y_temp1-=1;
if(y_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}

y_temp1=y_temp;
for(int i=1;i<6;i++){
y_temp1+=1;
if(y_temp1>this.height)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;

x_temp1=x_temp;
y_temp1=y_temp;
num=1;
for(int i=1;i<6;i++){
x_temp1-=1;
y_temp1-=1;
if(y_temp1<0 || x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}

x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<6;i++){
x_temp1+=1;
y_temp1+=1;
if(y_temp1>this.height || x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;

x_temp1=x_temp;
y_temp1=y_temp;
num=1;
for(int i=1;i<6;i++){
x_temp1+=1;
y_temp1-=1;
if(y_temp1<0 || x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}

x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<6;i++){
x_temp1-=1;
y_temp1+=1;
if(y_temp1>this.height || x_temp1<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
num++;
else
break;
}
if(num==5)
return true;
return false;
}

public void showSuccess(JPanel jp){
JOptionPane.showMessageDialog(jp,"你赢了","结果",
JOptionPane.INFORMATION_MESSAGE);
}

public void showDefeat(JPanel jp){
JOptionPane.showMessageDialog(jp,"你输了","结果",
JOptionPane.INFORMATION_MESSAGE);
}
}

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class ChessWindowEvent extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}

ChessWindowEvent()

{
}
}

public class FiveChessAppletDemo {
public static void main(String args[]){
ChessFrame cf = new ChessFrame();
cf.show();
}
}

import java.awt.;
import java.awt.event.
;
import java.applet.;
import javax.swing.
;
import java.io.PrintStream;
import javax.swing.JComponent;
import javax.swing.JPanel;
class MainPanel extends JPanel
implements MouseListener,MouseMotionListener{
private int width,height;
private ChessModel cm;

MainPanel(ChessModel mm){
cm=mm;
width=cm.getWidth();
height=cm.getHeight();
addMouseListener(this);
}

public void setModel(ChessModel mm){
cm = mm;
width = cm.getWidth();
height = cm.getHeight();
}

public void paintComponent(Graphics g){
super.paintComponent(g);
for(int j = 0; j <= height; j++){
for(int i = 0; i <= width; i++){
int v = cm.getarrMapShow()[i][j];
draw(g, i, j, v);
}
}
}

public void draw(Graphics g, int i, int j, int v){
int x = 20 * i+20;
int y = 20 * j+20;
if(i!=width && j!=height){
g.setColor(Color.darkGray);
g.drawRect(x,y,20,20);
}
if(v == 1 ){
g.setColor(Color.gray);
g.drawOval(x-8,y-8,16,16);
g.setColor(Color.black);
g.fillOval(x-8,y-8,16,16);
}
if(v == 2 ){
g.setColor(Color.gray);
g.drawOval(x-8,y-8,16,16);
g.setColor(Color.white);
g.fillOval(x-8,y-8,16,16);
}
if(v ==3){
g.setColor(Color.cyan);
g.drawOval(x-8,y-8,16,16);
}
}

public void mousePressed(MouseEvent evt){
int x = (evt.getX()-10) / 20;
int y = (evt.getY()-10) / 20;
System.out.println(x+" "+y);
if (evt.getModifiers()==MouseEvent.BUTTON1_MASK){
cm.play(x,y);
System.out.println(cm.getisOdd()+" "+cm.getarrMapShow()[x][y]);
repaint();
if(cm.judgeSuccess(x,y,cm.getisOdd())){
cm.showSuccess(this);
evt.consume();
ChessFrame.iscomputer=false;
}
if(ChessFrame.iscomputer&&!cm.getisExist()){
cm.computerDo(cm.getWidth(),cm.getHeight());
repaint();
if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getisOdd())){
cm.showDefeat(this);
evt.consume();
}
}
}
}

public void mouseClicked(MouseEvent evt){}
public void mouseReleased(MouseEvent evt){}
public void mouseEntered(MouseEvent mouseevt){}
public void mouseExited(MouseEvent mouseevent){}
public void mouseDragged(MouseEvent evt){}
public void mouseMoved(MouseEvent moveevt){
int x = (moveevt.getX()-10) / 20;
int y = (moveevt.getY()-10) / 20;
cm.readyplay(x,y);
repaint();
}
}

效果图

成员贡献

何佳蕾负责主程序代码的编写和程序的细节优化;
彭垚负责窗体设计的部分代码;
刘钦令负责窗体设计的部分代码和组合运行程序,以及本周博客编写。

转载于:https://www.cnblogs.com/2014530420145315deblog/p/5585702.html

名字真好听的五子棋——15周进度相关推荐

  1. 名字真好听的五子棋——12周进度

    名字真好听的五子棋--12周进度 伪代码 创建五子棋游戏主窗体和菜单 实现了整个五子棋程序算法的核心 棋盘的宽度.高度.棋盘 棋盘方格的横向.纵向坐标所对应的棋子颜色 判断下子的横向.纵向坐标是否越界 ...

  2. 第二周进度及工作量统计

    项目:词频统计修改 项目类型:个人项目 项目完成情况:已完成 项目改进:第二次改进 项目日期:2016.9.14----2.16.9.14 C C S E I T 分析 需求,设计 15:20 15: ...

  3. 中国大学 MOOC 课程Python语言程序设计 (第11期)测试答案(1-5周)

    中国大学 MOOC 课程Python语言程序设计 (第11期)测试答案(1-5周)  Lan   2020-05-03 14:21   369 人阅读  0 条评论 感谢中国大学MOOC提供的学习平台 ...

  4. SQL学习(2):大厂真题实例-视频平均播放进度

    SQL学习(2):大厂真题实例-视频平均播放进度 题目来源:<牛客题霸:SQL大厂真题>: 01 某音短视频 SQL2 平均播放进度大于60%的视频类别 文章目录 SQL学习(2):大厂真 ...

  5. 《Java程序设计》第15周课堂实践总结

    <Java程序设计>第15周课堂实践总结 实践一 教材代码检查-p242 要求 在IDEA中或命令行中运行P242 StackTraceDemo2.java 代码运行结果和教材一致吗?为什 ...

  6. Python全栈工程师-第15周-韦玮-专题视频课程

    Python全栈工程师-第15周-382人已学习 课程介绍         Python全栈工程师-第15周 课程收益     Python全栈工程师 讲师介绍     韦玮 更多讲师课程     企 ...

  7. 带栩字的优美古诗句_带栩字的女孩名字,好听气质又富有内涵

    "栩栩芳心恋艳阳,宿迷春色独怜香"表示出一种诗意美好的春季景色.特别是其中的"栩"字非常的生动,寓意活泼开朗.因此很多父母在为女孩起名时,都会选择"栩 ...

  8. 2012年第15周国内Android应用下载排名

    2012年第15周国内Android应用下载应用榜共包括腾讯应用中心等10家第三方应用商店数据,榜单总结:1):手机QQ仍然表现最好,在安智市场和3G安卓市场均排名第一:2):Instagram上周首 ...

  9. 2019年浙江高考数学真题(填空题1-5题)解析答案

    本文作者:vxbomath 大家好,今天给大家分享关于2019年浙江高考数学真题(填空题1-5题). 好了,今天分享就到这里了,需要2019年浙江高考数学真题电子版和各知识点视频教程可以私信或者留言.

最新文章

  1. html中有csstext方法吗,style对象的cssText方法有哪些使用方法
  2. java young gc_java old GC和young GC
  3. 音视频解决方案之二次开发
  4. Nginx+Redis+Ehcache:大型高并发与高可用的三层缓存架构总结
  5. Python 黑魔法 --- 描述器(descriptor)
  6. eas 在linux下安装_linux下easy_install的安装与使用详解
  7. 《数据库SQL实战》获取所有非manager的员工emp_no
  8. 推荐系统实例-基于矩阵分解
  9. .git文件过大,如何清理
  10. 一文搞懂Matlab的3种取整函数(round、ceil、floor)
  11. 06 Halcon 点云平面度测量
  12. Xcode Undefined symbols 错误
  13. Mapbox简易入门教程
  14. C语言编程学习制作最好玩的报数游戏
  15. 【复现笔记】Iterative Corresponding Geometry
  16. chmod -R xxx 3位数字权限对照表
  17. Leetcode Proble 汇总四
  18. 【大学物理·恒定电流的磁场】毕奥-萨伐尔定律
  19. 物联网充电桩(电动自行车)管理方案
  20. 大厂offer?拿来吧你!网易有道笔试编程题特辑

热门文章

  1. ABBYY FineReader v15.0.110.1875破解版
  2. MAC 搭建vue开发环境,配置环境变量
  3. Ubuntu移动文件
  4. LCD屏有几种类型?
  5. ProGuard混淆代码
  6. 通讯管理系统c语言程序设计,C语言程序设计_通讯 录管理系统.doc
  7. msfconsole后渗透指令
  8. 【3d face reconstruction】综述阅读
  9. php制作聊天室,workerman+thinkphp制作简易聊天室
  10. MySQL安装以及客户端的使用