标题:三子棋
取消只看楼主
奇数层
Rank: 2
等 级:论坛游民
帖 子:33
专家分:10
注 册:2022-8-20
结帖率:66.67%
已结贴  问题点数:10 回复次数:3 
三子棋
text.c
#include "game.h"
#define _CRT_SECURE_NO_WARNINGS




void menu()
{
    printf("********三子棋游戏********\n");
    printf("*****请选择->1玩0不玩*****\n");
}

void game()
{
    int num = 3;
    char board[ROW][COL] = { 0 };
    Initia_board(board, ROW, COL);
    Oput_board(board, ROW, COL);
    while (1)
    {
    Player_move(board, ROW, COL);
    Oput_board(board, ROW, COL);
    num= Is_victory(board, ROW, COL);
       if (num == 1)
       {
          printf("玩家赢\n");
          break;
       }
       if (num ==2)
       {
          printf("平局\n");
          break;
       }
    Computer_move(board, ROW, COL);
    Oput_board(board, ROW, COL);
    num = Is_victory(board, ROW, COL);
       if (num == 1)
       {
          printf("电脑赢\n");
          break;
       }
       if (num == 2)
       {
          printf("平局\n");
          break;
       }
    }
    printf("游戏结束\n");
   
}

int main()
{int input = 0;
 menu();
       do
       {
           scanf("%d", &input);
           switch (input)
           {
              case 1:
                  game();
                  break;
              case 0:
                  break;
              default:
                  printf("请重新输入\n");
                  break;
           }
       }
       while (input);
return 0;
}

game.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"


int Is_draw(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i =0 ; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (board[i][j] = ' ')
                return 0;
        }   
    }
    return 1;
}

void Initia_board(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < ROW;i++)
    {
        for (j = 0; j < COL; j++)
        {
            board[i][j] = ' ';
        }
    }
}

void Oput_board(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < ROW; i++)
    {
        for (j = 0; j < COL; j++)
        {
            printf(" %c ", board[i][j]);
            if (j < COL - 1)
            {
                printf("|");
            }   
         
        }
    printf("\n");   
        if (i < ROW - 1)
        {
            for (j= 0; j < COL; j++)
            {
                printf("--- ");
            }
        }
    printf("\n");
    }
}

void Player_move(char board[ROW][COL], int row, int col)
{
   
    int input1 = 0;
    int input2 = 0;
    while(1)
    {
        printf("输入下子坐标\n");
        scanf("%d %d", &input1,&input2);
        if ((input1 >= 1 && input1 <= row) && (input2 >= 1 && input2 <= col))
        {
            if (board[input1 - 1][input2 - 1] = ' ')
            {
                board[input1 - 1][input2 - 1] = '*';
                break;
            }
            else
            {
                printf("重复下子\n");
            }
        }
        else
        {
            printf("输入的坐标超出范围\n");
        }
    }
   
}

void Computer_move(char board[ROW][COL],int row,int col)
{
    srand((int)time(NULL));
    int x = 0;
    int y = 0;
    x = rand() % 3;
    y = rand() % 3;
    while (1)
    {
        x = rand() % 3;
        y = rand() % 3;
        if ((x >= 0 && y < row) && (y >= 0 && x < col))
        {
            if (board[x][y] = ' ')
            {
                 board[x][y] = '#';
                 break;
            }
        }
    }
}


int Is_victory(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    int ret = 0;
   
    for (i = 0; i < row; i++)
    {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2]&& board[i][0]!=' ')
            return 1;
    }
    for(j=0;j<col;j++)
    {
        if (board[0][j] == board[1][j] && board[1][j] == board[2][j]&& board[0][j]!=' ')
            return 1;
    }
    if (board[0][0] == board[1][1] == board[2][2] && board[0][0] != ' ')
    {
        return 1;
    }
    if (board[0][2] == board[1][1] == board[2][0] && board[2][0] != ' ')
    {
        return 1;
    }
    ret = Is_draw(board, ROW, COL);
    if (ret == 1)
    {
        return 2;
    }
    return 3;
   


}
game.h
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <math.h>
#include <time.h>

#define  ROW  3
#define  COL  3

void Initia_board(char board[ROW][COL],int row,int col);
void Oput_board(char board[ROW][COL],int row,int col);
void Player_move(char board[ROW][COL], int row, int col);
void Computer_move(char board[ROW][COL], int row,int col);
int Is_victory(char board[ROW][COL],int row,int col);



为什么第一次下的时后,电脑下完后没有同步我下的呢?但后面又同步了?
搜索更多相关主题的帖子: printf int ROW char board 
2022-09-01 22:03
奇数层
Rank: 2
等 级:论坛游民
帖 子:33
专家分:10
注 册:2022-8-20
得分:0 
回复 2楼 rjsp
感谢指正
2022-09-02 18:44
奇数层
Rank: 2
等 级:论坛游民
帖 子:33
专家分:10
注 册:2022-8-20
得分:0 
回复 3楼 ssl002
"*"是不是这里应该用单引号呢
2022-09-02 19:05
奇数层
Rank: 2
等 级:论坛游民
帖 子:33
专家分:10
注 册:2022-8-20
得分:0 
回复 6楼 rjsp
我是看到ssl002他改掉了。。。。。
2022-09-05 19:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-509931-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.208670 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved