input
stringlengths
10
828
output
stringlengths
5
107
public logic get game logic string game if game null return null if game equals annihilate return new annihilate logic else if game equals neighbor return new neighbor logic else if game equals slide return new slide logic else if game equals straight return new straight logic nothing to say return null
given the name of a game type return the appropriate
public void paint graphics g if controller null controller draw board g
every applet has a paint g method in which it can paint
public string get selected game type return game types get selected item
returns the game type that was selected
public string get oplay choice return o play choice get selected item
returns the entity computer or human playing o
private button get generate button if generate button null generate button new button generate button set label generate generate button set location new point 82 330 generate button set size new dimension 82 23 generate button add action listener new action listener public void action performed action event e generate elements return generate button
this method initializes generate button
public string get xplay choice return x play choice get selected item
returns the entity computer or human playing x
public void set move move move this move move
set the desired move for the user
public move decide move igame state board return move
simply returns the move decided via the mouse
public move decide move igame state game state while true try move m read move game state logic no move entered we must state that we forfeit our move if m null return null determine if move is valid if m is valid game state return m catch exception e e print stack trace offer something useful if we get here move is invalid system err println you entered invalid move try again
the interactive player retrieves desired move from the keyboard and tries
public void output string string output append string n
appends output to the output text area
public text field get xply if x ply null x ply new text field x ply set bounds new rectangle 368 152 25 20 return x ply
this method initializes x ply
public game controller logic logic if logic null throw new runtime exception game controller cannot have null logic board new tic tac toe board game state new tic tac toe state board logic this logic logic reset
constructs the controller and starts off with x ready to go
private text field get num field if num field null num field new text field num field set size new dimension 58 24 num field set location new point 152 300 return num field
this method initializes num field
public text field get oply if o ply null o ply new text field o ply set bounds new rectangle 369 203 25 20 return o ply
this method initializes o ply
public void action performed action event e tournament 100
play 100 games in the tournament
public iplayer get opponent player p if p null return null error if p x player return o player if p o player return x player no idea return null
return the opponent for the given player
private void draw spot graphics g int c int r char m erase if space if m tic tac toe board empty g set color color light gray g fill rect 30 cellsize c 30 cellsize r 40 40 return draw extra large character note that draw string is the only method that demands the anchor for the coordinates be lower left corner g set color color black g draw string m 40 cellsize c 60 cellsize r
draw marker for player in specific location
public cell interpret xy int x int y int col x offset x inset cellsize int row y offset y inset cellsize return new cell col row
interpret xy given the way the board is drawn and
public iplayer get oplayer return o player
returns the oplayer for the game being played
public int compute intersections intersection detection det detector det return apply intersections
compute the intersections in the model using the given algorithm
public void set oplayer iplayer player if player null throw new illegal argument exception oplayer cannot be null o player player
determines the oplayer for the game being played
public void set dynamic entity e e no items to contend with must leave now if items null return if e null truly no change if items 0 default entity return items 0 default entity else items 0 e reapply intersection algorithm apply intersections alert listeners model updated
set the dynamic entity being managed by the model
public e get dynamic entity if items 0 default entity return null return items 0
returns the dynamic entity or null if none set
public void set xplayer player player if player null throw new illegal argument exception xplayer cannot be null x player player
determines the xplayer for the game being played
public string algorithm time if detector null return return detector time
returns the time it took algorithm to compute intersection
public iplayer get xplayer return x player
returns the xplayer for the game being played
public int set completion state int state current state state return current state
set the completion state of game
private text field get text field3 if text field3 null text field3 new text field return text field3
this method initializes text field3
private void initialize this set size 850 600 this set content pane get panel this set title algorithms in a nutshell intersection example for model types
instantiate gui with reasonable title
private void report hashtable ipoint list icircle real ipoint p icircle one icircle two list icircle segs real get p if segs null segs new list icircle real put p segs segs append one segs append two
add the intersection to our report
public cell interpret xy int x int y if drawer null return drawer interpret xy x y nothing to say return null
return cell interpreting the x y point
private text field get text field4 if text field4 null text field4 new text field return text field4
this method initializes text field4
public move interpret move cell cell player p return logic interpret move game state cell col cell row p
given x y coordinates return the move equivalent
public void draw board graphics g drawer draw board g board
draws the board
private text field get text field5 if text field5 null text field5 new text field return text field5
this method initializes text field5
private text area get output if output null output new text area 6 100 output append output goes here output set preferred size new dimension 580 120 output set editable false return output
returns the output text area
public void add generator string name generator e g map put name g get current choice add name duplicate b c during startup we need both get current choice select name select name
add known generator to the pre installed set of generators
public boolean too many moves return number moves logic max number moves
some variations may have too many moves
public kdtree get tree return tree
access kdtree in model via its root node
public boolean set default string name if map contains key name get current choice select name update gui select name return true return false
of all the generators added make sure this one is the default
public imulti point get nearest return nearest
return the computed nearest point in this model
public void mouse moved mouse event e int x e get x int y e get y we need to convert the awt mouse coordinate into cartesian coordinates so it can be comparable with the cartesian coordinates in the model int ht canvas get height model compute nearest new hyperpoint new two dpoint x ht y if model get nearest null return canvas redraw state canvas repaint
automatically track and process nearest queries
public imulti point get target return target
return the target point in this model
public selectable multi point int x int y super x y
build a point from the details
public int get current turn if current player x player return xturn return oturn
return the current turn
public void set nearest imulti point p nearest p model updated
set the nearest point as determined by algorithm
public selectable multi point ipoint pt super int pt get x int pt get y
convenience constructor to draw information from ipoint
private checkbox get scale checkbox if scale checkbox null scale checkbox new checkbox scale points scale checkbox set size 40 20 scale checkbox add item listener new item listener public void item state changed item event e update display native items return scale checkbox
when scale is selected auto refresh objects
public selectable multi point imulti point pt throws illegal argument exception super int pt get coordinate 1 int pt get coordinate 2 if pt dimensionality 2 throw new illegal argument exception only able to construct from imulti point with 2 dimensions
convenience constructor to draw information from imulti point of two dimensions
public void deselect all for imulti point imp items iselectable imp select false
reset the selection status of all items
public void set active rectangle irectangle rect target rect rect alert listeners model updated
set the target query rectangle
private void initialize model new model model set listener this this set size 850 700 this set content pane get panel this set title algorithms in a nutshell nearest neighbor example
this method initializes this
public irectangle get active rectangle return target rect
return the target query
public void set balanced boolean b this balanced b
determine if the constructed kd tree is constructed by balanced algorithm
public double distance imulti point imp if imp dimensionality 2 throw new illegal argument exception distance computation can only be performed between two dimensional points double ox imp get coordinate 1 double oy imp get coordinate 2 return math sqrt ox x ox x oy y oy y
return the euclidean distance between the given multipoint
public selectable point int x int y super x y
build a point from the details
private list get style list if style list null style list new list style list choices length for string s style list choices style list add s style list set preferred size new dimension 97 117 style list select 0 style list add item listener new item listener public void item state changed item event e if e get state change item event selected style index style list get selected index if model items null algorithm algs style index refresh objects return style list
this method initializes style list
public void player iplayer player if player x player player o player throw new illegal argument exception unable to set player to non existing x or o player current player player
set the current player
protected void refresh objects if model items null error invalid model return message model items length entities constructed int num k model compute intersections algorithm message number of intersections num k message time to compute algorithm time get canvas redraw state get canvas repaint
if there is a line being dragged active line segment decorator we want
private text area get output if output null output new text area 6 100 output set size 580 120 output set editable false return output
returns the output text area
public iplayer opponent iplayer p if p x player return o player if p o player return x player nothing special to say return null
return opponent for the given player
public void drain dimensional node node super drain node selectable multi point node point select 1
mark as being drained
public void visit dimensional node node super visit node selectable multi point node point select true
mark as routine selection
private checkbox get show kdtree if show kdtree null show kdtree new checkbox show kdtree set bounds new rectangle 13 187 130 23 show kdtree set label show kd tree show kdtree add item listener new java awt event item listener public void item state changed java awt event item event e kdtree decorator set visible show kdtree get state get canvas redraw state get canvas repaint return show kdtree
this method initializes show kdtree
private void initialize model new model selectable multi point model set listener this start with brute force range query algorithm new brute force range query model this set size 800 720 this set content pane get panel this set title algorithms in a nutshell range query example
this method initializes this
public void draw element graphics sc iline segment ils convert to cartesian coordinates double x1 ils get start get x double y1 ils get start get y double x2 ils get end get x double y2 ils get end get y sc draw line int x1 int y1 int x2 int y2
provide concrete realization for drawing each element line segment
public void model updated algs example gui model model e model model e mod model e model int num k mod intersections size message number of intersections num k message time to compute mod algorithm time get canvas redraw state get canvas repaint
react to changes in model by requesting new intersection of the
public void set model model e model this model model
set the elements to be drawn by this canvas
public void draw element graphics sc icircle ic convert to cartesian coordinates double y ic get y sc draw oval int ic get x ic get radius int y ic get radius int ic get radius 2 int ic get radius 2
provide concrete realization for drawing each element line segment
protected void ensure image available not yet created the background image must do now can t do until we have a valid graphics object if screen image null screen image this create image this get width this get height
helper method to ensure that the off screen image is available
public void set drawer drawing canvas drawing this root drawing
set the decorator chain for this canvas
public void draw point graphics sc ipoint p convert to cartesian coordinates double x1 p get x double y1 p get y sc draw oval int x1 2 int y1 2 5 5
core functionality to draw a point in the canvas
public void redraw state ensure image available nothing to draw into must stop here if screen image null return clear the image graphics sc screen image get graphics sc set color color white sc fill rect 0 0 this get width this get height draw state sc have decorators take over now if root null root draw sc force the copy of the new state into our visible region repaint
draws the current state to an off screen buffer and then requests
public int compare imove o1 imove o2 jump move j1 jump move o1 jump move j2 jump move o2 int sc1 weights j1 from weights j1 over weights j1 to int sc2 weights j2 from weights j2 over weights j2 to place higher one first that is if sc1 is higher then returned value will be negative suggesting that j1 comes before j2 return sc2 sc1
if moves are assigned scores representing the pegs at play then
public void draw state graphics sc sc set color color black draw all points but only if items have been generated e its model items if its null return for e e its draw element sc e
draw the state by drawing each element
public boolean execute inode state jumping solitaire state jss jumping solitaire state state jss filled from false jss filled over false jss filled to true return true
make the move
public int eval inode state jumping solitaire state node jumping solitaire state state compute h n isolated pegs are a good thing less work to do int nf 0 for int i 0 i node filled length i if node filled i nf int ni num isolated node int hn nf ni compute g n int gn 0 depth transition t depth transition state stored data if t null gn t depth return gn hn
h n takes into account number of isolated pegs
public jumping solitaire state for int i 0 i filled length i filled i true filled 4 false
initialize to default configuration start w middle open
public inode copy return new jumping solitaire state filled
return copy of node
public jumping solitaire state int n if n 10 throw new illegal argument exception invalid triangle problem size must be 10 k n filled new boolean k for int i 0 i k i filled i true filled 4 false r 1 int math ceil math sqrt k compute moves
initialize to default configuration start w middle of third open
public void update display imulti point items if items null return model set items items transform into awt coordinates int width get canvas get width int height get canvas get height create set of entities proportional to the image native items items items transform items width height model set items items force redraw since state has changed and then repaint get canvas redraw state get canvas repaint
redisplay if possible based upon user input
public int eval inode state return eval2 state
h n takes into account number of isolated pegs
public int eval2 inode state jumping solitaire state node jumping solitaire state state compute h n isolated pegs are a good thing less work to do int nf 0 for int i 0 i node filled length i if node filled i nf int ni num isolated node int hn nf ni but tack on penalty for doubly isolated ones hn num double isolated node compute g n int gn 0 depth transition t depth transition state stored data if t null gn t depth return gn hn
h n takes into account number of isolated pegs
public int eval1 inode state jumping solitaire state node jumping solitaire state state compute h n isolated pegs are a good thing less work to do int nf 0 for int i 0 i node filled length i if node filled i nf int ni num isolated node int hn nf ni compute g n int gn 0 depth transition t depth transition state stored data if t null gn t depth return gn hn
h n takes into account number of isolated pegs
public int eval0 inode state jumping solitaire state node jumping solitaire state state double linked list imove mv node valid moves int hn mv size 2 isolated pegs count as penalty points int nf 0 for int i 0 i node filled length i if node filled i nf int ni num isolated node if ni 1 hn int math sqrt nf else hn nf ni ni compute g n int gn 0 depth transition t depth transition state stored data if t null gn t depth return gn hn
h n takes into account number of isolated pegs
public void model updated algs example gui model model imulti point model get canvas redraw state get canvas repaint
react to changes in model by requesting new intersection of the
public pair char diagonal int row this diagonal diagonal this row row
construct and make final
public string to string return diagonal row
reasonable to string
public pair nw char d2 char diagonal 1 int r2 row 1 if r2 0 return null if d2 a return null return new pair d2 r2
return nw location or null if invalid
public pair ne int r2 row 1 if r2 0 return null return new pair diagonal r2
return ne location or null if invalid
public pair e char d2 char diagonal 1 return new pair d2 row
return e location or null if invalid
public pair se char d2 char diagonal 1 int r2 row 1 return new pair d2 r2
return se location or null if invalid
public pair sw int r2 row 1 if r2 0 return null return new pair diagonal r2
return sw location or null if invalid
public pair w char d2 char diagonal 1 if d2 a return null return new pair d2 row
return nw location or null if invalid
public boolean equals object o if o null return false if o instanceof pair pair p pair o return p row row p diagonal diagonal return false
standard equals method
private checkbox get interactive if interactive checkbox null interactive checkbox new checkbox interactive checkbox set size 40 20 interactive checkbox set label interactive interactive checkbox set state true hook in the model interactive checkbox add item listener new java awt event item listener public void item state changed java awt event item event e if interactive checkbox get state model set listener main frame this else model set listener null get canvas redraw state get canvas repaint return interactive checkbox
this method initializes interactive choice
private void add to list igame state state move evaluation m list append new pair state copy m
add state to our list
public move evaluation compute best int ply igame state s iplayer player iplayer opponent this original player this state s copy move evaluation move minimax ply icomparator max player opponent return move
initiates the mini max computations by using its ply to determine the maximum
public int compare to jumping solitaire state jss for int i 0 i filled length i if filled i jss filled i return 1 if filled i jss filled i return 1 return 0 must be same
support some meaningful comparison
public int score igame state state iplayer player boolean negate false if this player player negate true int value table get mini state state state if negate value value return value
look up score from table