精华内容
下载资源
问答
  • 井字游戏下载
    2021-07-09 18:18:03

    此代码需要EasyX图形库
    EasyX下载地址

    界面绘制:

    void menu()				//画黑线
    {
    	int y = HIGH / 3;
    	int x = WIDE / 3;
    
    	setlinecolor(BLACK);
    	fillrectangle(0, y, WIDE, y + 1);
    	fillrectangle(0, y + y, WIDE, y + y - 1);
    	fillrectangle(x, 0, x + 1, HIGH);
    	fillrectangle(x + x, 0, x + x + 1, HIGH);
    }
    

    判断胜利条件:

    int judge()							//判断双方是否获胜
    {
    	int p = 0;
    	int t = 0;
    	int count = 0, count1 = 0;
    	int sum = 0, sum1 = 0;
    	for (int x = 0;x < 3;x++) {				//横
    		if (map[x][0] == 1) {
    			if (map[x][1] == 1) {
    				if (map[x][2] == 1) {
    					p = 1;
    				}
    			}
    		}
    		else if (map[x][0] == 2) {
    			if (map[x][1] == 2)
    				if (map[x][2] == 2)
    					t = 1;
    		}
    	}
    	for (int x = 0;x < 3;x++) {					//列
    		if (map[0][x] == 1) {
    			if (map[1][x] == 1)
    				if (map[2][x] == 1)
    					p = 1;
    		}
    		else if (map[0][x] == 2) {
    			if (map[1][x] == 2)
    				if (map[2][x] == 2)
    					t = 1;
    		}
    	}
    	for (int x = 0;x < 3;x++) {						//对角线
    		if (map[x][x] == 1) {
    			count++;
    		}
    		else if (map[x][x] == 2) {
    			sum++;
    		}
    	}
    	for (int x = 0, y = 2;x < 3;x++, y--) {						//对角线
    		if (map[x][y] == 1)
    			count1++;
    		if (map[x][y] == 2)
    			sum1++;
    	}
    	if (count == 3||count1==3)
    		p = 1;
    	else if (sum == 3||sum1==3)
    		t = 1;
    	if (p == 1)
    		MessageBox(0, _T("方块赢 按两下空格重来"), _T("hello"), 0);
    	else if (t == 1)
    		MessageBox(0, _T("圆赢 按两下空格重来"), _T("hello"), 0);
    	if (p == 1 || t == 1) return 1;
    	return 0;
    }
    

    根据鼠标点击绘制图案(写的很粗暴):

    void Draw(MOUSEMSG m,bool keydown)				//画方块
    {
    	int mx = WIDE / 3;
    	int my = HIGH / 3;
    	int interval = WIDE / 12;
    	if (key % 2 == 0) {
    		setfillcolor(GREEN);
    	}
    	else {
    		setfillcolor(BLUE);
    	}
    
    	if (keydown) {
    		if (m.x < mx) {									//右列
    			if (m.y < my) {						//左上角
    				if (key % 2 == 0 && a != 1) {
    					solidcircle(mx / 2, my / 2, 50);
    					map[0][0] = 2;
    					a = 1;
    					key++;
    				}
    				else if (a != 1) {
    					fillrectangle(interval, 10, interval + interval, 130);
    					map[0][0] = 1;
    					a = 1;
    					key++;
    				}
    			}
    			else if (m.y > my && m.y < my + my) {		//左中间
    				if (key % 2 == 0 && d != 1) {
    					solidcircle(mx / 2, HIGH / 2, 50);
    					map[1][0] = 2;
    					d = 1;
    					key++;
    				}
    				else if (d != 1) {
    					fillrectangle(interval, my + 10, interval + interval, my + 130);
    					map[1][0] = 1;
    					d = 1;
    					key++;
    				}
    			}
    			else if (m.y > my + my) {				//左下角
    				if (key % 2 == 0 && g != 1) {
    					solidcircle(mx / 2, my + my + my / 2, 50);
    					map[2][0] = 2;
    					g = 1;
    					key++;
    				}
    				else if (g != 1) {
    					fillrectangle(interval, my + 10 + my, interval + interval, my + my + 130);
    					map[2][0] = 1;
    					g = 1;
    					key++;
    				}
    			}
    		}
    		else if (m.x > mx && m.x < mx + mx) {					//中间
    			if (m.y < my) {						//中上角
    				if (key % 2 == 0 && b != 1) {
    					solidcircle(WIDE / 2, my / 2, 50);
    					map[0][1] = 2;
    					b = 1;
    					key++;
    				}
    				else if (b != 1) {
    					fillrectangle(interval+mx, 10, mx+interval + interval, 130);
    					map[0][1] = 1;
    					b = 1;
    					key++;
    				}
    			}
    			else if (m.y > my && m.y < my + my) {		//正中间
    				if (key % 2 == 0 && e != 1) {
    					solidcircle(WIDE / 2, HIGH / 2, 50);
    					map[1][1] = 2;
    					e = 1;
    					key++;
    				}
    				else if (e != 1) {
    					fillrectangle(mx+interval, my + 10, mx+interval + interval, my + 130);
    					map[1][1] = 1;
    					e = 1;
    					key++;
    				}
    			}
    			else if (m.y > my + my) {				//中下角
    				if (key % 2 == 0 && h != 1) {
    					solidcircle(WIDE / 2, my + my + my / 2, 50);
    					map[2][1] = 2;
    					h = 1;
    					key++;
    				}
    				else if (h != 1) {
    					fillrectangle(interval+mx, my + 10 + my, mx+interval + interval, my + my + 130);
    					map[2][1] = 1;
    					h = 1;
    					key++;
    				}
    			}
    		}
    		else if (m.x > mx + mx) {								//右边
    			if (m.y < my) {						//右上角
    				if (key % 2 == 0 && c != 1) {
    					solidcircle(mx / 2+mx+mx, my / 2, 50);
    					map[0][2] = 2;
    					c = 1;
    					key++;
    				}
    				else if (c != 1) {
    					fillrectangle(interval + mx+mx, 10, mx+mx + interval + interval, 130);
    					map[0][2] = 1;
    					c = 1;
    					key++;
    				}
    			}
    			else if (m.y > my && m.y < my + my) {		//右中间
    				if (key % 2 == 0 && f != 1) {
    					solidcircle(mx / 2 + mx + mx, HIGH / 2, 50);
    					map[1][2] = 2;
    					f = 1;
    					key++;
    				}
    				else if (f != 1) {
    					fillrectangle(mx + interval+mx, my + 10, mx+mx + interval + interval, my + 130);
    					map[1][2] = 1;
    					f = 1;
    					key++;
    				}
    			}
    			else if (m.y > my + my) {				//右下角
    				if (key % 2 == 0 && i != 1) {
    					solidcircle(mx / 2 + mx + mx, my + my + my / 2, 50);
    					map[2][2] = 2;
    					i = 1;
    					key++;
    				}
    				else if (i != 1) {
    					fillrectangle(interval + mx+mx, my + 10 + my, mx+mx + interval + interval, my + my + 130);
    					map[2][2] = 1;
    					i = 1;
    					key++;
    				}
    			}
    		}
    	}
    }
    

    最后的拼接:

    #include <stdio.h>
    #include <graphics.h>
    #include <conio.h>
    #include <windows.h>
    
    constexpr auto WIDE = 640;
    constexpr auto HIGH = 480;
    int key = 1;
    int a=0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0;			//重复按下条件
    int map[3][3];
    
    void menu();
    void Draw(MOUSEMSG m,bool keydown);
    int judge();
    
    int main()
    {
    	MOUSEMSG m;								//定义鼠标结构体
    	bool keydown = false;
    	int crux = 0;
    	initgraph(WIDE, HIGH);
    	setbkcolor(WHITE);
    	clearcliprgn();
    	menu();
    	for (int x = 0;x < 3;x++) {
    		for (int y = 0;y < 3;y++) {
    			map[x][y] = 0;
    		}
    	}
    	while (1) {
    		m = GetMouseMsg();						//获取(监听)鼠标消息
    		switch (m.uMsg) {
    		case WM_LBUTTONDOWN:
    			keydown = true;
    			break;
    		case WM_LBUTTONUP:
    			keydown = false;
    			break;
    		}
    		Draw(m, keydown);
    		crux = judge();
    		if (_kbhit()||crux==1) {				//按空格重新绘制
    			char word;
    			word = _getch();
    			if (word == ' '||crux==1) {
    				a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0;
    				clearcliprgn();
    				menu();
    				key = 1;
    				for (int x = 0;x < 3;x++) {
    					for (int y = 0;y < 3;y++) {
    						map[x][y] = 0;
    					}
    				}
    				crux = 0;
    			}
    		}
    	}
    	
    	system("pause");								//防止闪屏
    	return 0;
    }
    
    更多相关内容
  • 这是本人编的井字棋盘游戏,希望大家喜欢并下载
  • c#人机对战井字游戏源代码,可了解下棋盘的绘制、 电脑下棋的逻辑,判断输赢,修改所下棋子的颜色,修改该标签上的Owner的值为当前玩家;用到了C#自定义控件GameClass.dll,也包括有源码,生成GameClass.dll要拷贝到...
  • 井字游戏.zip易语言项目例子源码下载井字游戏.zip易语言项目例子源码下载 1.合个人学习技术做项目参考 2.适合学生做毕业设计参考 3.适合小团队开发项目参考
  • 这是一个拥有智能AI对手的井字棋小游戏,使用JavaScript语音进行开发,是一个前端小游戏,打开之后运行井字棋.html即可在网页上与AI进行井字游戏对战。也可以参考...
  • 井字游戏

    2021-02-23 10:57:28
    井字游戏面向对象的Javascrript的实践项目项目启动:02-21-2021索非亚·阿科斯塔(Sofia Acosta)和猫登顿(Cat Denton)项目描述Tic Tac Toe是两个玩家的游戏。眼镜Describe: Test: "It should return an array of ...
  • 颤振多平台井字游戏 井字游戏的多平台Flutter示例游戏。 谈话 我最近进行了演讲,在实时编码演示中构建了该应用程序,以展示Flutter,然后做了一些黑客操作,使其可在移动设备,台式机和Web上正常工作。 如果您对...
  • tic-tac-toe:井字游戏

    2021-05-23 16:52:15
    井字游戏或变形游戏 Tic-Tac-Toe是一款有趣的游戏,连续赢得3个即可获胜。 但是,该项目的主要部分是展示git-hub上的案例团队合作。 团队合作和沟通是任何项目中最伟大的创造者/破坏者之一。 使用以下方法开始小型...
  • 安装后,在chrome里点击右上角插件图标即可,玩多彩井字游戏,支持电脑对战和本地双人对战,电脑难度可选简单、中等、困难。 【插件网站】 http://playtcubed.com/games/tic-tac-toe 【插件更新】 2021-...
  • 井字游戏(ReactJS App) 一个简单的井字游戏,让您熟悉React。 我遵循了本教程: : 熟悉了React,这导致了常规的井字游戏。 然后,我继续使用该教程的知识来创建Ultimate Tic-Tac-Toe版本。 我CSS和HTML功能...
  • 这是用 Kotlin 编写的井字游戏概念示例。获取代码,运行它,然后对代码进行试验。玩得开心 效果展示: https://user-images.githubusercontent.com/19226563/40244373-5103fff4-5ae0-11e8-823e-b2f72667a2d3.png
  • 你可能听说过五子棋,但是你不一定知道井字游戏井字游戏是...本项目源码就是一套基于安卓的简单的基于安卓的井字游戏项目源码,本项目只有一个java文件,应该算是最简单的游戏了,想研究一下的朋友可以下载编译一下。
  • 井字游戏 基于终端的井字游戏,允许单人或两人游戏 将main.cpp文件下载到所需的目标位置 使您将main.cpp存储在当前目录中的任何文件夹 在终端运行 g ++ -std = c ++ 14 main.cpp -o tictactoe 其次是 ./tictactoe
  • 这是井字游戏的LabVIEW实现。 玩家可以是人类,也可以是AI。 AI有两个设置:哑和智能。 转储设置是随机放置令牌的,智能设置通过运行MiniMax算法来发挥最佳移动效果。 该可执行文件需要LabVIEW Run-Time Engine 2014...
  • 多个难度级别AI玩简单的经典游戏井字游戏 该项目包含构建井字游戏 AI 的完整教程,位于此处: 更多详情、使用方法,请下载后阅读README.md文件
  • 在一个实时编码演示中构建了这个应用程序来展示 Flutter,然后做了一些黑客操作以使其能够在移动设备、桌面和 Web 上运行 演示 您现在可以在浏览器中运行 Flutter ...更多详情、使用方法,请下载后阅读README.md文件
  • 使用git下载样板 $ git clone https://github.com/4GeeksAcademy/react-hello.git $ cd react-hello 并安装npm软件包: $ npm install 开始编码! 对于Windows,Mac,Linux或Gitpod,请通过实时重新启动来启动...
  • 井字游戏一个有趣的井字游戏(XO),可与您的朋友一起玩,是用c ++制作的。用法您可以在找到并下载执行的(.exe)代码。预览
  • Tic-Tac-Vue是一个简单的 JavaScript 单页应用程序 (SPA),使用Vue、Vuex和Vue Router编写。它的目标是成为想要学习 Vue 及其强大生态系统的初学者的“案例研究”。
  • 1 前言 在学习tkinter时打算开发一款井字游戏,之前试过利用pygame开发【利用pygame开发井字棋博客】。但是这个项目没有嵌入AI智能对战,只能由两个玩家操作。正好借此机会利用tkinter开发对战界面,并嵌入AI对战...

    1 前言

    在学习tkinter时打算开发一款井字棋游戏,之前试过利用pygame开发【利用pygame开发井字棋博客】。但是这个项目没有嵌入AI智能对战,只能由两个玩家操作。正好借此机会利用tkinter开发对战界面,并嵌入AI对战。
    游戏界面如下:
    在这里插入图片描述

    为了读者对游戏由更好的了解,这里放操作视频:

    20220813_105943

    为方便游玩,我利用pyinstaller打包了代码至exe文件可以让所有用户安装游玩!exe下载地址:软件下载
    若仅仅想获取代码,本文的代码就是全部代码,可放心使用!

    2 代码分模块介绍

    2.1 导入需要的库

    分别是tkinter 、random

    from tkinter import *
    import tkinter.messagebox as msq
    import random
    

    2.2 定义全局变量

    这里定义了窗体大小,窗体标题以及九个可落子位置的序号和变量值。

    def __init__(self, master=None):
            self.root = master  # 定义内部变量root
            self.root.title("井字棋")
            self.root.geometry('%dx%d' % (600, 400))  # 设置窗口大小
            self.panels = ["panel"]*10
            self.count = 0
            self.digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
            self.mark = ""
            self.btn_text1 = StringVar()
            self.btn_text2 = StringVar()
            self.btn_text3 = StringVar()
            self.btn_text4 = StringVar()
            self.btn_text5 = StringVar()
            self.btn_text6 = StringVar()
            self.btn_text7 = StringVar()
            self.btn_text8 = StringVar()
            self.btn_text9 = StringVar()
            self.computerLetter = "O"
            self.createPage()
    

    2.2 定义玩家类

    def read_name(self):
            return [0,"玩家"]
    
        def read_choose(self):
            return "X"
    

    2.3 定义页面类

    利用gird对页面进行合理的分布,下子的方框实则是button,利用command对button进行动态改变
    在这里插入图片描述

    def createPage(self):
            self.page = Frame(self.root)  # 创建Frame
            self.page.pack()
            Label(self.page, text="{0},您选择的棋子是{1},您为先手".format(self.read_name()[1], self.read_choose())).grid(row=0, stick=W, pady=10)
            button1 = Button(self.page, textvariable=self.btn_text1, width=10, font=('Times 16 bold'), height=3, command=lambda:self.update_btn_text(1))
            button1.grid(row=1, column=1)
            button2 = Button(self.page, textvariable=self.btn_text2, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(2))
            button2.grid(row=1, column=2)
    
            button3 = Button(self.page, textvariable=self.btn_text3, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(3))
            button3.grid(row=1, column=3)
            button4 = Button(self.page, textvariable=self.btn_text4, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(4))
            button4.grid(row=2, column=1)
    
            button5 = Button(self.page, textvariable=self.btn_text5, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(5))
            button5.grid(row=2, column=2)
            button6 = Button(self.page, textvariable=self.btn_text6, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(6))
            button6.grid(row=2, column=3)
    
            button7 = Button(self.page, textvariable=self.btn_text7, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(7))
            button7.grid(row=3, column=1)
            button8 = Button(self.page, textvariable=self.btn_text8, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(8))
            button8.grid(row=3, column=2)
    
            button9 = Button(self.page, textvariable=self.btn_text9, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(9))
            button9.grid(row=3, column=3)
    

    2.4 定义页面变化类以及玩家与AI轮流转换下子权限

    def update_btn_text(self, digit):
            playerLetter = self.read_choose()
            if playerLetter == "X":
                self.computerLetter == "O"
            else:
                self.computerLetter = "X"
            if digit == 1 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text1.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 2 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text2.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 3 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text3.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 4 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text4.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 5 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text5.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 6 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text6.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 7 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text7.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 8 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text8.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 9 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text9.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if (len(self.digits) == 1 and self.win(self.panels, 'X') == False and self.win(self.panels, 'O') == False):
                self.page.destroy()
                msq.showinfo("RESULT", "平局")
    

    2.5 定义判断胜负类

    def win(self, panels, sign):
            return ((panels[1] == panels[2] == panels[3] == sign)
                    or (panels[1] == panels[4] == panels[7] == sign)
                    or (panels[1] == panels[5] == panels[9] == sign)
                    or (panels[2] == panels[5] == panels[8] == sign)
                    or (panels[3] == panels[6] == panels[9] == sign)
                    or (panels[3] == panels[5] == panels[7] == sign)
                    or (panels[4] == panels[5] == panels[6] == sign)
                    or (panels[7] == panels[8] == panels[9] == sign))
    

    2.6 定义智能AI下子类

    def computer_move(self, panels, digits, computerLetter ,playerLetter):
            panelscopy = panels.copy()
            next_step1 = 0
            for i in digits:
                panelscopy[i] = computerLetter
                if (self.win(panelscopy, computerLetter)):
                    next_step1 = i
                    break
                panelscopy = panels.copy()
            panelscopy = panels.copy()
            for i in digits:
                panelscopy[i] = playerLetter
                if (self.win(panelscopy, playerLetter)):
                    next_step2 = i
                    break
                else:
                    next_step2 = random.choice(digits)
                panelscopy = panels.copy()
    
            if next_step1 == 0:
                next_step = next_step2
            elif next_step1 != 0:
                next_step = next_step1
            digits.remove(next_step)
            panels[next_step] = computerLetter
            if next_step == 1:
                self.btn_text1.set(computerLetter)
            elif next_step == 2:
                self.btn_text2.set(computerLetter)
            elif next_step == 3:
                self.btn_text3.set(computerLetter)
            elif next_step == 4:
                self.btn_text4.set(computerLetter)
            elif next_step == 5:
                self.btn_text5.set(computerLetter)
            elif next_step == 6:
                self.btn_text6.set(computerLetter)
            elif next_step == 7:
                self.btn_text7.set(computerLetter)
            elif next_step == 8:
                self.btn_text8.set(computerLetter)
            elif next_step == 9:
                self.btn_text9.set(computerLetter)
            if (self.win(panels, computerLetter)):
                self.page.destroy()
                msq.showinfo("RESULT", "AI获胜")
    

    3 整体代码展示(可直接使用!)

    # -*- coding: utf-8 -*-
    from tkinter import *
    import tkinter.messagebox as msq
    import random
    
    class fupage2_play(object):
        def __init__(self, master=None):
            self.root = master  # 定义内部变量root
            self.root.title("井字棋")
            self.root.geometry('%dx%d' % (600, 400))  # 设置窗口大小
            self.panels = ["panel"]*10
            self.count = 0
            self.digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
            self.mark = ""
            self.btn_text1 = StringVar()
            self.btn_text2 = StringVar()
            self.btn_text3 = StringVar()
            self.btn_text4 = StringVar()
            self.btn_text5 = StringVar()
            self.btn_text6 = StringVar()
            self.btn_text7 = StringVar()
            self.btn_text8 = StringVar()
            self.btn_text9 = StringVar()
            self.computerLetter = "O"
            self.createPage()
    
        def read_name(self):
            return [0,"玩家"]
    
        def read_choose(self):
            return "X"
    
    
        def createPage(self):
            self.page = Frame(self.root)  # 创建Frame
            self.page.pack()
            Label(self.page, text="{0},您选择的棋子是{1},您为先手".format(self.read_name()[1], self.read_choose())).grid(row=0, stick=W, pady=10)
            button1 = Button(self.page, textvariable=self.btn_text1, width=10, font=('Times 16 bold'), height=3, command=lambda:self.update_btn_text(1))
            button1.grid(row=1, column=1)
            button2 = Button(self.page, textvariable=self.btn_text2, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(2))
            button2.grid(row=1, column=2)
    
            button3 = Button(self.page, textvariable=self.btn_text3, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(3))
            button3.grid(row=1, column=3)
            button4 = Button(self.page, textvariable=self.btn_text4, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(4))
            button4.grid(row=2, column=1)
    
            button5 = Button(self.page, textvariable=self.btn_text5, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(5))
            button5.grid(row=2, column=2)
            button6 = Button(self.page, textvariable=self.btn_text6, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(6))
            button6.grid(row=2, column=3)
    
            button7 = Button(self.page, textvariable=self.btn_text7, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(7))
            button7.grid(row=3, column=1)
            button8 = Button(self.page, textvariable=self.btn_text8, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(8))
            button8.grid(row=3, column=2)
    
            button9 = Button(self.page, textvariable=self.btn_text9, width=10, height=3, font=('Times 16 bold'), command=lambda:self.update_btn_text(9))
            button9.grid(row=3, column=3)
    
        def update_btn_text(self, digit):
            playerLetter = self.read_choose()
            if playerLetter == "X":
                self.computerLetter == "O"
            else:
                self.computerLetter = "X"
            if digit == 1 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text1.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 2 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text2.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 3 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text3.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 4 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text4.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 5 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text5.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 6 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text6.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 7 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text7.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 8 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text8.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if digit == 9 and digit in self.digits:
                self.digits.remove(digit)
                self.panels[digit] = playerLetter
                self.btn_text9.set(playerLetter)
                if (self.win(self.panels, playerLetter)):
                    self.page.destroy()
                    msq.showinfo("RESULT", "{},你获胜!".format(self.read_name()[1]))
                else:
                    self.computer_move(self.panels, self.digits, self.computerLetter, playerLetter)
    
            if (len(self.digits) == 1 and self.win(self.panels, 'X') == False and self.win(self.panels, 'O') == False):
                self.page.destroy()
                msq.showinfo("RESULT", "平局")
    
    
        def win(self, panels, sign):
            return ((panels[1] == panels[2] == panels[3] == sign)
                    or (panels[1] == panels[4] == panels[7] == sign)
                    or (panels[1] == panels[5] == panels[9] == sign)
                    or (panels[2] == panels[5] == panels[8] == sign)
                    or (panels[3] == panels[6] == panels[9] == sign)
                    or (panels[3] == panels[5] == panels[7] == sign)
                    or (panels[4] == panels[5] == panels[6] == sign)
                    or (panels[7] == panels[8] == panels[9] == sign))
    
    
        def computer_move(self, panels, digits, computerLetter ,playerLetter):
            panelscopy = panels.copy()
            next_step1 = 0
            for i in digits:
                panelscopy[i] = computerLetter
                if (self.win(panelscopy, computerLetter)):
                    next_step1 = i
                    break
                panelscopy = panels.copy()
            panelscopy = panels.copy()
            for i in digits:
                panelscopy[i] = playerLetter
                if (self.win(panelscopy, playerLetter)):
                    next_step2 = i
                    break
                else:
                    next_step2 = random.choice(digits)
                panelscopy = panels.copy()
    
            if next_step1 == 0:
                next_step = next_step2
            elif next_step1 != 0:
                next_step = next_step1
            digits.remove(next_step)
            panels[next_step] = computerLetter
            if next_step == 1:
                self.btn_text1.set(computerLetter)
            elif next_step == 2:
                self.btn_text2.set(computerLetter)
            elif next_step == 3:
                self.btn_text3.set(computerLetter)
            elif next_step == 4:
                self.btn_text4.set(computerLetter)
            elif next_step == 5:
                self.btn_text5.set(computerLetter)
            elif next_step == 6:
                self.btn_text6.set(computerLetter)
            elif next_step == 7:
                self.btn_text7.set(computerLetter)
            elif next_step == 8:
                self.btn_text8.set(computerLetter)
            elif next_step == 9:
                self.btn_text9.set(computerLetter)
            if (self.win(panels, computerLetter)):
                self.page.destroy()
                msq.showinfo("RESULT", "AI获胜")
                
    if __name__ == "__main__":
        root = Tk()
        root.title('井字棋')
        fupage2_play(root)
        root.mainloop()
    
    

    最后展现界面:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    展开全文
  • 井字游戏 Carter Kerstetter创建的一款适用于Android手机的井字游戏。 该游戏具有容易,中等和困难的AI可供对抗。 要在Android手机上运行游戏,请执行以下操作: 下载位于TicTacToe / app / release中的app-...
  • TicTacToe:井字游戏

    2021-03-27 15:18:28
    井字游戏 井字游戏复制代码/下载文件,运行并享受游戏
  • 井字游戏-人工智能 概要 该项目旨在通过使用 minimax 和 alpha-beta 剪枝算法找到井字游戏的获胜者来展示人工智能技术。 代码示例 在代码上运行 java 编译器java TicTacToeAI OXX___O__ 示例输出 ` 初始板:OXX ___...
  • python小游戏井字

    2022-09-24 16:39:35
    用python编写的小游戏,需要用python运行,没有python的打开https://download.csdn.net/download/m0_63858034/85365688这个网址,下载我发布的python及组件安装。 Mini games written in python, need to run in ...
  • 井字游戏与javascript 您可以将游戏文件下载到文件夹game_tic_tac_toe中。 该代码将从index.html文件开始,然后转到start.html文件。 上面的文件只是一个例子。 index.html中的编码器 index.html是默认文件,它将...
  • vb井字棋源代码

    2021-05-07 09:31:20
    摘要:VB源码,游戏编程,井字棋 一款迷你井字棋小游戏源码,VB源代码下载
  • 井字棋小游戏

    2013-08-24 21:04:08
    附源代码,供c++初学者学习。建议下载dev-c。
  • 井字游戏 这是一个TicTacToeAI,它实现了Minimax算法以最大化其潜在结果并最小化玩家的结果。 安装 下载并解压缩zip,然后运行game.py。 python3 game.py 用法 Board layout : 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,551
精华内容 620
热门标签
关键字:

井字游戏下载