Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在MATLAB W/Helper函数中确定Tic Tac Toe的动作和结果_String_Matlab_Helper_Tic Tac Toe_Cell Array - Fatal编程技术网

String 在MATLAB W/Helper函数中确定Tic Tac Toe的动作和结果

String 在MATLAB W/Helper函数中确定Tic Tac Toe的动作和结果,string,matlab,helper,tic-tac-toe,cell-array,String,Matlab,Helper,Tic Tac Toe,Cell Array,注:我正在更新这一点,因为我继续下去。我添加注释是为了让您知道我是否添加了代码。由于我没有评论,我怀疑有人会感到困惑。无论谁投票反对我,我都会尽可能多地提供信息。冷静点 这是一个相当长的标题,但它总结了我需要为这个问题所做的一切。我得到了一个3x3单元格形式的tic-tac趾板和一条告诉我是谁移动的字符串(Player X或Player O)。我需要拿出一个2xN单元格,列出可以采取的行动,以及行动的结果。例如,玩家的名字是玩家X,我的棋盘看起来像: { 'x' 'o' 'x'

注:我正在更新这一点,因为我继续下去。我添加注释是为了让您知道我是否添加了代码。由于我没有评论,我怀疑有人会感到困惑。无论谁投票反对我,我都会尽可能多地提供信息。冷静点

这是一个相当长的标题,但它总结了我需要为这个问题所做的一切。我得到了一个3x3单元格形式的tic-tac趾板和一条告诉我是谁移动的字符串(Player X或Player O)。我需要拿出一个2xN单元格,列出可以采取的行动,以及行动的结果。例如,玩家的名字是玩家X,我的棋盘看起来像:

       { 'x' 'o' 'x'
         'o' 'x' 'o'
         'o' ' ' ' ' }
那么我的选择是

    { 'x' 'o' 'x'              {'x' 'o' 'x'
      'o' 'x' 'o'              'o' 'x' 'o'
      'o' 'x' ' '}             'o' ' ' 'x'}
 'Your move, player O.'       'Player X Wins!' }
空白点总是用空格表示。现在针对这个问题,我得到了一个名为“MoveEvaluator”的助手函数,它如下所示:

 Helper Function Name: moveEvaluator
 Helper Function Inputs (2): - (char) A 3x3 character array representing
                                        a tic tac toe board, after a move
                                        has been made.
                               - (char) A single character of the the
                                        player that just moved.
   Helper Function Outputs (1): - (char) A string indicating the outcome
                                         of the turn.

   The helper function moveEvaluator takes in a 3x3 character array of a
   tic tac toe board after a move has been made, with spaces used to 
   represent the empty spaces on the board, and a single character of the 
   player who just went. This 2nd input will be a string of either 'x' or 
   'o'. This helper function will then output the outcome of that turn: if
   it was player X's turn, then the two possible outcomes are 'Player X
   Wins!' or 'Your turn, player O.' Similarly, if it was player O's turn,
   then the two possible outcomes are 'Player O Wins!' or 'Your turn,
   player X.'
这基本上完成了我一半的功能。多亏了Hoki,我的工作似乎正常了:)

我只需要它来填充任意一个插槽中的一个x,而不是两个插槽中的一个x。此外,我需要做的是找出如何从这里使用helper函数来输出可能的字符串选项

Testcases:
 board1 = { 'x' 'o' 'x'
                'o' 'x' 'o'
                  'o' ' ' ' ' }
move1 = 'x'
possibleMoves1 = ticTacToeTurn(board1,move1)
这应该是一个2x2单元,看起来像:

Cell (1,1) This is a cell inside a cell. All of the 'x' and 'o's are a separate cell
{'x''o' 'x'
'o' 'x' 'o'
'o' 'x' ' '}
Cell (2,1) looks is the string that says "Your move, Player O"

Cell (1,2) Should be 
{ 'x'   'o' 'x'
'o' 'x' 'o'
'o'     ' ' 'x'}
 Cell (2, 2) looks is the string that says "Player X wins!'

我试图查看evaluator函数的代码,但它显然是一个p文件,所以我不太确定该怎么做。如果我错过了什么,请告诉我。应该就这些了。

好的,看到你自己试了一下,我会为你指出最后的方向。这不是一个“功能”,因此它不是对您的作业的直接回答,但它包括问题的完整解决。您只需将其重新打包到函数中即可

%% // Initial conditions
Board = {'x' 'o' 'x'
         'o' 'x' 'o'
         'o' ' ' ' ' };
charBoard = reshape( char(Board) , 3 , 3 ) ;
PlayerSymbol = 'x' ;

%% // Resolution
% // Find the indices of possible move
Move_Choices = find(ismember(Board, ' '));

% // count them
nChoices = numel(Move_Choices) ;

% // pre-allocate output
outcome = cell(nChoices,1) ;

% // Get outcome for each possible postition
for iSlot = 1:nChoices
   PossibleBoard = charBoard ; % // copy the initial board
   PossibleBoard( Move_Choices(iSlot) ) = PlayerSymbol ; % // Add an 'x' at one of the empty position
   disp(PossibleBoard) ; % display the currently played board / optional, just for your information
   outcome(iSlot) = moveEvaluator( PossibleBoard , PlayerSymbol ) ;
end
一点建议:Matlab是一种解释语言。这种类型的编程语言有其优点和缺点,但它很好的一点是,您可以执行代码行并直接看到结果,而无需复杂的调试器


因此,您可以尝试许多不同的表达式并对其进行优化,直到输出符合您的要求。从您最初的代码组织方式来看,您似乎可以从中受益匪浅。首先尝试在基本工作区中一点一点地解决您的问题,直到您很好地掌握了问题及其解决方案。在这个阶段,将其放入函数中会容易得多(只需计算出输入/输出,但计算的核心已经解决)。

因为这是作业,所以我在回答时要小心一点,不要透露太多(你的老师也知道这一点)。但我猜你对所有可能的董事会都不感兴趣,只对下一步感兴趣。但是,您可能需要考虑使用一个或两个for循环(Matlab使用柱状索引),这意味着您可以通过键入板{ 5 }获得元素板{2,2},否则您需要2个循环。使用单元阵列或结构阵列存储电路板。然后你为你的棋盘中的每个
选择一个元素到容器中(每个元素都是一个有可能移动的棋盘)。@patrik我的老师甚至不检查作业lol,助教也不在乎。只要部分代码是我的,他们可能会粗心。话虽如此,我理解列索引。我将我的板转换为字符串,以使更改“”更容易。虽然我对for循环很糟糕,但我不知道如何在这里使用while循环。或者如何一次添加一个元素,而不是以连续的顺序添加元素(它只会填满所有的空格)。@patrik我觉得我现在的主要任务是使用helper函数。我最终可以玩弄for和while循环,并让电路板工作,但正是helper函数让我产生了一个真正的循环。请尝试
test=moveEvaluator(['xox';'oxo';'ox'],'x')
并查看结果,它应该给你一个关于如何使用helper函数的线索。但是我该如何将其应用到我的原始函数中呢?关于
MATLAB
作为一个非常易于使用和直观的软件平台的最后几句话非常贴切,必须对OP的学习过程有很大帮助+我试过了。我用了六种不同的方法。我只是不太擅长编程。我更喜欢生物学。对我来说更有意义。@Hoki我在最后一行中遇到了一个奇怪的错误。结果不起作用。函数抱怨“从字符到单元格的转换是不可能的”,但我不知道为什么?它们都是字符格式的。-。我甚至还没有构建我的最终输出。我对你的代码做了一点修改,但没什么太疯狂的。我把大部分都修好了。刚刚有一些细胞异常。只是开玩笑不,我没有。谢谢你的帮助:)@JessicaMarie,这最后一行是我唯一不能自己尝试的,因为我没有你的助手功能。这可能是一个单元分配问题。尝试将
output(iSlot)=
替换为
output{iSlot}=
%% // Initial conditions
Board = {'x' 'o' 'x'
         'o' 'x' 'o'
         'o' ' ' ' ' };
charBoard = reshape( char(Board) , 3 , 3 ) ;
PlayerSymbol = 'x' ;

%% // Resolution
% // Find the indices of possible move
Move_Choices = find(ismember(Board, ' '));

% // count them
nChoices = numel(Move_Choices) ;

% // pre-allocate output
outcome = cell(nChoices,1) ;

% // Get outcome for each possible postition
for iSlot = 1:nChoices
   PossibleBoard = charBoard ; % // copy the initial board
   PossibleBoard( Move_Choices(iSlot) ) = PlayerSymbol ; % // Add an 'x' at one of the empty position
   disp(PossibleBoard) ; % display the currently played board / optional, just for your information
   outcome(iSlot) = moveEvaluator( PossibleBoard , PlayerSymbol ) ;
end