?? game.java
字號:
/*******************************************************************************
* Copyright (c) 2004 Berthold Daum.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Berthold Daum
*******************************************************************************
* Hex-7
* Version 2.0
* www.mazeworks.com
*
* Copyright (c) 2002 David Herzog
* All Rights Reserved.
*******************************************************************************/
package com.bdaum.Hex.game;
import org.eclipse.swt.widgets.Canvas;
import com.bdaum.Hex.views.IStatusListener;
public class Game implements Runnable, IGame {
public static final int SIZE = 7;
public static final int PLAYER = 0, COMPUTER = 1;
/* View */
// private HexView main;
private Canvas canvas;
private IStatusListener statusListener;
/* Komponenten */
private StaticEval stat;
private Board bd;
private AI piet;
private Thread pietMoveThread;
/* Spielablauf */
private int playerColor = WHITE, computerColor = BLACK;
private int level = LEVEL1;
private boolean playerTurn, gameOver;
private int ply;
public void setDrawingSurface(Canvas canvas) {
this.canvas = canvas;
}
public void setStatusListener(IStatusListener listener) {
this.statusListener = listener;
}
/**
* Spiel starten
*/
public void newGame() {
System.gc();
stop();
playerTurn = (playerColor == WHITE) ? true : false;
gameOver = false;
ply = 1;
bd = new Board(canvas, this);
stat = new StaticEval(bd, this);
piet = new AI(bd, stat, this);
bd.draw();
// white moves first
if (!playerTurn)
pietMoveStart();
else
statusListener.showMessage("Your Move");
canvas.setFocus();
}
/**
* Spiel beenden
*/
private void endGame(
int side) {
if (side == COMPUTER) {
// Computer gewinnt
if (computerColor == WHITE)
statusListener.showMessage("White wins");
else
statusListener.showMessage("Black wins");
} else {
// Spieler gewinnt
if (playerColor == WHITE)
statusListener.showMessage("White wins");
else
statusListener.showMessage("Black wins");
}
gameOver = true;
}
/**
* Spieler zieht
*
* @param i -
* Reihe
* @param j -
* Spalte
*/
private void playerMove(
int i, int j) {
if (ply != 1 || i != SIZE / 2 || j != SIZE / 2) {
if (bd.setHex(i, j, playerColor)) {
bd.draw();
if (bd.isWin(playerColor))
endGame(PLAYER);
// gefolgt von einem Computer-Zug falls der Spieler nicht gewonnen hat
else {
ply++;
pietMoveStart();
}
}
}
}
/**
* Piet zieht
*/
private void pietMoveStart() {
if (pietMoveThread == null) {
pietMoveThread = new Thread(this);
pietMoveThread.start();
}
}
/**
* Thread stoppen
*/
private void stop() {
if (pietMoveThread != null) {
//TODO Das sollte sauber implementiert werden.
pietMoveThread.stop();
pietMoveThread = null;
}
}
/**
* Piets Thread ausf黨ren
*/
public void run() {
playerTurn = false;
statusListener.showMessage("Thinking ...");
piet.makeMove();
bd.draw();
if (bd.isWin(computerColor))
endGame(COMPUTER);
else {
ply++;
playerTurn = true;
statusListener.showMessage("Your Move");
}
pietMoveThread = null;
}
/**
* Pause
*
* @param ms -
* Verz鰃erungszeit in msec
*/
protected void sleep(
int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
}
}
/**
* Spielst鋜ke zur點kgeben
*
* @return - Spielst鋜ke
*/
protected int getLevel() {
return level;
}
/**
* Farbe ermitteln
*
* @param side -
* Spieler oder Piet
* @return - Farbe
*/
protected int getColor(
int side) {
return (side == PLAYER) ? playerColor : computerColor;
}
/**
* Farbe der Gegenseite ermitteln
*
* @param color -
* Farbe
* @return - Farbe der Gegenseite
*/
protected int getOtherColor(
int color) {
return (color == WHITE) ? BLACK : WHITE;
}
/**
* Zugnummer ermitteln
*
* @return - Zugnummer
*/
protected int getPly() {
return ply;
}
/**
* Mausereignis behandeln
*
* @param i -
* Reihe
* @param j -
* Spalte
*/
protected void selectHex(
int i, int j) {
if (!gameOver && playerTurn) playerMove(i, j);
}
public void setPlayerColor(
int color) {
if (gameOver || playerTurn) {
if ((color == BLACK) && (playerColor == WHITE)) {
playerColor = BLACK;
computerColor = WHITE;
} else if ((color == WHITE) && (playerColor == BLACK)) {
playerColor = WHITE;
computerColor = BLACK;
}
if (!gameOver) pietMoveStart();
}
canvas.setFocus();
}
public void setLevel(
int level) {
this.level = level;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -