Вы находитесь на странице: 1из 7

The Game of Chess - Final Design

CS246
INTRODUCTION

The game of chess is played on a 8x8 checkerboard. Players take turns making moves one at a time. White
usually makes the rst move. Chess is turn based strategy game; the objective is to place the opponents king
under attack. There are 6 types of pieces, each having their own specic moves. Pieces can only move either in
straight lines or diagonally. Pieces also cannot jump over other pieces. The knight, however, can do move in an L
shape and can also jump over other pieces. There are also 3 special moves: Castling, Pawn Upgrade, and Pawn
Capture En Passant. The complete set of rules can be found online, and in the chess.pdf le supplied with this
project.

IMPLEMENTATION

To implement this game in C++ it would greatly simplify the process if we used OOP methodology. In terms of
organization and readability OOP is the way to go.
Initially, one must sit down and design the whole game; how many classes we need and how they will interact with
each other. To begin, I sat down and thought about what a chess game entails. I even played a few games to
become comfortable with the games rules. I then made acute observations of what each part of the game has
knowledge of, and how they interact with other parts of thef game. For example, the Board is made up of Squares
(64 squares), each square has coordinates on the board, yet the square themselves don't know that a board
exists. Another example is if we look at the Pieces. The Pieces only have knowledge of what kind of Piece they are
(i.e. K, Q, B etc) and what colour they are. They don't know where they are on the board, they are just placed on a
square which has the coordinates. Such acute observations greatly simplied my design process for the classes.
My nal design for the classes and their interactions is in the attached uml.pdf.

This initial plan was to get an outline of how everything would t together. Of course it is a very rough outline, and
many things will need to be changed. Once, that was done I would start on a rough outline of all the methods
within each class. I am starting with the following classes:
1. Piece > Sub Classes: Pawn, Rook, Knight, Bishop, Queen, King
2. Player > Made up of a pointer array to 16 Pieces.
3. Square - The board is made of these, each has an x and y coordinate.
4. Board - Made up of a 2D pointer array to 64 Squares
CS246
5. Game
6. AI
The numbering represents the priority of the classes. I will design Piece rst, and then Player.
Piece - Piece contains sub classes Pawn, Rook, Knight, Bishop, Queen, King. Piece has a private eld colour
which indicates its ownership. Each subclass has its own move verify method. For example, a Rook can only
move in straight lines, while a Queen can move both diagonally and straight. This will be implements here in each
piece. King, Rook and Pawn has an extra variable hasMoved, which is need to implement special moves such as
Castling and En Passant Capture.
Player - Contains name of player (white, black) and a pointer array to 16 Pieces. Player creates all the pieces.
Square - Contains a pointer to a Piece or NULL. Also has coordinates x, y. Has a set piece function which puts a
Piece on that specic square.
Board - Contains a 2D pointer array to 8x8 Squares. Creates and initializes the Squares, and puts pieces on each
square based on starting position. Inherits Square, Player and Piece. Also, holds the logic for nding out if Board is
in Check/Checkmate, if move is allowed, and specic ranking functions used by AI. Takes care of the special
moves such as Castling and En Passant Capture.
Game - Initializes both Players, the Board. Includes all the Classes above. Contains the command interpreter, and
displays all the output here. This is where all the magic happens. This is in constant communication with the
Board. I feel like I should've used the
AI - Each Piece was given a value. Pawn are 1, Knights and Bishop are 3, Rooks are 5 and Queen is 9. You really
cant put a value on the King, but for numerical purposes I gave the King a value of 20. Rough idea of moves
based on Piece Ranks, Square Rank, if Square is under attack. Losing a Queen is more valuable than a Pawn.
Putting a King on a Square that is being attacked is not allowed. Maybe looking into an opening move sequence
putting the AI is a good spot and then transitioning over to a more heuristic approach based on algorithms.
WHAT CHANGED

Firstly, I underestimated the difculty of this project. I was working alone and hence, it became really long and
tedious. I didnt realize how insufcient my initial planning was. The game and the UML diagram both became
almost twice as big as intended. I have never written so many lines of code in my life.
Move - Added a whole new class to simply sending and receiving moves from AI, Game, and Board;
AI - Opening Moves and and maxmin tree search could not be implemented is such a short span of time.
Every class is about twice the size that it was intended to be. The scale at which this project grew was crazy.
CS246

LOGIC

Check/Checkmate/Stalemate - Check surrounding 4 squares of King, and check boolean (isUnderAttack). If all
4 are true and there no valid moves left for the Player. then we have a stalemate. If all 4 are true and the King is
under attack then we have a CheckMate. If King in under attack and less than 4 are under attack, we have a
Check. Board is checked after every move.

EXTRA FEATURES

Init - During setup screen, the init command automatically puts all the pieces at their default locations for both
Black and White Players.
Undo - Implemented an undo command (unlimited) using a stack. The stack stores the state of the Board after
every turn.

QUESTIONS

Q: How would you implement a book of standard openings if required?
A: A book of standard opening moves and responses for the moves would be hard coded into a text le.
This would then be importing into the class Game. I would pick an opening sequence from the different opening
sequences in my le at random. Game would call methods in Board which would return back the move made by
the opponent. I would then search through my text le for that exact move. Maybe I could also implement a data
structure that would make this search much easier. For example a Trie, or a Hash Table would greatly speed things
up and allow for a much bigger moves le. We could have exponentially more moves since we can search through
them really fast. Potentially, we could make a very potent AI opponent just by using hardcoded responses to
moves.
Q: How would you implement a feature that would allow a player to undo his/her last move? What about
an unlimited number of undos? Implemented both features!
A: I would keep a data structure known as a Stack that would store within the value of every move a
player does. This history Stack can be placed in Game Class. Another more potent way would be to take
snapshots of the Board after every move and store them in a dynamic Stack. Essentially, we would be saving the
state of the board after every move. Undoing a move would be simply popping the rst element off the stack and
redisplaying the board. This would allow for an unlimited number of undos and would compensate for captured
pieces, and/or other special moves.
Q: Outline the changes that would be necessary to make your program into a four-handed chess game.
A: Game would create 4 players instead of 2, and give them each a name (which would also be their
colour). I would increase the size of the Board from a 8x8 to 14x14. The 2D pointer to Squares would initialize 196
squares, and give them all coordinates. In the Board initialization, I would deaden the 4 corner zones (9 * 4 = 36
total squares) by creating a boolean dead in Square, and set it to true for those squares. Based on the type of
Game, I would positing the Queens appropriately on the Board. The valid move logic found within Piece and Board
would need to tweaked to account for the dead zones. All, other logic will follow from before. Strategy for the AI
would have to be recongured for the new Board.

MORE QUESTIONS

1. What lessons did this project teach you about developing software in teams? If you worked alone, what
lessons did you learn about writing large programs?
2. What would you have done differently if you had the chance to start over?
A. If I was to start over, I would spend a much greater emphasis on the planning of the Classes, Methods,
and over all structure of my project. What I realized about writing large software is that its like constructing a
building from the foundation up. If you do not spend enough time, or do not plan out the foundation with enough
care, then later on youll be in big trouble. A simple issue, that wouldve taken you 10 minutes to x during the
projects initial stages (foundation), will now take you a few hours because of how big your program is. I learnt this
rst hand, as I was working alone on this project. Many times I would smack myself across the head for not
thinking about such a case when I was planning. As an individual, I learnt how to organize my code efciently and
utilize it effectively. Before this project, I really didnt appreciate the OOP methodology; I thought it was just super
clunky and over kill. How wrong I was.

Moreover, another more specic thing that I would change if I started again, was to create more Classes
and to spread out my les. I heavily relied on the Board class to implement all the chess game logic. This one le
became proportionally too big to manage, and it got really annoying trying to gure out where my code was. What
I should've done was to create a separate Chess Engine Class that would inherit from both Board, Player, Piece
and it would solve all the game logic and pass it on to Game.

HOW EVERYTHING CAME TOGETHER
1. Figure out relationship between Classes
2. Design UML
3. Design for Board, Square, Piece
4. Text Based GUI for the Board with Pieces
5. Create Game, Player Classes
6. Create a Simple Command Interpreter
7. Add Move Logic to Pieces and Board
8. Implement Special Moves (En Passant Capture, Castling)
9. Implement Logic to check for Check/Checkmate/Stalemate
10. Have Two Player Match. Test for possible bugs in logic etc.
11. Fix up GUI
12. Add AI Class, which includes Player, Board, Squares, Piece. Game includes AI.
13. Create a Random Valid Move Generator AI
14. Add opening sequence text le, create a efcient search data structure, and implement response to move
logic for AI.
15. Make changes to each class according to AI strategy. Add ranking system to each Piece and Square
(Heuristic AI)
16. Finish Final Report, Document Code and Submit to Marmoset



TIMELINE
Sharing of work
Since I am working on the project alone, I will be completing all of the following work.


Breakdown Items Estimated Time of Completion
1,2 Sunday, March 23, 2014
3,4,5 Monday, March 24, 2014
6 Wednesday, March 26, 2014
7,8 Thursday, March 27, 2014
9,10 Friday, March 28, 2014
11 Saturday, March 29, 2014
12,13 Monday, March 31, 2014
14 Tuesday, April 1, 2014
15 Thursday, April 3, 2014
16 Friday, April 4, 2014

Вам также может понравиться