Android Open Source - MiReversi Computer Player Level1






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//from w w  w .  ja  v  a 2  s .  c  o  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 ComputerPlayerLevel1 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 ComputerPlayerLevel1(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;
    }
    
    //????????????????????????????
    Collections.sort(available_cells, new WeightComparator(weight_table));

    if (isStopped()) return pos;          //????????????????????????????
    
    
    ArrayList<Cell> max_cells = new ArrayList<Cell>();
    max_cells.add(available_cells.get(0));            //?????????????????????????????????
    int max_weight = getWeight(available_cells.get(0), weight_table);
    
    //???????????????????????????????????????????????
    for (int i = 1; i < available_cells.size(); i++) {
      Cell current = available_cells.get(i);
      if (max_weight == getWeight(current, weight_table)){
        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