Five-in-a-row
Since the game of computer as a game platform, all kinds of chess games have
sprung up. The five-in-row is a game widely loved by the public. Its rules are simple
changeable, interesting and full of pleasure. At the same time, it is easy to learn. And
it also exercise your ability to program while it strengthen your intelligence.
First of all,it comes to the interface design. Control one co-own the PictureBox: a
start button named ‘btnStart’, a reset button named ‘btnReset, a label which is used
to display the state of the game. Then it comes to set the basic classes. Create a new
class named ‘MainSize’ to store the parameters that may be used on the interface.
The size of the main frame. body is 520*460, chessboard is a PictureBox control, the
size is 401*401.The chessboard has 20 rows and 20 columns, each grid’s length is 20,
and the checkerboard diameter is 16. A new ChessBoard class represents a
chessboard with a static function DrawBoard. The function code is as follows:
class ChessBoard
{
static readonly Color color = Color.Black;
static readonly float penWid = 1.0f;
static readonly Pen pen = new Pen(color, penWid);
public static void DrawCB(Graphics gra,PictureBox pic)
{
// Per row
int horC = MainSize.CBWid / MainSize.CBGap;
// interval
int gap = MainSize.CBGap;
Image img = new Bitmap(MainSize.CBWid, MainSize.CBHei);
gra = Graphics.FromImage(img);
gra.Clear(Color.White);
gra.DrawRectangle(pen, 0, 0, MainSize.CBWid, MainSize.CBHei);
// Draw a chessboard
for (int i = 0; i 0 ? bx - 4 : 0;
int b2 = (by - 4) > 0 ? by - 4 : 0;
//int buttom = b1 > b2 ? b2 : b1;
int val = ChessBack[bx, by];
for (int i = b1,j=b2; i 0 ? by - 4 : 0;
int val = ChessBack[bx, by];
for (int i = buttom; i 0?bx-4:0;
int val = ChessBack[bx,by];
for (int i = left; i < 16; i++)
{
if (ChessBack[i, by] == val ChessBack[i + 1, by] == val
ChessBack[i + 2, by] == val ChessBack[i + 3, by] == val
ChessBack[i + 4, by] == val)
return true;
}
return false;
}
#endregion
There is no problem in carrying out the tests after completion. Summed up the problems in
the compilation process, variable naming is not very good.Type, start and other variables are
easily confused with keywords. There are too many lines of code in the main function, and it is
not easy to read.And determine whether the checkerboard is full or moved to the chessboard
class. It is not convenient to add new game modes, such as adding AI and online game.The
modified code is a little bit more personal. The idea is to build a new box with AI and an online
frame. separately, and then modify the basic class.The maximum code reuse in the case.