/*
* Human.java
*
* Created on 26. november 2004, 10:11
*/
package checkers;
import jess.*;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.StringTokenizer;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Human player
* @author k380h11
*/
public class Human extends Player{
/**
* Creates a new instance of Human
* @param id name of the player
* @param color color of the player
*/
public Human
(String id,
boolean color
) {
super(id, color);
}
/**
* This method is used for the human player to enter the commands
* of what one whats to do
*/
public void play() {
boolean continu = true;
do {
continu = true;
try {
do {
try {
System.
out.
print("CMD " + (Checker.
getInstance().
getTurn() ? "white" : "red") + " >");
} while (st.countTokens() < 1);
int AgumentNumber= st.countTokens()-1;
String Command
= st.
nextToken().
toLowerCase();
for (int i = 0;st.hasMoreTokens(); i++)
line[i] = st.nextToken();
if (Command.equals("print")){
if (line[0].equals("board"))
System.
out.
println(Board.
getInstance());
else if (line[0].equals("grid"))
System.
out.
println(Grid.
getInstance());
else if (line[0].equals("piece")) {
Piece p
= Board.
select((short) Integer.
parseInt(line
[1]),
Boolean.
getBoolean(line
[2]));
} else System.
out.
println("syntax is: print <object> " +
"where <object> is board or grid or piece");
}
else if (Command.equals("move")) {
Piece p = selectPiece(in, color) ;
System.
out.
println("\n posible movement:");
if (p != null) {
Grid.Square sq[] =new Grid.Square[4];
sq[0] = p.getNLS();
sq[1] = p.getNRS();
sq[2] = p.getPLS();
sq[3] = p.getPRS();
if (sq[0].isFree() && sq[0].isLegal()){
} else sq[0] = null;
if (sq[1].isFree() && sq[1].isLegal()) {
} else sq[1] = null;
if (sq[2].isFree() && sq[2].isLegal() && p.isKing()) {
} else sq[2] = null;
if (sq[3].isFree() && sq[3].isLegal()&& p.isKing()) {
} else sq[3] = null;
if (sq[0] == null && sq[1] == null && sq[2] == null && sq[3] == null)
System.
out.
println("actualy, this piece can not be moved");
else {
Grid.Square s = selectSquare(in);
if (s!= null) {
if (s.equals(sq[0]) || s.equals(sq[1]) || s.equals(sq[2]) || s.equals(sq[3])) {
p.move(s);
continu = false;
} else {System.
out.
println("This square is not allowed");}
} else {
System.
out.
println("error on the command");
}
}
}
}
else if (Command.equals("jump")) {
Piece p = selectPiece(in, color) ;
Grid.Square sqJ[] =new Grid.Square[4];
Grid.Square sq[] =new Grid.Square[4];
sqJ[0] = p.getNLS();
sqJ[1] = p.getNRS();
sqJ[2] = p.getPLS();
sqJ[3] = p.getPRS();
sq[0] = p.getNNLS();
sq[1] = p.getNNRS();
sq[2] = p.getPPLS();
sq[3] = p.getPPRS();
System.
out.
println("\n posible jump:");
if (sq[0].isFree() && sq[0].isLegal() && !sqJ[0].isFree()
&& sqJ[0].PieceHasNotTheColor(p.getColor())
) {System.
out.
println(sq
[0].
getPiece());}
else sqJ[0] = null;
if (sq[1].isFree() && sq[1].isLegal() && !sqJ[1].isFree()
&& sqJ[1].PieceHasNotTheColor(p.getColor())
) {System.
out.
println(sq
[1].
getPiece());}
else sqJ[1] = null;
if (sq[2].isFree() && sq[2].isLegal() && !sqJ[2].isFree()
&& sqJ[2].PieceHasNotTheColor(p.getColor())
&& p.isKing()
) {System.
out.
println(sq
[2].
getPiece());}
else sqJ[2] = null;
if (sq[3].isFree() && sq[3].isLegal() && !sqJ[3].isFree()
&& sqJ[3].PieceHasNotTheColor(p.getColor())
&& p.isKing()
) {System.
out.
println(sq
[3].
getPiece());}
else sqJ[3] = null;
if (sqJ[0] == null && sqJ[1] == null && sqJ[2] == null && sqJ[3] == null)
System.
out.
println("actualy, this piece can not jump any other");
else {
Piece pToRemove = selectPiece(in, !color) ;
if (p != null && pToRemove != null) {
Grid.Square s = pToRemove.getSquare();
if (s.equals(sqJ[0]) || s.equals(sqJ[1]) || s.equals(sqJ[2]) || s.equals(sqJ[3])) {
p.jump(s);
continu = false;
} else {System.
out.
println("This piece is not allowed");}
} else {
System.
out.
println("error on the command");
}
}
}
else if (Command.equals("connect")) {
Checker.r.executeCommand("(load-package jessmr.pkg)");
int SMRNumber = 0;
Checker.r.executeCommand("(SMRConnect smr"+ SMRNumber +")");
Checker.r.executeCommand("(batch x)");
Checker.r.executeCommand("(go 2)");
}
else if (Command.equals("SMR-move")) {
Grid.Square sInit = selectSquare(in);
int initPosX = sInit.getX();
int initPosY = sInit.getY();
Grid.Square sFinal = selectSquare(in);
int finalPosX = sFinal.getX();
int finalPosY = sFinal.getY();
Checker.r.executeCommand("(assert(Move(init-pos-x " + initPosX +")(init-pos-y " +initPosY + ")(final-pos-x " + finalPosX +")(final-pos-x " + finalPosY + ")))");
continu = false;
}
else if (Command.equals("SMR-jump")) {
}
else if (Command.equals("jess")) {
while (!(cmd = in.readLine()).equals("quit")) {
Checker.r.executeCommand(cmd);
}
}
else if (Command.equals("exit")) {
while (!(cmd = in.readLine()).equals("quit")) {
Checker.r.executeCommand(cmd);
}
}
else {
System.
out.
println("Unknown command");
}
} catch (JessException e) {
}
} while (continu);
next.hasTurn();
}
/**
* This method provides a friendly way to ask the user to enter information about a specified square.
* @param in The input stream where the message will be displayed
* @throws IOException If the input stream can not be read
* @return The selected square
*/
System.
out.
println("Select a square :");
int row = 0;
int col = 0;
try {
row
= Integer.
parseInt(in.
readLine());
col
= Integer.
parseInt(in.
readLine());
return Grid.getSquare(row , col);
}
/**
* This method provides a friendly way to ask the user to enter information about a specified pieces nr.
* @return The selected piece's nr. and color
* @param color color of the piece selected
* @param in The input stream where the message will be displayed
* @throws IOException If the input stream can not be read
*/
System.
out.
println("Select a piece :");
System.
out.
print(" number :");
Piece p = null;
try {
int num
= Integer.
parseInt(in.
readLine());
p = Board.select(num, color);
return p;
}
}