游戏结束源代码

包com.fiveChess

导入Android . app . activity;

导入Android . OS . bundle;

导入Android . view . display;

导入Android . view . menu;

导入Android . view . menuitem;

导入Android . view . window;

导入Android . view . window manager;

公共类MainActivity扩展Activity {

GameView gameView = null

@覆盖

public void on create(Bundle saved instancestate){

super . oncreate(savedInstanceState);

this.getWindow()。requestFeature(窗口。特征_编号_标题);

this.getWindow()。setFlags(WindowManager。LayoutParams.FLAG_FULLSCREEN,WindowManager。layout params . FLAG _ full screen);

display display = this . getwindowmanager()。getDefaultDisplay();

gameView = new GameView(this,display.getWidth()、display . getheight());

setContentView(游戏视图);

}

@覆盖

public boolean onCreateOptionsMenu(菜单菜单){

Menu.add ("restart ")。seticon(Android . r . drawable . IC _ menu _ my places);

menu . add(" exit ");

return super . oncreateoptionsmenu(菜单);

}

@覆盖

public boolean onoptions item selected(菜单项){

If(item.getTitle()。等于(“重新启动”){

game view . can play = true;

game view . chess = new int[game view . row][game view . col];

game view . invalidate();

}else if(item.getTitle()。等于("退出")){

finish();

}

返回super . onoptionsitems elected(item);

}

}

包com.fiveChess

导入Android . app . alert dialog;

导入Android . content . context;

导入Android . content . dialog interface;

导入Android . graphics . canvas;

导入Android . graphics . color;

导入Android . graphics . paint;

导入Android . graphics . paint . style;

导入Android . view . motion event;

导入Android . view . view;

公共类游戏视图扩展视图{

Context上下文= null

int屏幕宽度,屏幕高度;

字符串消息= " ";//提示轮到哪个玩家了

int row,col//带下划线的行数和列数

int步长= 30;//每个网格的棋盘间距

int[][]chess = null;//0表示没有棋子,1表示黑棋,2表示白旗。

boolean isBlack = true

布尔canPlay = true

公共游戏视图(上下文Context,int screenWidth,int screenHeight) {

超级(上下文);

this.context = context

this . screen width = screen width;

this . screen height = screen height;

This.message = "黑的先走";

row =(screen height-50)/step length+1;

col =(屏幕宽度-10)/步长+1;

chess = new int[row][col];

}

@覆盖

受保护的void onDraw(画布画布){

super.onDraw(画布);

油漆油漆=新油漆();

paint.setColor(颜色。白色);

canvas.drawRect(0,0,screenWidth,screenHeight,paint);//绘制背景

paint.setColor(颜色。蓝色);

paint . settextsize(25);

canvas.drawText(message,(screenWidth-100)/2,30,paint);//画出最上面的字

paint.setColor(颜色。黑色);

//画一个棋盘

for(int I = 0;我& lt排;i++){

canvas.drawLine(10,50+I *步长,10+(col-1)*步长,50+I *步长,画图);

}

for(int I = 0;我& ltcoli++){

canvas . drawline(10+i*stepLength,50,10+I * stepLength,50+(row-1)*stepLength,paint);

}

for(int r = 0;r & lt排;r++){

for(int c = 0;c & ltcolc++){

if(chess[r][c] == 1){

paint.setColor(颜色。黑色);

paint.setStyle(Style。填充);

canvas . draw circle(10+c *步长,50+r *步长,10,画图);

}else if(chess[r][c] == 2){

//画白棋

paint.setColor(颜色。白色);

paint.setStyle(Style。填充);

canvas . draw circle(10+c *步长,50+r *步长,10,画图);

paint.setColor(颜色。黑色);

paint.setStyle(Style。笔画);

canvas . draw circle(10+c *步长,50+r *步长,10,画图);

}

}

}

}

@覆盖

public boolean onTouchEvent(运动事件事件){

如果(!can play){ return false;}

float x = event . getx();

float y = event . gety();

int r = math . round((y-50)/步长);

int c = math . round((x-10)/步长);

if(r & lt;0 | | r & gtrow-1 | | c & lt;0 | | c >;col-1){返回false}

if(chess[r][c]!= 0){返回false}//如果有棋子,就不再抽棋子。

如果(isBlack){

国际象棋[r][c]= 1;

isBlack = false

Message = "轮到白了";

}否则{

国际象棋[r][c]= 2;

isBlack = true

Message = "轮到黑棋了";

}

invalidate();

if(judge(r,c,0,1))返回false

if(judge(r,c,1,0))返回false

if(judge(r,c,1,1))返回false

if(judge(r,c,1,-1))返回false

返回super.onTouchEvent(事件);

}

Private Boolean Judge (int r,int c,int x,int y) {//r,c表示行和列,x表示y方向的偏移量,y表示x方向的偏移量。

int count = 1;

int a = r;

int b = c;

while(r & gt;= 0 & amp& ampr & lt排与排。& ampc & gt= 0 & amp& ampc & ltcol & amp& ampr+x & gt;= 0 & amp& ampr+x & lt;排与排。& ampc+y & gt;= 0 & amp& ampc+y & lt;col & amp& amp国际象棋[r][c] ==国际象棋[r+x][c+y]){

count++;

如果(y & gt0){

c++;

} else if(y & lt;0){

c-;

}

if(x & gt;0){

r++;

} else if(x & lt;0){

r-;

}

}

while(a & gt;= 0 & amp& ampa & lt排与排。& ampb & gt= 0 & amp& ampb & ltcol & amp& ampa-x & gt;= 0 & amp& ampa-x & lt;排与排。& ampb-y & gt;= 0 & amp& ampb-y & lt;col & amp& amp象棋[a][b] ==象棋[a-x][b-y]){

count++;

如果(y & gt0){

B-;

} else if(y & lt;0){

b++;

}

if(x & gt;0){

a-;

} else if(x & lt;0){

a++;

}

}

if(count & gt;=5){

String str =

如果(isBlack){

Str = "白棋胜利";

}否则{

Str = "黑胜利";

}

新建警报对话框。构建器(上下文)。settitle(“游戏结束”)。setmessage(字符串)。setpositivebutton("重新启动",新的对话界面。onclick listener () {

@覆盖

public void onClick(dialog interface dialog,int which) {

chess = new int[row][col];

invalidate();

}

}).setNegativeButton("看棋局",新的对话界面。onclick listener () {

@覆盖

public void onClick(dialog interface dialog,int which) {

canPlay = false

}

}).show();

返回true

}

返回false

}

}

PS:五子棋,直接在程序里画的,没有图片。注意,我发送了两个文件,一个活动文件和一个类文件。不要把它当成一个文件。