c和easyx写的五子棋怎么显示变化的黑棋数目代码
时间: 2023-03-26 13:03:45 浏览: 88
利用EasyX图形库实现五子棋程序(C语言课程设计)C语言入门 言简意赅
5星 · 资源好评率100%
以下是 C 和 EasyX 写的五子棋显示变化的黑棋数目代码:
```c
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CHESS_SIZE 40
#define CHESS_NUM 15
#define BOARD_SIZE (CHESS_SIZE * CHESS_NUM)
#define BOARD_LEFT 50
#define BOARD_TOP 50
#define BOARD_RIGHT (BOARD_LEFT + BOARD_SIZE)
#define BOARD_BOTTOM (BOARD_TOP + BOARD_SIZE)
#define WIN_SIZE 200
#define WIN_LEFT ((BOARD_LEFT + BOARD_RIGHT - WIN_SIZE) / 2)
#define WIN_TOP ((BOARD_TOP + BOARD_BOTTOM - WIN_SIZE) / 2)
#define WIN_RIGHT (WIN_LEFT + WIN_SIZE)
#define WIN_BOTTOM (WIN_TOP + WIN_SIZE)
int board[CHESS_NUM][CHESS_NUM] = {};
int player = 1;
int black_count = ;
void draw_board()
{
setbkcolor(RGB(200, 160, 60));
cleardevice();
setlinestyle(PS_SOLID, 2);
setlinecolor(BLACK);
for (int i = ; i < CHESS_NUM; i++)
{
line(BOARD_LEFT, BOARD_TOP + i * CHESS_SIZE, BOARD_RIGHT, BOARD_TOP + i * CHESS_SIZE);
line(BOARD_LEFT + i * CHESS_SIZE, BOARD_TOP, BOARD_LEFT + i * CHESS_SIZE, BOARD_BOTTOM);
}
setfillcolor(BLACK);
solidcircle(BOARD_LEFT + 3 * CHESS_SIZE, BOARD_TOP + 3 * CHESS_SIZE, 5);
solidcircle(BOARD_LEFT + 11 * CHESS_SIZE, BOARD_TOP + 3 * CHESS_SIZE, 5);
solidcircle(BOARD_LEFT + 7 * CHESS_SIZE, BOARD_TOP + 7 * CHESS_SIZE, 5);
solidcircle(BOARD_LEFT + 3 * CHESS_SIZE, BOARD_TOP + 11 * CHESS_SIZE, 5);
solidcircle(BOARD_LEFT + 11 * CHESS_SIZE, BOARD_TOP + 11 * CHESS_SIZE, 5);
settextcolor(BLACK);
settextstyle(20, , _T("宋体"));
outtextxy(WIN_LEFT + 20, WIN_TOP + 20, _T("黑方:"));
outtextxy(WIN_LEFT + 20, WIN_TOP + 60, _T("白方:"));
}
void draw_chess(int x, int y)
{
int left = BOARD_LEFT + x * CHESS_SIZE - CHESS_SIZE / 2;
int top = BOARD_TOP + y * CHESS_SIZE - CHESS_SIZE / 2;
if (board[x][y] == 1)
{
setfillcolor(BLACK);
solidcircle(left, top, CHESS_SIZE / 2 - 2);
black_count++;
TCHAR str[10];
_stprintf_s(str, _T("%d"), black_count);
settextcolor(BLACK);
settextstyle(20, , _T("宋体"));
outtextxy(WIN_LEFT + 100, WIN_TOP + 20, str);
}
else if (board[x][y] == 2)
{
setfillcolor(WHITE);
solidcircle(left, top, CHESS_SIZE / 2 - 2);
}
}
bool check_win(int x, int y)
{
int count = 1;
int i, j;
// 横向
for (i = x - 1; i >= && board[i][y] == player; i--)
count++;
for (i = x + 1; i < CHESS_NUM && board[i][y] == player; i++)
count++;
if (count >= 5)
return true;
// 纵向
count = 1;
for (j = y - 1; j >= && board[x][j] == player; j--)
count++;
for (j = y + 1; j < CHESS_NUM && board[x][j] == player; j++)
count++;
if (count >= 5)
return true;
// 左上到右下
count = 1;
for (i = x - 1, j = y - 1; i >= && j >= && board[i][j] == player; i--, j--)
count++;
for (i = x + 1, j = y + 1; i < CHESS_NUM && j < CHESS_NUM && board[i][j] == player; i++, j++)
count++;
if (count >= 5)
return true;
// 右上到左下
count = 1;
for (i = x + 1, j = y - 1; i < CHESS_NUM && j >= && board[i][j] == player; i++, j--)
count++;
for (i = x - 1, j = y + 1; i >= && j < CHESS_NUM && board[i][j] == player; i--, j++)
count++;
if (count >= 5)
return true;
return false;
}
void game_loop()
{
srand((unsigned)time(NULL));
while (true)
{
int x = rand() % CHESS_NUM;
int y = rand() % CHESS_NUM;
if (board[x][y] == )
{
board[x][y] = player;
draw_chess(x, y);
if (check_win(x, y))
{
TCHAR str[20];
_stprintf_s(str, _T("恭喜黑方获胜!"));
MessageBox(NULL, str, _T("游戏结束"), MB_OK);
break;
}
player = 3 - player;
}
Sleep(100);
}
}
int main()
{
initgraph(640, 480);
draw_board();
game_loop();
closegraph();
return ;
}
```
阅读全文