本帖最后由 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.