Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python代写:CSC108H Tic-Tac-Toe

Python代写:CSC108H Tic-Tac-Toe

原创
作者头像
拓端
发布于 2022-10-24 13:49:48
发布于 2022-10-24 13:49:48
91100
代码可运行
举报
文章被收录于专栏:拓端tecdat拓端tecdat
运行总次数:0
代码可运行

全文链接:tecdat.cn/?p=29592

Requirement

Tic-tac-toe is a two-player game that children often play to pass the time. The game is usually played using a 3-by-3 game board. Each player chooses a symbol to play with (usually an X or an O) and the goal is to be the first player to place 3 of their symbols in a straight line on the game board (either across a row on the board, down a column or along one of the two main diagonals).

In this Assignment, you are to complete some functions that make up part of a larger program for playing tic-tac-toe. In the program, game boards are not restricted to being 3-by-3, but are instead N-by-N, where N is one of the integers from 1 through 9, inclusive. Our game boards will always be square. When you have completed your functions for this Assignment, you will be able to play games of tic-tac-toe on your computer!

Analysis

Tic-tac-toe又称井字棋,通常是在3x3的棋盘上,双方轮流落子,先将3枚棋子连成一线的一方获胜。本题将游戏进行了拓展,变为NxN的棋盘,加大了难度。我们需要根据提供的框架实现游戏的逻辑部分,尤其是AI部分。 解题的关键需要理解游戏的规则,读懂整个框架,找到切入点,根据给定的测试集不断调试即可。

Tips

从测试集入手

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
>>> game_won('XXX-O-O--', 'X')True>>> game_won('XOXOXOOXO', 'X')False

从__main__开始一路调试,到

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def play_tictatoe():  ...  hava_a_winner = game_won(game_board, player_symbol)

进入函数后,增加处理逻辑,核心代码如下

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def game_won(game_board, symbol):  ...  for col in range(1, board_size + 1):    extract = tictactoe_functions.extract_line(game_board, 'dowm', col)      if extract == winning_string:        return True  for row in range(1, board_size + 1):    extract = tictactoe_functions.extract_line(game_board, 'across', row)      if extract == winning_string:        return True  ...  return False

.markdown-body pre,.markdown-body pre>code.hljs{color:#333;background:#f8f8f8}.hljs-comment,.hljs-quote{color:#998;font-style:italic}.hljs-keyword,.hljs-selector-tag,.hljs-subst{color:#333;font-weight:700}.hljs-literal,.hljs-number,.hljs-tag .hljs-attr,.hljs-template-variable,.hljs-variable{color:teal}.hljs-doctag,.hljs-string{color:#d14}.hljs-section,.hljs-selector-id,.hljs-title{color:#900;font-weight:700}.hljs-subst{font-weight:400}.hljs-class .hljs-title,.hljs-type{color:#458;font-weight:700}.hljs-attribute,.hljs-name,.hljs-tag{color:navy;font-weight:400}.hljs-link,.hljs-regexp{color:#009926}.hljs-bullet,.hljs-symbol{color:#990073}.hljs-built_in,.hljs-builtin-name{color:#0086b3}.hljs-meta{color:#999;font-weight:700}.hljs-deletion{background:#fdd}.hljs-addition{background:#dfd}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Python代码编写:CSC108H Tic-Tac-Toe
Tic-tac-toe is a two-player game that children often play to pass the time. The game is usually played using a 3-by-3 game board. Each player chooses a symbol to play with (usually an X or an O) and the goal is to be the first player to place 3 of their symbols in a straight line on the game board (either across a row on the board, down a column or along one of the two main diagonals).
拓端
2022/10/25
7680
Codeforces Beta Round #3 C. Tic-tac-toe
Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3grid (one player always draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced.
glm233
2020/09/28
5860
794. Valid Tic-Tac-Toe State
思路: 1. 由规则可知,”X”一定最先开始,所以当前局面存在”O”的个数大于”X”的个数为非法。 2. 其次,由于”X”和”O”轮流,因此,当前局面中”X”的个数要么和”O”相等,要么比”O”多一。 3. “O”在当前局面赢得比赛的情况下,上一轮的”X”一定不能赢得局面。 4. “O”在当前局面赢得比赛的情况下,上一轮的”X”没有赢得局面时,合法局面必须满足”O”的个数等于”X”的个数。 5. “X”在当前局面赢得比赛的情况下,意味着上一轮”O”没有赢得局面,合法局面下,”X”的个数正好比”O”的个数多一。
用户1147447
2019/05/26
8830
在家隔离,不忘学习-三连棋游戏 Tic-tac-toe
两人轮流在印有九格方盘上划“X”或“O”字, 谁先把三个同一记号排成横线、直线、斜线, 即是胜者)。 以下是这个游戏的一个案例:
Antony
2020/12/01
8560
在家隔离,不忘学习-三连棋游戏 Tic-tac-toe
【LeetCode第 165 场周赛】找出井字棋的获胜者
版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。
韩旭051
2019/12/03
6630
常见鸿蒙应用开发面试题
.markdown-body{word-break:break-word;line-height:1.75;font-weight:400;font-size:16px;overflow-x:hidden;color:#252933}.markdown-body h1,.markdown-body h2,.markdown-body h3,.markdown-body h4,.markdown-body h5,.markdown-body h6{line-height:1.5;margin-top:35px;margin-bottom:10px;padding-bottom:5px}.markdown-body h1{font-size:24px;line-height:38px;margin-bottom:5px}.markdown-body h2{font-size:22px;line-height:34px;padding-bottom:12px;border-bottom:1px solid #ececec}.markdown-body h3{font-size:20px;line-height:28px}.markdown-body h4{font-size:18px;line-height:26px}.markdown-body h5{font-size:17px;line-height:24px}.markdown-body h6{font-size:16px;line-height:24px}.markdown-body p{line-height:inherit;margin-top:22px;margin-bottom:22px}.markdown-body img{max-width:100%}.markdown-body hr{border:none;border-top:1px solid #ddd;margin-top:32px;margin-bottom:32px}.markdown-body code{word-break:break-word;border-radius:2px;overflow-x:auto;background-color:#fff5f5;color:#ff502c;font-size:.87em;padding:.065em .4em}.markdown-body code,.markdown-body pre{font-family:Menlo,Monaco,Consolas,Courier New,monospace}.markdown-body pre{overflow:auto;position:relative;line-height:1.75}.markdown-body pre>code{font-size:12px;padding:15px 12px;margin:0;word-break:normal;display:block;overflow-x:auto;color:#333;background:#f8f8f8}.markdown-body a{text-decoration:none;color:#0269c8;border-bottom:1px solid #d1e9ff}.markdown-body a:active,.markdown-body a:hover{color:#275b8c}.markdown-body table{display:inline-block!important;font-size:12px;width:auto;max-width:100%;overflow:auto;border:1px solid #f6f6f6}.markdown-body thead{background:#f6f6f6;color:#000;text-align:left}.markdown-body tr:nth-child(2n){background-color:#fcfcfc}.markdown-body td,.markdown-body th{padding:12px 7px;line-height:24px}.markdown-body td{min-width:120px}.markdown-body blockquote{color:#666;padding:1px 23px;margin:22px 0;border-left:4px solid #cbcbcb;background-color:#f8f8f8}.markdown-body blockquote:after{display:block;content:""}.markdown-body blockquote>p{margin:10px 0}.
万少
2025/03/27
2140
常见鸿蒙应用开发面试题
Python 进阶指南(编程轻松进阶):十五、面向对象编程和类
对于小程序来说,OOP 与其说是增加了组织,不如说是增加了官僚主义。虽然有些语言,比如 Java,要求你将所有代码组织成类,但是 Python 的 OOP 特性是可选的。程序员可以在需要时利用类,或者在不需要时忽略它们。Python 核心开发人员 Jack Diederich 在 PyCon 2012 的演讲“停止编写类”(youtu.be/o9pEzgHorH0)中指出,在许多情况下,程序员编写类时,更简单的函数或模块会工作得更好。
ApacheCN_飞龙
2023/04/09
9870
Python 进阶指南(编程轻松进阶):十五、面向对象编程和类
[译]Python 和 TOML:新最好的朋友 (2) 使用Python操作TOML
TOML 文档在 Python 中表示为字典。TOML 文件中的所有表和子表都显示为嵌套字典
一只大鸽子
2024/03/14
7290
[译]Python 和 TOML:新最好的朋友 (2) 使用Python操作TOML
python tictactoe游戏
import random, sys, time from tkinter import * from tkinter.messagebox import showinfo, askyesno from guimaker import GuiMakerWindowMenu
用户5760343
2022/05/13
1.6K0
python tictactoe游戏
用Vue实现井字棋
大家应该都玩过井字棋,棋手分为O、X,在一个3*3的格子中落子,只要能连成一条线,则代表获胜。
半月无霜
2025/01/17
1950
Golang Project: Tic Tac Toe
In this article, I’ll go through my process of writing a simple Tic-Tac-Toe game in Golang. Error handling, closures, iota as well as other golang features are used to create the game.
Miigon
2022/10/27
8290
实现一个 TicTacToe 游戏 —— 编程训练
这里我们给大家讲讲一个好玩的编程练习,很多同学想到编程练习就会觉得与算法有关。但是往往在编程的过程中,我们要实现某种逻辑或者是功能的时候,确实是需要用到算法。但是我觉得 Winter 老师说的也挺对的。
三钻
2020/11/09
1.6K0
实现一个 TicTacToe 游戏 —— 编程训练
为什么你需要编程assignment指导帮助?
计算机编程一直都不是一个简单的领域,即使是对于那些痴迷于计算机编程的同学,乃至大神们,也很难掌握所有的理论和概念。但是,教授、讲师们不可能延长课时,让同学们有充分时间去领悟其中的精髓,甚至是精通计算机编程。 那有什么办法可以让同学们熟练掌握计算机编程呢?7*24的图书馆学习吗?当然不是!俗话说的好,“火车跑得快,全靠车头带”,同学们需要的是一名专业的工程师,在前方带领大家。
拓端
2022/10/24
4580
边玩边学,30个Python小游戏(含源码)
经常听到有朋友说,学习编程是一件非常枯燥无味的事情。其实,大家有没有认真想过,可能是我们的学习方法不对?
小F
2023/08/21
8.5K4
边玩边学,30个Python小游戏(含源码)
自定义markdown代码高亮显示-cnblog
https://www.cnblogs.com/liutongqing/p/7745413.html
Noneplus
2019/09/24
8280
自定义markdown代码高亮显示-cnblog
[译]Python 和 TOML:新最好的朋友 (1) 了解TOML
TOML[2](Tom's Obvious Minimal Language)是一种相当新的配置文件格式。Python社区在过去几年中已经接受了它,许多流行的工具都使用TOML 进行配置,您将在构建和分发自己的包时可能就会使用 pyproject.toml 。
一只大鸽子
2024/03/14
8680
[译]Python 和 TOML:新最好的朋友 (1) 了解TOML
思源笔记—代码片段
捞月亮的小北
2024/02/17
4050
自定義代碼配色
在Hexo的根目錄source文件夾下創立一個self文件夾,在self文件夾創建一個Kimbiedark.css文件
零式的天空
2022/03/24
3830
自定義代碼配色
Butterfly 自定义代码高亮字体
但是该博客下提供的代码高亮css文件项目已经失效了,既然如此,我这里就再提供一篇较为完整的教程,仅供参考
一只野生彩色铅笔
2022/07/01
1.5K0
Python 小型项目大全 76~81
井字棋是一种在3 × 3网格上玩的经典纸笔游戏。玩家轮流放置 X 或 O 标记,试图连续获得三个。大多数井字棋都以平局告终,但如果你的对手不小心,你也有可能智胜他们。
ApacheCN_飞龙
2023/04/12
1.2K0
Python 小型项目大全 76~81
相关推荐
Python代码编写:CSC108H Tic-Tac-Toe
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验