Skip to content

Commit

Permalink
Added Dot Box game.
Browse files Browse the repository at this point in the history
  • Loading branch information
nayandeep111 authored Oct 22, 2022
1 parent febdd8a commit f5f4961
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
53 changes: 53 additions & 0 deletions games/cpp/dot box/Dot_box.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
void game()
{
int key = 0, i, j, x, y;
int margin = 4; /* margin for clicking around the dots */
int LEFTMOUSE = 1, RIGHTMOUSE = 2;

while (key != 1)
{
while (!kbhit())
{
get_mouse_pos();
if (mousebutton == LEFTMOUSE)
{
for (i = 0, x = START_X; i < ROWS - 1; i++, x += BOX_WIDTH)
{
for (j = 0, y = START_Y; j < ROWS; j++, y += BOX_HEIGHT)
{
if (mousex >= x - margin && mousex <= x + margin && mousey >= y - margin && mousey <= y + margin)
{
if (matrix_h[j][i] != 1)
{
matrix_h[j][i] = 1;
line(x, y, x + BOX_WIDTH, y);
player_lines[PLAYER - 1]++;
}
}
}
}
}
if (mousebutton == RIGHTMOUSE)
{
for (i = 0, x = START_X; i < ROWS; i++, x += BOX_WIDTH)
{
for (j = 0, y = START_Y; j < ROWS - 1; j++, y += BOX_HEIGHT)
{
if (mousex >= x - margin && mousex <= x + margin && mousey >= y - margin && mousey <= y + margin)
{
if (matrix_v[j][i] != 1)
{
matrix_v[j][i] = 1;
line(x, y, x, y + BOX_HEIGHT);
player_lines[PLAYER - 1]++;
}
}
}
}
}
}
ii.h.ah = 0;
int86(22, &ii, &oo);
key = oo.h.ah;
}
}
14 changes: 14 additions & 0 deletions games/cpp/dot box/Dots and Boxes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Dots and Boxes

Dots and Boxes is a pencil-and-paper game for two players (sometimes more). It was first published in the 19th century by French mathematician Édouard Lucas, who called it la pipopipette.It has gone by many other names,including the dots and dashes, game of dots, dot to dot grid, boxes, and pigs in a pen.
The game starts with an empty grid of dots. Usually two players take turns adding a single horizontal or vertical line between two unjoined adjacent dots. A player who completes the fourth side of a 1×1 box earns one point and takes another turn. A point is typically recorded by placing a mark that identifies the player in the box, such as an initial. The game ends when no more lines can be placed. The winner is the player with the most points. The board may be of any size grid. When short on time, or to learn the game, a 2×2 board (3×3 dots) is suitable. A 5×5 board, on the other hand, is good for experts.

# Strategy

For most novice players, the game begins with a phase of more-or-less randomly connecting dots, where the only strategy is to avoid adding the third side to any box. This continues until all the remaining (potential) boxes are joined together into chains – groups of one or more adjacent boxes in which any move gives all the boxes in the chain to the opponent. At this point, players typically take all available boxes, then open the smallest available chain to their opponent. For example, a novice player faced with a situation like position 1 in the diagram on the right, in which some boxes can be captured, may take all the boxes in the chain, resulting in position 2. But with their last move, they have to open the next, larger chain, and the novice loses the game.

A more experienced player faced with position 1 will instead play the double-cross strategy, taking all but 2 of the boxes in the chain and leaving position 3. The opponent will take these two boxes and then be forced to open the next chain. By achieving position 3, player A wins. The same double-cross strategy applies no matter how many long chains there are: a player using this strategy will take all but two boxes in each chain and take all the boxes in the last chain. If the chains are long enough, then this player will win.

The next level of strategic complexity, between experts who would both use the double-cross strategy (if they were allowed to), is a battle for control: an expert player tries to force their opponent to open the first long chain, because the player who first opens a long chain usually loses. Against a player who does not understand the concept of a sacrifice, the expert simply has to make the correct number of sacrifices to encourage the opponent to hand them the first chain long enough to ensure a win. If the other player also sacrifices, the expert has to additionally manipulate the number of available sacrifices through earlier play.

### You can try this game here : https://gametable.org/games/dots-and-boxes/

0 comments on commit f5f4961

Please sign in to comment.