| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 979 人关注过本帖
标题:三子棋
只看楼主 加入收藏
奇数层
Rank: 2
等 级:论坛游民
帖 子:33
专家分:10
注 册:2022-8-20
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:6 
三子棋
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:10 
假如连编译器给出的警告信息你都无视的话,那……

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;
}
warning C4706: 条件表达式内的赋值

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");
        }
    }
   
}
warning C4706: 条件表达式内的赋值

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;
            }
        }
    }
}
warning C4706: 条件表达式内的赋值
2022-09-02 07:38
ssl002
Rank: 2
等 级:论坛游民
威 望:2
帖 子:75
专家分:43
注 册:2017-6-13
收藏
得分:0 
为什么我运行了报错
错误(活动)    E0513    不能将 "const char *" 类型的值分配到 "char" 类型的实体    69   
void Player_move(char board[ROW][COL], int row, int col)
{
    int input1 = 0;
    int input2 = 0;
    while (1)
    {
        printf("输入下子坐标\n");
        scanf_s("%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");
        }
    }
}
这里报错,不能把const char分配给char?求解!
2022-09-02 17:08
奇数层
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
是,但你原先代码中本来写的就是 board[input1 - 1][input2 - 1] = '*';
为什么又不声不响地改掉了?
2022-09-03 18:22
奇数层
Rank: 2
等 级:论坛游民
帖 子:33
专家分:10
注 册:2022-8-20
收藏
得分:0 
回复 6楼 rjsp
我是看到ssl002他改掉了。。。。。
2022-09-05 19:24
快速回复:三子棋
数据加载中...
 
   



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

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