web123456

Gozi Chess Game

  • #define _CRT_SECURE_NO_WARNINGS
  • #include<>
  • #include<>//Header file for multimedia device
  • #pragma comment(lib,"")//Play music library files
  • #include<>
  • #include<>
  • #include<>
  • #include<>
  • #include<>
  • int judge(int a, int b);
  • int flag = 0;//Black and white chess alternate
  • int board[21][21] = { 0 };//0Indicates the status of no chess pieces. Tells the computer whether there are chess pieces.
  • void initGame()//Initialize the game
  • {
  • //Drawing environment
  • initgraph(600, 550);//Width Height
  • //setbkcolor(LIGHTCYAN);//The paint is ready
  • //cleardevice();//Paint
  • //Postings
  • loadimage(NULL, L"");
  • //Background music Multimedia control interface
  • mciSendString(L"open Wangjiangnan.mp3", 0, 0, 0);
  • mciSendString(L"play Wangjiangnan.mp3", 0, 0, 0);
  • setlinecolor(BLACK);
  • //Draw a chessboard
  • //Draw lines20×20Chessboard
  • for (int i = 25; i <=500; i+=25)
  • {
  • line(i, 0, i, 500);
  • line(0, i, 500, i);
  • }
  • line(501, 0, 501, 500);//Achieve bolding effect
  • outtextxy(510, 100, L"Player 1: Black Chess");
  • outtextxy(510, 200, L"Player 2: White Chess");
  • }
  • //Play chess
  • void playChess()
  • {
  • //Play chess with the mouse
  • MOUSEMSG m;//Save mouse information
  • int x=0, y=0;
  • int a=0, b=0;
  • HWND hwnd = GetHWnd();
  • while (1)
  • {
  • m = GetMouseMsg();
  • for (int i = 0; i <= 20; i++)
  • {
  • for (int j = 0; j <= 20; j++)
  • {
  • if (abs( - j * 25) < 12.5 && abs( - i* 25)<12.5)
  • {
  • //i is understood as row j is understood as column
  • //ifIn the statement, it is OK to change i to j and j to i, because i and j are0-20cycle
  • //It's nothing more than finding the right i for the outer loop first, and then finding the right j for the inner loop
  • //Or find the appropriate j first, and then find the appropriate i in the outer loop
  • x = j * 25; //The upper left corner of the board is (00) horizontal axis x vertical axis y
  • y = i * 25;
  • a = i;
  • b = j;
  • //a=j b=i can do it, but I don't know why?
  • }
  • }
  • }
  • //Press the left mouse button
  • if ( == WM_LBUTTONDOWN)
  • {
  • //Overlapping problems occur
  • if (board[a][b] != 0)
  • {
  • MessageBox(hwnd, L"There are already chess pieces here, please re-select", L"hint", MB_OK);
  • continue;//Exit this loop and re-enter
  • }
  • if (flag % 2 == 0)
  • {
  • setfillcolor(BLACK);
  • solidcircle(x, y, 10);//Coordinates, chess piece radius
  • board[a][b] = 1;//1Indicates playing black chess
  • }
  • else//odd number
  • {
  • setfillcolor(WHITE);
  • solidcircle(x, y, 10);
  • board[a][b] = 2;
  • }
  • flag = 1 - flag;//flag in01Switch
  • //solidcircle(/25*25,/25*25,10);//This is not a strong experience in writing chess.
  • }
  • if (judge(a, b))
  • {
  • if (flag % 2 == 0)
  • {
  • MessageBox(hwnd, L"Player 2 Victory", L"game over", MB_OK);
  • return;//Program ends
  • }
  • else
  • {
  • MessageBox(hwnd, L"Player 1 wins", L"game over", MB_OK);
  • return;
  • }
  • }
  • }
  • }
  • //Judge win or lose
  • int judge(int a, int b)
  • {
  • int i, j;
  • int t = 2 - flag % 2;//1 2
  • //a=10,b=12Vertical From top to bottom
  • for (i = a - 4, j = b; i <= a; i++)//b is column a is row fixed j is constant i changes look at the vertical direction
  • {
  • if (i >= 0 && i <= 16 && t == board[i][j] &&
  • t == board[i + 1][j] && t == board[i + 2][j] &&
  • t == board[i + 3][j] && t == board[i + 4][j])
  • //iThe first chess piece The fifth chess piece i+4<20So i<16
  • {
  • return 1;
  • }
  • }
  • //Vertical From bottom to top
  • for (i =a+4, j = b; i >= a; i--)//b is column a is row fixed j is constant i changes look at the vertical direction
  • {
  • if (i <=20 && t == board[i][j] && t == board[i - 1][j] && t == board[i - 2][j] && t == board[i - 3][j] && t == board[i - 4][j])
  • //iThe first chess piece The fifth chess piece i+4<20So i<16
  • {
  • return 1;
  • }
  • }
  • //Some writing redundantly. No matter where chess is played, it will traverse from which direction.
  • //a=10,b=12Horizontal judgment from left to right
  • for (i = a, j = b-4; j <= b; j++)//b is column a is row fixed i is unchanged j changes look at the horizontal direction
  • {
  • if (j >= 0 && j < 16 && t == board[i][j] && t == board[i][j + 1] && t == board[i][j + 2] && t == board[i][j + 3] && t == board[i][j + 4])
  • //jThe first chess piece, the fifth chess piece, j+4<20So j<16
  • {
  • return 1;
  • }
  • }
  • //a=10,b=12Horizontal judgment from right to left
  • //for (i = a, j = b + 4; j >= b; j--)//b is column a is row fixed i is unchanged j changes look at the horizontal direction
  • //{
  • // if (j <=20 && t == board[i][j] && t == board[i][j - 1] && t == board[i][j - 2] && t == board[i][j - 3] && t == board[i][j - 4])
  • // //jThe first chess piece, the fifth chess piece, j+4<20So j<16
  • // {
  • // return 1;
  • // }
  • //}
  • //a=10,b=12Slanted Lower Right From left to right
  • for (i = a-4, j = b - 4; i<= a,j <= b; i++,j++)
  • {
  • if (i >= 0 && i <=16 &&j >= 0 && j <= 16 && t == board[i][j] &&
  • t == board[i+1][j + 1] && t == board[i + 2][j + 2] &&
  • t == board[i + 3][j + 3] && t == board[i + 4][j + 4])
  • //iThe first chess piece The fifth chess piece i+4<20So i<16
  • {
  • return 1;
  • }
  • }
  • // //a=10,b=12Slanted upper left oblique from right to left
  • //for (i = a + 4, j = b + 4; i >= a, j >= b; i--, j--)
  • //{
  • // if (i <= 20 && j <=20 && j <= 16 && t == board[i][j] && t == board[i - 1][j - 1] && t == board[i - 2][j - 2] && t == board[i - 3][j - 3] && t == board[i - 4][j - 4])
  • // //iThe first chess piece The fifth chess piece i+4<20So i<16
  • // {
  • // return 1;
  • // }
  • //}
  • //
  • //a=10,b=12Left bottom oblique from right to left
  • for (i = a - 4, j = b + 4; i <= a, j >= b; i++, j--)
  • {
  • if (i >=0 && i<=16 && j <=20 && t == board[i][j] &&
  • t == board[i + 1][j - 1] && t == board[i + 2][j - 2] &&
  • t == board[i + 3][j - 3] && t == board[i + 4][j - 4])
  • {
  • return 1;
  • }
  • }
  • /*
  • The lower left oblique and the upper right oblique are consistent. The upper right oblique and the lower left oblique are consistent.
  • */
  • //a=10,b=12Top right diagonal from left to right
  • //for (i = a + 4, j = b - 4; i >= a, j <= b; i--, j++)
  • //{
  • // if (i <= 20 && j >= 0 && j <= 16 && t == board[i][j] && t == board[i - 1][j + 1] && t == board[i - 2][j + 2] && t == board[i - 3][j + 3] && t == board[i - 4][j + 4])
  • // //iThe first chess piece The fifth chess piece i+4<20So i<16
  • // {
  • // return 1;
  • // }
  • //}
  • return 0;
  • }
  • //int judge(int a, int b)
  • //{
  • // int i, j;
  • // int t = 2 - flag % 2;
  • // for (i = a - 4, j = b; i <= a; i++)
  • // {
  • // if (i >= 1 && i < 16 && t == board[i][j] && t == board[i + 1][j] && t == board[i + 2][j]
  • // && t == board[i + 3][j] && t == board[i + 4][j])
  • // return 1;
  • // }
  • // for (i = a, j = b - 4; j <= b; j++)
  • // {
  • // if (j >= 1 && j < 16 && t == board[i][j] && t == board[i][j + 1]
  • // && t == board[i][j + 2] && t == board[i][j + 3] && t == board[i][j + 4])
  • // return 1;
  • // }
  • // for (i = a - 4, j = b - 4; i <= a, j <= b; i++, j++)
  • // {
  • // if (i >= 1 && i < 16 && j >= 1 && j < 16 && t == board[i][j] && t == board[i + 1][j + 1] &&
  • // t == board[i + 2][j + 2] && t == board[i + 3][j + 3] && t == board[i + 4][j + 4])
  • // return 1;
  • // }
  • // for (i = a - 4, j = b + 4; i <= a, j >= b; i++, j--)
  • // {
  • // if (i >= 1 && i < 16 && j >= 1 && j < 16 && t == board[i][j] && t == board[i + 1][j - 1] &&
  • // t == board[i + 2][j - 2] && t == board[i + 3][j - 3] && t == board[i + 4][j - 4])
  • // return 1;
  • // }
  • // return 0;
  • //
  • //}
  • int main()
  • {
  • initGame();
  • playChess();
  • closegraph();
  • return 0;
  • }