自己乱写的人机五子棋程序(电脑会乱下)

#define _CRT_SECURE_NO_WARNINGS
#define MAX_ROW 15
#define MAX_COL 15
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
//游戏介绍
//棋盘大小为15X15
//人和机器,谁的五颗棋子先连成一条线则为谁赢
//人的棋子为'X',电脑为'O' 人先下
void Menu() {
	printf("=======欢迎来到五子棋游戏!=======\n");
	printf("=======1.开始游戏================\n");
	printf("=======2.退出游戏================\n");
	printf("请输入您的选择:\n");
}

void Init(char chessboard[MAX_ROW][MAX_COL]){
	//对棋盘进行初始化
	memset(chessboard, ' ', MAX_ROW*MAX_COL);
}

//打印棋盘(第一次棋盘全为空格)
void Print(char chessboard[MAX_ROW][MAX_COL]) {
	printf("\n");
	int row,col;
	printf("  |0 1 2 3 4 5 6 7 8 9 A B C D E\n");
	printf("  ------------------------------\n");
	for (row = 0; row < MAX_ROW; row++) {
		if (row <= 9) {
			printf(" %d|", row);
		}
		else {
			printf("%d|", row);
		}
		for (col = 0; col < MAX_COL; col++) {
			printf("%c ", chessboard[row][col]);
		}
		printf("\n");
	}
}

//玩家落子
void PlayerMove(char chessboard[MAX_ROW][MAX_COL]) {
	int row, col;
	printf("请输入落子位子\n");
	while (1) {
		scanf("%d %d", &row, &col);
		//条件判断,如果这个位置为非法位置,则重新输入
		if (row<0 || row>=MAX_ROW || col<0 || col>=MAX_COL) {
			printf("您的输入有误请重新输入\n");
			continue;
		}
		//条件判断,这个位置为' ',则可以进行落子
		if (chessboard[row][col] != ' ') {
			printf("该位置已经有棋子了哦\n");
			continue;
		}
		chessboard[row][col] = 'X';
		break;
	}
}

//电脑落子
void ComputerMove(char chessboard[MAX_ROW][MAX_COL]) {
	int row;
	int col;
	while (1) {
		row = rand() % MAX_ROW;
		col = rand() % MAX_COL;
		if (chessboard[row][col] != ' ') {
			continue;
		}
		chessboard[row][col] = 'O';
		break;
	}
}

//判断是否获胜
//1.每一行上有连续五颗棋子
//2.每一列上有连续五颗棋子
//3.写到这里下周要考机械制造工艺学,还没开始复习,心里慌的一批
int Check(char chessboard[MAX_ROW][MAX_COL]) {
	int row;
	int col;
	for (row = 0; row < MAX_ROW; row++) {
		int count = 0;
		int comcut = 0;
		for (col = 0; col < MAX_COL; col++)
			if (row > 0) {
				if (chessboard[row][col] == 'X') {
					if (chessboard[row][col - 1] == 'X') {
						count += 1;
					}
					else {
						count = 0;
					}
				}
				if (chessboard[row][col] == 'O') {
					if (chessboard[row][col - 1] == 'O') {
						comcut+=1;
					}
					else {
						comcut = 0;
					}
				}
			}
		if (count == 4) {
			Print(chessboard);
			printf("你赢了 厉害\n");
			return count;
		}
		if (comcut == 4) {
			Print(chessboard);
			printf("你真菜,连电脑都干不过\n");
			return comcut;
		}
	}
	//判断每一列上是否有赢家
	for (col = 0; col < MAX_COL; col++) {
		int count = 0;
		int comcut = 0;
		for (row = 0; row < MAX_ROW; row++)
			if (col > 0) {
				if (chessboard[row][col] == 'X') {
					if (chessboard[row - 1][col] == 'X') {
						count += 1;
					}
					else {
						count = 0;
					}
				}
				if (chessboard[row][col] == 'O') {
					if (chessboard[row - 1][col] == 'O') {
						comcut += 1;
					}
					else {
						comcut= 0;
					}
				}
			}
		if (count == 4) {
			Print(chessboard);
			printf("恭喜你 你赢了\n");
			return count;
		}
		if (comcut == 4) {
			Print(chessboard);
			printf("你真菜,连电脑都干不过\n");
			return comcut;
		}
	}
	//判断右对角斜线上有没有连续(左下部分)
	for (int i = 0; i <= MAX_ROW - 5; i++) {
		int count=0;
		int comcut=0;
		for (row = i; row < MAX_ROW; row++) {
			if (row > 0) {
				if (chessboard[row][row - i] == 'X') {//画图推出来的联系 i为查找次数
					if (chessboard[row - 1][row - i - 1] == 'X') {
						count += 1;
					}
					else {
						count = 0;
					}
				}
				if (chessboard[row][row - i] == 'O') {//画图推出来的联系 i为查找次数
					if (chessboard[row - 1][row - i - 1] == 'O') {
						comcut += 1;
					}
					else {
						comcut= 0;
					}
				}
			}
		}
		if (count == 4) {
			Print(chessboard);
			printf("你赢了 陕科大棋王!\n");
			return count;
		}
		if (comcut == 4) {
			Print(chessboard);
			printf("你真菜,连电脑都干不过\n");
			return comcut;
		}
	}
	//右上部份
	for (int i = 0; i <= MAX_ROW - 5; i++) {
		int count=0;
		int comcut=0;
		for (col = i; col < MAX_ROW; col++) {
			if (col > 0) {
				if (chessboard[col - i][col] == 'X') {//画图推出来的联系 i为查找次数
					if (chessboard[col - i - 1][col - 1] == 'X') {
						count += 1;
					}
					else {
						count = 0;
					}
				}
				if (chessboard[col - i][col] == 'O') {//画图推出来的联系 i为查找次数
					if (chessboard[col - i - 1][col - 1] == 'O') {
						comcut += 1;
					}
					else {
						comcut= 0;
					}
				}
			}
		}
		if (count == 4) {
			Print(chessboard);
			printf("你赢了 陕科大棋王!\n");
			return count;
		}
		if (comcut == 4) {
			Print(chessboard);
			printf("你真菜,连电脑都干不过\n");
			return comcut;
		}
	}
	//左斜线上半部分
	for (int i = MAX_ROW - 1; i >= 4; i--) {
		int count=0;
		int comcut=0;
		for (row = i; row >= 0; row--) {
			if (chessboard[row][i - row] == 'X') {//画图推出来的联系 i为查找次数
				if (chessboard[row + 1][i - row - 1] == 'X') {
					count += 1;
				}
				else {
					count = 0;
				}
			}
			if (chessboard[row][i - row] == 'O') {//画图推出来的联系 i为查找次数
				if (chessboard[row + 1][i - row - 1] == 'O') {
					comcut += 1;
				}
				else {
					comcut= 0;
				}
			}
		}
		if (count == 4) {
			Print(chessboard);
			printf("你赢了 陕科大棋王!\n");
			return count;
		}
		if (comcut == 4) {
			Print(chessboard);
			printf("你真菜,连电脑都干不过\n");
			return comcut;
		}
	}
	//左斜线下半部分
	for (int i = 0; i <= MAX_ROW - 5; i++) {
		int count=0;
		int comcut=0;
		for (col = i; col < MAX_ROW; col++) {
			if (chessboard[MAX_ROW - col - 2][col] == 'X') {//画图推出来的联系 i为查找次数
				if (chessboard[MAX_ROW - col - 1][col - 1] == 'X') {
					count += 1;
				}
				else {
					count = 0;
				}
			}
			if (chessboard[MAX_ROW - col - 2][col] == 'O') {//画图推出来的联系 i为查找次数
				if (chessboard[MAX_ROW - col - 1][col - 1] == 'O') {
					comcut += 1;
				}
				else {
					comcut = 0;
				}
			}
		}
		if (count == 4) {
			Print(chessboard);
			printf("你赢了 陕科大棋王!\n");
			return count;
		}
		if (comcut == 4) {
			Print(chessboard);
			printf("你真菜,连电脑都干不过\n");
			return comcut;
		}
	}
	return 0;
}

void Game(char chessboard[MAX_ROW][MAX_COL]) {
	//游戏步骤;
	//1.创建两个空棋盘
	Init(chessboard);
	while (1) {
		//2.打印棋盘(首次为空棋盘)
		Print(chessboard);
		//3.玩家落子(提示玩家落子)
		PlayerMove(chessboard);
		//4.检查游戏是否结束
		if (Check(chessboard)) {
			break;
		}
		//5.电脑落子
		ComputerMove(chessboard);
		//6.检查游戏是否结束
		if (Check(chessboard)) {
			break;
		}
		//7.如果未结束返回2

	}
}

int main() {
	while (1) {
		Menu();
		srand((unsigned)time(NULL));
		int choice = 0;
		scanf("%d", &choice);
		if (choice == 1) {
			char cheesboard[MAX_ROW][MAX_COL];
			Game(cheesboard);
			break;
		}
		else if (choice == 0) {
			break;
		}
		else {
			printf("您的输入有误,请重新输入!\n");
		}
	}
	system("pause");
	return 0;
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的人机对战五子棋的Python代码,使用了基本的算法和GUI库Tkinter: ```python from tkinter import * import random # 初始化棋盘 def init_board(): global board, player, winner, end board = [] for i in range(15): row = [] for j in range(15): row.append(0) board.append(row) player = 1 winner = 0 end = False # 判断游戏是否结束 def check_win(): global board, player, winner, end for i in range(15): for j in range(11): if board[i][j] == player and board[i][j+1] == player and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player: winner = player end = True return for i in range(11): for j in range(15): if board[i][j] == player and board[i+1][j] == player and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player: winner = player end = True return for i in range(11): for j in range(11): if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == player: winner = player end = True return for i in range(11): for j in range(4, 15): if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == player: winner = player end = True return for i in range(15): for j in range(15): if board[i][j] == 0: return winner = 0 end = True # 画棋盘 def draw_board(): global board_canvas for i in range(15): board_canvas.create_line(30, 30+i*30, 450, 30+i*30) board_canvas.create_line(30+i*30, 30, 30+i*30, 450) # 画棋子 def draw_piece(x, y): global board_canvas, player if player == 1: color = 'black' else: color = 'white' board_canvas.create_oval(x*30-15, y*30-15, x*30+15, y*30+15, fill=color, outline=color) # 玩家落子 def player_move(event): global board, player, end if end: return x = (event.x-15)//30+1 y = (event.y-15)//30+1 if board[y-1][x-1] != 0: return board[y-1][x-1] = player draw_piece(x, y) check_win() if not end: player = 3 - player computer_move() # 电脑落子 def computer_move(): global board, player, end if end: return scores = [] for i in range(15): row = [] for j in range(15): if board[i][j] == 0: score = 0 for m in range(max(0, i-4), min(15, i+5)): for n in range(max(0, j-4), min(15, j+5)): if board[m][n] != 0: if board[m][n] == player: score += 10 else: score += 100 row.append(score) else: row.append(-1) scores.append(row) max_score = -1 max_positions = [] for i in range(15): for j in range(15): if scores[i][j] > max_score: max_score = scores[i][j] max_positions = [(i, j)] elif scores[i][j] == max_score: max_positions.append((i, j)) position = random.choice(max_positions) x = position[1] + 1 y = position[0] + 1 board[y-1][x-1] = player draw_piece(x, y) check_win() if not end: player = 3 - player # 主程序 def main(): global board_canvas root = Tk() root.title('五子棋') root.resizable(False, False) board_canvas = Canvas(root, width=480, height=480) board_canvas.pack() board_canvas.bind('<Button-1>', player_move) init_board() draw_board() root.mainloop() if __name__ == '__main__': main() ``` 运行代码后会弹出一个窗口,你可以通过鼠标点击棋盘上的空白位置进行落子。电脑会根据当前棋局情况计算分数,选择分数最高的位置落子。注意,这个算法非常简单,可能会有明显的漏洞,但足以体验五子棋人机对战的基本操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值