井字棋游戏课设高尚Word文档格式.docx

上传人:b****1 文档编号:1510738 上传时间:2023-04-30 格式:DOCX 页数:12 大小:59.74KB
下载 相关 举报
井字棋游戏课设高尚Word文档格式.docx_第1页
第1页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第2页
第2页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第3页
第3页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第4页
第4页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第5页
第5页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第6页
第6页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第7页
第7页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第8页
第8页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第9页
第9页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第10页
第10页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第11页
第11页 / 共12页
井字棋游戏课设高尚Word文档格式.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

井字棋游戏课设高尚Word文档格式.docx

《井字棋游戏课设高尚Word文档格式.docx》由会员分享,可在线阅读,更多相关《井字棋游戏课设高尚Word文档格式.docx(12页珍藏版)》请在冰点文库上搜索。

井字棋游戏课设高尚Word文档格式.docx

}

publicinterfaceUI{

PointgetUserInput();

voidreport(Strings);

voidprompt(Strings);

voidpromptChess(Pointp,intplayer);

}publicinterfaceMessage{

Stringprompt();

//游戏必须具备的操作,向外界返回游戏提示

Stringreport();

//游戏必须具备的操作,向外界返回游戏状态

4、该程序有三个类,一个Main,一个TwoPlayer(抽象类),一个TwoPlay(实例化)。

抽象类中的方法为:

voidsetPlayer(intstarter)//设置游戏开始的人

intgetPlayer()//得到游戏开始的一方

voidchangePlayer()//交换游戏双方

publicabstractStringgetRules();

//抽象方法,返回二人游戏的规则

publicabstractintisOver();

//抽象方法,游戏的结果

实例化中的方法为:

@Override

publicStringgetRules()判断游戏的状态

publicintisOver()启动并且控制游戏运行

publicvoidplay(UIui)控制游戏进行(覆盖)

publicStringprompt()游戏当前选手的提示信息

publicStringreport()游戏结果的提示信息

剩余的那一个类就是讲该程序以图形的形式表现出来。

类图如下:

具体的游戏代码如下所示:

packageTHeSecondClassDesign;

//启动游戏,并对游戏进行控制,并将显示对象传递给游戏,以便游戏利用显示对象来显示游戏的信息

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.Cursor;

importjava.awt.Font;

importjava.awt.GridLayout;

importjava.awt.Point;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JPanel;

publicclassMainextendsJFrameimplementsActionListener,UI{

privateJLabelreport=newJLabel("

"

JLabel.CENTER);

//显示游戏状态的标签

privateJLabelprompt=newJLabel("

//显示游戏提示信息的标签

privateJPanelpanel=newJPanel(newGridLayout(3,3,2,2));

privateJButton[][]btns=newJButton[3][3];

//井字棋棋盘,3行3列,由9个按钮组成

privatePointpoint=null;

//某个盘格子的行号和列号

privateFontfont=newFont("

隶书"

Font.PLAIN,15);

//文字的字体和字号

publicMain(){//构造方法

super("

井字棋"

);

panel.setBackground(Color.green);

getContentPane().add(report,BorderLayout.SOUTH);

report.setFont(font);

getContentPane().add(prompt,BorderLayout.NORTH);

prompt.setFont(font);

prompt.setOpaque(true);

//prompt.setBackground(Color.yellow);

//放9个按钮,3行3列,组成棋盘,用来显示棋子,并且让选手选择棋盘格子

for(inti=0;

i<

btns.length;

i++){

for(intj=0;

j<

btns[i].length;

j++){

btns[i][j]=newJButton();

btns[i][j].addActionListener((ActionListener)this);

//为按钮添加事件处理

btns[i][j].setCursor(newCursor(Cursor.HAND_CURSOR));

btns[i][j].setFont(font);

panel.add(btns[i][j]);

}

}

getContentPane().add(panel);

setSize(3*60,3*60+60);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

//处理事件,获取用户所点棋盘格子的坐标

publicvoidactionPerformed(ActionEvente){

Objectobj=e.getSource();

for(inti=0;

3;

i++)

for(intj=0;

j++)

if(obj==btns[i][j]){

point=newPoint(i,j);

return;

//利用二重循环,确定选手所选按钮(每个按钮代表一个棋盘格子)

//的位置(行列号),保存到point对象中。

//主方法

publicstaticvoidmain(String[]args){

UIui=newMain();

//创建游戏界面对象

//创建游戏对象

Controlgame=newTwoPlay();

//向游戏对象发消息,开始游戏

game.play(ui);

publicPointgetUserInput(){

while(point==null);

Pointp=point;

returnp;

publicvoidreport(Strings){

report.setText(s);

publicvoidprompt(Strings){

prompt.setText(s);

publicvoidpromptChess(Pointp,intplayer){

if(player==TwoPlayer.PLAYER_ONE)

btns[p.x][p.y].setForeground(Color.blue);

if(player==TwoPlayer.PLAYER_TWO)

btns[p.x][p.y].setForeground(Color.green);

btns[p.x][p.y].setText("

X"

publicinterfaceMessage{

publicclassTwoPlayextendsTwoPlayer{

publicstaticfinalintROWS=3;

//棋盘行数

publicstaticfinalintCOLS=3;

//棋盘列数

privateint[][]chessboard=newint[ROWS][COLS];

//井字棋盘,是二维数组,0表示没有棋子,当一个选手下了一个棋子后,将该选手的编号填在棋盘相应的元素中。

privateintchessCount=0;

//棋子数量

publicStringgetRules(){

return"

一人一步,先连3子为赢"

;

//判断游戏的状态

publicintisOver(){

if(chessboard[0][0]==chessboard[0][1]&

&

chessboard[0][1]==chessboard[0][2]&

chessboard[0][0]!

=0)

returnWINNER;

//有人获得胜利

if(chessboard[1][0]==chessboard[1][1]&

chessboard[1][1]==chessboard[1][2]&

chessboard[1][0]!

//有人获得胜利

if(chessboard[2][0]==chessboard[2][1]&

chessboard[2][1]==chessboard[2][2]&

chessboard[2][0]!

if(chessboard[0][0]==chessboard[1][0]&

chessboard[1][0]==chessboard[2][0]&

if(chessboard[0][1]==chessboard[1][1]&

chessboard[1][1]==chessboard[2][1]&

chessboard[0][1]!

if(chessboard[0][2]==chessboard[1][2]&

chessboard[1][2]==chessboard[2][2]&

chessboard[0][2]!

if(chessboard[0][0]==chessboard[1][1]&

chessboard[1][1]==chessboard[2][2]&

if(chessboard[0][2]==chessboard[1][1]&

chessboard[1][1]==chessboard[2][0]&

if(chessCount>

=ROWS*COLS)returnGAME_OVER;

returnNO_WINNER;

//启动并且控制游戏运行

publicvoidplay(UIui){

ui.report(getRules());

//在界面的状态栏显示游戏规则

while(isOver()==NO_WINNER){

ui.prompt(prompt());

//在界面的提示栏显示游戏提示信息

Pointpoint=ui.getUserInput();

////从界面得到要下棋子的行号和列号

if(chessboard[point.x][point.y]==0){

chessboard[point.x][point.y]=getPlayer();

//棋子放入棋盘

chessCount++;

//棋子数量加1

ui.promptChess(point,getPlayer());

//通知界面把这个棋子画出来

changePlayer();

//切换另外一个棋手

}else{

ui.prompt("

不能再有棋子的格再下棋,重下"

欢迎再来"

ui.report(report());

//在界面的状态栏显示游戏状态

publicStringprompt(){

现在该【"

+getPlayer()+"

】号选手下棋"

publicStringreport(){

intstate=isOver();

if(state==WINNER){

【"

】号选手输"

}elseif(state==GAME_OVER){

平局"

继续下棋"

publicabstractclassTwoPlayerimplementsControl{

publicstaticfinalintWINNER=0;

//常量,有获胜者,游戏结束

publicstaticfinalintNO_WINNER=1;

//常量,无获胜者,游戏未结束

publicstaticfinalintGAME_OVER=2;

//常量,无获胜者,游戏结束

publicstaticfinalintPLAYER_ONE=1;

//常量,第1个选手

publicstaticfinalintPLAYER_TWO=2;

//常量,第2个选手

privateintonePlaysNext=PLAYER_ONE;

//默认第1位选手操作先操作

publicvoidsetPlayer(intstarter){//指定当前操作者

onePlaysNext=starter;

publicintgetPlayer(){//返回当前操作者

returnonePlaysNext;

publicvoidchangePlayer(){//改变当前操作的选手

if(onePlaysNext==PLAYER_ONE)

onePlaysNext=PLAYER_TWO;

//第2名选手操作

else

onePlaysNext=PLAYER_ONE;

//第1名选手操作

//抽象方法,返回二人游戏是否结束

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2