3子琪实现

着急的先看完整代码;

‘tset.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu() {
	printf("*************************\n");
	printf("*  1:>玩游戏  0:>退出   *\n");
	printf("*************************\n");
}
void game() {
	char board[3][3] = { 0 };
	InitBoard(board,ROW,COL);
	//dayin
	DisplayBoard(board, ROW, COL);

	while (1) {
		printf("玩家走\n");
		player_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		if (judge(board, ROW, COL) != 1) {
			if (judge(board, ROW, COL) == 'p') {
				printf("平局\n");
				break;
			}
			else if (judge(board, ROW, COL) == '*') {
				printf("玩家赢\n");
				break;
			}
			else {
				printf("机器赢");
				break;
			}
		}
		printf("机器走\n");
		computer_mave(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		if (judge(board, ROW, COL) != 1) {
			if (judge(board, ROW, COL) == 'p') {
				printf("平局\n");
				break;
			}
			else if (judge(board, ROW, COL) == '*') {
				printf("玩家赢\n");
				break;
			}
			else {
				printf("机器赢");
				break;
			}
		}
	}
}

void test() {
	int input = 0;
	do {
		menu();
		printf("请输入1/0\n");
		scanf("%d", &input);
		switch (input) {
		case 1:
			game();
			printf("玩游戏\n");
			break;
		case 3:
			printf("退出\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}

	} while (input);
}
int main() {
	srand((unsigned int)time(NULL));
	test();
	return 0;
}

game.c

#include"game.h"

void InitBoard(char board[ROW][COL], int row, int col) {
	int i = 0;
	for (i = 0; i < row; i++) {
		int j = 0;
		for (j = 0; j < col; j++) {
			board[i][j] = ' ';
		}
	}
}
void DisplayBoard(char board[ROW][COL], int row, int col) {
	int i = 0;
	for (i = 0; i < row; i++) {
		int j = 0;
		for (j = 0; j < col; j++) {
			printf(" %c ",board[i][j]);
			if(j= 1 && x <= row && y >= 1&& y <= col) {
			//xia
			if (board[x - 1][y - 1] == ' ') {

				board[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("非法请重新输入");
		}
		else
			printf("输入错误");
	}
}
void computer_mave(char board[ROW][COL], int row, int col) {
	while (1) {
		int x = rand() % 3+1, y = rand() % 3+1;
			if (x > 0 && x <=row && y >0 && y <= col) {
				if (board[x-1][y-1] == ' ') {
					board[x-1][y-1] = '#';
					break;
				}
			}
	}
}

char judge(char board[ROW][COL], int row, int col) {
	while (1) {
		int i = 0;
		for (; i < row; i++) {
			if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
				if(board[i][0]!=' ')
				 return board[i][0];
			}
		}
		for (i = 0; i < col; i++) {
			if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
				if (board[0][i] != ' ')
				 return board[0][i];
			}
		}
		if (board[0][0] == board[1][1] && board[1][1] == board[2][2] || board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
			if (board[1][1] != ' ')
			   return board[1][1];
		}
		for (i = 0; i < row; i++) {
			int j = 0;
			for (j = 0; j < col; j++) {
				if (board[i][j] == ' ')
					return 1;
			}
		}
		return 'p';
	}
}

game.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#define ROW 3
#define COL 3
void InitBoard(char board[ROW][COL], int row, int col);


void DisplayBoard(char board[ROW][COL],int row,int col);

void player_move(char board[ROW][COL],int row, int col);
void computer_mave(char board[ ROW][ COL],int row,int col);

char judge(char board[ROW][COL], int row, int col);

先写游戏的框架

选1开始玩游戏,选0退出游戏

其他的选择错误;

void menu() {
	printf("*************************\n");
	printf("*  1:>玩游戏  0:>退出   *\n");
	printf("*************************\n");
}
void test() {
	int input = 0;
	do {
		menu();
		printf("请输入1/0\n");
		scanf("%d", &input);
		switch (input) {
		case 1:
			//game();
			printf("玩游戏\n");
			break;
		case 3:
			printf("退出\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}

	} while (input);
}
int main() {
	srand((unsigned int)time(NULL));
	test();
	return 0;
}

在game 函数实现游戏

void game() {
	char board[3][3] = { 0 };
}

先建一个3*3的数组

先初始化成空格

void InitBoard(char board[ROW][COL], int row, int col) {
	int i = 0;
	for (i = 0; i < row; i++) {
		int j = 0;
		for (j = 0; j < col; j++) {
			board[i][j] = ' ';
		}
	}
}

开始打印棋盘

void DisplayBoard(char board[ROW][COL], int row, int col) {
	int i = 0;
	for (i = 0; i < row; i++) {
		int j = 0;
		for (j = 0; j < col; j++) {
			printf(" %c ",board[i][j]);
			if(j

test.c 里的game

void game() {
	char board[3][3] = { 0 };
	InitBoard(board,ROW,COL);

	DisplayBoard(board, ROW, COL);
}

 写玩家代码

void player_move(char board[ROW][COL], int row, int col) {
	int x, y;
	while (1){
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1&& y <= col) {
			//xia
			if (board[x - 1][y - 1] == ' ') {

				board[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("非法请重新输入");
		}
		else
			printf("输入错误");
	}
}

 机器走的代码

void computer_mave(char board[ROW][COL], int row, int col) {
	while (1) {
		int x = rand() % 3+1, y = rand() % 3+1;
			if (x > 0 && x <=row && y >0 && y <= col) {
				if (board[x-1][y-1] == ' ') {
					board[x-1][y-1] = '#';
					break;
				}
			}
	}
}

test.c  里的game

void game() {
	char board[3][3] = { 0 };
	InitBoard(board,ROW,COL);

	DisplayBoard(board, ROW, COL);  
    while(1) {
        printf("玩家走\n");
		player_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);

		printf("机器走\n");
		computer_mave(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
     }

}

加个判断函数

玩家赢返回个*  机器赢返回个#  平局返回个  p   不是平局也没人应返回个1

char judge(char board[ROW][COL], int row, int col) {
	while (1) {
		int i = 0;
		for (; i < row; i++) {
			if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
				if(board[i][0]!=' ')
				 return board[i][0];
			}
		}
		for (i = 0; i < col; i++) {
			if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
				if (board[0][i] != ' ')
				 return board[0][i];
			}
		}
		if (board[0][0] == board[1][1] && board[1][1] == board[2][2] || board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
			if (board[1][1] != ' ')
			   return board[1][1];
		}
		for (i = 0; i < row; i++) {
			int j = 0;
			for (j = 0; j < col; j++) {
				if (board[i][j] == ' ')
					return 1;
			}
		}
		return 'p';
	}
}

test 里的game 

void game() {
	char board[3][3] = { 0 };
	InitBoard(board,ROW,COL);

	DisplayBoard(board, ROW, COL);

	while (1) {
		printf("玩家走\n");
		player_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		if (judge(board, ROW, COL) != 1) {
			if (judge(board, ROW, COL) == 'p') {
				printf("平局\n");
				break;
			}
			else if (judge(board, ROW, COL) == '*') {
				printf("玩家赢\n");
				break;
			}
			else {
				printf("机器赢");
				break;
			}
		}
		printf("机器走\n");
		computer_mave(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		if (judge(board, ROW, COL) != 1) {
			if (judge(board, ROW, COL) == 'p') {
				printf("平局\n");
				break;
			}
			else if (judge(board, ROW, COL) == '*') {
				printf("玩家赢\n");
				break;
			}
			else {
				printf("机器赢");
				break;
			}
		}
	}
}

你可能感兴趣的:(c语言,算法)