模块化编程——五子棋游戏

关于下棋后无法打印棋盘的问题

img

代码如下:
main.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "include\game.h"
#define SIZE 15

int main()
{
    int input = 0;
    do
    {
        srand((unsigned int)time(NULL));
        menu();
        printf("请选择(1—play/0—exit):>");
        scanf("%d", &input);
        switch (input)
        { 
        case 1:
            printf("五子棋游戏\n");
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("输入错误,重新输入\n");
            break;
        }
    } while (input);
    return 0;
}

game.c


```c
#include "game.h"
#define SIZE 15
int board[SIZE][SIZE];
int player = 1;
int game_over = 0;
int i,j;
void menu()
{
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
    printf("=+= Wellcome to Five-in-a-Row! =+=\n");
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
}
void init_game() {
    for ( i = 0; i < SIZE; i++) {
        for ( j = 0; j < SIZE; j++) {
            board[i][j] = 0;
        }
    }
    player = 1;
    game_over = 0;
}
void draw_board() {
    printf("\n");
    for ( i = 0; i <= SIZE; i++) {
        printf("%2d", i);
        if (i == 0) {
            printf(" ");
        } else {
            printf("|");
        }
        for ( j = 1; j <= SIZE; j++) {
            if (i == 0) {
                printf("%2d", j);
            } 
            else {
                if (board[i-1][j-1] == 0) {
                    printf("%2c", ' ');
                } 
                else {
                    printf("%2c", board[i-1][j-1] == 1 ? 'X' : 'O');
                }
            }
            printf("|");
        }
        printf("\n");
        if (i == 0) {
            for (j = 0; j <= SIZE; j++) {
                printf("---");
            }
            printf("\n");
        }
    }
    printf("\n");
}
int check_pos(int x, int y) {
    if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
        return -1;
    } else {
        return board[x][y];
    }
}
int check_line(int x, int y, int dx, int dy) {
    for ( i = 0; i < 5; i++) {
        if (check_pos(x + i*dx, y + i*dy) != player) {
            return 0;
        }
    }
    return 1;
}
int check_win() {
    for ( i = 0; i < SIZE; i++) {
        for (j = 0; j < SIZE; j++) {
            if (check_line(i, j, 1, 0) || check_line(i, j, 0, 1) || check_line(i, j, 1, 1) || check_line(i, j, 1, -1)) {
                return 1;
            }
        }
    }
    return 0;
}
void game_loop() {
    int x, y;
    draw_board();
    while (!game_over) {
        printf("Player %d's turn.\n", player);
        printf("Enter X coordinate: ");
        scanf("%d", &x);
        printf("Enter Y coordinate: ");
        scanf("%d", &y);
        if (board[x-1][y-1] != 0) {
            printf("Invalid move. Please try again.\n");
            continue;
        }
        board[x-1][y-1] = player;
        if (check_win()) {
            printf("Player %d wins!\n", player);
            game_over = 1;
        } else if (player == 1) {
            player = 2;
        } else {
            player = 1;
        }
        draw_board();
    }
}
void game()
{
    init_game();
    game_loop();
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
    printf("=+= Thank you for playing Five-in-a-Row! =+=\n");
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
}

game.h


```c
#ifndef _GAME_H
#define _GAME_H
void menu();
void game();
void init_game();
void draw_board();
int check_pos(int x, int y);
int check_line(int x, int y, int dx, int dy);
int check_win();
void game_loop();


#endif

这是项目里的情况

img

img

img

resouce文件夹里是空的

如果有知道请帮忙发一下修改后的源码。

把定义为全局变量的x和y改为局部变量,你的x和y定义的全局变量,下面好多函数又对x和y进行赋值,程序会死在check_win() 这个函数上,这个函数出现死循环。

#include "game.h"
#define SIZE 15
int board[SIZE][SIZE];
int player = 1;
int game_over = 0;
void menu()
{
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
    printf("=+= Wellcome to Five-in-a-Row! =+=\n");
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
}
void init_game() 
{
    int i, j;
    for (i = 0; i < SIZE; i++) 
    {
        for (j = 0; j < SIZE; j++) 
        {
            board[i][j] = 0;
        }
    }
    player = 1;
    game_over = 0;
}
void draw_board() 
{
    int i, j;
    printf("\n");
    for (i = 0; i <= SIZE; i++) 
    {
        printf("%2d", i);
        if (i == 0) 
        {
            printf(" ");
        }
        else {
            printf("|");
        }
        for (j = 1; j <= SIZE; j++) 
        {
            if (i == 0) 
            {
                printf("%2d", j);
            }
            else 
            {
                if (board[i - 1][j - 1] == 0) 
                {
                    printf("%2c", ' ');
                }
                else 
                {
                    printf("%2c", board[i - 1][j - 1] == 1 ? 'X' : 'O');
                }
            }
            printf("|");
        }
        printf("\n");
        if (i == 0) 
        {
            for (j = 0; j <= SIZE; j++) 
            {
                printf("---");
            }
            printf("\n");
        }
    }
    printf("\n");
}
int check_pos(int x, int y) 
{
    int i, j;
    if (x < 0 || x >= SIZE || y < 0 || y >= SIZE)
    {
        return -1;
    }
    else {
        return board[x][y];
    }
}
int check_line(int x, int y, int dx, int dy) 
{
    int i, j;
    for (i = 0; i < 5; i++) 
    {
        if (check_pos(x + i * dx, y + i * dy) != player) 
        {
            return 0;
        }
    }
    return 1;
}
int check_win() 
{
    int i, j;
    for (i = 0; i < SIZE; i++) 
    {
        for (j = 0; j < SIZE; j++) 
        {
            if (check_line(i, j, 1, 0) || check_line(i, j, 0, 1) || check_line(i, j, 1, 1) || check_line(i, j, 1, -1))
            {
                return 1;
            }
        }
    }
    return 0;
}
void game_loop()
{
    int x, y;//
    draw_board();
    while (!game_over) 
    {
        printf("Player %d's turn.\n", player);
        printf("Enter X coordinate: ");
        scanf("%d", &x);
        printf("Enter Y coordinate: ");
        scanf("%d", &y);
        if (board[x - 1][y - 1] != 0)
        {
            printf("Invalid move. Please try again.\n");
            continue;
        }
        board[x - 1][y - 1] = player;
        if (check_win()) 
        {
            printf("Player %d wins!\n", player);
            game_over = 1;
        }
        else if (player == 1)
        {
            player = 2;
        }
        else 
        {
            player = 1;
        }
        draw_board();
    }
}
void game()
{
    init_game();
    game_loop();
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
    printf("=+= Thank you for playing Five-in-a-Row! =+=\n");
    printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n");
}

改完之后的结果:

img


我只改了你说的无法打印棋盘问题,其他的还没看检查。

谢谢,感谢大佬的解答!

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇博客: 【牛客刷题】上手用C语言写一个三子棋小游戏超详解哦(电脑优化)中的 game.c代码 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    #include "game.h"
    void Chess(char chess[Row][Col], int row, int col) {
    	int i = 0, j = 0;
    	for (i = 0; i < row; i++) {
    		for (j = 0; j < col; j++) {
    			chess[i][j] =' ';
    		}
    		printf("\n");
    	}
    }
    void Chessboard(char chess[Row][Col], int row, int col) {
    	int i = 0, j = 0;
    	for (i = 0; i < row; i++) {
    		for (j = 0; j < col; j++) {
    			printf(" %c ",chess[i][j]);
    			if (j < col - 1)
    				printf("|");
    		}
    		printf("\n");
    		if (i < row - 1) {
    			for (j = 0; j < col; j++) {
    				printf("---");
    				if (j < col-1)
    					printf("|");
    			}
    			printf("\n");
    		}
    	}
    }
    void playermove(char chess[Row][Col], int row, int col) {
    	int x = 0, y = 0;
         printf("玩家走\n");
    	while (1) {
    		printf("请输入要下的棋子坐标\n");
    		scanf("%d %d", &x, &y);
    		if (x <= row && x >= 1 && y >= 1 && y <= col) {
    			if (chess[x - 1][y - 1] == ' ') {
    				chess[x - 1][y - 1] = 'X';
    				break;
    			}
    			else
    				printf("坐标被占用,请重新输入\n");
    		}
    		else
    			printf("非法输入,请重新输入\n");
    	}
    }
    int AImove(char chess[Row][Col], int row, int col) {
    	int i = 0, j = 0;
    	if (chess[0][0] == chess[1][1] && chess[1][1] == chess[0][2] && chess[1][1] != ' ' && chess[2][2] == ' '&&chess[2][0]==' ')
    	{
    		chess[2][0] = 'O';
    		return 1;
    	}
    	for (i = 0; i < row; i++) {
    		if (chess[i][0] == chess[i][1]&&chess[i][1]!=' '&&chess[i][2]==' ') {
    			chess[i][2] = 'O';
    			return 1;
    		}
    			
    		if (chess[i][1] == chess[i][2] && chess[i][1] != ' '&&chess[i][0]==' ') {
    			chess[i][0] = 'O';
    			return 1;
    		}
    			
    		if (chess[i][0] == chess[i][2] && chess[i][0] != ' '&&chess[i][1]==' ') {
    			chess[i][1] = 'O';
    			return 1;
    		}	
    	}
    	for (j = 0; j < col; j++) {
    		if (chess[0][j] == chess[1][j] && chess[1][j] != ' '&&chess[2][j]==' ') {
    			chess[2][j] = 'O';
    			return 1;
    		}
    			
    		if (chess[1][j] == chess[2][j] && chess[1][j] != ' '&&chess[0][j]==' ') {
    			chess[0][j] = 'O';
    			return 1;
    		}
    			
    		if (chess[0][j] == chess[2][j] && chess[2][j] != ' '&&chess[1][j]==' ') {
    			chess[1][j] = 'O';
    			return 1;
    		}
    	}
    	if (chess[1][1] == chess[0][0] && chess[1][1] != ' '&&chess[2][2]==' ')
    	{
    		chess[2][2] = 'O';
    		return 1;
    	}
    	if (chess[1][1] == chess[2][2] && chess[1][1] != ' '&&chess[0][0]==' ')
    	{
    		chess[0][0] = 'O';
    		return 1;
    	}
    	if (chess[2][2] == chess[0][0] && chess[2][2] != ' '&&chess[1][1]==' ')
    	{
    		chess[1][1] = 'O';
    		return 1;
    	}
    	if (chess[1][1] == chess[0][2] && chess[1][1] != ' '&&chess[2][0]==' ')
    	{
    		chess[2][0] = 'O';
    		return 1;
    	}
    	if (chess[1][1] == chess[2][0] && chess[1][1] != ' '&&chess[0][2]==' ')
    	{
    		chess[0][2] = 'O';
    		return 1;
    	}
    	if (chess[0][2] == chess[2][0] && chess[2][0] != ' '&&chess[1][1]==' ')
    	{
    		chess[1][1] = 'O';
    		return 1;
    	}
    }
    void Computermove(char chess[Row][Col], int row, int col) {
    	printf("电脑走\n");
    	while (1) {
    		int ret = AImove(chess, Row, Col);
    		if (ret == 1)
    			break;
    		else {
    		int x = rand() % row;
    		int y = rand() % col;
    		if (chess[x][y] == ' ') {
    			chess[x][y] = 'O';
    			break;
    		}
    		}
    		
    	}
    }
    int Isfull(char chess[Row][Col], int row, int col) {
    	int i = 0, j = 0;
    	for (i = 0; i < row; i++) {
    		for (j = 0; j < col; j++) {
    			if (chess[i][j] == ' ')
    				return 1;
    		}
    	}
    	return 0;
    }
    char Result(char chess[Row][Col], int row, int col) {
    	int i = 0, j = 0;
    	for (i = 0; i < row; i++) {
    		if (chess[i][0] == chess[i][1]&&chess[i][1] == chess[i][2] && chess[i][1] != ' ') {
    			return chess[i][0];
    		}
    	}
    	for (j = 0; j < col; j++) {
    		if (chess[0][j] == chess[1][j]&&chess[1][j] == chess[2][j] && chess[1][j] != ' ') {
    			return chess[1][j];
    		}
    	}
    	if (chess[0][0] == chess[1][1]&&chess[1][1] == chess[2][2] && chess[1][1] != ' ') {
    		return chess[1][1];
    	}
    	if (chess[0][2] == chess[1][1]&&chess[1][1] == chess[2][0] && chess[1][1] != ' ') {
    		return chess[1][1];
    	}
    	int ret=Isfull(chess, row, col);
    	if (ret == 1) {
    		return 'C';
    	}
    	else
    		return 'P';
    }
    

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^