c新手吧 关注:8,602贴子:48,094
  • 5回复贴,共1

C语言写的一个井字棋人机小游戏

只看楼主收藏回复

有个小BUG自己实在找不出来 发出来希望大佬能帮一下。。源代码放在下面


1楼2017-12-09 20:22回复
    #include <stdio.h> /* include information about standard library stdio.h */
    #include <string.h> /* include information about standard library string.h */
    #include <Windows.h> /* include information about standard library Windows.h */
    #include<stdlib.h> // 使用了srand和rand函数
    #include<time.h> // 使用了time函数
    int row = 0; /* declare six variables of type of integer namely player,winner,choice,row,column and line */
    int column = 0; /* and their value all equal to 0 */
    int choice = 0;
    int player;
    int winner = 0;
    int line = 0;
    int selection = 0;
    char board[3][3] = /* Declaring a two - dimensional array of length 3 namely board */
    {
    { '1','2','3' },
    { '4','5','6' },
    { '7','8','9' }
    };
    void gambo() {
    printf(" # # \n"); /*print out the chess board on the screen */
    printf(" %c # %c # %c \n", board[0][0], board[0][1], board[0][2]);
    printf(" # # \n");
    printf("#################\n");
    printf(" # # \n");
    printf(" %c # %c # %c \n", board[1][0], board[1][1], board[1][2]);
    printf(" # # \n");
    printf("#################\n");
    printf(" # # \n");
    printf(" %c # %c # %c \n", board[2][0], board[2][1], board[2][2]);
    printf(" # # \n");
    }
    void end() {
    printf("\n\n");
    gambo();
    if (winner == 0)
    printf("\nIt is drawn that no one is winner.\n");
    else if (player == 1)
    printf("\nCongrations! player ,YOU ARE THE WINNER!\n");
    else printf("\ncomputer is the winner\n");
    printf("Do you want to play again?press y/n\n");
    int rep;
    rep = getchar();
    if (rep == 'y') {
    system("cls");
    board[0][0] = '1'; board[0][1] = '2'; board[0][2] = '3';
    board[1][0] = '4'; board[1][1] = '5'; board[1][2] = '6';
    board[2][0] = '7'; board[2][1] = '8'; board[2][2] = '9';
    winner = 0;
    main();
    }
    else
    printf("\n\nThank you for playing the tic-tac-toe game, please press a key to close the program\n\n");/*End the program by pressing a key*/
    exit(0);
    }
    void check() {
    if ((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
    (board[0][2] == board[1][1] && board[0][2] == board[2][0]))
    {
    winner = player;
    end();
    }
    else
    for (line = 0; line <= 2; line++)
    if ((board[line][0] == board[line][1] && board[line][0] == board[line][2]) ||
    (board[0][line] == board[1][line] && board[0][line] == board[2][line]))
    {
    winner = player; /*Check rows and columns for a winning line */
    end();
    }
    }
    void com() {
    int dice;
    srand((unsigned int)time(NULL));
    while (board[row][column]>'9') {
    dice = rand() % 9 + 1;
    row = --dice / 3; /*give the value to the variables named row and column depending on the choice*/
    column = dice % 3;
    };
    board[row][column] = 'O';
    }
    void human() {
    printf("\n(If you enter the number out of range, "
    "you need to reenter the number until the right number.)\n");
    do {
    printf("\nPlayer, please enter the number of the chess board where you want to place your %c: ",'X');
    scanf("%d", &choice);
    getchar();
    rewind(stdin);
    row = --choice / 3;
    column = choice % 3;
    } while (choice<0 || choice>9 || board[row][column]>'9');
    board[row][column] = 'X';
    }
    int main() /* define a function named main */
    {
    do {
    printf("Do you want to go first or seconf? press 1 or 2 to select.\n");
    scanf("%d", &selection);
    getchar();
    } while (selection < 1 || selection > 2);
    rewind(stdin);
    int i,j;
    if (selection == 1)j = 5;
    else j = 4;
    for (i = 0; i<j && winner == 0; i++) /*using a loop to renew the chess board */
    {
    system("cls");
    gambo();
    if (selection == 1) {
    player = 1;
    human();
    check();
    player = 2;
    com();
    check();
    }
    else {
    player = 2;
    com();
    system("cls");
    gambo();
    check();
    player = 1;
    human();
    check();
    }
    }
    end();
    }


    2楼2017-12-09 20:23
    回复
      当平局的时候最后一步棋下不进去 程序会卡住


      3楼2017-12-09 20:25
      回复
        希望真的有人会运行下试试吧


        4楼2017-12-09 20:28
        回复
          回楼主我运行了下
          觉得可以加一个结束重开的程序
          另外我的Dev C++提示
          if (rep=='y')
          这行有错误!


          来自iPhone客户端5楼2017-12-20 18:04
          回复
            这个是人机之间的,那有没有两个玩家玩的?


            6楼2020-05-14 12:19
            回复