游戏:井字棋

炸鸡写的第一个游戏:井字棋~

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_ROW 3;
#define MAX_COL 3;
char ches[3][3];
int menu() {
	while (1) {
		int choose;
		printf("******************\n");
		printf("***** 1.play *****\n");
		printf("***** 2.exit *****\n");
		printf("******************\n");
		printf("please enter your choose:");
		scanf("%d", &choose);
		if (choose == 2) {
			printf("goodbye!\n");
			system("pause");
			return  1;
		}
		else if (choose != 1) {
			printf("illegal input ! please enter again:\n");
		}
		else {
			break;
		}
	}
	return 0;
}
int printboard() {
	printf(" - - -\n");
	printf("|%c|%c|%c|\n", ches[0][0], ches[0][1], ches[0][2]);
	printf(" - - -\n");
	printf("|%c|%c|%c|\n", ches[1][0], ches[1][1], ches[1][2]);
	printf(" - - -\n");
	printf("|%c|%c|%c|\n", ches[2][0], ches[2][1], ches[2][2]);
	printf(" - - -\n");
	return 0;
}
int playermove() {
	int row, col;
	while (1) {
		getchar();/*清空输入缓存区*/
		printf("please enter the coordinate you want to place your chess,use ','to separate :\n ");
		scanf("%d,%d", &row, &col);
		if (row >3 || row < 0 || col>3 || col < 0) {
			printf("please enter the correct coordinate!\n");
			continue;
		}
		if (ches[row - 1][col - 1] == '\0') {
			ches[row - 1][col - 1] = 'X';
			printf("player place the chess in'%d,%d'\n", row, col);
			break;
		}
		else {
			printf("The coordinate you enter has been occupied !\n");
			continue;
		}
	}
	printboard();
	return 0;
}
int winner_checker() {

	if ((ches[0][0] == 'X'&&ches[0][1] == 'X'&&ches[0][2] == 'X') ||
		(ches[1][0] == 'X'&&ches[1][1] == 'X'&&ches[2][2] == 'X') ||
		(ches[2][0] == 'X'&&ches[2][1] == 'X'&&ches[2][2] == 'X') ||
		(ches[0][0]== 'X'&&ches[1][0]== 'X'&&ches[2][0] == 'X')||
		(ches[0][1] == 'X'&&ches[1][1] == 'X'&&ches[2][1] == 'X') ||
		( ches[0][2] == 'X'&&ches[1][2] == 'X'&&ches[2][2] == 'X')){
	return 0;//0 mean player win
	}
	else if ((ches[0][0] == 'X'&&ches[1][1] == 'X'&&ches[2][2] == 'X')||
				(ches[0][2] == 'X'&&ches[1][1] == 'X'&&ches[2][0] == 'X')) {
		return 0;
	}
	if ((ches[0][0] == 'O'&&ches[0][1] == 'O'&&ches[0][2]=='O') ||
		(ches[1][0] == 'O'&&ches[1][1] == 'O'&&ches[2][2]=='O') ||
		(ches[2][0] == 'O'&&ches[2][1] == 'O'&&ches[2][2]=='O') ||
		(ches[0][0] == 'O'&&ches[1][0] == 'O'&&ches[2][0] == 'O') ||
		(ches[0][1] == 'O'&&ches[1][1] == 'O'&&ches[2][1] == 'O') ||
		(ches[0][2] == 'O'&&ches[1][2] == 'O'&&ches[2][2] == 'O')){
		return 1;// 1 means computer win
	}
	else if ((ches[0][0] == 'O'&&ches[1][1] == 'O'&&ches[2][2] == 'O')||
				(ches[0][2] == 'O'&&ches[1][1] == 'O'&&ches[2][0] == 'O')){
		return 1;
	}
	return 2;
}
int computermove() {
	srand((unsigned int)time(0));
	while (1) {
		int col = rand() % 3 + 1;
		int row = rand() % 3 + 1;
		if (ches[row - 1][col - 1] == '\0') {
			ches[row - 1][col - 1] = 'O';
			printf("computer place the chess in'%d,%d'\n", row, col);
			break;
		}
		else {
			continue;
		}
	}
	printboard();
	return 0;
}
int full_check() {
	int row, col;
	for (row = 0; row < 3; ++row) {
		for (col =0; col < 3; ++col) {
			if (ches[row][col] == '\0') {
				return 0;
			}
		}
	}
	return 1;
}
int game() {
	int win = 100;
	int full = 2;
	printboard();
	while (1) {
		playermove();
		win = winner_checker();
		if (win == 0) {
			printf("player win\n");
			break;
		}
		if (full_check() == 1) {
			printf("draw!\n");
			break;
		}
		computermove();
		win=winner_checker();
		if (win == 1) {
			printf("computer win!\n");
			break;
		}
	}
	return 0;
}
int main() {
	if (menu() == 1) {
		return 0;
	}
	game();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值