-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #779 from zjhua/master
- Loading branch information
Showing
13 changed files
with
962 additions
and
1 deletion.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901144/Block.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com1314080901144; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.example.com1314080901144.util.Coordinate; | ||
import com.example.com1314080901144.util.Function; | ||
|
||
/** | ||
* 棋块 | ||
* | ||
*/ | ||
public class Block { | ||
private List<Coordinate> block=new ArrayList<Coordinate>(); | ||
private int airCount=0;//气数 | ||
private int bw;//颜色 | ||
|
||
public Block(int bw){ | ||
this.bw=bw; | ||
} | ||
|
||
public int getBw(){ | ||
return bw; | ||
} | ||
|
||
public void add(Coordinate c){ | ||
block.add(c); | ||
} | ||
|
||
public void addAir(int air){ | ||
airCount+=air; | ||
} | ||
|
||
public boolean isLive(){ | ||
if(airCount>0 && block.size()>0)return true; | ||
return false; | ||
} | ||
|
||
public void each(Function f){ | ||
for(Coordinate c:block){ | ||
f.apply(c); | ||
} | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901144/Board.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package com1314080901144; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import android.os.Bundle; | ||
|
||
import com.example.com1314080901144.util.Coordinate; | ||
import com.example.com1314080901144.util.Function; | ||
import com.example.com1314080901144.util.Utils; | ||
|
||
public class Board { | ||
|
||
public static int n = 19; | ||
public static final int None = 0; | ||
public static final int Black = 1; | ||
public static final int White = 2; | ||
|
||
//行棋记录 | ||
private List<PieceProcess> list = new ArrayList<PieceProcess>(); | ||
|
||
private Grid currentGrid = new Grid();//当前盘面 | ||
|
||
private int expBw = Black;//轮到哪一方下 | ||
private Function listener; | ||
|
||
//------------------------------------------------------------------ | ||
|
||
public boolean put(int x, int y) { | ||
Coordinate c = new Coordinate(x, y); | ||
PieceProcess p=new PieceProcess(expBw,c); | ||
|
||
if (currentGrid.putPiece(p)) { | ||
if(!check(p)){ | ||
currentGrid.executePieceProcess(p, true); | ||
return false; | ||
} | ||
|
||
list.add(p); | ||
finishedPut(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void finishedPut(){ | ||
expBw = Utils.getReBW(expBw); | ||
postEnvet(); | ||
} | ||
|
||
//打劫检测 | ||
private boolean check(PieceProcess p){ | ||
int i=0; | ||
for(PieceProcess pp:list){ | ||
if(pp.resultBlackCount==p.resultBlackCount | ||
&& pp.resultWhiteCount==p.resultWhiteCount){ | ||
if(isOverEqualse(i,p)){ | ||
return false; | ||
} | ||
} | ||
i++; | ||
} | ||
return true; | ||
} | ||
|
||
private boolean isOverEqualse(int position,PieceProcess p){ | ||
Board sb=getSubBoard(position+1); | ||
return sb.currentGrid.equals(this.currentGrid); | ||
} | ||
|
||
//------------------------------------------------------------------rebuilt | ||
|
||
public SubBoard getSubBoard(int index){ | ||
SubBoard board=new SubBoard(this); | ||
board.gotoIt(index); | ||
return board; | ||
} | ||
|
||
protected void cleanGrid(){ | ||
this.currentGrid = new Grid(); | ||
} | ||
|
||
protected void addPieceProcess(PieceProcess p) { | ||
currentGrid.executePieceProcess(p, false); | ||
list.add(p); | ||
finishedPut(); | ||
} | ||
|
||
protected void removePieceProcess(){ | ||
if(list.size()==0)return; | ||
PieceProcess p=list.remove(getCount()-1); | ||
currentGrid.executePieceProcess(p, true); | ||
finishedPut(); | ||
} | ||
|
||
//------------------------------------------------------------------getter | ||
|
||
|
||
public int getValue(int x, int y) { | ||
return currentGrid.getValue(new Coordinate(x, y)); | ||
} | ||
|
||
private void postEnvet() { | ||
if(listener==null)return; | ||
listener.apply(getCount(),expBw); | ||
} | ||
|
||
public void setListener(Function listener){ | ||
this.listener=listener; | ||
} | ||
|
||
public Coordinate getLastPosition() { | ||
if(getCount()==0)return null; | ||
return list.get(getCount() - 1).c; | ||
} | ||
|
||
public int getCount() { | ||
return list.size(); | ||
} | ||
|
||
public PieceProcess getPieceProcess(int i){ | ||
if(i>=getCount())return null; | ||
return list.get(i); | ||
} | ||
|
||
//------------------------------------------------------------------status | ||
|
||
public Bundle saveState() { | ||
Bundle map = new Bundle(); | ||
map.putInt("count", getCount()); | ||
int i=0; | ||
for(PieceProcess p:list){ | ||
map.putInt("x"+i, p.c.x); | ||
map.putInt("y"+i, p.c.y); | ||
i++; | ||
} | ||
return map; | ||
} | ||
|
||
public void restoreState(Bundle map) { | ||
int n=map.getInt("count"); | ||
for(int i=0;i<n;i++){ | ||
int x=map.getInt("x"+i); | ||
int y=map.getInt("y"+i); | ||
|
||
this.put(x, y); | ||
} | ||
} | ||
} |
191 changes: 191 additions & 0 deletions
191
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901144/Grid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package com1314080901144; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.example.com1314080901144.util.Coordinate; | ||
import com.example.com1314080901144.util.Function; | ||
import com.example.com1314080901144.util.Utils; | ||
|
||
public class Grid { | ||
private int[][] a; | ||
|
||
public Grid() { | ||
a = new int[Board.n][Board.n]; | ||
} | ||
|
||
public int getValue(Coordinate c) { | ||
return a[c.x][c.y]; | ||
} | ||
|
||
private void setValue(Coordinate c, int value) { | ||
a[c.x][c.y] = value; | ||
} | ||
|
||
//执行棋子过程 | ||
public void executePieceProcess(PieceProcess p, boolean reverse) { | ||
if (!reverse) { | ||
setValue(p.c, p.bw); | ||
for (PieceProcess pp : p.removedList) { | ||
setValue(pp.c, Board.None); | ||
} | ||
} else { | ||
for (PieceProcess pp : p.removedList) { | ||
setValue(pp.c, pp.bw); | ||
} | ||
setValue(p.c, Board.None); | ||
} | ||
} | ||
|
||
//落子 | ||
public boolean putPiece(PieceProcess piece) { | ||
if (!piece.c.isValid()) | ||
return false; | ||
if (getValue(piece.c) != Board.None) | ||
return false; | ||
|
||
setValue(piece.c, piece.bw); | ||
startPick(piece.c, piece.bw, piece.removedList); | ||
|
||
piece.resultBlackCount=getPieceCount(Board.Black); | ||
piece.resultWhiteCount=getPieceCount(Board.White); | ||
|
||
return true; | ||
} | ||
|
||
//统计盘面棋子数量 | ||
private int getPieceCount(int bw) { | ||
int c = 0; | ||
for (int i = 0; i < Board.n; i++) { | ||
for (int j = 0; j < Board.n; j++) { | ||
if (a[i][j] == bw) { | ||
c++; | ||
} | ||
} | ||
} | ||
return c; | ||
} | ||
|
||
// ------------------------------------------------------------------提子 | ||
|
||
private void startPick(Coordinate c, int bw, List<PieceProcess> removedList) { | ||
int reBw = Utils.getReBW(bw); | ||
pickOther(c, reBw, removedList); | ||
|
||
if (removedList.size() > 0) | ||
return; | ||
pickSelf(c, bw, removedList); | ||
} | ||
|
||
private void pickOther(Coordinate c, int bw, List<PieceProcess> removedList) { | ||
boolean[][] v = new boolean[Board.n][Board.n]; | ||
|
||
for (int i = 0; i < 4; i++) { | ||
Coordinate nc = c.getNear(i); | ||
Block block = new Block(bw); | ||
pick(nc, v, bw, block); | ||
|
||
if (!block.isLive()) { | ||
deleteBlock(block, removedList); | ||
} | ||
} | ||
} | ||
|
||
private void pickSelf(Coordinate c, int bw, List<PieceProcess> removedList) { | ||
boolean[][] v = new boolean[Board.n][Board.n]; | ||
Block block = new Block(bw); | ||
pick(c, v, bw, block); | ||
if (!block.isLive()) { | ||
deleteBlock(block, removedList); | ||
} | ||
} | ||
|
||
// 递归构造为块 | ||
private void pick(Coordinate c, boolean[][] v, int bw, Block block) { | ||
if (c == null) | ||
return; | ||
if (v[c.x][c.y] == true) | ||
return; | ||
|
||
if (getValue(c) == Board.None) { | ||
block.addAir(1); | ||
return; | ||
} else if (getValue(c) != bw) { | ||
return; | ||
} | ||
|
||
v[c.x][c.y] = true; | ||
block.add(c); | ||
|
||
for (int i = 0; i < 4; i++) { | ||
Coordinate nc = c.getNear(i); | ||
pick(nc, v, bw, block); | ||
} | ||
} | ||
|
||
private void deleteBlock(final Block block, final List<PieceProcess> removedList) { | ||
block.each(new Function() { | ||
@Override | ||
public Object apply(Object... obj) { | ||
Coordinate c = (Coordinate) obj[0]; | ||
|
||
a[c.x][c.y] = Board.None; | ||
removedList.add(new PieceProcess(block.getBw(),c,null)); | ||
|
||
return null; | ||
} | ||
}); | ||
} | ||
|
||
// ------------------------------------------------------------------ | ||
|
||
@Override | ||
public String toString() { | ||
String s = ""; | ||
for (int j = 0; j < Board.n; j++) { | ||
for (int i = 0; i < Board.n; i++) { | ||
if (a[i][j] == Board.None) { | ||
s += " +"; | ||
} else if (a[i][j] == Board.White) { | ||
s += " o"; | ||
} else if (a[i][j] == Board.Black) { | ||
s += " x"; | ||
} | ||
} | ||
s += "\n"; | ||
} | ||
return s; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + Arrays.hashCode(a); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
Grid other = (Grid) obj; | ||
if (!myEqualse(a, other.a,Board.n)) | ||
return false; | ||
return true; | ||
} | ||
|
||
private boolean myEqualse(int[][] a,int b[][],int n){ | ||
for (int j = 0; j < n; j++) { | ||
for (int i = 0; i < n; i++) { | ||
if(a[j][i]!=b[j][i]) | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.