Reply to topic  [ 64 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next
OOP problems 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Nick wrote:
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*

Sounds like it's going well!

As far as the AI is concerned I would set up a random number generator that then selects one of the pieces to move and a random valid square to move it to. Then check the destination square isn't under threat before moving.

If a piece is under threat then likewise, choose that piece and then move it to a safe square.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Feb 03, 2010 8:47 am
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Right, so having said Queen will be easy - that's the one I'm having trouble with! :lol:

The problem is that because Queen is basically a combination of both Bishop and Rook, I want to validate the Queen's moves by calling the validate method on both Rook and Bishop. If either one of those come back as valid, then the queen can move. If they both say invalid - then she can't move. Simples. ;)

The problem is - Piece is abstract, and has an abstract method to validate a move.

Each subclass then has it's own validation method - and that works a treat.

The problem is, how do I get Bishop.validate() and Rook.validate() to run? They're not static, so it doesn't work as I've been trying it - and they can't be made static of course.

Is there a solution? Or will I just have to copy and paste the code? I hope not - that seems pretty stupid, seeing as it's already sat there!

_________________
Image


Thu Feb 04, 2010 1:22 am
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Nick wrote:
Right, so having said Queen will be easy - that's the one I'm having trouble with! :lol:

The problem is that because Queen is basically a combination of both Bishop and Rook, I want to validate the Queen's moves by calling the validate method on both Rook and Bishop. If either one of those come back as valid, then the queen can move. If they both say invalid - then she can't move. Simples. ;)

The problem is - Piece is abstract, and has an abstract method to validate a move.

Each subclass then has it's own validation method - and that works a treat.

The problem is, how do I get Bishop.validate() and Rook.validate() to run? They're not static, so it doesn't work as I've been trying it - and they can't be made static of course.

Is there a solution? Or will I just have to copy and paste the code? I hope not - that seems pretty stupid, seeing as it's already sat there!


What do your validate moves take in? I would use the current position and the position to move to as a parameter

that way you can call
if(Bishop.validate (queenpos, newpos) || Rook.validate(queenpos, newpos))

Your validate move will need to check the actual line of movement isn't blocked as well, as you can't move Qw---Kw----Qb as your own king is in the way, same with Qw---Qb-----Bb as the black queen is in the way

_________________
Twitter
Charlie 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.


Thu Feb 04, 2010 1:54 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
finlay666 wrote:
What do your validate moves take in? I would use the current position and the position to move to as a parameter

that way you can call
if(Bishop.validate (queenpos, newpos) || Rook.validate(queenpos, newpos))

Your validate move will need to check the actual line of movement isn't blocked as well, as you can't move Qw---Kw----Qb as your own king is in the way, same with Qw---Qb-----Bb as the black queen is in the way

To do that he would have to make the methods static which would work.

Is there a "more OO" way of doing it?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Feb 04, 2010 2:16 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Fin - that is exactly how it's working at the moment - they assess the current location and target location, then return true or false.

The problem is that because the Piece class has an abstract method validate(), Bishop.validate() doesn't work. As Oli says, to do that the method has to be static. I can't make it static obviously, so is there another way it can be done?

As a last resort I can copy and paste the code - but that just seems wrong!

_________________
Image


Thu Feb 04, 2010 3:42 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Oh god - I've just realised that this could be a really major issue for my application! :shock:

Obviously, the king isn't allowed to move into a location that is in striking range of the opponant - so I need the King to be able to quiz the other pieces locations in order to find out if his desired location is valid for them. If it is - he can't move there, but as it stands he can't do that of course.

Also - the AI is going to need to be able to tell which pieces are in range of each other, and needs to be able to run this validation method in order to decide on moves etc.

I've not been able to find a solution yet.

I'm really hoping someone here will be able to give me a suggestion or two?

_________________
Image


Thu Feb 04, 2010 5:29 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Nick wrote:
Oh god - I've just realised that this could be a really major issue for my application! :shock:

Obviously, the king isn't allowed to move into a location that is in striking range of the opponant - so I need the King to be able to quiz the other pieces locations in order to find out if his desired location is valid for them. If it is - he can't move there, but as it stands he can't do that of course.

Also - the AI is going to need to be able to tell which pieces are in range of each other, and needs to be able to run this validation method in order to decide on moves etc.

I've not been able to find a solution yet.

I'm really hoping someone here will be able to give me a suggestion or two?


You COULD implement a solution where you check for all other move types to see if that move is valid? or just query each piece the opponent has

OR

Before the king moves, find all squares that aren't being 'guarded' by the opponent? you can do that as your check/checkmate stuff checking too then

_________________
Twitter
Charlie 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.


Thu Feb 04, 2010 5:51 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Thanks for the reply Fin - I've managed to cobble something together.

It aint pretty - but it's working.

Piece has a new method - it now looks something like this:

Code:
public static boolean getValidateMove(int targetX, int targetY, int currentX, int currentY, Piece pieceObject) {
      boolean returnVal = pieceObject.validateMove( targetX, targetY, currentX, currentY );
      return returnVal;
   }
   
abstract public boolean validateMove(int targetX, int targetY, int currentX, int currentY);


It's a messy solution, but it will have to do!

I'll post a link to all my code once the deadline has passed, for anybody who may be interested.

_________________
Image


Thu Feb 04, 2010 6:01 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Hows it going?

You managed to get the AI stuff working yet?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Sun Feb 14, 2010 3:17 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
It's all done and handed in.

I handed it in a week ago.

I will post my code tomorrow - just incase any of you need a laugh at work. ;) :P

In the end it was hacked and bodged together - but it (very nearly) works.

It validates the user's moves alright - there are a couple of bugs, but it's pretty good.

The computer does move - but not with any real intelligence. If a piece is at risk, it will move that piece. If there is not a piece at risk, it will move what ever it fancies.

There are a couple of glaring problems - checkmate doesn't work, for example! :lol:

We had a lecture, and our lecturer basically gave us all a, ... well, lecture really lol about how obviously at least half of us haven't understood the OOP principle, and then fired very basic questions about how to move data around, how to pass control between objects etc etc etc

The upshot is that I'm feeling more confident about my coursework. I'm relatively comfortable with OOP, and although my code might be a bit ugly it does use OOP principles, and that's what he wants more than elegant code.

As I said, I'll post my code tomorrow - I'm not on my desktop right now. :)

_________________
Image


Mon Feb 15, 2010 1:36 am
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Game code + OOP doesn't mix very well

A game programmers way is brute forcing something into working most of the time :)

_________________
Twitter
Charlie 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.


Mon Feb 15, 2010 11:47 am
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Okay - drum roll please!

*pats desk rapidly*

[url="http://www.file-cabinet.co.uk/x404/Code.zip"]Here it is[/url]

You'll have to "javac" "java" it - I've only included the .java files. :)

_________________
Image


Mon Feb 15, 2010 1:05 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
I had a brief look at the code and it's all very readable :D

Looks good.

I thought I'd tag on to this thread also :D

I've been doing some iPhone stuff and I've had my head stuck in OOP mode for most of the week.

Well, I finally set up my program (about the 5th or 6th iteration due to learning whilst programming and testing principals) in an OO way that I'm actually quite pleased with :D

I've set up a generic WebService class that I can then subclass to create specific web services with their own specific methods.

I've also got a generic XML parser that I can subclass and it will parse any XML from a given table (back end database table) and return an Array containing Dictionaries given specific field names and table names.

Lastly I've got a subClass of the Array that gets returned which contains methods for either getting out all the [given field name] values from the array or it will return a Dictionary for a given field/value pair :D

I'm quite pleased with it and I think this thread definitely helped me designing it :D

Thanks

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Feb 19, 2010 5:58 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Excellent - a win, win thread! :D

I'm glad you found my code readable - I always try hard to make it easy to follow. Seeing as it's going to have to be marked by someone, it's in my interest to use sensible names for variables, and plenty of comments.

I think we might be re-writing the game as part of a further assignment though. I'm not sure if that's a good or bad thing though. If I was asked to re-do it right now I know there are a few things I would do differently, but in time it will slip to the back of my mind and I'm afraid I might make the same mistakes all over again. Hopefully reading the code back will remind me of the problems I was having towards the end of the project.

_________________
Image


Fri Feb 19, 2010 8:25 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Nick wrote:
I think we might be re-writing the game as part of a further assignment though. I'm not sure if that's a good or bad thing though. If I was asked to re-do it right now I know there are a few things I would do differently, but in time it will slip to the back of my mind and I'm afraid I might make the same mistakes all over again. Hopefully reading the code back will remind me of the problems I was having towards the end of the project.

With almost every program I write (especially with a new language :lol:) I always find I "write" it a few times before getting it to the state I am happy with.

It sounds a bit stupid but I always find that by doing it a couple of times before designing it on paper I get a feel for what can be done and how to do it.

i.e. make a few mistakes first and then design what you want and then do it properly :D

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Feb 19, 2010 8:42 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 64 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

Who is online

Users browsing this forum: No registered users and 18 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

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.