導入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:五子棋,直接在程序裏畫的,沒有圖片。註意,我發送了兩個文件,壹個活動文件和壹個類文件。不要把它當成壹個文件。