Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Board Game - how to display?

I know this is probably a really stupid question, but what is the best way to handle the display of my pieces vs my opponents pieces during a board game?

What I mean is, say I am playing a turn-based game, where I make a move, send it to my opponent via e-mail, he responds back, etc.

When I am playing the game on my end, my pieces will always be at the "bottom" of the screen...my opponents pieces will be at the top of the screen.

When my opponent plays on his machine, his pieces will be at the bottom, and mine at the top.  We're playing the same game on the same board, but with different perspectives.

A piece that I move "up" one square at the bottom right corner of my board will move "down" from the top left corner on my opponents display (perspective).

What is the SMART way to handle this transition of how the pieces look?  Have any of you ever programmed a simple board game?

What way will accomodate future changes to the game (say there are 3 players in version 2.0 of my game, or 4 players? 10 players?
Avatar of robert_marquardt
robert_marquardt

You will have a data structure describing the board with lists of piece data structures which describe where the piece is currently placed. For each player you will have a list with his pieces.
The board data structure needs a list of sides the board has with each side connected to a player (chess having two sides).
Now the board painting function needs to be able to paint the board with a side as parameter.
like robert said you could use a data structure to describe everything.  like if it was chess.. each player has a color, white or black.  if your game is like this you could referance you players peice set with 'white' or 'black' (or '1' or '2', whatever) and then each individual piece (queen, king, pawn1, pawn2, etc).  to make a move, after referancing the piece and owner, you could just use some very general directional phrases like 'forward 2', 'backward 6', 'left 5' etc.  in each case your program would have a perspective to go off of (the pieces perspective) and could move accordingly.  all this could pretty easily be simplified to 1 line of text (for email purposes). like:

(black pawn3 0 -1)
move black pawn3 left 1
(black pawn3 1 2)
move black pawn3 forward 1 right 2

etc.  this could also be expanded to fit more players (more colors, sides)

felonious
No, i would prefer the absolute coordinates chess already has and extend it t other games.
Avatar of Tom Knowlton

ASKER

Can someone give me an example?

Say the board is 3 x 3.

I would see the board as:

X X -
- - O
- O -

My opponnet would see the board as (the reverse):

- O -
O - -
- X X

I want to keep track of piece movement, etc. so that when my opponent moves and sends the move back to me, I see his move, but from MY perspective.

I this a really hard thing to do?

Are there books, websites, etc. that can explain this to me?  It is intuitive to me when I play a board game, but I don't understand how to program it.

I feel really dumb for having trouble with such a simple concept.

Increasing points to 90.
websites?

books?

anyone?
I don't need help so much with drawing the board or displaying the game pieces.

I need to know how to store it in a data structure, and use the same data structure to display the game for opponent A or opponent B.
robert_marquardt

I think I see where you are going with this.

Have you actually programmed board games before?

Thanks.
No, i only know how to structure data.
For the lists you can use TObjectList.

Start with a game object which carries a player list and the board object. A moves history list and actual player element is needed. That is an index to the player list.
Each player object in the player list carries a pieces list and a "color" element. Each piece contains an ID element ("knight" etc). An enum is sufficient for that. Then a position element is needed for pece object. This is the connection to the board object.

With this structuring you should be able to easily create games like chess or checkers which mainly differ in pieces.

Drawing the board is drawing the game structure. The actual player determines the way you draw the board.
This is a view to your data.
ASKER CERTIFIED SOLUTION
Avatar of comptebidon81
comptebidon81

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello - , I'm not sure what kind of answer you expect for your question, you might want to ask a less "general" question. Here are a couple of Web Links to download a couple of computer games that were made by Michael J. Mefford of PC Magazine. Both of these games (checkers and backgammon) come with a .ZIP file that contains the complete Delphi project Code. Although these look like they were made in Delphi 1 and will not compile in newer Delphi's without changes, you can still see the methods and data structures he used for these programs.
. . . If you don't have much experience with this, then you might begin by making a simple "Tic, Tac, Toe" game and build from there.

checkers
http://www.zdnet.com/downloads/stories/info/0,10615,77351,00.html

backgammon
http://www.zdnet.com/downloads/stories/info/0,10615,77343,00.html

+ + +
as to the second player perspective thing. .  draw a checker board on a piece of paper and label the rows "1" "2" "3" "4" ect. . . . and lable the columbs "A" "B" "C" "D" ect. .  now rotate the checker board to your opponents persctive (upside down) and look at the order of the labels, now you will have to label the rows and columbs on his board in reverse to match the rows and columbs on your board
I would prefer to have the positions numbered with a single index (chess board = 1..64).
This allows to have an abstract pices which only contains no information about the layout of the board.
So you can implement games with hex field board and more than two players.

Base class is TGame containing many lists.
Players, Board, unused Pieces, Pieces on Board, Move History. A Player list element (aka player) contains a list of Pieces and taken Pieces.
Pieces do not contain graphics. Piece graphics are located in a list in the Board class. A Tile graphic list is also a goo idea. With that you can change to a new board style by exchanging the graphics lists and repaint.

With the above general layout you can implement games like Chess or Scrabble.

The general handling should be to mimic a real game. That is move the Piece objects from unused list to player list to taken Pieces list etc.
The game is driven by moves. The moves are placed in the Move History list. Game ID and Moves are the only information exchanged if the game is played with two instances of the program (ie via email).
Saving a game is mainly writing the Move History to a file.
Loading a game is initializing a game and replaying the moves from file.
You can convert X,Y position to index and the opposite with a simple transformation, if you know the number of lines and columns.

Index := nbElemLine * (LineNumber - 1) + ColumnNumber;