⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 position.java

📁 ErGo是一个很早的Java通用围棋服务器(IGS/NNGS)客户端程序。有全部源码和文档
💻 JAVA
字号:
package ergo.util;

// $Id: Position.java,v 1.2 1999/08/13 01:20:43 sigue Exp $

/*
 *  Copyright (C) 1999  Carl L. Gay and Antranig M. Basman.
 *  See the file copyright.txt, distributed with this software,
 *  for further information.
 */

/**
  * The Position class encodes a given board position and deals with
  * administrivia relating to position names.
  */
public class Position {
  public int row;		// from bottom
  public int column;		// from left
  private String name;

  private Position() {}

  public Position(int row, int col) {
    this.row = row;
    column = col;
  }

  public void setPosition(int row1, int column1) {
    row = row1;
    column = column1;
  }

  /*
   * Given a move spec like "A1", return the row and column of the move.
   * See comment above Position.toString() about coordinates.
   * Remember the letter I is not used in move names.
   */
  // When using this constructor it is the caller's responsibility to
  // check for reasonable values of row and column.
  public Position (String s, int maxsize) throws ParseException {
    ParsedMessage p = new ParsedMessage(s, " %c%i");
    int ch = (int) ((Character) p.matchAt(0)).charValue();
    row = ((Integer) p.matchAt(1)).intValue() - 1;
    column = ch - ((ch < (int) 'J') ? (int) 'A' : (int) 'B');
    if (row < 0 || column < 0 || row >= maxsize || column >= maxsize)
      throw new ParseException("Position string not within bounds");
  }

  /*
   * Takes the row and column of this position and converts them to
   * a move name string such as "A17".  Note that the letter 'I' is skipped.
   * Row and column correspond exactly to how they are displayed on the
   * screen.  i.e., row=0 col=0 corresponds to A1 and row=1 col=1 is B2
   */
  public String toString () {
    if (name == null)
      name = "" + (char) ((int) 'A' + ((column < 8) ? column : column + 1))
	+ (row + 1);
    return name;
  }

  public boolean equals (Position p) {
    return (row == p.row && column == p.column);
  }

  public boolean equals (Object o) {
    if (! (o instanceof Position))
      return false;
    else
      return equals((Position) o);
  }

  public int hashCode () {
    return row + 1000 * column;
  }

  /*
   * In SGF, [aa] names the position A19, i.e., the top-left corner.
   * The character 'i' is _not_ omitted.
   * SGF3 allows 26x26 max ([aa] ... [zz])
   * SGF4 allows 52x52 max ([aa] ... [zz], [AA] ... [ZZ])
   */
  public static String asSGF (int boardSize, int row, int column) {
    int x = ((column < 26) ? (column + (int) 'a') : (column - 26 + (int) 'A'));
    int y = boardSize - 1 - row;
    y = ((y < 26) ? (y + (int) 'a') : (y - 26 + (int) 'A'));
    return ("[" + (char) x + (char) y + "]");
  }

  public String asSGF (int boardSize) {
    return asSGF(boardSize, row, column);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -