Hello fellow Clojurians,
I've started building an extendible board-game engine (chess/checkers
at the moment) and I'm facing a few problems...Let me explain...
For checkers I'd like to represent the board as a list of 31
positions. Of course there has to be a mapping from the 1d list to a
2d grid and vice-versa so i can later create a nice gui. Anyway I've
got all that, I've also got the Piece as a protocol which dispatches
fast based on the type of Piece when the times comes to move (there is
going to be a lot of moving). I also understand that each move should
return the updated board in order to be able to chain moves together.
My problem comes when trying to construct the new board after each
move efficiently...the move function is indeed very simple:
(defn move
"The function responsible for moving Pieces. Each piece knows how to
move itself."
[p coords]
{:pre [(== 2 (count coords))]} ;safety comes first
(do (. p update-position coords) ;coords should be of the form [x, y]
(build-board)))
but I'm not sure how to proceed to write the 'build-board'
fn...Ideally, I'd want this function to return a seq like this (this
is the initial arrangement of the board):
Clondie24.core=> (pprint (conj me (conj (repeat 8 nil) opp)))
nil((({:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=1,y=0]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=3,y=0]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=5,y=0]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=7,y=0]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=6,y=1]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=4,y=1]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=2,y=1]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=0,y=1]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=1,y=2]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=3,y=2]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=5,y=2]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=0,b=0]>,
:position #<Point java.awt.Point[x=7,y=2]>,
:rank soldier})
nil
nil
nil
nil
nil
nil
nil
nil)
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=6,y=5]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=4,y=5]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=2,y=5]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=0,y=5]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=1,y=6]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=3,y=6]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=5,y=6]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=7,y=6]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=6,y=7]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=4,y=7]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=2,y=7]>,
:rank soldier}
{:color #<Color java.awt.Color[r=255,g=255,b=0]>,
:position #<Point java.awt.Point[x=0,y=7]>,
:rank soldier})
I've spent a couple of hours on this and unless I use a map it is not
obvious to me how to do that...how can I loop through a list of Pieces
(records) , look at their position (list position-NOT grid) and
associate them to appropriate vector indices? For example let's say
that I encounter a piece with position 3 - how do I put it in the 3rd
index of a vector?
Thanks in advance...
Jim