C language implements backgammon game

  • 2020-07-21 09:17:37
  • OfStack

This article shares the concrete code of C language to realize 5 child chess for your reference, the concrete content is as follows


#include <stdio.h>
#include <bios.h>
#include <ctype.h>
#include <conio.h>
#include <dos.h>
#define CROSSRU 0xbf /* The top right corner points */
#define CROSSLU 0xda /* The upper left corner point */
#define CROSSLD 0xc0 /* The lower left corner points */
#define CROSSRD 0xd9 /* The lower right corner point */
#define CROSSL 0xc3 /* On the left */
#define CROSSR 0xb4 /* On the right */
#define CROSSU 0xc2 /* above */
#define CROSSD 0xc1 /* below */
#define CROSS 0xc5 /*10 Word intersection */
 
/* Defines the position of the upper left corner of the checkerboard point on the screen */
#define MAPXOFT 5
#define MAPYOFT 2
 
/* define 1 Number player's operation key key code */
#define PLAY1UP 0x1157/* Move up --'W'*/
#define PLAY1DOWN 0x1f53/* Move down --'S'*/
#define PLAY1LEFT 0x1e41/* Shift to the left --'A'*/
#define PLAY1RIGHT 0x2044/* Moves to the right --'D'*/
#define PLAY1DO 0x3920/* Move later -- The blank space key */
 
/* define 2 Number player's operation key key code */
#define PLAY2UP 0x4800/* Move up -- The direction key up*/
#define PLAY2DOWN 0x5000/* Move down -- The direction key down*/
#define PLAY2LEFT 0x4b00/* Shift to the left -- The direction key left*/
#define PLAY2RIGHT 0x4d00/* Moves to the right -- The direction key right*/
#define PLAY2DO 0x1c0d/* Move later -- The enter key Enter*/
 
/* You want to quit in the middle of the game ,  According to  Esc  key */
#define ESCAPE 0x011b
 
/* Defines the state of the intersections on the checkerboard ,  That is, the point has no pieces  */
/* If you have any pieces ,  You should also be able to identify which player's pieces are  */
#define CHESSNULL 0 /* No pieces */
#define CHESS1 'O'/*1 A player's piece */
#define CHESS2 'X'/*2 A player's piece */
 
/* Define key categories */
#define KEYEXIT 0/* Escape key */
#define KEYFALLCHESS 1/* Move later key */
#define KEYMOVECURSOR 2/* Cursor movement key */
#define KEYINVALID 3/* Invalid key */
 
/* Defining symbolic constants :  true ,  false  ---  True for 1,  False to 0 */
#define TRUE 1
#define FALSE 0
 
/**********************************************************/
/*  Define the data structure  */
 
/* The data structure of the coordinates of the checkerboard intersections */
struct point
{
int x,y;
};
 
/**********************************************************/
/* Custom function prototype description  */
void Init(void);
int GetKey(void);
int CheckKey(int press);
int ChangeOrder(void);
int ChessGo(int Order,struct point Cursor);
void DoError(void);
void DoOK(void);
void DoWin(int Order);
void MoveCursor(int Order,int press);
void DrawCross(int x,int y);
void DrawMap(void);
int JudgeWin(int Order,struct point Cursor);
int JudgeWinLine(int Order,struct point Cursor,int direction);
void ShowOrderMsg(int Order);
void EndGame(void);
/**********************************************************/
 
/**********************************************************/
/*  Define global variables  */
int gPlayOrder; /* Indicates the current player  */
struct point gCursor; /* The position of the cursor on the board  */
char gChessBoard[19][19];/* Used to record the state of the points on the board */
/**********************************************************/
 
/**********************************************************/
/* The main function */
void main()
{
int press;
int bOutWhile=FALSE;/* Exit loop flag */
printf("Welcome [url]www.schoolhacker.com[/url]");
Init();/* Initialize images, data */
 
while(1)
{
press=GetKey();/* Gets the user's key value */
switch(CheckKey(press))/* Determine key category */
{
/* Is out of key */
case KEYEXIT:
clrscr();/* Clear the screen */
bOutWhile = TRUE;
break;
 
/* Is key move later */
case KEYFALLCHESS:
if(ChessGo(gPlayOrder,gCursor)==FALSE)/* Playing chess */
DoError();/* Move later error */
else
{
DoOK();/* Move later right */
 
/* If the player in the current row wins */
if(JudgeWin(gPlayOrder,gCursor)==TRUE)
{
DoWin(gPlayOrder);
bOutWhile = TRUE;/* The exit loop flag is set to true */
}
/* Otherwise, */
else
/* Switch sides */
ChangeOrder();
ShowOrderMsg(gPlayOrder);
}
break;
 
/* Is the cursor movement key */
case KEYMOVECURSOR:
MoveCursor(gPlayOrder,press);
break;
 
/* Is an invalid key */
case KEYINVALID:
break;
}
 
if(bOutWhile==TRUE)
break;
}
 
/* Game over */
EndGame();
}
/**********************************************************/
 
/* Interface initialization, data initialization */
void Init(void)
{
int i,j;
char *Msg[]=
{
"Player1 key:",
" UP----w",
" DOWN--s",
" LEFT--a",
" RIGHT-d",
" DO----space",
"",
"Player2 key:",
" UP----up",
" DOWN--down",
" LEFT--left",
" RIGHT-right",
" DO----ENTER",
"",
"exit game:",
" ESC",
NULL,
};
 
/*  Solution for 1 A player no.  */
gPlayOrder = CHESS1;
/*  Checkerboard data cleared ,  There are no pieces at the beginning of each point on the board  */
for(i=0;i<19;i++)
for(j=0;j<19;j++)
gChessBoard[i][j]=CHESSNULL;
/* Initial cursor position */
gCursor.x=gCursor.y=0;
 
/* Drawing board */
textmode(C40);
DrawMap();
 
/* Displays instructions for the operation keys */
i=0;
textcolor(BROWN);
while(Msg[i]!=NULL)
{
gotoxy(25,3+i);
 
cputs(Msg[i]);
i++;
}
 
/* Displays the current row */
ShowOrderMsg(gPlayOrder);
/* Move the cursor to the top left corner of the board */
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}
 
/* Drawing board */
void DrawMap(void)
{
int i,j;
 
clrscr();
 
for(i=0;i<19;i++)
for(j=0;j<19;j++)
DrawCross(i,j);
 
}
 
/* Draw the intersections on the board */
void DrawCross(int x,int y)
{
gotoxy(x+MAPXOFT,y+MAPYOFT);
/* Student: At the intersection point 1 A player's piece */
if(gChessBoard[x][y]==CHESS1)
{
textcolor(LIGHTBLUE);
putch(CHESS1);
return;
}
/* Student: At the intersection point 2 A player's piece */
if(gChessBoard[x][y]==CHESS2)
{
textcolor(LIGHTBLUE);
putch(CHESS2);
return;
}
 
textcolor(GREEN);
 
/* Upper left intersection */
if(x==0&&y==0)
{
putch(CROSSLU);
return;
}
 
/* Intersection in the lower left corner */
if(x==0&&y==18)
{
putch(CROSSLD);
return;
}
 
/* Intersection in the upper right corner */
if(x==18&&y==0)
{
putch(CROSSRU);
return;
}
 
/* Intersection in the lower right corner */
if(x==18&&y==18)
{
putch(CROSSRD);
return;
}
 
/* Left boundary intersection point */
if(x==0)
{
putch(CROSSL);
return;
}
 
/* Right boundary intersection point */
if(x==18)
{
putch(CROSSR);
return;
}
 
/* Upper boundary intersection point */
if(y==0)
{
putch(CROSSU);
return;
}
 
/* Lower boundary intersection point */
if(y==18)
{
putch(CROSSD);
return;
}
 
/* The intersections in the middle of the board */
putch(CROSS);
}
 
/* Switch sides */
int ChangeOrder(void)
{
if(gPlayOrder==CHESS1)
gPlayOrder=CHESS2;
else
gPlayOrder=CHESS1;
 
return(gPlayOrder);
}
 
/* Get key value */
int GetKey(void)
{
char lowbyte;
int press;
 
while (bioskey(1) == 0)
;/* If the user has no keys, empty loop */
 
press=bioskey(0);
lowbyte=press&0xff;
press=press&0xff00 + toupper(lowbyte);
return(press);
}
 
/* Drop error handling */
void DoError(void)
{
sound(1200);
delay(50);
nosound();
}
 
/* Win processing */
void DoWin(int Order)
{
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
nosound();
 
textcolor(RED+BLINK);
gotoxy(25,20);
if(Order==CHESS1)
cputs("PLAYER1 WIN!");
else
cputs("PLAYER2 WIN!");
gotoxy(25,21);
cputs(" \\<^+^>/");
getch();
}
 
/* Playing chess */
int ChessGo(int Order,struct point Cursor)
{
/* Determine if there are any pieces at the intersection */
if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL)
{
/* If there are no pieces ,  Then you can drop the seed */
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
textcolor(LIGHTBLUE);
putch(Order);
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
gChessBoard[Cursor.x][Cursor.y]=Order;
return TRUE;
}
else
return FALSE;
}
 
/* Determines whether a player wins after the current line has been played */
int JudgeWin(int Order,struct point Cursor)
{
int i;
for(i=0;i<4;i++)
/* Determines whether there is continuity in the specified direction 5 A piece on the side of a line */
if(JudgeWinLine(Order,Cursor,i))
return TRUE;
return FALSE;
}
 
/* Determines whether there is continuity in the specified direction 5 A piece on the side of a line */
int JudgeWinLine(int Order,struct point Cursor,int direction)
{
int i;
struct point pos,dpos;
const int testnum = 5;
int count;
 
switch(direction)
{
case 0:/* In horizontal direction */
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y;
dpos.x=1;
dpos.y=0;
break;
case 1:/* In the vertical direction */
pos.x=Cursor.x;
pos.y=Cursor.y-(testnum-1);
dpos.x=0;
dpos.y=1;
break;
case 2:/* Oblique direction from lower left to upper right */
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y+(testnum-1);
dpos.x=1;
dpos.y=-1;
break;
case 3:/* Oblique direction from top left to bottom right */
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y-(testnum-1);
dpos.x=1;
dpos.y=1;
break;
}
 
count=0;
for(i=0;i<testnum*2+1;i++)/*????????i<testnum*2-1*/
{
if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18)
{
if(gChessBoard[pos.x][pos.y]==Order)
{
count++;
if(count>=testnum)
return TRUE;
}
else
count=0;
}
pos.x+=dpos.x;
pos.y+=dpos.y;
}
 
return FALSE;
}
 
/* Move the cursor */
void MoveCursor(int Order,int press)
{
switch(press)
{
case PLAY1UP:
if(Order==CHESS1&&gCursor.y>0)
gCursor.y--;
break;
case PLAY1DOWN:
if(Order==CHESS1&&gCursor.y<18)
gCursor.y++;
break;
case PLAY1LEFT:
if(Order==CHESS1&&gCursor.x>0)
gCursor.x--;
break;
case PLAY1RIGHT:
if(Order==CHESS1&&gCursor.x<18)
gCursor.x++;
break;
 
case PLAY2UP:
if(Order==CHESS2&&gCursor.y>0)
gCursor.y--;
break;
case PLAY2DOWN:
if(Order==CHESS2&&gCursor.y<18)
gCursor.y++;
break;
case PLAY2LEFT:
if(Order==CHESS2&&gCursor.x>0)
gCursor.x--;
break;
case PLAY2RIGHT:
if(Order==CHESS2&&gCursor.x<18)
gCursor.x++;
break;
}
 
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}
 
/* End of game processing */
void EndGame(void)
{
textmode(C80);
}
 
/* Displays the current row */
void ShowOrderMsg(int Order)
{
gotoxy(6,MAPYOFT+20);
textcolor(LIGHTRED);
if(Order==CHESS1)
cputs("Player1 go!");
else
cputs("Player2 go!");
 
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}
 
/* The drop is handled correctly */
void DoOK(void)
{
sound(500);
delay(70);
sound(600);
delay(50);
sound(1000);
delay(100);
nosound();
}
 
/* Check the user's key category */
int CheckKey(int press)
{
if(press==ESCAPE)
return KEYEXIT;/* Is out of key */
 
else
if
( ( press==PLAY1DO && gPlayOrder==CHESS1) ||
( press==PLAY2DO && gPlayOrder==CHESS2)
)
return KEYFALLCHESS;/* Is key move later */
 
else
if
( press==PLAY1UP || press==PLAY1DOWN ||
press==PLAY1LEFT || press==PLAY1RIGHT ||
press==PLAY2UP || press==PLAY2DOWN ||
press==PLAY2LEFT || press==PLAY2RIGHT
)
return KEYMOVECURSOR;/* Is the cursor movement key */
 
else
return KEYINVALID;/* The key is invalid */
}

Related articles: