三子棋、井字棋 -python版本代码-C语言版本代码

技术宅学长 2024-08-27 15:35:02 阅读 65

三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉棋、一条龙、井字棋等。游戏分为双方对战,双方依次在9宫格棋盘上摆放棋子,率先将自己的三个棋子走成一条线就视为胜利,而对方就算输了,但是三子棋在很多时候会出现和棋的局面。

8a552e3aa3a249f29da36578e001b2a1.png

废话不多说,直接上代码,好用请给个三连关注谢谢,我的B站是技术宅学长的个人空间-技术宅学长个人主页-哔哩哔哩视频 (bilibili.com)

<code>#作者:技术宅学长(CSDN、B站、抖音均为该ID)

#电赛交流1群:586475593

#电赛交流2群:878095340

#电赛交流3群:873779462

#电赛交流4群:208635284

#电赛交流5群:903429622

import random

# 定义棋盘大小和玩家符号

ROWS, COLS = 3, 3

PLAYER_X = 'X'#玩家

PLAYER_O = 'O'#电脑AI

EMPTY = ' '

# 函数:打印棋盘

def print_board(board):

for row in board:

print(" | ".join(row))

print("-" * 9)

# 函数:检查当前状态是否是结束状态(胜负或平局)

def check_game_over(board):

# 检查行和列

for i in range(ROWS):

if board[i][0] == board[i][1] == board[i][2] != EMPTY: # 检查行

return board[i][0]

if board[0][i] == board[1][i] == board[2][i] != EMPTY: # 检查列

return board[0][i]

# 检查对角线

if board[0][0] == board[1][1] == board[2][2] != EMPTY: # 主对角线

return board[0][0]

if board[0][2] == board[1][1] == board[2][0] != EMPTY: # 副对角线

return board[0][2]

# 检查是否平局

if all(board[i][j] != EMPTY for i in range(ROWS) for j in range(COLS)):

return "Draw"

# 游戏未结束

return None

# 函数:实现Minimax算法来找到最优的下棋位置

def minimax(board, depth, is_maximizing):

result = check_game_over(board)

if result == PLAYER_X:

return -1 # X赢了

elif result == PLAYER_O:

return 1 # O赢了

elif result == "Draw":

return 0 # 平局

if is_maximizing:

best_score = -float('inf')

for i in range(ROWS):

for j in range(COLS):

if board[i][j] == EMPTY:

board[i][j] = PLAYER_O

score = minimax(board, depth + 1, False)

board[i][j] = EMPTY

best_score = max(score, best_score)

return best_score

else:

best_score = float('inf')

for i in range(ROWS):

for j in range(COLS):

if board[i][j] == EMPTY:

board[i][j] = PLAYER_X

score = minimax(board, depth + 1, True)

board[i][j] = EMPTY

best_score = min(score, best_score)

return best_score

# 函数:让电脑选择最优的下棋位置

def computer_move(board):

best_move = None

best_score = -float('inf')

for i in range(ROWS):

for j in range(COLS):

if board[i][j] == EMPTY:

board[i][j] = PLAYER_O

score = minimax(board, 0, False)

board[i][j] = EMPTY

if score > best_score:

best_score = score

best_move = (i, j)

board[best_move[0]][best_move[1]] = PLAYER_O

# 主函数:控制游戏流程

def main():

board = [[EMPTY for _ in range(COLS)] for _ in range(ROWS)]

current_player = PLAYER_O#设置先行玩家PLAYER_X为人,PLAYER_O为AI

print("欢迎使用三子棋程序!")

print_board(board)

while True:

if current_player == PLAYER_X:

row = int(input("请输入你要下的行 (0, 1, 2) :"))

col = int(input("请输入你要下的列 (0, 1, 2) :"))

if board[row][col] == EMPTY:

board[row][col] = PLAYER_X

current_player = PLAYER_O

else:

print("移动错误,当前位置有棋,请重下")

continue

else:

print("AI走棋")

computer_move(board)

current_player = PLAYER_X

print_board(board)

game_result = check_game_over(board)

if game_result:

if game_result == "Draw":

print("平局!")

else:

if game_result == 'O':

print(f" AI获胜!")

else:

print(f" 恭喜你获胜!")

break

if __name__ == "__main__":

main()

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define ROWS 3

#define COLS 3

#define PLAYER_X 'X'

#define PLAYER_O 'O'

#define EMPTY ' '

// 打印棋盘

void print_board(char board[ROWS][COLS]) {

for (int i = 0; i < ROWS; ++i) {

printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);

if (i != ROWS - 1) {

printf("---|---|---\n");

}

}

}

// 检查游戏是否结束(胜负或平局)

char check_game_over(char board[ROWS][COLS]) {

// 检查行和列

for (int i = 0; i < ROWS; ++i) {

if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != EMPTY) {

return board[i][0];

}

if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != EMPTY) {

return board[0][i];

}

}

// 检查对角线

if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != EMPTY) {

return board[0][0];

}

if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != EMPTY) {

return board[0][2];

}

// 检查是否平局

bool is_draw = true;

for (int i = 0; i < ROWS; ++i) {

for (int j = 0; j < COLS; ++j) {

if (board[i][j] == EMPTY) {

is_draw = false;

break;

}

}

if (!is_draw) {

break;

}

}

if (is_draw) {

return 'D'; // 平局

}

return ' '; // 游戏未结束

}

// Minimax算法实现

int minimax(char board[ROWS][COLS], int depth, bool is_maximizing) {

char result = check_game_over(board);

if (result == PLAYER_X) {

return -1; // X赢了

} else if (result == PLAYER_O) {

return 1; // O赢了

} else if (result == 'D') {

return 0; // 平局

}

if (is_maximizing) {

int best_score = -1000; // 初始最小值

for (int i = 0; i < ROWS; ++i) {

for (int j = 0; j < COLS; ++j) {

if (board[i][j] == EMPTY) {

board[i][j] = PLAYER_O;

int score = minimax(board, depth + 1, false);

board[i][j] = EMPTY;

best_score = score > best_score ? score : best_score;

}

}

}

return best_score;

} else {

int best_score = 1000; // 初始最大值

for (int i = 0; i < ROWS; ++i) {

for (int j = 0; j < COLS; ++j) {

if (board[i][j] == EMPTY) {

board[i][j] = PLAYER_X;

int score = minimax(board, depth + 1, true);

board[i][j] = EMPTY;

best_score = score < best_score ? score : best_score;

}

}

}

return best_score;

}

}

// 计算并返回电脑的最优下棋位置

void computer_move(char board[ROWS][COLS]) {

int best_score = -1000;

int best_move_row = -1;

int best_move_col = -1;

for (int i = 0; i < ROWS; ++i) {

for (int j = 0; j < COLS; ++j) {

if (board[i][j] == EMPTY) {

board[i][j] = PLAYER_O;

int score = minimax(board, 0, false);

board[i][j] = EMPTY;

if (score > best_score) {

best_score = score;

best_move_row = i;

best_move_col = j;

}

}

}

}

board[best_move_row][best_move_col] = PLAYER_O;

}

// 主函数:控制游戏流程

int main() {

char board[ROWS][COLS];

char current_player = PLAYER_X;//先落子玩家,PLAYER_X为玩家,PLAYER_O为电脑

// 初始化棋盘

for (int i = 0; i < ROWS; ++i) {

for (int j = 0; j < COLS; ++j) {

board[i][j] = EMPTY;

}

}

// 游戏循环

while (true) {

// 打印当前棋盘状态

printf("\nCurrent board:\n");

print_board(board);

// 检查游戏是否结束

char game_result = check_game_over(board);

if (game_result != ' ') {

if (game_result == 'D') {

printf("It's a draw!\n");

} else {

printf("%c wins!\n", game_result);

}

break;

}

// 根据当前玩家决定下棋

if (current_player == PLAYER_X) {

int row, col;

printf("\nEnter row number (0, 1, 2) for your move: ");

scanf("%d", &row);

printf("Enter column number (0, 1, 2) for your move: ");

scanf("%d", &col);

if (board[row][col] == EMPTY) {

board[row][col] = PLAYER_X;

current_player = PLAYER_O;

} else {

printf("Invalid move! That position is already taken. Try again.\n");

continue;

}

} else {

printf("\nComputer's turn:\n");

computer_move(board);

current_player = PLAYER_X;

}

}

return 0;

}

 

 

 



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。