//*************************************************************** // Class Definition for Instance of TPI-dispute in CNF-SAT Game. //*************************************************************** // The GameRecord class monitors the progress of the TPI-dispute // game on CNF formulae translated into Argumentation Systems. // // Its principal objects are: // // 1) A linked list of GamePosn objects where a GamePosn records // data relevant to the current move. A GamePosn object // has fields: // int MoveNum : the Move number (from 0) // char MoveMade : the Move (A, C, R) made. // int Arg : the argument invoked on this move // boolean[] Defence : the Current Defence Committments. // boolean[] Attack : the Current Attack. // // Although the Linked List structure is not needed for the // game mechanism to operate (only the most recent GamePosn // is ever used), it does provide a convenient mechanism for // reporting an entire game in a single structure. //****************************************************** // 2) A linked list of Retraction objects where a Retraction // is a // boolean[] Retracted : committments retracted by Defence. // // This must be a dynamic structure, however, it can be recovered // from the GameRecord linked list. //*************************************************************** // N.B. Implementation is as Linked List given in COMP102 notes. //*************************************************************** // Paul E. Dunne // Dept. of Computer Science // Univ. of Liverpool // Version: 22/05/2002 //****************************************************** import GamePosn; public class GameRecord { // // Definitions for Linked List Structure of GamePosn // private class GameCell { private GamePosn Move; private GameCell Next; // private GameCell(GamePosn head, GameCell next_cell) { Move = head; Next=next_cell; } } //****************************************************** // GameRecord Fields * //****************************************************** private GameCell Head; private GameCell Tail; private int MovesMade; // The number of moves in this instance. //****************************************************** // GameRecord Constructor * //****************************************************** public GameRecord() { Head = null; Tail = null; MovesMade=0; // Initiates the empty list. } //****************************************************** // Instance Methods * //****************************************************** // // Test if this instance is the Empty list. // public boolean IsEmpty() { return (MovesMade==0); // `Safer' than testing for null reference. }; // //************************************************************ // Add a new GameCell as the first cell in this Linked List * // i.e. The `new' Linked List is [Datum]::[Previous List]. * //************************************************************ // public void AddHead ( GamePosn Datum ) { if (IsEmpty()) // If it's currently empty; { Head = new GameCell(Datum,Head); // Create the new Head. Tail = null; // The link is still `null' MovesMade=1; // and this list now contains 1 cell. } else // If it's not empty { Head = new GameCell(Datum,Head); // Add the new List head Datum, Tail = Head.Next; // Reset the `Tail' of the list. MovesMade++; // and this list now has 1 extra cell. }; } // //************************************************************************ // Remove the GameCell at the start of this Linked List * // i.e. if the current list is [Head]::[Tail], the `new' one is [Tail]. * //************************************************************************ // public void RemoveHead() { if (MovesMade<=1) // There is an argument for raising { // an exception if CellCount=0 // however, we adopt the convention // that RemoveHead() applied to // the empty list leaves the empty list. Head = null; MovesMade=0; } else { Head = Tail; // Tail is never the null reference here. MovesMade--; // and this list has one fewer cell. if (MovesMade==1) Tail=null; // If there's only a single cell then its Link field // must point to the empty list (i.e. null reference) else Tail = Head.Next; // Otherwise we can update Tail without any problem. }; } // //********************************************************************** // Obtain the GamePosn in the Datum field of the ListCell at * // the start of this Linked List. * // This method will not change the current instantiation of this List.* //********************************************************************** // public GamePosn GetHead() { if (!(MovesMade==0)) // If there's anything to return return Head.Move; // then return it. else return null; // otherwise return a null reference. } // //********************************************************************** // Obtain the (reference) to the Linked List for the Link field of the* // ListCell at the start of this Linked List * // This method will not change the current instantiation of this List.* //********************************************************************** // public GameRecord GetTail() { GameRecord temporary= new GameRecord(); temporary.Head=Head; temporary.Tail=Tail; temporary.MovesMade = MovesMade; temporary.RemoveHead(); return temporary; }; // //************************************************************** // Convert this Linked List into a String in which each * // Datum of the component ListCells is separated by a newline. * //************************************************************** // public String toString() { String res = new String(); GameRecord temporary = new GameRecord(); temporary.Head = Head; temporary.Tail = Tail; temporary.MovesMade=MovesMade; while (!temporary.IsEmpty()) { res = res+(temporary.GetHead()).toString()+"\n"; temporary = temporary.GetTail(); }; return res; } }