简易五子棋

#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;
}









//太奇妙了
//和搭积木一样
//遍历每一个点然后每一个点去验证上下左右
//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值