input
stringlengths
10
828
output
stringlengths
5
107
private void validate int b int found 0 0 0 0 0 0 0 0 0 string builder bs new string builder for int r 0 r max r r for int c 0 c max c c found b r c 1 bs append b r c bs append for int i 0 i found length i if found i 0 throw new illegal argument exception illegal board state bs
validate the given board state
public boolean remove middle of last three if has three return false something to do ipoint last points remove last points remove last remove them middle one points insert last return true
returns middle of last three
public string to string if root null return return format node root
create string representation of the tree
public boolean has three double node ipoint p points first int ct 0 while p null ct p p next if ct 2 return true return false
determine if there are more than 2 points currently in the partial hull
public int size double node ipoint p points first int ct 0 while p null ct p p next return ct
helper function to report number of points in the hull
private string format node segment tree node node string builder response new string builder if node lson null response append format node node lson response append node to string if node rson null response append format node node rson response append flatten return response to string
format the node recursively
public boolean equivalent inode n if n null return false eight puzzle node state eight puzzle node n for int r 0 r max r r for int c 0 c max c c if board r c state board r c return false return true
determine equivalence of state
private int count mark tic tac toe board board int c int r int dc int dr int sz char m keep track of number seen so far int ct 0 note advance condition below increment i and update c r for int i 0 i sz i c dc r dr skip empty spaces if board is clear c r continue found opponent leave now if board get c r m return 1 we have one ct return our count return ct
starting from c r count three in direction dc dr and
public int max number moves return 9
most tic tac toe variations are guaranteed to have a fixed number of
public string rules return rules n n the regular rules of tic tac toe apply
rules of straight tic tac toe apply
public void initialize state tic tac toe state state
enable variations to instantiate the stored data with each game state
public straight logic copy if copy null copy new straight logic now return return copy
each logic must implement copy to properly be used when
public move interpret move igame state game state int col int row player player tic tac toe state state tic tac toe state game state tic tac toe board board state board if col 0 return null if col board num columns return null if row 0 return null if row board num rows return null if board is clear col row return new place mark col row player nothing to say return null
method to determine the type of move that the user has
public boolean point on right ipoint p if is vertical return floating point lesser start get x p get x else if is horizontal return floating point greater p get y start get y double res p get y end get y m p get x end get x return floating point lesser res sign 0
given the line segment in its reverse directed orientation from lower to upper
public boolean equals object o if o null return false if o instanceof eight puzzle node return equivalent eight puzzle node o return false
determine equals via equivalence of state
public string to string return straight logic rules
return name for logic
public boolean is valid igame state game state tic tac toe state state tic tac toe state game state tic tac toe board board state board if col 0 return false if col board num columns return false if row 0 return false if row board num rows return false return board is clear col row
determines if move is valid
public boolean execute igame state game state invalid place mark move if is valid game state return false go ahead and make the move tic tac toe state state tic tac toe state game state tic tac toe board board state board move this into the empty board board set col row player get mark return true
place a mark on the tic tac toe board
public int cell int r int c return board r c
return contents of cell r c
public boolean equals object o if o null return false if o instanceof place mark place mark po place mark o return po col col po player player po row row nope return false
determine equality based on structure
public cell int c int r this col c this row r
constructs a cell object given a column and row
public tic tac toe state copy tic tac toe state state new tic tac toe state new tic tac toe board board logic copy state ctr this ctr return state
copy full state information
public tic tac toe board board return board
expose board state
public boolean is adjacent cell c if equals c return false see if we are vertically adjacent same column if col c col return math abs row c row 1 see if we are horizontally adjacent same row if row c row return math abs col c col 1 nope return false
determines if this is adjacent to the given cell
public string to string return place player get mark col row
return object in readable form
public string to string return col row
return representation of cell
public logic logic return logic
external state may be found in logic
public boolean swap int from r int from c int to r int to c if is adjacent and empty from r from c to r to c return false int tmp board to r to c board to r to c board from r from c board from r from c tmp return true
swap contents of neighboring cells
public string to string string s board to string n s stored data n stored return s
expose board state as a string
public char get mark return mark
return the mark for the player
public char get opponent mark if mark omark return xmark we must be xmark so our opponent is omark return omark
return opponent mark
public player char mark if mark empty throw new illegal argument exception player cannot have space empty as a mark if mark xmark mark omark throw new illegal argument exception player mark must be either xmark or omark this mark mark this score new igame score public int score igame state state iplayer player return 0
creates a player with a certain mark
public void reset tic tac toe board new board board new board
resets to new game with new board state and x once again starting
public int num columns return max c 1
return the number of cells on the board
public boolean equivalent igame state game state if game state null return false tic tac toe state state tic tac toe state game state return board same board state board
determine whether the state is the same by comparing the
public string to string return player mark
reasonable to string method revealing logic for player
public int eval igame state state if score null return 0 return score score state this
return the evaluation of this game state from the perspective of
public intelligent agent char mark ievaluation algorithm super mark this algorithm algorithm
construct an agent that selects the next move using the provided
public void opponent iplayer opponent this opponent opponent
set the opponent for this agent which is needed because the search for
public int num rows return max r 1
return the number of rows on the board
public igame move decide move igame state game state nothing to do in these circumstances if game state is win return null if game state is draw return null return algorithm best move game state this opponent
make an intelligent move given the board state game logic
public void visit node igraph entity n if n instanceof tic tac toe state tic tac toe state state tic tac toe state n string id s state counter nodes put id state copy return if n instanceof alpha beta debug node alpha beta debug node node alpha beta debug node n string id a node counter nodes put id node copy return super visit node n
mark node as being visited
public void logic logic logic this logic logic
set the logic of the game being played
protected string get key igraph entity value if value instanceof tic tac toe state tic tac toe state state tic tac toe state value string id s state counter return id if value instanceof alpha beta debug node alpha beta debug node state alpha beta debug node value string id a state counter return id return super get key value
helper function to reverse locate the key
public igame score score return score
returns scoring method
public line segment pair iline segment il1 iline segment il2 one il1 two il2
construct the pair
public boolean equals object o if o null return false if o instanceof line segment pair line segment pair lsp line segment pair o return lsp one one lsp two two lsp two one lsp one two return false
equality based on whether one two one two or two one
public tic tac toe board make 3x3 array cells new char 3 3 enter in values for int c 0 c max c c for int r 0 r max r r cells c r empty
constructor for tic tac toe board
public string node label dotty requires column ordered state so we rotate information string builder sb new string builder for int c 0 c tic tac toe board max c c sb append for int r 0 r tic tac toe board max r r char val board get c r if val tic tac toe board empty sb append else sb append val if r tic tac toe board max r 1 sb append sb append if c tic tac toe board max c 1 sb append return sb to string
note that game state changes constantly so we can do nothing
public string to string return one two
reasonable to string
public void score igame score score this score score
set the scoring method to use
public int compare to fifteen puzzle node n return to string compare to n to string
offer rudimentary compare to method by comparing boards
public int compare ipoint o1 ipoint o2 double fp floating point value o1 get y o2 get y if fp 0 return 1 if fp 0 return 1 must be same revert to second condition fp floating point value o1 get x o2 get x if fp 0 return 1 if fp 0 return 1 same return 0
comparison assumes a horizontal sweep line starting from the top
public tic tac toe board tic tac toe board board make 3x3 array cells new char 3 3 enter in values for int c 0 c max c c for int r 0 r max r r cells c r board cells c r
copy constructor for tic tac toe board
public int compare event point o1 event point o2 double fp floating point value o1 point get y o2 point get y if fp 0 return 1 if fp 0 return 1 must be same revert to second condition fp floating point value o1 point get x o2 point get x if fp 0 return 1 if fp 0 return 1 same return 0
comparison assumes a horizontal sweep line starting from the top
public line sweep super
construct a sweep object
public event point ipoint p this point p
constructor for the event point when not an upper start endpoint
public int compare event point o1 event point o2 double fp floating point value o1 point get y o2 point get y if fp 0 return 1 if fp 0 return 1 must be same revert to second condition fp floating point value o1 point get x o2 point get x if fp 0 return 1 if fp 0 return 1 return 0
comparison assumes a horizontal sweep line starting from the top
public void insert segments list iline segment list node iline segment node list head while node null dlstate insert node value node node next
insert the set of line segments into the state at their proper location
public boolean is empty int r int c return board r c empty mark
determine is the given r c in the board is empty
public void add upper line segment iline segment upper if upper get start equals point upper segments append upper else throw new illegal argument exception improper attempt to add line segment whose upper point does not match event point
add this line segment but only if its upper i
public void delete range double node iline segment left double node iline segment right double node iline segment t if left null t dlstate first else t successor left while t right dlstate remove t note we always go back to the original spot since we want to drain everything in between note that left never leaves the tree convenient if left null t dlstate first else t successor left
delete all line segments from the state in the range left right
public void set sweep point ipoint pt this sweep pt pt
set where the sweep line is to appear
public boolean is empty return events size 0
return whether the event queue is empty
public int hash code int hash 1 for int c 0 c max c c for int r 0 r max r r hash c 3 r cells c r return hash
to enable this board to be a key in a hashtable
public void insert event point ep event point old one events insert ep ep if old one null make sure upper end points are inserted ep add upper line segments old one upper endpoint segments
insert the event point into the queue taking care to properly
public void insert segments list iline segment list node iline segment node list head while node null state insert node value node node next
insert the segments into the line state
public void add lower line segment iline segment lower if lower get end equals point lower segments append lower else throw new illegal argument exception improper attempt to add line segment whose lower point does not match event point
add this line segment but only if its lower i
public event point min balanced binary node event point event point bn events first node event point key bn key events remove key we can t just return the key since in our implementation the value contains the full set of upper line segments this is why we return the value even though the actual event point coordinates would otherwise be the same return bn value
remove and return left most child the smallest one
public boolean contains event point ep balanced binary node event point event point bn events get entry ep return bn null
determine whether event point already exists within the queue
public event point event event point ep balanced binary node event point event point bn events get entry ep if bn null return null return bn key
determine whether event point already exists within the queue and
public void add intersecting line segment iline segment ints intersecting segments append ints
add this line segment as an intersecting one
public void add upper line segments list iline segment list upper segments concat list
batch process a set of upper insertions here
public string node label string builder sb new string builder for int c 0 c max c c sb append for int r 0 r max r r int val cell r c if val empty mark sb append else sb append val if r max r 1 sb append sb append if c max c 1 sb append try to cross purpose when scores are available if debug sb append append score append score append return sb to string
return label to appear within the debugger output
public string to string string s note that must go row by row for int r 0 r max r r for int c 0 c max c c s s cells c r if c cells length 1 s s if r cells 0 length 1 s s n n fill in the calculation here return s
return the state of the board as a string
private string concat list iline segment list string builder res new string builder node iline segment n list head while n null res append n value n n next if n null res append return res to string
helper for to string in concatenating lists
public void insert event point ep iterator event point it events iterator while it has next event point exist it next if exist equals ep exist add upper line segments ep upper endpoint segments return if we get here then wasn t in set to begin with events add ep
insert the event point into the queue taking care to properly
public event point min return events poll
remove and return left most child the smallest one
public int compare to eight puzzle node n return to string compare to n to string
offer rudimentary compare to method by comparing boards
public boolean contains event point ep return events contains ep
determine whether event point already exists within the queue
public boolean game won return game won player xmark game won player omark
determines whether game has been won
public imulti point nearest imulti point x double dist integer max value int idx 1 double xraw x raw for int i 0 i points size i double d 0 double rawpt points get i for int j 0 j xraw length j double delta xraw j rawpt j d delta delta d math sqrt d if d dist dist d idx i return results idx
return the closest point to x within the input set p
public uniform generator int ratio this length 1 0 ratio
ratio is in the range 1
public grid generator double height double skew max height this skew skew
maximum height of the ladder
public double generator double max double length this length length this max max if length max throw new illegal argument exception unable to consistently generate line segments of length length within square box of dimension max
prepare generator with boundaries of the square together with the desired
public integer generator int max int length this length length this max max
prepare generator with boundaries of the square together with the desired
public hub generator double length double x double y max length this x x this y y
hub needs its length x and y coordinate
public sliding ladder generator double height max height
maximum height of the ladder
public uniform generator double radius this radius radius
radius of the circle to be generated
private void propagate augmented node k t augmented node k lf augmented node k rt while t null lf t left rt t right boolean updated false if t min equals lf min t min lf min updated true if t max equals rt max t max rt max updated true continue if updated break t t parent
update min max segments up the tree
public circle generator this 1
construct default generator with radius of 1
public uniform generator int dimensions double scale this dimensions dimensions this scale scale
construct generator given desired number of dimensions and scale
public circle generator int r this radius r
construct generator with given radius
public long time return computed time
return last time to complete algorithm in milliseconds
public char get int col int row return cells col row
returns marker at given spot
public vertex info int previous boolean forward this previous previous this forward forward
constructs a vertex in the augmenting path where previous records the prior
public vertex info int previous this previous true
by default the vertex info in the path is forward looking
public boolean add a a b b if set b contains key a set a contains key b throw new illegal argument exception non bipartite graph would be formed by adding edge a b if set a contains key a set a put a ctr map a put ctr a if set b contains key b set b put b ctr map b put ctr b edge info ei new edge info set a get a set b get b 1 if pairings contains key ei return false pairings put ei true return true
add a pairing a b to the set
public search flow network network this source index network source index this sink index network sink index this num vertices network num vertices
configure this augmenting path search with the information it needs to process
public boolean is clear int col int row return cells col row empty
determines if cell is empty