Android Open Source - MiReversi Computer Player Level2






From Project

Back to project page MiReversi.

License

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.

Java Source Code

/*
* Copyright (c) 2011 Makoto Ishida//  w  w  w .  j  ava 2 s  .  co  m
* Please see the file MIT-LICENSE.txt for copying permission.
*/

package com.example.mireversi.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import android.graphics.Point;
import com.example.mireversi.model.Cell.E_STATUS;

public class ComputerPlayerLevel2 extends ComputerPlayer{

  private static int WAIT_MSEC = 10;
  private Random mRnd;

  //???????????????
  protected static final int[][] weight_table 
    = { { 40,-12,  0, -1, -1,  0,-12, 40 }, 
      {-12,-15, -3, -3, -3, -3,-15,-12 },
      {  0, -3,  0, -1, -1,  0, -3,  0 },
      { -1, -3, -1, -1, -1, -1, -3, -1 },
      { -1, -3, -1, -1, -1, -1, -3, -1 },
      {  0, -3,  0, -1, -1,  0, -3,  0 },
      {-12,-15, -3, -3, -3, -3,-15,-12 },
      { 40,-12,  0, -1, -1,  0,-12, 40 }
       };

  public ComputerPlayerLevel2(E_STATUS turn, String name, Board board){
    super(turn, name, board);
    mRnd = new Random();
  }

  @Override
  protected Point think() {
    Point pos = null;
    
    try {
      Thread.sleep(WAIT_MSEC);
    } catch (InterruptedException e) {
      setStopped(true);
    }
    if (isStopped()) return pos;          //????????????????????????????
    
    //????????????????????????????
    ArrayList<Cell> available_cells = mBoard.getAvailableCells();
    if (available_cells.size() == 0){
      return pos;
    }
    
    if (isStopped()) return pos;          //????????????????????????????
    
    for (int i = 0; i < available_cells.size(); i++) {
      Cell cur = available_cells.get(i);
      //1???????????????
      Board new_board = mBoard.clone();
      //1??????
      new_board.changeCell(cur.getPoint(), new_board.getTurn());
      //????????????????????????????
      cur.setEval(getWeightTotal(new_board, weight_table));
    }
    
    //????????????????????????????
    Collections.sort(available_cells, new EvaluationComparator());

////DEBUG
//Utils.d(String.format("Sorted available cells:\n"));
//for (int i = 0; i < available_cells.size(); i++) {
//  Cell cur = available_cells.get(i);
//  Utils.d(String.format("%d x,y=%d,%d   val=%d", i, cur.getCol(), cur.getRow(),  cur.getEval()));
//}

    ArrayList<Cell> max_cells = new ArrayList<Cell>();
    max_cells.add(available_cells.get(0));          //?????????????????????????????????
    int max_eval = available_cells.get(0).getEval();
    
    //???????????????????????????????????????????????
    for (int i = 1; i < available_cells.size(); i++) {
      Cell current = available_cells.get(i);
      if (max_eval == current.getEval()){
        max_cells.add(current);
      } else {
        break; 
      }
    }
    
    //??????????????????????????????????????????????????
    int n = mRnd.nextInt(max_cells.size());
    Cell chosenCell = max_cells.get(n);
    pos = chosenCell.getPoint();
    
    return pos;
  }
  

}




Java Source Code List

com.example.mireversi.GameActivity.java
com.example.mireversi.Pref.java
com.example.mireversi.ReversiView.java
com.example.mireversi.Utils.java
com.example.mireversi.exceptions.InvalidMoveException.java
com.example.mireversi.model.Board.java
com.example.mireversi.model.Cell.java
com.example.mireversi.model.ComputerPlayerLevel0.java
com.example.mireversi.model.ComputerPlayerLevel1.java
com.example.mireversi.model.ComputerPlayerLevel2.java
com.example.mireversi.model.ComputerPlayerLevel3.java
com.example.mireversi.model.ComputerPlayer.java
com.example.mireversi.model.HumanPlayer.java
com.example.mireversi.model.IPlayerCallback.java
com.example.mireversi.model.Player.java