五子棋 |
Cobb 590 0

五子棋程序



#include <iostream>
#include <string.h>
#include "array.h"
#include <stdio.h>


int main()
{
    // 五子棋的棋盘,初始全为0
    int board[12][12] = { 0 };
    int flag = 0; // 0:甲   1:已
    bool gameOver = false;

    // 开始下棋
    // 死循环,最终判断谁赢了,用break跳出该循环
    for (;;)
    {
        // 打印棋盘的内容
        for (int i = 0; i < 12; i++)
        {
            for (int j = 0; j < 12; j++)
            {
                if (board[i][j] == 0)
                {
                    printf("%d ", board[i][j]);
                }
                else
                {
                    printf("%c ", board[i][j]);
                }
            }
            printf("\n");
        }

        if (gameOver)
        {
            break;
        }

        // 提示当前下棋的人开始走棋
        int row = 0;
        int col = 0;

        // 循环判断用户的输入,直到用户输入的数据是合法的
        for (;;)   
        {
            printf("%s下棋:", (flag == 0 ? "甲" : "乙"));
            scanf("%d %d", &row, &col);

            if (row >= 12 || col >= 12)
            {
                printf("row或者col不合法! 重新输入\n");
            }
            else if (board[row][col] != 0)
            {
                printf("当前位置不能下棋! 重新输入\n");
            }
            else
            {
                // 输入合法
                break;
            }
        }

        // 接收下棋人输入的坐标,修改board相应坐标的数字 甲:1   乙:2
        if (flag == 0) // 甲下棋
        {
            board[row][col] = '*';
            flag = 1;
        }
        else // 乙下棋
        {
            board[row][col] = '#';
            flag = 0;
        }

        // 判断输赢状态  board[row][col]
        // 横方向判断
        int count = 0;
        for (int j = col + 1; j < 12; j++) // 判断右方向
        {
            if (board[row][j] == board[row][col])
                count++;
            else
                break;
        }
        for (int j = col - 1; j >= 0; j--) // 判断左方向
        {
            if (board[row][j] == board[row][col])
                count++;
            else
                break;
        }
        if (count + 1 >= 5)
        {
            printf("%s 赢了!\n", (flag == 0 ? "乙" : "甲"));
            gameOver = true;
            continue;
        }

        // 垂直方向 row col 
        count = 0;
        for (int i = row - 1; i >= 0; i--)
        {
            if (board[i][col] == board[row][col])
                count++;
            else
                break;
        }
        for (int i = row + 1; i < 12; i++)
        {
            if (board[i][col] == board[row][col])
                count++;
            else
                break;
        }
        if (count + 1 >= 5)
        {
            printf("%s 赢了!\n", (flag == 0 ? "乙" : "甲"));
            gameOver = true;
            continue;
        }

        // 左上 - 右下
        count = 0;
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--)
        {
            if (board[i][j] == board[row][col])
                count++;
            else
                break;
        }
        for (int i = row + 1, j = col + 1; i < 12 && j < 12; i++, j++)
        {
            if (board[i][j] == board[row][col])
                count++;
            else
                break;
        }
        if (count + 1 >= 5)
        {
            printf("%s 赢了!\n", (flag == 0 ? "乙" : "甲"));
            gameOver = true;
            continue;
        }

        // 左下 - 右上
        count = 0;
        for (int i = row + 1, j = col - 1; i < 12 && j >= 0; i++, j--)
        {
            if (board[i][j] == board[row][col])
                count++;
            else
                break;
        }
        for (int i = row - 1, j = col + 1; i >= 0 && j < 12; i--, j++)
        {
            if (board[i][j] == board[row][col])
                count++;
            else
                break;
        }
        if (count + 1 >= 5)
        {
            printf("%s 赢了!\n", (flag == 0 ? "乙" : "甲"));
            gameOver = true;
            continue;
        }
    }
}

评论区