View unanswered posts | View active topics
It is currently Sun Jun 08, 2025 1:42 pm
Author |
Message |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|

I'm at a good point to stop working and go to bed happy now.  I've written all my classes. Players, pieces, and the board. Also written the code to setup the pieces on the board and print out the board at will in a cool ASCII art fake GUI style in the terminal. My aim for tomorrow is to enable the players to move their pieces around the board freely. From there I can add rules to restrict and validate the movements. Cheers for the tip about the array instantiation Oli - that really helped! Another problem I was having was that I was trying to setup the board and pieces in the constructor of the ChessBoard class, which is the main class. I don't know, but I've got a hunch that perhaps the array isn't properly declared/instantiated/ready for use until the constructor has finished running. As it stands, I've got a new method in the class .populateBoard() which gets run from main, and that works fine. If I put the same code from .populateBoard() into the constructor it doesn't work. *Shrug* Hopefully I won't need to come back and ask more questions, but I wouldn't bet against it! Goodnight y'all! 
_________________
|
Fri Jan 29, 2010 1:09 am |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
LOL, that's very odd. Although, I have seen similar stuff happen with my own code. Glad it's working now  Question: What happens if you run the populateBoard at the end of the constructor of ChessBoard? i.e. as the final line of the constructor.
|
Fri Jan 29, 2010 7:25 am |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
I'd be interested to see how you do the moving of pieces.
I can only think (after about 10 seconds of thought) of one way of doing it.
Also, I'd be very interested to hear if you are writing an AI player with any intelligence.
|
Fri Jan 29, 2010 7:35 am |
|
 |
finlay666
Spends far too much time on here
Joined: Thu Apr 23, 2009 9:40 pm Posts: 4876 Location: Newcastle
|
I'd suggest you have a function that when called for each piece returns legal moves, this will be based on the rules of the game for each piece
Other thing I would suggest is to instead of having a piece type of empty, have a 3rd player called 'empty' for empty squares, OR for each square on the board have an enum to describe it 0 - unoccupied 1 - player 1 2 - player 2 then you can query the position to see if it's occupied. I thought to suggest this as otherwise every square has a player assigned to it which could make testing/debugging complex
_________________TwitterCharlie Brooker: Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.
|
Fri Jan 29, 2010 1:04 pm |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|

Thanks for the replies guys. Oli - I don't plan to make the computer very intelligent, although there are marks available for making the computer "reactive". So if I put his king in check and he moves it, that is worth marks. There are extra marks available for making the computer player makes his decisions on piece value.
The computer player doesn't have to attack at all, just move it's own pieces out of danger when appropriate.
So far today I have got the pieces to move around the board freely.
Now I'm trying to incorporate rules, and I planned to give every sublcass (Rook, Pawn etc) a method .validMove( int[] currentLocation, int[] targetLocation ) so they can run some checks and ensure that they can move to the user's desired square. Returns boolean.
So, the plan is to get the start location from the user, and run .validMove() on the object stored at that location.
The problem I'm having is that when I try to do this, it tries to run .validMove() from the superclass Piece, not the subclass Rook, Knight, Pawn etc I've used .getClass() and can confirm that my pieces are stored in the array as their correct subclasses, so why does it want to run the .validMove() from the Piece superclass? And is there a way to run .validMove() on the subclass?
_________________
|
Fri Jan 29, 2010 1:42 pm |
|
 |
finlay666
Spends far too much time on here
Joined: Thu Apr 23, 2009 9:40 pm Posts: 4876 Location: Newcastle
|
You can, all it will need to do is have an overloaded function from the parent class, does .validMove() take in the position to move to, then take the type then check a move? You could change the function name in the superclass so it takes a piece type in the parameter as well as the position so something like:
_________________TwitterCharlie Brooker: Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.
|
Fri Jan 29, 2010 1:55 pm |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|
Well right now I don't have .validMove() in the superclass at all. I thought that not including it in the superclass would make it run on the subclass, perhaps I've misunderstood the principal? Are you suggesting that I should have .validmove() in both the subclass and superclass, and then the superclass can act as a middle man?
_________________
|
Fri Jan 29, 2010 2:10 pm |
|
 |
EddArmitage
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 9:40 pm Posts: 5288 Location: ln -s /London ~
|
If it's not in the super class, how does something that only knows you have a piece know that, for example, a pawn has a .validMove() method. Make piece abstract (ie. it just contains variable names and method stubs), and it'll all become clear.
|
Fri Jan 29, 2010 2:18 pm |
|
 |
finlay666
Spends far too much time on here
Joined: Thu Apr 23, 2009 9:40 pm Posts: 4876 Location: Newcastle
|
Yes, kindof in the superclass have a virtual function .validMove() to check the position, this is just stubbed, you also have a function with overloaded parameters the same as .validMove() but with the type too, or pointer to the class etc. in the inheriting class you implement the .validMove() function (as in write the function) and have it call the parent class function of .validMove(type). As this isn't overloaded in the child class the parent one is called. Might seem confusing but if you look at inheritance in functions and overloads/virtual functions it should be some more help. Its a much more elegant way of doing it than writing excess code 
_________________TwitterCharlie Brooker: Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.
|
Fri Jan 29, 2010 2:58 pm |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|
Thanks for all the tips and guidance everyone. I followed your advice Edd and Fin, and made the superclass Piece abstract. My chess game is working!  <---- shock  <---- happiness  <---- smugness at being the closest to finishing this assignment of all my friends. At the moment, you can only move the pawns - but the moves are validated and they do what you ask them. The good news is that getting this far confirms my program structure is working, and it's just a case of adding rules to validate the other Pieces moves. The bad news is that the other pieces are more complicated to validate than the Pawn! I'm pretty sure that once all the pieces move, and only valid moves are allowed, that's a pass. From there I will work on the computer player. Thanks again guys, I'm chuffed!
_________________
|
Sat Jan 30, 2010 12:23 am |
|
 |
finlay666
Spends far too much time on here
Joined: Thu Apr 23, 2009 9:40 pm Posts: 4876 Location: Newcastle
|
this moveset is kindof an abstract, but it's actually simple thinking about it mathematically
King can move x,y where -1<x, y < 1 (so anything in a 3x3 grid) bishop can move diagonally, so 0-x<x<8-x and 0-y<y<8-y and x==y rook can move either vertical/horizontal so 0-x<x<8-x || o-y<y<9-y knight is surprisingly easy, you just generate a set of valid squares (there are only 8 at most for any square on the board), where it's in the form 3x +/-y or 3y +/-x where x = 1/-1 Queen is a combination of bishop and rook rule sets
do the pawns take correctly? as in valid taking squares are diagonal ahead?
In my intelligent systems my lecturer keeps trying to tell me that chess AI is a true intelligent system, it's hard to show that a program he couldn't beat in 38k of memory is in fact pretty damn dumb from true AI lol
_________________TwitterCharlie Brooker: Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.
|
Sat Jan 30, 2010 12:56 am |
|
 |
forquare1
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 6:36 pm Posts: 5150 Location: /dev/tty0
|
This thread it too much to read through for me now. If you are using ArrayLists in Java 1.5 or up, you really should be using generics. Generics are the "<E>" in the API (Class ArrayList<E>). Without generics the ArrayList will hold anything of type Object. Seeing as everything in Java descends from the Object class, the ArrayList will accept ANYTHING in it. Generics make ArrayLists safer by restricting the type of object you can put in them: This will work. If you do this to the above ArrayList: This will not work because String isn't a descendant of Piece. Generics work with Vectors too, which as I said before are thread safe. Makes me wonder why the ArrayList class hasn't been depreciated...
|
Sat Jan 30, 2010 2:29 pm |
|
 |
EddArmitage
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 9:40 pm Posts: 5288 Location: ln -s /London ~
|
Oooh, good spot Ben! I hadn't noticed there weren't generics there. And thank you for reminding me yet again to use Vector not ArrayList. I swear next time I have to do any Java work I'll still default to using ArrayLists though!
Did you make any progress, Nick?
|
Mon Feb 01, 2010 5:24 pm |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|
I'm still working on the rules for different pieces. A lot of trial and error involved! 
_________________
|
Tue Feb 02, 2010 4:05 pm |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|
I've had a pretty good evening tonight - I've got Pawns, Knights and Rooks working including their capturing moves. That means I've only got to do Bishop and King - Queen is going to be easy because it's just a combined Rook and Bishop effectively
I also got some validation going on the user input, so it shouldn't crash when the user asks to do stupid stuff.
So hopefully I'll get all the pieces working tomorrow, then it's just the AI. *gulp*
_________________
|
Wed Feb 03, 2010 12:26 am |
|
|
Who is online |
Users browsing this forum: No registered users and 4 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum
|
|