Back to project page MiReversi.
The source code is released under:
MIT License
If you think the Android project MiReversi listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (c) 2011 Makoto Ishida/*w w w .j a va 2 s .co m*/ * Please see the file MIT-LICENSE.txt for copying permission. */ package com.example.mireversi.model; import java.util.Comparator; import android.graphics.Point; import android.os.Handler; import com.example.mireversi.model.Cell.E_STATUS; public abstract class ComputerPlayer extends Player implements Runnable { private Handler mHandler = new Handler(); private IPlayerCallback mCallback; private Thread mThread; private boolean mStopped; public ComputerPlayer(E_STATUS turn, String name, Board board){ super(turn, name, board); mStopped = false; } @Override public boolean isHuman() { return false; } @Override public synchronized void startThinking(IPlayerCallback callback) { mCallback = callback; mStopped = false; if (mBoard.getAvailableCellCount(true) == 0){ callback.onEndThinking(null); return; } //??????????????????????????? mThread = new Thread(this); mThread.start(); } @Override public synchronized void stopThinking() { mStopped = true; } @Override public void run() { //???????????? final Point pos = think(); //?????????????????UI?????????????????????????? mHandler.post(new Runnable(){ @Override public void run(){ mCallback.onEndThinking(pos); } }); } protected abstract Point think(); public void setStopped(boolean mStopped) { this.mStopped = mStopped; } public boolean isStopped() { return mStopped; } protected void onProgress(final int percent){ this.setProgress(percent); mHandler.post(new Runnable(){ @Override public void run(){ mCallback.onProgress(); } }); } public int getWeight(Cell cell, int[][] weight_table){ Point pt = cell.getPoint(); return weight_table[pt.y][pt.x]; } public int getWeight(Point pt, int[][] weight_table){ return weight_table[pt.y][pt.x]; } /*** * ??????????????????????????????????Comparator???? * @author mike * */ public class WeightComparator implements Comparator<Cell> { private int[][] mWeightTable; public WeightComparator(int[][] weight_table){ mWeightTable = weight_table; } @Override public int compare(Cell cell1, Cell cell2) { //0?????????1???????????-1??????????? int weight1 = getWeight(cell1, mWeightTable); int weight2 = getWeight(cell2, mWeightTable); if (weight1 > weight2) return -1; if (weight1 < weight2) return 1; return 0; } } /*** * ??????????????????????????????????Comparator???? * @author mike * */ public class EvaluationComparator implements Comparator<Cell> { @Override public int compare(Cell cell1, Cell cell2) { //0?????????1???????????-1??????????? int val1 = cell1.getEval(); int val2 = cell2.getEval(); if (val1 > val2) return -1; if (val1 < val2) return 1; if (val1 == val2){ if (cell1.getNextAvaiableCnt() > cell2.getNextAvaiableCnt()) return -1; if (cell1.getNextAvaiableCnt() < cell2.getNextAvaiableCnt()) return 1; } return 0; } } public int getWeightTotal(Board board, int [][] weight_table){ int total = 0; Cell[][] cells = board.getCells(); E_STATUS player_turn = board.getTurn(); E_STATUS opp_turn = Cell.getOpponentStatus(player_turn); // int cur_count = 0, opp_count = 0, blank_count = 0; for (int i = 0; i< Board.ROWS; i++ ){ for (int j =0; j < Board.COLS; j++){ E_STATUS st = cells[i][j].getStatus(); if (st == player_turn){ // cur_count++; total += getWeight(cells[i][j], weight_table); } else if (st == opp_turn){ // opp_count++; total -= getWeight(cells[i][j], weight_table); } else { // blank_count++; } } } return total; } }