2

I'm making a chess game, rendered with OpenGL.

I'm not looking for somebody to tell me all of the answers, I would like to figure the code out on my own, but pointing me to the right concepts is what I really need. At this point, I'm not sure where to start. Here is what I've figured out:

An enumeration, TurnState, with the following values:

  • playerOneTurn
  • playerTwoTurn
  • Stopped

An enumeration, GameState, with the following values:

  • playerOneCheck
  • playerTwoCheck
  • playerOnecCheckMate
  • PlayerTwoCheckMate
  • InitializingGame
  • Tie
  • NormalPlay

An abstract class, Player, and a subclass, Computer.

A class, ChessGame, with the following fields:

Player p1, p2
TurnState turnState
GameState gameState 

A class, Move, with the following fields:

*Piece
Location origin
Location destination

A class, Location, with the following fields:

row
col
*ChessBoard 

A class, ChessBoard, with one method, isValid, which takes a Move and checks if the move is valid or not.

An abstract class, ChessPieces, with the following methods:

GetValue()      // returns an int value of the piece (for scoring)
GetPosition()   // returns the current position of a piece
getIsSelected() // returns a boolean, true if selected, false if unselected
move()          // moves the piece in a way dependent upon what piece 

And the following subclasses:

  • Pawn
  • Rook
  • Queen
  • King
  • Knight
ziggystar
  • 28,410
  • 9
  • 72
  • 124
frostbytes89
  • 29
  • 1
  • 5
  • Stack Overflow's formatting, Markdown, kind of messed up your original formatting. I've edited it to be a bit more readable, but you may want to edit it further. – icktoofay Sep 12 '12 at 01:05

3 Answers3

3

As to the AI part of the chess game:

To get a chess AI, or any sort of turn based game AI, you will need to calculate the "value" of the game in a given turn (that's important) (i.e. you assign each piece a value and sum the values for player1 and player2 and then you do score = player1score - player2score, so negative values will benefit player 2 and positive ones, player 1, that's just a basic example and not a very efficient one, but it's the most basic way to explain what the "value" of the game would be).

After you can calculate that you need to be able to calculate every possible move of a player given a certain configuration of the board.

With that you will be able to build a decision tree in which you will have as the root node the current state of the game. The next "level" of the tree will represent every possible state you can get to from the current state (and so forth). It's important to notice that if you consider player1 possible moves in on level of the tree you will consider player two possible moves in the next.

Next thing to do would be:

suppose player1 is gonna make a move, he will look into in the tree until depth 5 (for a chess game you'll never look in the whole tree). So he will choose a move that will be optimized for him, that would mean: at each level he'll consider HIS best move or player2's best move (so he will work on the worst case scenario), so he'll move the the highest valued node in the next level of the tree.

To calculate a value of a node you do the following: NOTE: considering root node is of depth 0, every odd depth node need to be maxValue for player1 and every even depth node minValue for player2.

You'll expand the tree to the max depth you define, for the node in the maxDepth you'll just calculate the value of the board (which I mentioned in the beginning of my answer), for upper nodes you'll do:

even node's value : minValue between all child nodes odd node's value : maxValue between all child nodes

So basically you'll do the regression to find the value of a node based on the value of deeper nodes.

Well, that's the basic idea, from it you can research some other stuff, if you want you can PM me, I've done some work on this kind of search, and I just described the most basic idea here, for an efficient code you'll need lots of optimization techniques.

Hope it helped a little

PHMitrious
  • 43
  • 7
1

First of all: Separate the two: AI and GUI/OpenGL. In chess it is normal to have the GUI and the AI (the "Engine" in computer chess lingo) in two different processes that's communicating with a predefined protocol. The two most popular protocols for this are UCI and WinBoard.

For the chess engine part, you basically need three thing:

  1. A board/position representation
  2. A leaf node evaluation function
  3. A search algorithm

I suggest you read:

  1. Chess Programming WIKI
  2. TalkChess forum for computer chess
  3. Study a open source computer chess engine, like Stockfish, Crafty or Fruit.
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
0

This may not be directly answering your question (actually what is your question?), but you mentioned you wanted pointers to the right concepts.

oysteijo is right, one of the concepts that is very important is separating parts of a program from each other.

For something like chess there exist many efficient and elegant representations of the state of a chess game. I would say that the MVC (model, view, controller) design pattern works quite well for a chess game.

Hopefully this will make some sense, if not I suggest you read up on MVC some more.

Your model is going to primarily involve the datastructure which stores the representation of state of the game, this is the chessboard. A piece can only be on one of 64 spots, and there are limitations on the types of pieces and how many there are and what each of them do. The model will be responsible for dealing with this stuff. It would also make sense to give the model the logic for determining the legality of any given move (i.e. the properties of the game which don't necessarily involve the state of any given instance of a game).

The view is where all of your presentation related code goes. All that OpenGL is going in here, as would a "debug" routine which might (for instance) print an ASCII representation of the chessboard to the console.

The controller might have some functions which interface with the user to process input. The controller is the part of code which manipulates the model ("move E5 to D3": a function in your controller might call model.moveKnight('D3')) and the view ("draw the board in glorious 3D": the controller might do something like calling openGLView.draw(model))

One of the primary goals that MVC helps achieve is the independence of parts of code that perform different tasks. If some change in your AI causes problems with a rendering algorithm, it is a frustrating and difficult position to be in. An experienced programmer would go to some great lengths to ensure that this couldn't happen.

You might be wondering at this point where your AI code fits into the picture. Well, it's really up to you. Use your best judgement. It could be a part of the controller. Personally I'd have it be a whole nother controller (chessAIController) which implements the AI algorithms, but it is just as easy to have all of it contained within the main controller.

The point is, it doesn't really matter how you actually organize the code so long as it is done in some kind of logical way. The reason that MVC is so widespread is that those 3 components are usually present in most software and it usually makes sense to separate them. Note they're not actually really separated... the controller often directly manipulates both the view and model. Restrictions such as not allowing the view to manipulate anything helps code to stay clean and intelligible.

When you have no structure or organization in a programming project it can be nearly impossible to avoid having huge routines which do a little bit of everything because there is really only one place in the code in which to build functionality upon. What this generates invariably is a tangled mass of spaghetti code that no language, no matter how high-level, can save you from. This creates code that just plain sucks because nobody else can understand it, and even you will be unable to understand it two weeks from the time it is written.

Steven Lu
  • 41,389
  • 58
  • 210
  • 364