前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python代写:CSC108H Tic-Tac-Toe

Python代写:CSC108H Tic-Tac-Toe

原创
作者头像
拓端
发布2022-10-24 21:49:48
7760
发布2022-10-24 21:49:48
举报
文章被收录于专栏:拓端tecdat拓端tecdat

全文链接: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
复制
>>> game_won('XXX-O-O--', 'X')True>>> game_won('XOXOXOOXO', 'X')False

从__main__开始一路调试,到

代码语言:javascript
复制
def play_tictatoe():  ...  hava_a_winner = game_won(game_board, player_symbol)

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

代码语言:javascript
复制
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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 全文链接:tecdat.cn/?p=29592
  • Requirement
  • Analysis
  • Tips
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档