Question

In: Computer Science

Write a simple code for a simple connect the dots game in plain C coding.

Write a simple code for a simple connect the dots game in plain C coding.

Solutions

Expert Solution

Based on the above Question:
Solution :
We are going to write code


/* get current directory */
char CURDIR[MAXPATH];
strcpy(CURDIR, "X:\\");
CURDIR[0] = 'A' + getdisk();
getcurdir(0, CURDIR+3);
/* initialize graphics */
int gd = VGA, gm = VGAHI;
initgraph(&gd, &gm, CURDIR);
Initializing Mouse

The following code initializes the mouse operations in C.


union REGS ii, oo;
ii.x.ax = 0;
int86(0x33, &ii, &oo);
if (oo.x.ax == 0)
{
printf("Failed to initiliaze Mouse.");
return;
}
The Game Loop

The following code shows the game loop. The loop will execute until the user presses the Escape key (keycode=1) in the keyboard. Inside the loop, the mouse press events are captured and the corresponding action is taken. For every mouse click, it checks whether the clicked position is around a dot (with a margin of 4 pixels). When the user presses the left-mouse-button, a horizontal line will be drawn from the clicked dot to the adjacent dot at the right. If the user presses the right-mouse-button, a vertical line will be drawn from the clicked dot to the adjacent dot at the bottom.


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;
}
}
Full source code (C language) of this game is available for download below. You need Turbo C Compiler for DOS version 3.0 to compile and run this code.


I have tried my best to resolve it.
If you have any Queries please comment here.
If you like my answer Please Upvote / Like it.
Thank you


Related Solutions

Write a code for simple racing game (using dots) on c coding.
Write a code for simple racing game (using dots) on c coding.
write a code for a typing game in plain C coding.
write a code for a typing game in plain C coding.
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
Simple code for a game on C coding.
Simple code for a game on C coding.
Write a MATLAB *function* that draws a spiral by using the plot() command to connect-the-dots of...
Write a MATLAB *function* that draws a spiral by using the plot() command to connect-the-dots of a set of points along the spiral's trajectory. The function should have three input arguments: the number of points along the trajectory, the number of rotations of the spiral, and the final radius of the spiral. The function does not need any output arguments. Use nargin to provide default values for the input arguments. The spiral should begin at the origin. At each step...
The following is a C program that is a video game version of Connect 4. The...
The following is a C program that is a video game version of Connect 4. The code works fine as is now but I want to change the way you input which column you drop a disk into. Currently, you type in 1 and it goes into column 1. If you type in 6, it goes into column 6 and so on. But I want to make it so you input A or a, then it goes into column 1...
Write a LISP program to play either the game "Connect Three" on a size 4x4 game...
Write a LISP program to play either the game "Connect Three" on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (connect-3) The game is single player, human vs the computer AI.
Write a C++ code to print to the user a simple menu of a fast food...
Write a C++ code to print to the user a simple menu of a fast food restaurant. You should allow the user to select his/her preferred burgers and/or drinks and you should display the final bill to the user for payment. The user should be able to select more than one item. You should use the switch statement.
In C, build a connect 4 game with no GUI. Use a 2D array as the...
In C, build a connect 4 game with no GUI. Use a 2D array as the Data structure. First, build the skeleton of the game. Then, build the game. Some guidelines include... SKELETON: Connect Four is a game that alternates player 1 and player 2. You should keep track of whose turn it is next. Create functions: Initialization – print “Setting up the game”. Ask each player their name. Teardown – print “Destroying the game” Accept Input – accept a...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT