MVC and chess game design
Hi,
I am trying to do a chess game using mvc. The view obviously should handle all the drawing stuff and the model the logic, which means in mvc I have to have a pawn concrete class (model) and a pawnviewer class (view)?
If that is the case is there a better solution than to check for the chess piece type everytime in a switch statement ->
switch(pieceType)
{
if(pieceType == Model.PAWN)
{
drawPiece(
}
etc
}
[483 byte] By [
Galaxya] at [2007-10-2 21:08:55]

> Hi,
>
> I am trying to do a chess game using mvc. The view
> obviously should handle all the drawing stuff and the
> model the logic, which means in mvc I have to have a
> pawn concrete class (model) and a pawnviewer class
> (view)?
>
> If that is the case is there a better solution than
> to check for the chess piece type everytime in a
> switch statement ->
>
> switch(pieceType)
> {
>if(pieceType == Model.PAWN)
>
>drawPiece(
>
> etc
> }
switch statements? that's so procedural. that's what polymorphism was born to eliminate.
wouldn't it be better to have a ChessPiece interface and have King, Queen, Pawn, etc. implement it according to their special needs?
go back to writing C or Fortran. That's not Java. It's not even object-oriented.
%
You've got bigger problems than MVC and drawing pieces. I'd be reading about minmax and such: http://www.gamedev.net/reference/programming/features/chess1/ http://en.wikipedia.org/wiki/Computer_chess%
Thanks for the reply.
Well if im right I have to seperate the view(display) and the model(logic) in a MVC. In a mvc (if im right) I can not use polymorpishm in a chesspiece (which is part of the model) to handle the drawing needs (image and animations) as that would violate the design. So the only solution is to create a pawnviewer which is responsible for all image and animations problems. And usually in a mvc there is an observer construction in which the model updates the view and my problem is that I have to assiocate a pawn with its pawnviewer.
Thanks for the article (really helpful!!) but minimax is about the AI and that is a problem later in the design process in my opinion.
Message was edited by:
Galaxy
why can't you use polymorphism here? I can't see why you're having to couple code to specific pieces. why do you specifically need a pawnviewer? as far as the view is concerned, the only differences between pieces is the shape that has to be drawn. chesspiece isn't restricted to being part of the model, anyway.
why bother with MVC in this case at all? MVC allows us to be adaptive, and responsive to change. are you preparing for the release of Chess 2 or something?
Well If I only have to use images for the pieces then a pawnviewer class might be too much. But it has some animations with it ( for moves and capture) which is specific to the piece itself. As for using MVC(If you got a design pattern or architecture which is better suited of a GUI based chess program please let me know!), it would be a great way to learn using it as MVC is often used for GUI-based programs.
And for chesspiece I mean the chesspiece class which is responsible for the logic(allowed moves, rules etc) which is (if im right) restricted to the model.
Message was edited by:
Galaxy
Message was edited by:
Galaxy
even in the animation scenario, you can still extract a common supertype for all the pieces. whether or not there is a "better" pattern for a chess game doesn't matter, you should consider what advantages MVC is actually giving you, in this particular case. I actually don't think it's such a great way to learn MVC because you know exactly what the system has to do already, and that won't change. why not write a "board game" framework, that allows you to plug in, say, checkers or chess to the same basic application, with as little effort as possible? that would be quite a good way to learn about the MVC, and it's advantages and disadvantages. a nice challenge would be to write the board game application, that could provide checkers, chess or a similar game, purely through configuration. the idea is to be able to write adaptable code, that's why we separate systems into layers
>> And for chesspiece I mean the chesspiece class which is responsible for the logic(allowed moves, rules etc) which is (if im right) restricted to the model
sort-of. the logic such as rules enforcement do belong in the model, but the view has to have a counterpart object. something has to make a decision about how to display the objects, which isn't really the responsibilty of the model
the more I think about it, the more I think you should try a generic boad-game application, and try to make plugging chess or checkers into it as easy as possible. sounds like an interesting excercise
A boardgames framework is a great idea!!
"sort-of. the logic such as rules enforcement do belong in the model, but the view has to have a counterpart object. something has to make a decision about how to display the objects, which isn't really the responsibilty of the model"
Thats why I have a pawnviewer(in the view package inheriting from pieceviewer, naming should have been better) and a pawn class in the model(inheriting from IPiece). The pawnviewer is responsible for all actions in the visual field, but my problem (the reason I started this thread) is looking for a way to associate a pawn with a pawnviewer. One solution is to use that switch statement above (which is indeed ugly), second a entity manager class. Any suggestions?
Message was edited by:
Galaxy
Message was edited by:
Galaxy
> Thanks for the reply.
>
> Well if im right I have to seperate the view(display)
> and the model(logic) in a MVC.
Yes, correct.
> In a mvc (if im right)
> I can not use polymorpishm in a chesspiece (which is
> part of the model) to handle the drawing needs (image
> and animations) as that would violate the design.
You're quite wrong here. I think that's taking it too far. There's "logic" model and "view" model. Even Swing has Model classes (e.g., javax.swing.table.AbstractTableModel). You are being far too extreme in saying that you can't use polymorphism.
> So
> the only solution is to create a pawnviewer which is
> responsible for all image and animations problems.
> And usually in a mvc there is an observer
> construction in which the model updates the view and
> my problem is that I have to assiocate a pawn with
> its pawnviewer.
You sound like you've got "small boy with a pattern" disease.
> Thanks for the article (really helpful!!) but minimax
> is about the AI and that is a problem later in the
> design process in my opinion.
You might be focusing on the wrong thing. I'd recommend getting the game logic right first, without a graphic UI. If you can manage that with only a text UI first you'll be guaranteed of good layering and MVC separation. You're going about it wrong by starting with the graphics first.
%
> A boardgames framework is a great idea!!
Yes, that's where minmax fits in.
> Thats why I have a pawnviewer(in the view package
> inheriting from pieceviewer, naming should have been
> better) and a pawn class in the model(inheriting from
> IPiece). The pawnviewer is responsible for all
> actions in the visual field, but my problem (the
> reason I started this thread) is looking for a way to
> associate a pawn with a pawnviewer. One solution is
> to use that switch statement above (which is indeed
> ugly), second a entity manager class. Any
> suggestions?
"Small boy with a pattern disease".
Forget about the piece viewers and managers. Get the logic right with a text UI first. Then when you can easily add on the graphic UI you'll know that you've gotten the MVC separation right.
%
Hi,
>Yes, that's where minmax fits in.
minmax do you mean min max search, which is used to generate moves for the computer AI. If so I fail to see what it has to do with making the initial framework as the AI can be implemented later (which is specific on which boardgame ). If not can you provide a link, thanks.
>You're quite wrong here. I think that's taking it too far. There's "logic" model and >"view" model. Even Swing has Model classes (e.g., >javax.swing.table.AbstractTableModel). You are being far too extreme in saying >that you can't use polymorphism.
Sorry if it was unclear. But Im talking about chesspiece in the model which shouldnt have any drawing methods. I do plan to use polymorphism in the viewer in the displaying the animations and images.
>You sound like you've got "small boy with a pattern" disease.
The goal of this entire chess game is to learn about design patterns, so it is good to have a pattern frenzy :).
Well the model as it now stands is looking good, the problem I have is with the view project.
I just want to find a way to display the images and animations when the model updates the view with events.
Thanks for any suggestions.
Message was edited by:
Galaxy
null
> Hi,
>
> >Yes, that's where minmax fits in.
>
> minmax do you mean min max search, which is used to
> generate moves for the computer AI. If so I fail to
> see what it has to do with making the initial
> framework as the AI can be implemented later (which
> is specific on which boardgame ). If not can you
> provide a link, thanks.
Like this - game moves.
http://www.cs.mcgill.ca/~cs251/OldCourses/1997/topic11/
See? Games, not AI.
> Sorry if it was unclear. But Im talking about
> chesspiece in the model which shouldnt have any
> drawing methods. I do plan to use polymorphism in the
> viewer in the displaying the animations and images.
> The goal of this entire chess game is to learn about
> design patterns, so it is good to have a pattern
> frenzy :).
Yes. It'll be good to realize when you've gone too far.
> Well the model as it now stands is looking good, the
> problem I have is with the view project.
> I just want to find a way to display the images and
> animations when the model updates the view with
> events.
>
> Thanks for any suggestions.
>
> Message was edited by:
> Galaxy
%
Ok, maybe some other guy can point out that I am really wrong about this. But what your articles states: " Now, we can use this game tree to analyze the best possible move." The purpose of that game tree is to get the next best move so that is an AI isnt it (otherwise why would one need such extensive algorithm for a chess game with two human players, so if you would make a chess game in which only human players can play do you see a need for this?).
Like I said I still fail to see the use of min max search tree in a generic boardgame framework in which you (as georgemc describes it) can fit in a checkers or chess gam. As the implementation of that kinda tree would depend on the game specific rules.
Anywaythanks for the suggestions.
> Ok, maybe some other guy can point out that I am
> really wrong about this. But what your articles
> states: " Now, we can use this game tree to analyze
> the best possible move." The purpose of that game
> tree is to get the next best move so that is an AI
> isnt it (otherwise why would one need such extensive
> algorithm for a chess game with two human players, so
> if you would make a chess game in which only human
> players can play do you see a need for this?).
I'll have to go back through the thread to see where you stated that you were writing this for two human players. If I don't find it, you'll have to at least concede that it would be reasonable for someone to assume that you would need a capability to have the machine be one of the players.
Yes, you might call it "AI", but it's been about calculating a "best move" for a game all along. It's certainly part of any gaming framework.
> Like I said I still fail to see the use of min max
> search tree in a generic boardgame framework in which
> you (as georgemc describes it) can fit in a checkers
> or chess gam. As the implementation of that kinda
> tree would depend on the game specific rules.
True. I'll concede that.
%
Well this is what I stated:
>Thanks for the article (really helpful!!) but minimax is about the AI and that
>is a
>problem later in the design process in my opinion.
Sure its great to have a minmax algorithm in the game,
But I dont really need it now(its way down on my to-do list).
The two players example is to show that it is not usefull in a chess game without an AI. Which is a reaction to this:
>See? Games, not AI.
Anyway I dont know how a question about Chess view design turns into an AI debate, but I found some solution for it.
Thanks for the suggestions!
Topic can be closed!