练习:3

c语言练习

三子棋

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define MAX_ROW 3
#define MAX_COL 3

int chess_board[MAX_ROW][MAX_COL];
void chessauto(){
	for (int i = 0; i < MAX_ROW; i++){
		for (int j = 0; j < MAX_COL; j++){
			chess_board[i][j] = ' ';
		}
	}
}
void print(){
	for (int i = 0; i < MAX_ROW; i++){
		printf("|%c|%c|%c|\n", chess_board[i][0], chess_board[i][1], chess_board[i][2]);
		if (i < MAX_ROW - 1){
			printf("|-|-|-|\n");
		}
	}
}
void playermove(){
	printf("玩家落子\n");
	int row = 0, col = 0;
	while (1){
		printf("输入坐标(例如3 3):\n");
		scanf("%d %d", &row, &col);
		if (row < 0 || row > 2 || col < 0 || col > 2){
			printf("坐标输入有误!\n");
			continue;
		}
		if (chess_board[row][col] != ' '){
			printf("坐标已经被占用了!\n");
			continue;
		}
		chess_board[row][col] = 'x';
		break;
	}
}
void compmove(){
	printf("计算机落子\n");
	srand((unsigned int)time(0));
	while (1){
		int row = rand() % 3;
		int col = rand() % 3;
		if (chess_board[row][col] != ' '){
			continue;
		}
		chess_board[row][col] = 'o';
		break;
	}
}
char check(){
	int row, col;
	for (int row = 0; row < 3; row++){
		if (chess_board[row][0] == chess_board[row][1] && chess_board[row][0] == chess_board[row][2]){
			return chess_board[row][0];
		}
	}
	for (int col = 0; col < 3; col++){
		if (chess_board[0][col] == chess_board[1][col] && chess_board[0][col] == chess_board[2][col]){
			return chess_board[0][col];
		}
	}
	if (chess_board[0][0] == chess_board[1][1] && chess_board[0][0] == chess_board[2][2]){
		return chess_board[0][0];
	}
	if (chess_board[0][2] == chess_board[1][1] && chess_board[0][2] == chess_board[2][0]){
		return chess_board[0][2];
	}
	if (isfull() == 1){
		return 'q';
	}
	return ' ';
}
int isfull(){
	int row, col;
	for (row = 0; row < MAX_ROW; row++){
		for (col = 0; col < MAX_COL; col++){
			if (chess_board[row][col] == ' '){
				break;
			}
		}
		if (col != 3){
			break;
		}
	}
	if (row == 3 && col == 3){
		return 1;
	}
}

void game(){
	char flag = ' ';
	chessauto();
	while (1){
		print();
		playermove();
		flag = check();
		if (flag != ' '){
			break;
		}
		compmove();
		flag = check();
		if (flag != ' '){
			break;
		}
	}
	print();
	if (flag == 'x'){
		printf("赢\n");
	}
	else if (flag == 'o'){
		printf("输\n");
	}
	else if (flag == 'q'){
		printf("和\n");
	}
	else
		printf("error!\n");
	Sleep(1000);
	system("cls");
}
int main(){
	int choice = 0;
	while (1)
	{
		printf("**************************************\n");
		printf("1.begin\n");
		printf("2.exit\n");
		printf("**************************************\n");
		scanf("%d", &choice);
		if (choice == 1){
			game();
			system("cls");
		}
		else if (choice == 2){
			break;
		}
		else{
			printf("error!!");
			continue;
		}
	}
	system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值