C语言——三子棋

来源:互联网 发布:上海云计算公司 编辑:程序博客网 时间:2024/06/08 13:00
game.h
#ifndef _GAME_H_#define _GAME_H_#include<stdio.h>#include<stdlib.h>#include<string.h>#define ROWS 3#define COLS 3void InitBoard(char board[ROWS][COLS], int row, int col);void DisplayBoard(char board[ROWS][COLS], int row, int col);void PlayMove(char board[ROWS][COLS], int row, int col);void ComputerMove(char board[ROWS][COLS], int row, int col);static int IsWin(char board[ROWS][COLS], int row, int col);char IsWiner(char board[ROWS][COLS], int row, int col);#endif
test.c
#include<stdio.h>#include<time.h>#include "game.h"#include<stdlib.h>void menu(){    printf("**********************************************************\n");    printf("**********  1-play  ****************  0-exit  ************\n");    printf("**********************************************************\n");}void game(){    char board[ROWS][COLS] = { 0 };    char ret = 0;    InitBoard(board, ROWS, COLS);//初始化棋盘    DisplayBoard(board, ROWS, COLS);//打印棋盘    srand((unsigned int)rand(NULL));    while (1)    {        PlayMove(board, ROWS, COLS);//玩家走        if ((ret = IsWiner(board, ROWS, COLS)) != ' ')            break;        DisplayBoard(board, ROWS, COLS);        printf("-----------------------------\n");        printf("-----------------------------\n");        ComputerMove(board, ROWS, COLS);//电脑走        if ((ret = IsWiner(board, ROWS, COLS)) != ' ')            break;        DisplayBoard(board, ROWS, COLS);    }    //结果评定    if (ret == 'X')    {        DisplayBoard(board, ROWS, COLS);        printf("\n");        printf("哇!你赢啦~~真棒!!\n");    }    if (ret == '0')    {        DisplayBoard(board, ROWS, COLS);        printf("你好笨哦~电脑赢了你耶= =\n");    }    if (ret == 'q')    {        DisplayBoard(board, ROWS, COLS);        printf("你和电脑半斤八两,打了个平手啊~\n");    }}int  main(){    int input = 0;    do    {        menu();        printf("请选择:> ");        scanf_s("%d", &input);        switch (input)        {        case 1:            game();            break;        case 0:            printf("退出游戏!\n");            break;        }    } while (input);    return 0;}
game.c
#include"game.h"void InitBoard(char board[ROWS][COLS], int row, int col){    memset(board, ' ', col*row*sizeof(char));}void DisplayBoard(char board[ROWS][COLS], int row, int col){    int i = 0;    for (i = 0; i < row; i++)    {        printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);        if (i != 2)            printf("---|---|---\n");    }}void PlayMove(char board[ROWS][COLS], int row, int col){    int x = 0;    int y = 0;    while (1)    {        printf("请输入你要下位置的坐标:>   ");        scanf_s("%d%d", &x, &y);        x--;        y--;        if (((x >= 0) && (x <= 2)) && ((y >= 0) && (y <= 2)))        {            if (board[x][y] == ' ')            {                board[x][y] = 'X';                break;            }            else            {                printf("你输入的坐标有误,请重新输入!\n");                //break;            }        }        else        {            printf("你输入的坐标有误,请重新输入!\n");            //break;        }    }}void ComputerMove(char board[ROWS][COLS], int row, int col){    while (1)    {        int x = rand() % 3;        int y = rand() % 3;        if (board[x][y] == ' ')        {            board[x][y] = '0';            break;        }    }}static int IsWin(char board[ROWS][COLS], 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;}char IsWiner(char board[ROWS][COLS], int row, int col){    int i = 0;    //横3排    for (i = 0; i < row; i++)    {        if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][0] != '0'))            return board[i][0];    }    //竖3排    for (i = 0; i < col; i++)    {        if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] != '0'))            return board[0][i];    }    //斜“\”   if((board[0][0]==board[1][1])&& (board[1][1] == board[2][2])&& (board[1][1] != ' '))        return board[1][1];   //斜“/”    if ((board[0][2] == board[1][1])&& (board[1][1] == board[2][0]) && (board[1][1] != ' '))        return board[1][1];    if (IsWin(board, row, col))    {        return 'q';    }    return ' ';}

图:

这里写图片描述

原创粉丝点击