本帖最后由 victor666 于 2020-9-13 20:48 编辑

如题,一个作业要完成在周二之前, 写4个function补充这个游戏,主题程序已完成,function程序里都有注释(英文)可以百度翻译一下,运行效果要求如下

[B1 = newBoard(3, ' ');

playerP = input('Do you want to be: O or X ?', 's');

status = 0;

%Print empty board

printBoard(B1);

while (true)

fprintf('\n\n');

pmove = input('Enter Move: ', 's');

result = playHumanMove(B1, pmove, playerP);

if (result ~= -1)

%Player made a valid move so update board.

B1 = result;

else

%Player made an error so skip rest of loop and try again ...

continue;

end

%SHow players move

printBoard(B1);

% See if player's  move has changed status

status = checkVictory(B1, playerP);

if (status == 1)

fprintf('You have won the game !!!\n');

break;

elseif (status == 2)

fprintf('The computer has beaten you !!!\n');

break;

elseif (status == 3)

fprintf('It''s a Draw....\n');

break;

end

%Now let computer make its move...

B1 = computerMove(B1, playerP);

fprintf('Computers move:   \n\n');

printBoard(B1);

% See if computers  move has changed status

status = checkVictory(B1, playerP);

if (status == 1)

fprintf('You have won the game !!!\n');

break;

elseif (status == 2)

fprintf('The computer has beaten you !!!\n');

break;

elseif (status == 3)

fprintf('It''s a Draw....\n');

break;

end

end

][function R = newBoard(N, char)%   Creates a new NxN playing board.  A new board is an NxN matrix of ' '.

%Params: char - specifies the character to place into new board.  Normally,

%this would be a ' '.  But we can specify other parameters to make testing

%easier.

%This function is already fully implemented.

for i=1:N

for j = 1:N

R(i, j) = char;

end

end

end]

[function printBoard(B)

%This function prints out the board as shown in the specification.

%Params: B - a playing board which is an NxN martix of characters.  Valid

%characters are: ' ', 'X' and 'O';

N = length(B);

% Not implemented yet so we just dump B 'as is'. Note that since the board

% is blank initially, it won't be visible !!

disp(B);

% Heres a hint to get started ....

fprintf( ' %c  %c  %c \n', B(1,1), B(1, 2), B(1, 3)  );

]

[function R = playHumanMove(B, Move, Piece)

%Params:

%   B - Board depicting current state of game (NxN matrix of characters)

%   Move - Move by player. (2 character string)

%   Piece - which symbol player is using: 'X' or 'O'

%

%

%Return:

%   R - Updated Board taking into account player's move.

%

%

% This function applies the move specified by Move to the board B and then

% returns the resultant board in R. Note that B must be unaltered.

%

% Move is a two character string consisting of: 'M' where n is an integer

% specifying a square starting at the top-left (1) through to

% bottom-right(9).

%

% If the Move is valid, the board is updated with either an 'X' or an 'O'

% (as specified by Piece)  and returned in R.  If the move is invalid,

% an error message is printed  to the screen and -1 is returned.

%

% Some examples of valid moves: M1, M4, M9

%

% Some examples of invaluid moves: M01, M10, m1, h1, 5

%

%

%

N = length(B);

% Copy board to result.

R = B;

fprintf('playHumanMove not implemented yet!!\n');

]

[function R = computerMove(B, Piece)

%Params:

%   B - Board depicting current state of game (NxN matrix of characters)

%   Piece - which symbol player is using: 'X' or 'O'.  Computer will

%   obviously use the opposite symbol to Piece !!

%Return:

%   R - Updated Board taking into account computers's move.

%

% This function will apply the computers move.  A very basic strategy would

% be to scan for the first vacant square and place the computr's piece

% there.  You might be able to come up with a better strategy ...

N = length(B);

R = B;

if (Piece == 'X' )

Comp = 'O';

else

Comp = 'X';

end

for i = 1:3

for j = 1:3

if ( R(i, j)      ) % Check if this space is empty ( empty is a ' ')

R(i,j) = Comp;

%something here ....

end

end

end

]

[function R = checkVictory(B, Piece)

%Params:

%   B - board

%   Piece - the piece that human player is using: 'X' or 'O'

% Returns:

%   R -  0 No winners yet

%        1 Human has won

%        2 Computer has won

%        3 Draw.

% This function will scan and determine if a victory has occured.  There are four possibilities:

% 1. There are three consecutive squares with the players Piece - player (return 1)

% 2. There are three consecutive squares with the computers piece - computer wins. (return 2)

% 3. No one has won but all sqaure have a piece (board is full) - draw  (return 3)

% 4. If none of the above, then there is no change (return 0).

R=B;

if ( B(1,1) == B(1,2) && B(1,2) == B(1,3) && B(1,1) ~= ' '  )  % winner row 1

disp('Someone won the game in row 1!!');

%Workout who won using Piece

%if ( Piece       )

% then R = 1 (human won)

%else

% or R =2  (computer won)

%end

%return;

%elseif( .......)

end

% There are no winners because we're still here !!

% check for a draw  (no spaces left in board)

%    R=3

%

%  if we get to here there are no winners and NOT a draw so

R=0;  % game continues

]

players take turns putting their

pieces (a naught or a cross) in a blank square on a 3x3 grid. The first player to get three pieces in a

row is the winner. If the board becomes full before someone wins, it's a draw.

In this section, I'll provide a run through showing you how yourfinished gameshould look. Here is

the first round of a game:

>> nac

Do you want to be: O or X ?X

| |

---------

| |

---------

| |

Enter Move: M1

X | |

---------

| |

---------

| |

Computers move:

X | O |

---------

| |

---------

| |The game begins by asking the play whether they want to be Naughts 'O' or Crosses 'X'. In this

example, I selected 'X' so the computer will play with 'O'.

The game then asks the user to enter a move. A move is specified as a two characters: 'M' followed

by a number between 1 and 9 which specifies which square to place the piece. The square are

numbered starting with 1 at the top-left corner through to 9 at the bottom-right. In this example, my

first move was 'M1': the state of the board is then printed with my X placed in square 1. After the

players move, the computer makes its move and the board is printed again – in this cases, the

computer placed a 'O' in sqaure 2.

The cycle then continues until one of the players wins or its a draw. Following is the rest of the

example game:

Enter Move: M5

X | O |

---------

| X |

---------

| |

Computers move:

X | O | O

---------

| X |

---------

| |

Enter Move: M1

Invalid move - Square occupied!

Enter Move: m7

Invalid Move - Bad format!

Enter Move: M7

X | O | O

---------

| X |

---------

X | |Computers move:

X | O | O

---------

O | X |

---------

X | |

Enter Move: M9

X | O | O

---------

O | X |

---------

X | | X

You have won the game !!!

Notice that when the player entered invalid moves, no change was made to the board and they were

asked to re-enter the move.

matlab实验报告井字棋,有偿井字棋游戏300+相关推荐

  1. 控制工程matlab实验报告小结,控制工程MATLAB实验报告.doc

    控制工程MATLAB实验报告 浙江科技学院 机电系统开放性实验 设计题目: MATLAB控制机电系统实分析 专 业: 机 械 设 计 制 造及 其 自 动 化 班 级: 机 制 122 学生姓名: 王 ...

  2. MATLAB环境认识实验报告,matlab实验报告ljg

    matlab实验报告ljg 第 1 页 共 3 页电子信息工程学系实验报告课程名称: MATLAB 程序设计 实验项目名称: MATLAB 数值计算 实验时间:2011.4.26 班级:电信 081 ...

  3. 向量收敛在matlab中,matlab实验报告

    MATLAB 实验报告 1.在区间[-1,1]上分别取n=10.20用两组选中节点对龙格函数22511)(x x f += 作多项式插值及三次样条插值,对每个n 值,分别画出插值函数及f(x)的图形. ...

  4. matlab dsp实验报告,matlab实验报告14.pdf

    matlab实验报告14 Matlab实验报告 院系名称 :信息科学与工程学院 专业班级 :通信工程 1303 指导老师 :陈科文 ,支国明 ,张金焕 ,周扬 学生姓名 : 学号 : 目录 实验一 熟 ...

  5. gramer法则matlab,东南大学几何与代数matlab实验报告(大一专用).doc

    东南大学几何与代数matlab实验报告(大一专用).doc 数学实验报告学号:,姓名:吴雪松,得分:实验1求解线性方程组实验内容:用MATLAB求解如下线性方程组Ax=b,其中A=,b=[090093 ...

  6. matlab+nnf.m,中南大学-信号与系统matlab实验报告.doc

    中南大学-信号与系统matlab实验报告.doc 实验一 基本信号的生成 1 实验目的 学会 使用 MATLAB 产生各种常见的连续时间信号与离散时间信号: 通过 MATLAB 中的绘图工具对产生的信 ...

  7. matlab m文件的编写,Matlab实验报告(四)M文件的编写.doc

    Matlab实验报告(四)M文件的编写.doc 实验目的 学习MATLAB屮的关系运算和逻辑运算,掌握它们的表达形式和川法. 掌握MATLAB中的选择结构和循环结构. 学会用MATLAB进行M文件的编 ...

  8. matlab矩阵处理实验报告,matlab实验报告一二三

    <matlab实验报告一二三>由会员分享,可在线阅读,更多相关<matlab实验报告一二三(37页珍藏版)>请在金锄头文库上搜索. 1.2015 秋秋 2013 级级MATLA ...

  9. 信号与系统 matlab实验报告,信号与系统 MATLAB实验报告

    文档收集于互联网,已重新整理排版.word 版本可编辑,有帮助欢迎下载支持. 1文档来源为:从网络收集整理.word 版本可编辑. <信号与系统>MATLAB 实验报告 院系: 专业: 年 ...

  10. gramer法则matlab,线性代数-matlab实验报告.doc

    <线性代数-matlab实验报告.doc>由会员分享,提供在线免费全文阅读可下载,此文档格式为doc,更多相关<线性代数-matlab实验报告.doc>文档请在天天文库搜索. ...

最新文章

  1. 人工智能是这样理解真正的活过了
  2. 团队项目—每日记录2
  3. HashMap的put方法返回值问题
  4. [Manifest]关于version
  5. Android视频会议--彩蛋
  6. 如何在windows2008/2012上安装启明星系统。
  7. 动态贝叶斯网络DBN
  8. 百度网盘青春版未推出前,使用这个网盘高速下载工具
  9. 分享一个微信公众号管理平台源码
  10. 网传前端大神司徒正美突发病逝,再度思考健康与金钱
  11. windows10安装更新很慢ndows,win10系统更新后运行速度变慢的解决方法 - 系统家园...
  12. pcs9000系统plat服务器,CSD361現场调试手册.docx
  13. 什么是好用的身份证实名认证api接口?其应用场景有哪些?
  14. 二手华为p40手机现在多少钱
  15. 28_RTC实时时钟BKP备份寄存器
  16. ionic3 添加蒙版,弹出悬浮框
  17. KSO-sql server获取当月天数
  18. stm32在官网下载标准库
  19. bean是什么,bean和javabean
  20. 黑马程序员Java基础班+就业班课程笔记全发布(持续更新)

热门文章

  1. 日本某地(我猜应该是在米花町)发生了一件谋杀案,警察通过排查确定杀人凶手必为4个嫌疑犯的一个。
  2. DOM算法系列002-寻找指定DOM节点的上一个或下一个节点
  3. 手把手教你玩转android应用Microsoft Remote Desktop
  4. 车载应用--AUXIN 利用 surfaceView 预览 Camera 数据
  5. Unity GL函数库的简单使用
  6. word中插入的图片会覆盖文字
  7. 【解决方案】如何搭建运动场体育赛事直播方案:EasyCVR综合智能化体育赛事直播
  8. HTML 转 PDf 方法一 wkhtmltopdf.exe
  9. Windows 10 20H1 2004新功能
  10. 在线问诊小程序|互联网医院系统好处有哪些?