2014-10-17 7 views
0

참고 : 필자는이 작업을 진행하면서 최신으로 업데이트하고 있습니다. 코드에 추가했는지 여부를 알려주는 주석을 추가합니다. 아무런 코멘트가 없으므로 누구나 혼란 스러울 것입니다. 그리고 저에게 투표 한 사람 중 누구든지 가능한 한 많은 정보를 제공 할 것입니다. 진정하다.MATLAB 승/도우미 함수에서 Tic-Tac-Toe에 대한 이동 및 결과 결정

꽤 긴 제목이지만이 문제에 필요한 모든 것을 요약합니다. 나는 3x3 셀 형태의 틱택 (tic-tac-toe) 보드와 누구의 이동인지를 알려주는 문자열을 받았습니다 (플레이어 X 또는 플레이어 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!' } 

공백이 있습니다.

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.' 

그래서 기본적으로 나를 위해 반 제 기능을 수행합니다 지금은이 문제에 대해,이처럼 보이는 'moveEvaluater'라는 도우미 기능을 부여했습니다. 나는 호키이 작업 덕분에 갖고있는 것 같다 :)

function[Move_Choices] = ticTacToeTurn(Board, str) 

charBoard = reshape(char(Board) , 3 , 3) ; 
PlayerSymbol = str ; 

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

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

% // pre-allocate output 
outcome = cell(nChoices,1) ; 
Options = ''; 
Strings = ''; 
% // 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 
PossibleBoard = char(PossibleBoard); 
    outcome = moveEvaluator(PossibleBoard, str); 
    Strings = [Strings {outcome}]; 
end 
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 
PossibleBoard = cellstr(PossibleBoard); 
    Options = [Options, {PossibleBoard}]; 
end 
Move_Choices = [Options;Strings]; %// Here's my issue. This outputs a cell, but the cellstr function separated the x's and o's into cells of three. I need each spot to be their own cell :/ 
end 

나는 그냥 둘, 하나의 슬롯에 하나의 X를 입력해야합니다. 또한 여기에서 도우미 함수를 사용하여 가능한 문자열 옵션이 무엇인지 출력하는 방법을 알아야합니다.

Testcases: 
board1 = { 'x' 'o' 'x' 
       'o' 'x' 'o' 
        'o' ' ' ' ' } 
move1 = 'x' 
possibleMoves1 = ticTacToeTurn(board1,move1) 

이 제공해야하는 것 같습니다 × 2 셀 수 : 그래서 너무 확실하지 않다 나는 평가자 기능에 대한 코드를 살펴하려

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!' 

,하지만 분명히 AP 파일 무엇 할 것. 어떤 도움을 주시면 감사하겠습니다 :) 내가 무엇이든 놓친다면 알려주지. 모두가되어야합니다.

+2

숙제이기 때문에 나는 많은 것을 밝히지 않을 때 조심해야한다. (선생님도 알고있다.) 그러나 나는 당신이 가능한 모든 이사회에 관심이 없다고 생각하지만 다음 이사 만 할 것입니다. 그러나 하나 또는 두 개의 루프를 사용하는 것을 고려해야 할 수도 있습니다 (matlab에서는 열 단위 인덱스를 사용합니다. 즉, 보드 {5}를 입력하면 요소 보드 {2,2}를 얻을 수 있습니다. 그렇지 않으면 2 for 루프가 필요합니다). 보드를 저장하려면 셀 배열이나 구조체 배열을 사용하십시오.(모든 요소는 가능한 이동이 가능한 보드 임)에 대해 컨테이너에 요소를 추가합니다. – patrik

+0

@patrik 선생님은 숙제를 확인하지 않고 TA는 신경 쓰지 않습니다. 코드의 일부가 제 것이면 부주의 할 수 있습니다. 그 말로는 칼럼 색인 작성을 이해합니다. 내 보드를 문자열로 변환하여 쉽게 변경할 수있게했습니다. 나는 for 루프와 함께 끔찍하지만, while 루프를 사용하는 방법을 잘 모르겠습니다. 또는 한 번에 하나씩 요소를 추가하고 연속 된 순서로 요소를 추가하지 않는 방법 (모든 공간을 채우는 곳). –

+0

@patrik 지금 당장은 도우미 기능을 사용하고 있습니다. 결국 for 루프와 while 루프로 장난감을 가지고 보드를 작동시킬 수는 있지만 실제 루프를 위해 나를 던진 헬퍼 함수입니다. –

답변

2

좋아, 너 혼자 힘으로 보니 최종 방향을 알려줄거야. 이것은 "기능"이 아니므로 과제에 대한 직접적인 대답은 아니지만 문제의 완전한 해결 방법을 포함합니다. 당신은 당신의 기능에 그것을 다시 포장해야합니다.

%% // 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에는 언어를 해석이다. 이 유형의 프로그래밍 언어는 장점과 단점을 가지고 있지만, 복잡한 디버거를 사용하지 않고도 코드 행을 실행하고 결과를 직접 볼 수 있다는 점에서 매우 좋습니다.

그래서 다양한 결과를 얻을 수있을 때까지 다양한 표현을 시도해 볼 수 있습니다. 처음 코드가 어떻게 구성되었는지 보면서 많은 이점을 얻을 수있는 것처럼 보입니다. 문제와 해결책을 잘 이해할 때까지 기본 작업 공간에서 조금씩 문제를 조금씩 풀어보십시오. 이 단계에서는 함수에 넣는 것이 훨씬 쉬울 것입니다 (단지 입출력을 처리해야하지만 계산의 핵심은 이미 해결되었습니다).

+0

매우 사용하기 쉽고 직관적 인 소프트웨어 플랫폼 인 'MATLAB'에 대한 마지막 단어는 매우 적절하며 학습 과정에서 OP를 많이 도와야한다. +1에 그 – Divakar

+0

나는 그것을 시험해 보았다. 6 가지 방법으로 해냈습니다. 나는이 프로그래밍 전체를별로 잘하지 못합니다. 나는 생물학에 더 가깝다. 나 한테 더 이해가 돼. –

+0

@Hoki 마지막 줄에 이상한 오류가 나타납니다. 결과가 작동하지 않습니다. 이 함수는 "char에서 셀로 변환 할 수 없습니다."라는 오류가 발생합니다. 왜 그런지 모르겠다. 그들은 모두 챠콜 형태입니다 .-. 나는 나의 최종 산출물을 아직 만들지 않았다. 나는 당신의 코드를 조금 바꿨지만, 너무 미친 것은 아무것도 없다. –