#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include <graphics.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
IMAGE chess;
int num = -1;
int arr[16][16];
void putbackground() {
putimage(0, 0, &chess);
}
void draw_line() {
setlinecolor(BLACK);
for (int x = 30; x < 480; x += 30) {
line(x, 30, x, 450);
}
for (int y = 30; y < 480; y += 30) {
line(30, y, 450, y);
}
}
int checkpiece(int x, int y) {
if (arr[x][y] != 0) {
return 0;
}
else {
arr[x][y] = num;
}
return 1;
}
void draw_piece(int x, int y) {
if (num == 1) {
setfillcolor(WHITE);
}
else {
setfillcolor(BLACK);
}
//更加的简化了
// x = (x + 15) / 30;
// y = (y + 15) / 30;
if (x % 30 > 15)
{
x = x / 30 + 1;
}
else
{
x = x / 30;
}
if (y % 30 > 15) {
y = y / 30 + 1;
}
else {
y = y / 30;
}
if (checkpiece(x, y) == 0) {
return;
}
fillcircle(x * 30, y * 30, 10);
num *= -1;
}
void draw_point() {
setfillcolor(BLACK);
fillcircle(120, 120, 3);
fillcircle(120, 360, 3);
fillcircle(240, 240, 3);
fillcircle(360, 120, 3);
fillcircle(360, 360, 3);
}
int check_five(int x, int y) {
int count = 1;
int color = arr[x][y];
int i, j;
// 横向判断
i = x - 1, j = y;
while (i >=1 && arr[i][j] == color) {
count++;
i--;
}
i = x + 1, j = y;
while (i < 16 && arr[i][j] == color) {
count++;
i++;
}
if (count >= 5) return 1;
// 纵向判断
count = 1;
i = x, j = y - 1;
while (j >= 1 && arr[i][j] == color) {
count++;
j--;
}
i = x, j = y + 1;
while (j < 16 && arr[i][j] == color) {
count++;
j++;
}
if (count >= 5) return 1;
// 左下到右上判断
count = 1;
i = x - 1, j = y + 1;
while (i >= 1 && j < 16 && arr[i][j] == color) {
count++;
i--;
j++;
}
i = x + 1, j = y - 1;
while (i < 16 && j >= 1 && arr[i][j] == color) {
count++;
i++;
j--;
}
if (count >= 5) return 1;
return 0;
}
int checkover() {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
if (arr[i][j] != 0) {
if (check_five(i, j)) {
return 1;
}
}
}
}
return 0;
}
int main() {
initgraph(480, 480, NULL);
MOUSEMSG m;
loadimage(&chess, _T("background.jpg"));
putbackground();
draw_line();
draw_point();
memset(arr, 0, sizeof(arr));
while (1) {
m = GetMouseMsg();
if (m.uMsg == WM_LBUTTONDOWN) {
draw_piece(m.x, m.y);
}
if (checkover() == 1) {
settextcolor(RED);
setbkmode(TRANSPARENT);
settextstyle(42, 20, _T("隶书"));
outtextxy(150, 200, _T("Game Over"));
break;
}
}
getchar();
closegraph();
return 0;
}
//太奇妙了
//和搭积木一样
//遍历每一个点然后每一个点去验证上下左右
//
简易五子棋
最新推荐文章于 2024-09-12 19:02:20 发布