搜档网
当前位置:搜档网 › 五子棋游戏源代码

五子棋游戏源代码

五子棋游戏源代码
五子棋游戏源代码

JAVA期末课题实践考察--五子棋游戏设计

import java.awt.Color;

import java.awt.Container;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JRadioButtonMenuItem;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

/*

*main方法创建了ChessFrame类的一个实例对象(cf),

*并启动屏幕显示显示该实例对象。

**/

public class FiveChessAppletDemo {

public static void main(String args[]){

ChessFrame cf = new ChessFrame();

cf.setVisible(true);

}

}

/*

*类ChessFrame主要功能是创建五子棋游戏主窗体和菜单

**/

class ChessFrame extends JFrame implements ActionListener {

/**

*

*/

private static final long serialVersionUID = 2183726320279905885L; private String[] strsize={"20x15","30x20","40x30"};

private String[] strmode={"人机对弈","人人对弈"};

public static boolean iscomputer=true;

public static boolean 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(20,15);

JMenuBar mbar = new JMenuBar();

this.setJMenuBar(mbar);

JMenu gameMenu = new JMenu("游戏");

mbar.add(makeMenu(gameMenu, new Object[] {

"开局", "棋盘","模式", null, "退出"

}, this));

JMenu lookMenu =new JMenu("视图");

mbar.add(makeMenu(lookMenu,new Object[] { "Metal","Motif","Windows"

},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

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

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 * 20+50 , h * 20+100 );

if(!ChessFrame.checkcomputer) {

ChessFrame.iscomputer=false;

} else {

ChessFrame.iscomputer=true;

}

mp.setModel(cm);

mp.repaint();

}

public boolean getiscomputer(){

return ChessFrame.iscomputer;

}

public void restart(){

int modeChess = cm.getModeChess();

if(modeChess <= 3 && modeChess >= 1){

cm = new ChessModel(modeChess);

MapSize(cm.getWidth(),cm.getHeight());

}else{

System.out.println("\u81EA\u5B9A\u4E49");

}

}

public void actionPerformed(ActionEvent e){

String arg=e.getActionCommand();

try{

if (arg.equals("Windows"))

UIManager.setLookAndFeel(

"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); else if(arg.equals("Motif"))

UIManager.setLookAndFeel(

"com.sun.java.swing.plaf.motif.MotifLookAndFeel"); else

UIManager.setLookAndFeel(

"javax.swing.plaf.metal.MetalLookAndFeel" ); SwingUtilities.updateComponentTreeUI(this);

}catch(Exception ee){}

if(arg.equals("20x15")){

this.width=20;

this.height=15;

cm=new ChessModel(1);

MapSize(this.width,this.height);

SwingUtilities.updateComponentTreeUI(this);

}

if(arg.equals("30x20")){

this.width=30;

this.height=20;

cm=new ChessModel(2);

MapSize(this.width,this.height);

SwingUtilities.updateComponentTreeUI(this);

}

if(arg.equals("40x30")){

this.width=40;

this.height=30;

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(this, "五子棋游戏测试版本", "关于", 0); if(arg.equals("退出"))

System.exit(0);

}

}

/*

*类ChessModel实现了整个五子棋程序算法的核心

*/

class ChessModel {

//棋盘的宽度、高度、棋盘的模式(如20×15)

private int width,height,modeChess;

//棋盘方格的横向、纵向坐标

private int x=0,y=0;

//棋盘方格的横向、纵向坐标所对应的棋子颜色,

//数组arrMapShow只有3个值:1,2,3,-5,

//其中1代表该棋盘方格上下的棋子为黑子,

//2代表该棋盘方格上下的棋子为白子,

//3代表为该棋盘方格上没有棋子,

//-5代表该棋盘方格不能够下棋子

private int[][] arrMapShow;

//交换棋手的标识,棋盘方格上是否有棋子的标识符

private boolean isOdd,isExist;

public ChessModel() {}

//该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘

public ChessModel(int modeChess){

this.isOdd=true;

if(modeChess == 1){

PanelInit(20, 15, modeChess);

}

if(modeChess == 2){

PanelInit(30, 20, modeChess);

}

if(modeChess == 3){

PanelInit(40, 30, 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] = -5;

}

}

}

//获取是否交换棋手的标识符

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;

//judge right

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;

}

//judge left

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;

//judge up

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;

}

//judge down

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;

//judge left_up

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;

}

//judge right_down

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;

//judge right_up

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;

}

//judge left_down

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;

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)

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,"你赢了,好厉害!","win", https://www.sodocs.net/doc/6a11581938.html,RMATION_MESSAGE);

}

//输棋后的提示

public void showDefeat(JPanel jp){

JOptionPane.showMessageDialog(jp,"你输了,请重新开始!","lost", https://www.sodocs.net/doc/6a11581938.html,RMATION_MESSAGE);

}

}

/*

*类MainPanel主要完成如下功能:

*1、构建一个面板,在该面板上画上棋盘;

*2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)**/

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

}

//根据坐标计算出棋盘方格棋子的信息(如白子还是黑子),//然后调用draw方法在棋盘上画出相应的棋子

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.white);

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;

// System.out.println(evt.getX());

// System.out.println(evt.getX());

int y = (evt.getY()-10) / 20;

// System.out.println(evt.getY());

// System.out.println(evt.getY());

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

https://www.sodocs.net/doc/6a11581938.html,puterDo(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();

}

}

class ChessWindowEvent extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);

}

ChessWindowEvent(){}

}

Java五子棋游戏源代码(人机对战)

//Java编程:五子棋游戏源代码 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; /* *main方法创建了ChessFrame类的一个实例对象(cf), *并启动屏幕显示显示该实例对象。 **/ public class FiveChessAppletDemo { public static void main(String args[]){ ChessFrame cf = new ChessFrame(); cf.show(); } } /* *类ChessFrame主要功能是创建五子棋游戏主窗体和菜单**/ class ChessFrame extends JFrame implements ActionListener { private String[] strsize={"20x15","30x20","40x30"}; 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(20,15); JMenuBar mbar = new JMenuBar(); this.setJMenuBar(mbar); JMenu gameMenu = new JMenu("游戏");

基于Java五子棋游戏的设计源代码及全套资料

分类号: U D C:D10621-408-(2007)5738-0 密级:公开编号:21 成都信息工程学院 学位论文 基于Java的五子棋游戏的设计 论文作者姓名:赵小龙 申请学位类别:计算机科学与技术 申请学位类别:工学学士 指导教师姓名(职称):吴春旺 论文提交日期:2007年06月10日

基于Java的五子棋游戏的设计 摘要 五子棋作为一个棋类竞技运动,在民间十分流行,为了熟悉五子棋规则及技巧,以及研究简单的人工智能,决定用Java开发五子棋游戏。主要完成了人机对战和玩家之间联网对战2个功能。网络连接部分为Socket编程应用,客户端和服务器端的交互用Class Message定义,有很好的可扩展性,客户端负责界面维护和收集用户输入的信息,及错误处理。服务器维护在线用户的基本信息和任意两个对战用户的棋盘信息,动态维护用户列表。在人机对弈中通过深度搜索和估值模块,来提高电脑棋手的智能。分析估值模块中的影响精准性的几个要素,以及提出若干提高精准性的办法,以及对它们搜索的节点数进行比较,在这些算法的基础上分析一些提高电脑AI方案,如递归算法、电脑学习等。算法的研究有助于理解程序结构,增强逻辑思维能力,在其他人工智能方面也有很大的参考作用。 关键词:深度搜索;估值;电脑AI;五子棋;算法

Gobang Java-based games design Abstract As a sport, gobang is very popular in civil, in order to become familiar with gobang rules and techniques, and the study of simple artificial intelligence, I decide to use the Java to develope gobang games and complete the two functions including man-machine war and man-man war. Network Connection is Socket Programming for some applications, client and server interaction is definited by Class Message, which is a very good scalability, Client interface is responsible for the collection and maintenance of user input information, and error handling. Server users maintain online basic information and arbitrary two-time users of the chessboard of information, dynamic maintenance user list. During the man-machine players, it improves intelligence of the computer players through depth search and valuation module. Analyzes Module valuation of the precise elements, as well as a number of increased precision, and compares their search for nodes, which raises some computer AI programs on the basis of analysis, such as recursive algorithm, computer learning. Algorithm of procedures contribute to the understanding of the structure, logical thinking ability, In other areas of artificial intelligence has great references. . Key words:Search depth; Valuation; Computer AI; Gobang ; Algorithm

五子棋源代码(C++),完美版

#include #include using namespace std; #define WIDE_AND_LONG 20 //棋盘的长和宽 #define NAME_LEN 20 //输入姓名的长度 typedef class Gobang { public: int InitPlayerName(char *, char *); int CheckInput(char,int); int CheckIndexInput(char, char, char); int GetPlayerName(); int InitBoard(); int WriteBoard(char, char, bool); int BeginOrNot(); int CheckRow(); int CheckColumn(); int CheckTopLeft(); int CheckTopRight(); int CheckDownLeft(); int CheckDownRight(); int CheckDraw(); int CheckFinish(); int Chess(); int ShowBoard(); private: char acBoard[WIDE_AND_LONG][WIDE_AND_LONG]; char acPlayerOneName[NAME_LEN]; char acPlayerTwoName[NAME_LEN]; }GOBANG; //初始化姓名的缺省值 int GOBANG::InitPlayerName(char *pPlayerOne,char *pPlayerTwo) { strcpy(acPlayerOneName,pPlayerOne); strcpy(acPlayerTwoName,pPlayerTwo); return 0; } //检查输入姓名时是否含非法字符空格和Tab键 int GOBANG::CheckInput(char ch, int iNameLen) { if(' ' == ch || '\t' == ch) { cout<<"含有非法字符!"<

五子棋游戏代码(Java语言)

五子棋游戏代码(Java语言) import java.awt.*; import java.awt.event.*; import javax.swing.*; class mypanel extends Panel implements MouseListener { int chess[][] = new int[11][11]; boolean Is_Black_True; mypanel() { Is_Black_True=true; for(int i=0;i<11;i++) { for(int j=0;j<11;j++) { chess[i][j] = 0; } } addMouseListener(this); setBackground(Color.RED); setBounds(0, 0, 360, 360); setVisible(true); } public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); if(x < 25 || x > 330 + 25 ||y < 25 || y > 330+25) {return;} if(chess[x/30-1][y/30-1] != 0) {return;} if(Is_Black_True==true) { chess[x/30-1][y/30-1] = 1; Is_Black_True=false; repaint(); Justisewiner(); return; }

if(Is_Black_True==false) { chess[x/30-1][y/30-1]=2; Is_Black_True=true; repaint(); Justisewiner(); return; } } void Drawline(Graphics g) { for(int i=30;i<=330;i+=30) { for(int j = 30;j <= 330; j+= 30) { g.setColor(Color.GREEN); g.drawLine(i, j, i, 330); } } for(int j = 30;j <= 330;j+=30) { g.setColor(Color.GREEN); g.drawLine(30, j, 330, j); } } void Drawchess(Graphics g) { for(int i=0;i < 11;i++) { for(int j = 0;j < 11;j++) { if(chess[i][j] == 1) { g.setColor(Color.BLACK); g.fillOval((i+1)*30-8, (j+1)*30-8, 16, 16); } if(chess[i][j]==2) { g.setColor(Color.WHITE); g.fillOval((i+1)*30-8, (j + 1) * 30-8, 16, 16); }

Python-大作业之五子棋游戏(附代码)

Python 大作业——五子棋游戏 姓名:学号: 姓名:学号: 一游戏介绍: 我们设计的是五子棋游戏,支持两人一个鼠标对下,黑方用左键单击,白方用右键单击,谁先下均可,落子无悔,下过的棋子对方点击后不会变色,程序可自行判断输赢并在五子连珠时弹出结果对话框,游戏双方需遵守不在空地点击和一次下一子的规则。 二游戏代码设计: 代码均为原创,没有借鉴和抄袭,首先是用户GUI界面设计,点击start进入游戏界面,点击quit则退出程序,为了方便判断和记录,我们按从左到右,从上到下的顺序给15x15=225颗棋子编号225,左键绑定函数callback1,点击后可算出它位于哪颗棋子上再画出来黑子,并把对应编号计入record这个列表,之后进入判断函数。右键绑定函数callback2,点击后画出白子,对应编号计入recor这个列表,之后进入判断函数,其中总列表rec的作用是使棋子不被下第二遍。 三作业感想 这个游戏虽然很小但是可以供室友们晚上娱乐之用,我们倾注了很多心血,之前采用模块化编程失败了很多次,有事件响应问题,参数传递问题,到第七个程序才成功,感谢张同珍老师指点了很多,

我们学会了使用类,受益匪浅,对Python产生了浓厚的兴趣。四过程截图 五、实验代码 from Tkinter import * from tkMessageBox import * class Game: def __init__(self): self.A=[] self.B=[] self.record=set()

self.recor=set() self.rec=self.record|self.recor self.root=Tk() self.root.geometry("180x250") self.root.title("Wu Zi Qi Game") self.r=Canvas(self.root,width=180,height=210,bg="purple") pic=PhotoImage(file="beijing.gif") self.r.create_image(90,100,image=pic) self.r.place(x=0,y=15) Label(self.root,text="***Wu Zi Qi Game***",fg="red").place(x=20,y=0) Button(self.root,text="start",command=self.start).place(x=30,y=230) Button(self.root,text="quit ",command=self.root.destroy).place(x=100,y=230) self.r.mainloop() def start(self): self.root.destroy() self.top=Tk() self.top.title("Game Start") self.c=Canvas(self.top,width=480,height=480,bg="white") self.c.pack() self.c.create_rectangle(25,25,455,455,fill="gray") for i in range(30,451,30): for j in range(30,451,30): self.c.create_oval(i-2,j-2,i+2,j+2,fill="blue") for i in range(1,16): self.c.create_line(30,30*i,450,30*i) self.c.create_line(30*i,30,30*i,450) self.c.create_oval(234,234,246,246,fill="black") self.c.create_oval(115,115,125,125,fill="black") self.c.create_oval(355,115,365,125,fill="black") self.c.create_oval(115,355,125,365,fill="black") self.c.create_oval(355,355,365,365,fill="black") self.c.bind("",self.callback1) self.c.bind("",self.callback2) self.c.mainloop() def callback1(self,event): u,v=event.x,event.y s=u/15 if s%2==1: self.x=(s+1)/2 else: self.x=s/2

C语言五子棋游戏源代码

ncl ud e< #define N 10 void welcome(); void initqipan(); void showqi(int i); void save(int p); void panduan(int p); void heqi(); void over(); int zouqihang(); int zouqilie(); /****************** 结构体**************** */ struct zuobiao { int x[N*N]; int y[N*N]; }weizhi[N *N]; 主函数**************** */ /****************** void main() { int p=0; welcome();

for(p=1;p<=N*N;p++) { weizhi[p].x[p]=zouqihang(); weizhi[p].y[p]=zouqilie(); save(p); showqi(p); panduan(p); } if(p==N*N) heqi(); over(); } /****************** 建立棋盘*****************/ void initqipan() { int i,j; for(i=0;i

for(i=1;i

基于JAVA的五子棋游戏系统设计与实现

基于JA V A的五子棋游戏系统设计与实现专业电子信息工程 学生董永杰 指导教师曾玉

摘要 目前,随着计算机网络的的发展,以计算机技术和网络技术为核心的现代网络技术已经在现实生活和生产中得到了广泛的使用,已经成为多数人群的休闲方式,也为多数人所喜好。当然,为了满足没有网络同样能娱乐的要求,许多小游戏做成了单机和网络的双功能。 本软件使用JAVA语户端之间的连接,利用多线程技术言实现,通过对图形界面,绘图,布局管理器等去构造出游戏的单机功能,在此基础上,利用SCOKET 的知识,建立起服务器与客来处理服务器端与客户端之间的数据传输,通信问题,使得客户端和服务器端之间能够同步的进行处理。 通过对软件的编写,更深入的理解了面向对象的概念,也体会到利用面向对象语言处理一些问题的优势。同时也加深了对多线程,流套接字等高级技术的理解。 关键词:多线程;流套接字;数据传输;同步。

ABSTRACT ABSTRACT At present, With the rapid development of computer network. Taking computer technology and the network technology as the core, modern network technology is already used in the real life and the production and already became the leisure mode of the most peo ple. And most people like them. Of course, it’s a pity that there still have some clients lacking of network because of various causes. In order to satisfy the above clients’ requirements. A large number of games ,usually named as “small games” by players, are designed for involving two kinds of different function. The former game is often played by these players whose computers never connect with the network. It’s called for stand-alone version games. Just as its name implies, the later is named as online version games This software implemented with JAVA language, and according to the understanding of SCOKET ,GUI and paint image ichnology. Established in these foundation , the server co ects with the multi- client, and transmission the information between many clients using the multi-thread proceeding technology. it is very convenient for both client and server to do the synchronous processing. Through to the software compilation, deepen understanding and grasp to the technology above understanding and holding. Key Words: multiple thread, Socket, transmission-data, synchronism.

五子棋程序代码

/************************************* ** 程序名称:五子棋 ** ** 编译环境:vs2012 ** ** 编辑作者:往事随风<1034882113> ** ** 最后修改:2013-07-25 ** ** 项目类型:win32控制台程序 ** **************************************/ #include #include// _kbhit() _getch() /****************** 宏定义区 **********************/ #define BOARD_LEN 640 // 棋盘大小 #define BOARD_WIDTH 640 #define EXTRA_LEN 200 // 右边提示区域的长度 #define SIZE 40 // 棋盘小方格大小 #define ROW 14 // 棋盘为14*14 /****************** 数据类型定义区******************/ /****************** 全局变量区**********************/ IMAGE img_chessboard; // 背景 IMAGE img_box_black; // 黑色棋盒 IMAGE img_box_white; // 白色棋盒 IMAGE img_bkbox; // 右边区域背景 IMAGE img_time; // 显示时间 IMAGE img_count[11]; // 十个数字和一个冒号 int m_x = (BOARD_LEN - SIZE * ROW)/2; // 居中 int m_y = (BOARD_WIDTH - SIZE * ROW)/2; byte gobang[ROW+1][ROW+1] = {0};// byte不能表示负数0:无子1:黑子2:白子byte type = 0; // 1: 白棋0:黑棋 bool replay = true; // false 游戏结束,true代表游戏运行中 DWORD b_oldtime; // 黑棋花费时间 DWORD w_oldtime; // 白棋花费时间 DWORD start_time; // 游戏起始时间 /****************** 函数声明区**********************/ void init_system(); void init_img(); void init_chessboard(); void getMouse(POINT *point); void deal_mousemsg(POINT point); void game_win(int x,int y); void quit_game();

五子棋(JAVA版)实习报告及原代码

实习报告 课程名称信息系统认知实习实习题目java五子棋专业 班级 学号 学生姓名 实习成绩 指导教师 2010年1月 前言

摘要 五子棋作为一个棋类竞技运动,在民间十分流行,为了熟悉五子棋规则及技巧,以及研究简单的人工智能,决定用Java开发五子棋游戏。主要完成了人机对战和玩家之间联网对战2个功能。网络连接部分为Socket编程应用,客户端和服务器端的交互用Class Message定义,有很好的可扩展性,客户端负责界面维护和收集用户输入的信息,及错误处理。服务器维护在线用户的基本信息和任意两个对战用户的棋盘信息,动态维护用户列表。在人机对弈中通过深度搜索和估值模块,来提高电脑棋手的智能。分析估值模块中的影响精准性的几个要素,以及提出若干提高精准性的办法,以及对它们搜索的节点数进行比较,在这些算法的基础上分析一些提高电脑AI方案,如递归算法、电脑学习等。算法的研究有助于理解程序结构,增强逻辑思维能力,在其他人工智能方面也有很大的参考作用。 1引言 1.1课题背景 五子棋是起源于中国古代的传统黑白棋种之一。现代五子棋日文称之为连珠,英译为Renju,英文称之为Gobang或FIR(Five in a Row 的缩写),亦有连五子、五子连、串珠、五目、五目碰、五格等多种称谓。 五子棋起源于古代中国,发展于日本,风靡于欧洲。对于它与围棋的关系有两种说法,一说早于围棋,早在“尧造围棋”之前,民间就已有五子棋游戏;一说源于围棋,是围棋发展的一个分支。在中国的文化里,倍受人们的青睐。本世纪初五子棋传入欧洲并迅速风靡全欧。通过一系列的变化,使五子棋这一简单的游戏复杂化、规范化,而最终成为今天的职业连珠五子棋,同时也成为一种国际 比赛棋。 Java语言是当今最为流行的程序设计语言之一作为一门非常优秀和极为健壮的编程语言,它同时具有的面向对象,与平台无关,分布式应用,安全,稳定和多线程等优良的特征,使用Java语言,不仅可以开发出功能强大的大型应用程序,而且Java语言本身突出的跨平台的特性也使得它特别适合于Internet上的应用开发,可以这样说,Java的出现使得所开发的应用程序“一次编写,处处可用”的 实现成为了可能。 1.2本课题研究的意义 近来随着计算机的快速发展,各种各样的电脑游戏层出不穷,使得我们能有更多的娱乐项目,而棋类游戏能起到锻炼人的思维和修身养性的作用,而且棋类游戏水平颇高,大有与人脑分庭抗礼之势。其中战胜过国际象棋世界冠军-卡斯帕罗夫的“深蓝”便是最具说服力的代表;其它像围棋的“手淡”、象棋的“将族”

java五子棋游戏软件设计报告

佛山科学技术学院 《可视化编程技术》课程设计报告 五子棋软件设计 学生姓名:凌健铭 学号:2011924133 年级专业:11级教育技术学2班 指导老师:容汝佳 学院:教育科学学院 广东★佛山 提交日期:2013年6月

目录 1. 前言 (2) 2.概要设计 (3) 2.1 开发环境 (3) 2.2 五子棋功能 (3) 2.3 界面设计 (3) 2.4 类的框架结构图 (4) 3. 详细设计 (5) 3.1 五子棋使用的JAVA类的说明 (5) 3.2 类的主要方法 (6) 3.2.1下放棋子 (6) 3.2.2 判断输赢 (7) 3.2.3 重新开始 (10) 3.2.4 悔棋 (10) 3.2.5 程序流程图 (11) 4. 运行结果 (12) 5. 测试分析 (15) 6. 源程序 (15) 参考文献 (26) 设计总结 (26)

摘要:该程序是一个图形界面的简单的java五子棋游戏,具有良好的界面,使用人员能快捷简单地进行操作。人们可以在空闲时使用该程序进行五子棋对战,达到娱乐休闲的目的。该五子棋程序设有悔棋、重新开始和退出功能。界面为黄色棋盘。 关键字:java五子棋游戏软件,娱乐类电子游戏设计 1 前言 五子棋作为一个棋类竞技活动,其满足了人民娱乐休闲的需要,在民间十分流行。 Java是由Sun公司开发的新一代纯面向对象的网络编程语言。其目标是建 立一种在任意种机器、任一种操作系统的网络环境中运行的软件,实行所谓的“程序写一次,到处运行”的目标。正因为如此,Java已成为当今Internet上最流行、最受欢迎的一种程序开发语言。 Java开发小组把Java按特性分为基本版、移动版、企业版,每个版本有一个软件开发包。Java基本版本叫Java 2标准版(Java 2 Standard Edition,J2SE),它包含建立Java应用程序或者是Applet所需的应用程序编程接口(API)。Java 2移动版(The Java 2 Mobile Edition,J2ME)包含创建无线Java应用程序的API。还有Java 2企业版(The Java 2 Enterprise,J2EE)是J2SE的增强版本,包含建立多层架构应用程序API。 Java语言是由C++语言发展起而来的,是一种彻底的面向对象的程序设计语言。作为一种纯面向对象的程序设计语言,它非常适合大型软件的开发。Java 语言去掉了C++语言的一些容易引起错误的特性。Java语言的特点有:面向对象、跨平台、安全性、多线程和图形功能强。 2 概要设计 2.1开发环境 开发平台:Microsoft Windows XP Professional Service Pack 2

课程设计-c语言设计-五子棋游戏 0528

课程设计-c语言设计-五子棋游戏

河南城建学院 测绘与城市空间信息系 测绘程序设计 题目: 五子棋游戏 班级: 0614112 人数: 3人 成员: 学号: 指导老师: 时间:2012年6月

目录 1课程设计报告-------------------2 1.1问题描述----------------------2 1.2 任务分工- - - - - - - - - - - - - - - 2 1.3需求分析---------------------------3 1.4概要设计-----------------------3 1.5详细设计-----------------------4 1.6调试分析---------------------5 2源程序---------------------6 3程序的说明文件-------------------12 4课设总结-----------------------13

1.课程设计报告 1.1问题描述 连珠(五子棋)是有两个人在一盘棋上进行对抗的竞技运动。在对局开始时,先由用户选择哪方先开局,先开局一方将一枚棋子落在一点上,然后由另一方在对方棋周围的交叉点上落子,如此轮流落子,直到某一方首先在棋盘的直线、横线或斜线上形成连续的五子则该方就算获胜。此时,算法结束。当有任何一方想退出时,都可在算法中实现。 1.2 五子棋的背景 传统五子棋的棋具与围棋相同,棋子分为黑白两色,棋盘为15×15,棋子放置于棋盘线交叉点上。两人对局,各执一色,轮流下一子,先将横、竖或斜线的5个或5个以上同色棋子连成不间断的一排者为胜。 因为传统五子棋在落子后不能移动或拿掉,所以也可以用纸和笔来进行游戏。 1.2 任务分工 组长:赵哲武 负责小组程序的输入和创新部分,分配任务,使工作衔接有序,以

java五子棋小游戏实验报告(附源代码)

手机五子棋游戏的设计与实 现 专业: 姓名: 班级: 学号: 指导教师:

J2ME(Java 2 Micro Edition)是近年来随着各种不同设备,尤其是移动通信设备的飞速发展而诞生的一项开发技术。它因其“write once,run anywhere”的Java特性而提高了开发的效率。随着手机性能的不断提高,手机休闲娱乐应用将成为PC休闲娱乐应用之后又一重要业务增长点。棋类游戏规则单一,比较适合在手机等便携终端推广。 由于具有跨平台、易于移植、占用空间小的优势,J2ME成为移动应用开发平台的主流,并提供了很多用以支持移动应用软件的开发的API。现将该技术用于这次的手机游戏开发,可以实现游戏的快速开发,不但便于查看游戏运行过程中内存的占用量和程序的每一部分代码消耗了多少处理器时间,而且可以不断地优化代码,使代码具有高度的复用性、可扩展性、可维护性。 游戏的开发以J2ME为平台,利用Java技术,结合J2ME的MIDP技术,并对于程序设计思想,重要类、方法等展开讨论。在对弈部分,分析设计走棋算法,选择合适的方式组织成代码,实现基本的人工智能。过程中使用了J2ME中的CLDC/MIDP软件体系,主要运用了MID Profile的特定类的支持,来完成游戏的开发。 关键词:J2ME;CLDC;MIDP

J2ME is a kind of fast developing technology implemented on various devices especially mobile communication equipments. It improves the efficiency of the development process because of its "write once, run anywhere" nature. The development trend of the entertainment market based on the cell phone is very obvious because the handset performance enhances unceasingly. The entertainment market based on the cell phone will to be the new important business growth point follow the PC entertainment market. As the rules of a single chess game, it is more suitable for mobile phones and other portable terminal extension. J2ME has been the preferred platform for development because of its platform independent and compatibility, and provides a lot of APIs to support the development of mobile application software. The technology for mobile game development, can achieve the rapid development of the game. It is not only easy to observe the memory consumption and processor consumed time during the operation of the game, but also can optimize the code, so that the code has a high degree of reusability, scalability, maintainability. The game has designed by J2ME, the Java technology and the MIDP technology. I studied the procedure thought, the important class and the method. In the playing chess part, I have analyzed the algorithm, choosed the appropriate way to organize the code and realized the basic artificial intelligence. On the other hand,I learned software system of CLDC/MIDP and the specific class of the MID Profile to complete the game development. Key words: J2ME;CLDC;MIDP

五子棋C语言代码

#include "graphics.h" /*图形系统头文件*/ #define LEFT 0x4b00 /*光标左键值*/ #define RIGHT 0x4d00 /*光标右键值*/ #define DOWN 0x5000 /*光标下键值*/ #define UP 0x4800 /*光标上键值*/ #define ESC 0x011b /* ESC键值*/ #define ENTER 0x1c0d /* 回车键值*/ int a[8][8]={0},key,score1,score2;/*具体分数以及按键与存放棋子的变量*/ char playone[3],playtwo[3];/*两个人的得分转换成字符串输出*/ void playtoplay(void);/*人人对战函数*/ void DrawQp(void);/*画棋盘函数*/ void SetPlayColor(int x);/*设置棋子第一次的颜色*/ void MoveColor(int x,int y);/*恢复原来棋盘状态*/ int QpChange(int x,int y,int z);/*判断棋盘的变化*/ void DoScore(void);/*处理分数*/ void PrintScore(int n);/*输出成绩*/ void playWin(void);/*输出胜利者信息*/ /******主函数*********/ void main(void) { int gd=DETECT,gr; initgraph(&gd,&gr,"c:\\tc"); /*初始化图形系统*/ DrawQp();/*画棋盘*/ playtoplay();/*人人对战*/ getch(); closegraph();/*关闭图形系统*/ } void DrawQp()/*画棋盘*/ { int i,j; score1=score2=0;/*棋手一开始得分都为0*/ setbkcolor(BLUE); for(i=100;i<=420;i+=40) { line(100,i,420,i);/*画水平线*/ line(i,100,i,420); /*画垂直线*/ } setcolor(0);/*取消圆周围的一圈东西*/ setfillstyle(SOLID_FILL,15);/*白色实体填充模式*/ fillellipse(500,200,15,15); /*在显示得分的位置画棋*/ setfillstyle(SOLID_FILL,8); /*黑色实体填充模式*/ fillellipse(500,300,15,15); a[3][3]=a[4][4]=1;/*初始两个黑棋*/ a[3][4]=a[4][3]=2;/*初始两个白棋*/

五子棋-Java课程设计

《面向对象程序设计》 课程设计报告 实验时间:2010年10月26日 实验班级:********************** 实验报告总份(片)数: 1 份(片) 实验指导老师:***** ******* 设计小组 湖南省吉首市吉首大学 课程设计报告 简单的游戏——五子棋 小组成员(姓名、学号): **(组长)** ** ** ** ** 一、实验分工

二、开发环境(实验编译以及测试环境) 硬件环境: CPU:Intel 奔腾双核E5200 主频2.5GHz 内存:2G 软件环境: 操作系统:Windows 7 编程环境JDK7.0 开发工具:Eclipse SDK 三、使用环境(用户运行环境) 硬件环境: CPU主频在500MHZ以上,内存在128M以上 软件环境: JAVA运行环境+ Windows XP或Windows 2000 以上操作系统 目录 第一章总体设计.............................................................................................................................. 1 1.1设计的目的.......................................................................................................................... 1 1.2本系统的主要功能.............................................................................................................. 1 1.3系统包含的类及类之间的关系。...................................................................................... 1 1.4 Java源文件及其功能......................................................................................................... 2 1.5 项目构建思路..................................................................................................................... 2第二章模块功能介绍.................................................................................................................. 12 2.1主类Chess...................................................................................................................... 12

相关主题