黑框框里的三子棋

game.h:

#ifndef __GAME_H__
#define __GAME_H__


#include<stdio.h>
#include<string.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>




#define ROWS 3
#define COLS 3


//srand((unsigned int)time(NULL));
//void init_board(char board[ROW][COL], int row, int col);
void init_board(char board[ROWS][COLS], int row, int col);
void display_board(char board[ROWS][COLS], int row, int col);
void player_move(char board[ROWS][COLS], int row, int col);
void computer_move(char board[ROWS][COLS], int row, int col);
static int is_full(char board[ROWS][COLS], int row, int col);
char check_win(char board[ROWS][COLS], int row, int col);






#endif//__GAME_H__


game.c:

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


void init_board(char board[ROWS][COLS], int row, int col)
{
memset(board, ' ', row*col*sizeof(char));
}
void display_board(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 player_move(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入:");
scanf("%d %d", &x, &y);
x--;
y--;
if ((x >= 0) && (x <= 2) && (y <= 2) && (y >= 0))
{
if (board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
else
{
printf("下标有误,请重新输入!");
}
}
else
{
printf("下标有误,请重新输入!");
}
}
}
void computer_move(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] = 'O';
break;
}
}
}
static int is_full(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;
}
}
}
char check_win(char board[ROWS][COLS], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][1] != ' '))
return board[i][1];
}
for (i = 0; i < col; i++)
{
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[1][i] != ' '))
return board[1][i];
}
if ((board[0][0] == board[1][1]) && (board[2][2] == board[1][1]) && (board[1][1]!= ' '))
{
return board[1][1];
}
if ((board[0][2] == board[1][1]) && (board[2][0] == board[1][1]) && (board[1][1]!= ' '))
{
return board[1][1];
}
if (is_full(board, row, col))
{
return 'q';
}
return ' ';
}


test.c:

#define _CRT_SECURE_NO_WARNINGS
//#include<stdio.h>
//#include<string.h>
//#include<time.h>
//#include<math.h>




#include "game.h"
char board[ROWS][COLS];
void game()
{
//char board[ROWS][COLS];
char ret = 'O';
init_board(board, ROWS, COLS);
srand((unsigned int)time(NULL));
display_board(board, ROWS, COLS);
//srand((unsigned int)time(NULL));


while (1)
{
player_move(board, ROWS, COLS);
if ((ret = check_win(board, ROWS, COLS)) != ' ')
break;
display_board(board, ROWS, COLS);
computer_move(board, ROWS, COLS);
if ((ret = check_win(board, ROWS, COLS)) != ' ')
break;
display_board(board, ROWS, COLS);
}
if (ret == 'X')
{
printf("玩家赢\n");
}
else if (ret == 'O')
{
printf("电脑赢\n");
}
else if (ret == 'q')
{
printf("平局\n");
}
else if(ret == 'p')
{
printf("不赢不输\n");
}
display_board(board, ROWS, COLS);
}
void menu()
{
printf("********************************************");
printf("\n");
printf("**********1表示开始游戏,2表示退出**********");
printf("\n");
printf("********************************************");
printf("\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请输入1或0:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误,请重新输入!");
break;
}
} while (input);
return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值