<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel><generator>iloblog 1.0</generator><title>My tech stuff Feed</title><link>http://sabretooth.sabrefilms.co.uk/</link><description></description><item><title>Chess Engine Part 4: Moving Pieces</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=19</link><description><![CDATA[ Well, they don't yet move to valid location only, but one step at a time ;)  The ChessBoard class now has the following instance variables:  boolean canAccessList = true; //the ai will update the list. While this is the case the list will be inaccessable.   int startField; //start square for a move  int endField; //end square for a move  int xCoord;  int yCoord;  As I wanted to paint a selected square orange and allow the user to drag the mouse around the board painting the current hovered square green if it is a valid move and red if not, there needed to be changes to the paintComponent method. Before discussing this however, we need to see the new method validMove() which will eventually check for valid moves.  public boolean validMove(int moveNum)  {  return true; //just for now...  }  Also there is a very nice helper method called checkStatus. The method comment should explain this well:   /**  * Used by paintComponent() and mouseReleased()  * Checks status of a currently hovered square during a mousedrag  * Returns true only if the list is currently accessible  * AND it is a valid move  * The result of this method determines whether the square should be painted  * green (valid) or red (invalid)  * @param square  * @return  */  private boolean checkStatus(int square)  {  return canAccessList &amp;&amp; validMove(startField*100+endField);  }  Note that startField*100+endField gives the unique move ID mentioned in the last post.  Here is the extra paintComponent code which is added between the code to paint the squares and the code to paint the pieces (once again, apologies for lack of formatting)   if(i == endField)  {  if(checkStatus(i))  {  g.setColor(Color.GREEN);  }  else  {  g.setColor(Color.RED);  }  }  if(i == startField)  {  g.setColor(Color.ORANGE);  }  Hopefully this will be self explanatory. Notice that code to paint the start square comes after the code to paint the currently hovered square (end square). This is so that if we hover over the start square, the colour is always overwritten with orange.  Finally there are the mouse methods. mousePressed and mouseDragged contain the same code:   xCoord = e.getX() / SQUARE;  if(xCoord &lt; 0)  {  xCoord = 0;  }  if(xCoord &gt; 7)  {  xCoord = 7;  }   yCoord = e.getY() / SQUARE;  if(yCoord &lt; 0)  {  yCoord = 0;  }  if(yCoord &gt; 7)  {  yCoord = 7;  }    /*  * removing the 26 start squares we are looking   * for yCoord squares down (hence *12 = a whole row)  * and then across x squares  */  startField = yCoord*12 + xCoord + 26;  repaint();    mouseReleased looks like this:   public void mouseReleased(MouseEvent e)   {  if(checkStatus(endField))  {  makeMove(startField,endField);  }  startField = 0;  endField = 0;  repaint();  }  mouseReleased also uses the checkStatus method to commit to executing a move. Orange and green/red squares are reset by editing startfield and endField.   Here  is a video demonstration of the app at this stage  
 ]]></description><pubDate>Fri, 04 May 2012 20:48:51 +0100</pubDate><category>Programming</category></item><item><title>Chess Engine Aside: Pre-Algorithm Planning</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=18</link><description><![CDATA[ Having done a lot of research on the topic I have decided to capture possible valid moves at any given point as unique integers. I have seen a few ways of doing this. One of the most popular looks similar to this:  startSquare * 100 + endSquare  Meaning if we are looking at a white pawn on square 86 and moving it forward a row to square 74 we would have this move stored as:  86*100 +74 == 8674  which in effect is basically startsquareendsquare.  This seems like a pretty logical move as it would be then very easy to map to chess notation (in this case a2-&gt;a3)  The idea is to add all valid moves at a given time to an array to then be evaluated.   Next part of the ChessEngine   
 ]]></description><pubDate>Fri, 04 May 2012 17:12:29 +0100</pubDate><category>Programming</category></item><item><title>Chess Engine Part 3: Rewriting To Conform With Swing</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=17</link><description><![CDATA[ So it seems I messed up ;)  The paintSquare() method requires an argument of type Graphics which is passed to it from paintComponent. This is all very well but here is my original code to execute an already validated move:   public void makeMove(int start, int end)  {  board[end] = board[start]; //move from start to end position  board[start] = 00; //empty the start position   paintSquare(g,start);  paintSquare(g,end);  }  Anyone see the issue yet? g is not defined. I could of course attempt to call in the current component graphics using Graphics g = getGraphics(); then try passing it to paintSquare but Swing pretty much discourages this as the Graphics object should really only be modified in a paint or paintComponent method.  We could also try a repaint(); but this will paint the whole board again making the paintSquare method obsolete.  As at this stage I am looking at (fairly efficient working system &gt; totally efficient non-working system) I have decided to repaint the whole board (for now)  I have therefore commented out the paintSquare method and added the code to paintComponent() like so:   for(int i = 26; i &lt;= 117; i++) //shown squares  {   int piece; // piece to draw  int x = getXCoord(i);   int y = getYCoord(i);    if((x + y) % 2 == 0) //alternate squares  {  g.setColor(Color.GRAY);   }  else  {  g.setColor(Color.DARK_GRAY);  }  g.fillRect(x * SQUARE, y * SQUARE, SQUARE, SQUARE); //fill square    piece = board[i] - 11; //remove 11 because ex: WQ is 12 but WQ is array 1. 12-11 = 1; etc  if(piece &gt;= 0)   g.drawImage(pieces[piece], x * SQUARE, y * SQUARE, SQUARE, SQUARE, this);    if(i % 12 == 9) //skip 4 (two right, two left on the row below)  {  i += 4;  }  }   }  Also as you may have noticed, I put the code to calculate x and y in their own getter methods:   private int getXCoord(int square) // mod by total row squares to get columns  {  return (square - 26) % 12;  }   private int getYCoord(int square) // div by total row squares to get rows  {  return (square - 26) / 12;  }    Finally, makemove() now simply looks as follows:  public void makeMove(int start, int end)  {  board[end] = board[start]; //move from start to end position  board[start] = 00; //empty the start position   repaint();  }   Bear in mind that I am totally aware that this is a blow to efficiency, but I also know that in the long run, it won't make 'too much' of a difference. Refactoring and optimisation will be required at some stage in the process.   (Btw - I really need to sort out a code formatter)    Next part of the ChessEngine   
 ]]></description><pubDate>Fri, 04 May 2012 10:08:53 +0100</pubDate><category>Programming</category></item><item><title>Chess Engine Aside: Removing Early Complications</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=16</link><description><![CDATA[ One thing which was bothering me was the discrepency between the board array (0-based index) and the code to count squares (1-based index) Evidently it would be easier if these managed to coincide. therefore the code to draw squares is now one less making it a 0-based index also.   Image   Hopefully this will help to avoid any unnecessary confusion when moving pieces around and working on the algorithm.    Next part of the ChessEngine   
 ]]></description><pubDate>Thu, 03 May 2012 21:04:43 +0100</pubDate><category>Programming</category></item><item><title>Chess Engine Part 2: Painting The Pieces</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=15</link><description><![CDATA[ I have been unsure on how to document this particular section of the project and have decided to keep this to a bare minimum, showing snippets of code in image format. Firstly, the (rather ugly) piece icons which I am using come from  here . I will eventually find a more aesthetically pleasing iconset but for now these will do fine.  This image shows how the pieces are represented:  Image   This image shows loading the images into the app and adding them to a MediaTracker:  Image   This image shows the initial starting position of the board array and what it is reset to when the New Game button is used:  Image   This image shows how the pieces are drawn over the current square in the paintSquare method based on the array value (EDIT: This method has now been changed for efficiency: Thanks dloser and livinskull for input and advice):  Image    Finally, here is how the board currently looks:   Next step - move pieces and highlight moves.   Next part of the ChessEngine   
 ]]></description><pubDate>Wed, 02 May 2012 18:33:51 +0100</pubDate><category>Programming</category></item><item><title>Chess Engine: Aside - MediaTracker</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=14</link><description><![CDATA[ I have been told that a MediaTracker object in java would be useful for handling the pieces and making sure all of them are painted onto the board before progressing.  As I have never used MediaTracker before, I'm not entirely sure how to go about this, but looking at the  documentation , it seems that I need to set each image with an id of 0 (or similar) and have the app wait on all of them.  Here's to learning something new!   Next part of the ChessEngine   
 ]]></description><pubDate>Fri, 27 Apr 2012 17:41:44 +0100</pubDate><category>Programming</category></item><item><title>Chess Engine - Part 1: Interface Issue.</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=13</link><description><![CDATA[ Well I knew this was going to be tough, but I didn't expect the interface to cause a problem.  Here is what the interface currently looks like:   And here is how  NOT  to achieve the grid effect (my first attempt):   public void paintComponent(Graphics g) {  super.paintComponent(g);  int x = 0;  int y = 0;  int blackSquare = -1;  for (int i = 0; i &lt; 64; i++)  {  if(i != 0 &amp;&amp; i % 8 == 0)  {  blackSquare = -blackSquare;  y++;  x = 0;  }  if(blackSquare != 1)  {  g.setColor(Color.WHITE);  }  else  {  g.setColor(Color.BLACK);  }  g.fillRect(x * SQUARE, y * SQUARE, SQUARE, SQUARE); //SQUARE is an int set to 40  x++;  blackSquare = -blackSquare;  } }  But why not? It is fairly efficient. It gets the job done. Well, when a piece 'moves' onto or from a square, it would be more beneficial to redraw that square without having to redraw the whole grid. After some thought on the matter and a little research into the subject I eventually decided on the method below. Bear in mind here that I have decided to make a 12*12 grid rather than an 8*8 because I am keeping a 2 square border around the visible grid to handle illegal moves so that the pieces will not fall out of the array boundary. Let's call it a lazy way to avoid exceptions ;)   public void paintComponent(Graphics g)  {  /*  * The board looks like this with a 2 square border around it  *  * 001 002 003 004 005 006 007 008 009 010 011 012  * 013 014 015 016 017 018 019 020 021 022 023 024  * 025 026 027 028 029 030 031 032 033 034 035 036  * 037 038 039 040 041 042 043 044 045 046 047 048  * 049 050 051 052 053 054 055 056 057 058 059 060  * 061 062 063 064 065 066 067 068 069 070 071 072  * 073 074 075 076 077 078 079 080 081 082 083 084  * 085 086 087 088 089 090 091 092 093 094 095 096  * 097 098 099 100 101 102 103 104 105 106 107 108  * 109 110 111 112 113 114 115 116 117 118 119 120  * 121 122 123 124 125 126 127 128 129 130 131 132  * 133 134 135 136 137 138 139 140 141 142 143 144  *  * Only the inner 8*8 are needed (starting at 27, ending at 118), therefore on  * 34, 46, 58 (basically: i mod 12 == 10) add 4 to account for  * right and left borders  */   for(int i = 27; i &lt;= 118; i++)  {  paintSquare(g,i);  if(i % 12 == 10)  {  i += 4;  }  }   }   public void paintSquare(Graphics g, int square)  {  int x = (square - 27) % 12; // mod by total row squares to get columns  int y = (square - 27) / 12; // div by total row squares to get rows   if((x + y) % 2 == 0)  {  g.setColor(Color.LIGHT_GRAY);  }  else  {  g.setColor(Color.DARK_GRAY);  }  g.fillRect(x * SQUARE, y * SQUARE, SQUARE, SQUARE);  }  As you can see paintComponent() calls the paintSquare method and 'skips over' the two border fields on the right, and the two border fields on the left (the line below) hence the i +=4 It might look a bit of a mess at the moment, but each field is now drawn individually and we are ready to add some pieces! \O/   Edit: Might I add that I do know how to format my code. It just seems that my blog application does not ;)    Next part of the ChessEngine   
 ]]></description><pubDate>Fri, 27 Apr 2012 08:51:50 +0100</pubDate><category>Programming</category></item><item><title>Creating a Chess Engine</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=12</link><description><![CDATA[ So I finally decided to start tackling the hackquest programming challenge: Move It!  The challenge idea is straightforward enough - Win against a (fairly weak to moderate) chess AI. Each move must be completed in 7 seconds. The board is presented as a PNG image.  Now I am aware from the forums that it is possible to pass this challenge using ready-made engines which can be tweaked to play against Kender's AI, however if I'm going to do something I might as well do it properly, right?   I am planning on documenting in the blog how the process is going (and I expect it to take a significant amount of time) for the chess engine and interface in general, however out of respect for Kender and hackquest.com I will not be documenting how I adapted the program to attack the challenge.  These challenges exist so people can learn from them and attempt them using their own methods.  My language of choice for this project is of course Java.  So here starts the adventure!    Next part of the ChessEngine   
 ]]></description><pubDate>Mon, 23 Apr 2012 14:30:43 +0100</pubDate><category>Programming</category></item><item><title>PHP exam at KnowledgeBlackBelt</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=11</link><description><![CDATA[ This has been around for a while now but I notice I didn't blog about it yet. Having come up with the idea for this and have written a lot of the questions, I am the exam leader for PHP fundamentals at KnowledgeblackBelt (previously JavaBlackBelt)  If you're a PHP coder or just curious, take the test and see how you do:  PHP Fundamentals   Thanks to the contributors.  
 ]]></description><pubDate>Thu, 03 Nov 2011 13:39:39 +0000</pubDate><category>Sites</category></item><item><title>How to PM someone in Google+ (and my thoughts on G+)</title><link>http://iloapp.sabrefilms.co.uk/blog/sabretooth?Home&amp;post=10</link><description><![CDATA[ I have had a few questions about this. The 'problem' is that at the moment there is no PM option. This does however work more like Twitter than Facebook when it comes to private messaging.  In your post start your message with @ or + and type the first letter of the recipients name. This will bring up a drop-down list like so:     Select the user who you are sending the PM to and you will see that in the bottom 'sharing' bar, their name has appeared next to public (in this example):     Now simply remove the public option (and any others) by clicking on their x :      Et voila. One private message. (Thanks Andrea) So although this might seem rather complicated just to send a PM, it really isn't. Is it that bad though that some people need an explanation? Yes, I would say so.  Remember that G+ is still in beta and there is a lot of work still to be done, although that is no excuse really.  So while I'm on the subject I'll answer two more 'frequently asked' questions about G+ on a more personal nature:  1. Do I like G+? Yes I like it. To me, this is what social networking should be about - Sharing updates with friends and meeting new people. I don't need 15000 apps. I have a phone for that. It's simple and effective. This is the reason I use Twitter rather than Facebook.  This brings me to the 2nd question:  2. Do I think G+ will topple Facebook? No I don't. Google+ will succeed regardless, but I think Facebook will always have a larger following. But maybe the same was said about Facebook when MySpace was the SN leader. There will always be something new coming along and I'm sure in future there will be something come up and race ahead which will leave Facebook, Google+, and all the other networks we know and love (or hate) in the dust.  
 ]]></description><pubDate>Fri, 05 Aug 2011 09:54:45 +0100</pubDate><category>Misc</category></item></channel>
</rss>
