package test.util.proc;
import net.sf.nebulacards.comm.*;
import net.sf.nebulacards.game.*;
import net.sf.nebulacards.main.*;
import net.sf.nebulacards.util.*;
import net.sf.nebulacards.util.proc.*;
import junit.framework.*;
import OneProcGame;
import Utility;
import CountingUI;
/**
* Test the TricksProc class. This test does not guarantee the
* correct functioning of a subclass of TricksProc. It only attempts
* to ensure that TricksProc obeys its contract of when each of its
* methods is called.
* @author James Ranson
*/
public class TricksProcTest
extends TestCase
{
final int NUM_PLAYERS = 3;
OneProcGame opg;
Queue eventLog;
public TricksProcTest(String s) { super(s); }
class MyEvent
{
public int id;
public String msg;
public MyEvent() {}
public MyEvent(int a, String b) { id = a; msg = b; }
public String toString() { return "" + id + " " + msg; }
public boolean equals(Object o)
{
if (!(o instanceof MyEvent)) return false;
return (((MyEvent)o).id == id && ((MyEvent)o).msg.equals(msg));
}
}
protected void log(MyEvent me)
{
eventLog.enqueue(me);
//System.out.println(me);
}
protected void log(int id, String msg)
{
log(new MyEvent(id, msg));
}
protected MyEvent readLog() { return (MyEvent)eventLog.dequeue(); }
protected void expect(String err, String s)
{
assertEquals(err, readLog().msg, s);
}
protected void expect(String err, int id, String s)
{
MyEvent me = readLog();
assertEquals(err, me.id, id);
assertEquals(err, me.msg, s);
}
class MyTricksProc extends TricksProc
{
boolean shouldAccept = false;
public MyTricksProc(CardGame g)
{
super(g);
}
protected void score(PileOfCards[] hands, Player[] p)
{
log(-1, "score");
}
protected void trickDone(Tableau t, int winner)
{
log(winner, "trickDone");
super.trickDone(t, winner);
}
protected boolean validPlay(PileOfCards beenPlayed,
Tableau tableau, PileOfCards hand, PlayingCard c)
{
log(-1, "validPlay");
final boolean result = shouldAccept;
shouldAccept = true;
return result;
}
protected int whoPlaysNext(boolean first, int lastCard, int lastWinner)
{
final int who = (first) ? lastWinner : (lastCard + 1) % NUM_PLAYERS;
log(who, "whoPlaysNext");
return who;
}
protected int whoWinsTrick(Tableau t)
{
// FIXME: vary the winner!
final int who = 0;
log(who, "whoWinsTrick");
return who;
}
}
class MyPlayer extends UIAdapter
{
private PlayingCard play = NullPlayingCard.getInstance();
private void log(String s)
{
TricksProcTest.this.log(getPosition(), s);
}
public void dealHand(PileOfCards h)
{
log("dealHand");
super.dealHand(h);
}
public void yourTurn()
{
log("yourTurn");
submitPlay(play);
play = getHand().get(0);
}
public void endHand() { log("endHand"); }
public void clearTableau(int winner) { log("clearTableau"); }
public void accepted()
{
log("accepted");
getHand().remove(0);
try { play = getHand().get(0); }
catch (ArrayIndexOutOfBoundsException e) {}
}
public void rejected() { log("rejected"); }
}
public void setUp()
{
eventLog = new Queue();
opg = new OneProcGame("TricksProc Test", NUM_PLAYERS);
opg.setProc(new MyTricksProc(opg));
ComManager.AddResult cmar;
MyPlayer mp;
while ((cmar = opg.add(mp = new MyPlayer())) != null)
{
for (int i = 0; i < 2; i++)
opg.getHands()[cmar.getPosition()].add(new PlayingCard(i, cmar.getPosition()));
cmar.getCommunicator().dealHand(opg.getHands()[cmar.getPosition()]);
cmar.publicize();
}
}
public void testRun()
throws InterruptedException
{
GameProcedure gp = opg.getNextProcedure();
gp.runProcedure(Utility.getComs(opg), opg.getBroadcastCommunicator());
// Everyone should be out of cards.
boolean properQuit = true;
for (int i = 0; i < opg.getCapacity(); i++)
properQuit &= opg.getHands()[i].isEmpty();
assertTrue(properQuit);
// were the hands dealt?
for (int i = 0; i < opg.getCapacity(); i++)
expect("dealHand", "dealHand");
// two tricks, the first one has rejected plays from each player
for (int trick = 0; trick < 2; trick++)
{
for (int player = 0; player < opg.getCapacity(); player++)
{
String err = "Trick = " + trick + "; player = " + player;
expect(err, "whoPlaysNext");
expect(err, player, "yourTurn");
if (trick == 0)
{
expect(err, "rejected");
expect(err, player, "yourTurn");
if (player == 0)
{
expect(err, "validPlay");
expect(err, "rejected");
expect(err, player, "yourTurn");
}
}
expect(err, "validPlay");
expect(err, player, "accepted");
}
expect("", "whoWinsTrick");
expect("", "trickDone");
// were the tableaux cleared?
for (int i = 0; i < opg.getCapacity(); i++)
expect("" + i, "clearTableau");
}
expect("", "score");
for (int i = 0; i < opg.getCapacity(); i++)
expect("" + i, "endHand");
assertNull(readLog());
}
}