I wish to make a Interactive tic tac toe game for a project.
    10 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
I wish to make it so that player 2 can be a direct input from a person rather than a random row and column how do I do this?
% first draft of the tictactoe player for BME60B 2021 summer
% Initial template is taken from : https://www.mathworks.com/matlabcentral/answers/313999-help-with-tictactoe-code
%
wincounter = [0,0,0];
historyofgamestates = {};
for game = 1:10                                     % you can change how many games you play on this line
    gamestate= initializegamestate();
    historyofgamestate = gamestate;
    loopcounter = 2;
    while gamestate.finish == false
        if gamestate.turn ==1
            move = player1(gamestate);  
        elseif gamestate.turn == -1
            move = player2(gamestate);
        else
            print('error')
        end
        gamestate = makeamove(gamestate,move);
        drawmove(gamestate,move)                            % if you don't want to see every move being played, then comment out this line
        historyofgamestate(loopcounter) = gamestate; 
        loopcounter = loopcounter +1;                                           %increment counter
        drawnow
        pause(1)                                            % comment out this line if you don't want the script to pause after every move                                                                %wait 1 second before going
        if loopcounter == 11
            break
        end
    end
    if gamestate.finish == false
        title('Game is a tie')  
        wincounter= wincounter+[0,1,0];
    elseif gamestate.turn ==1 
        title('Winner is player 2') 
        wincounter= wincounter+[0,0,1];
    elseif gamestate.turn ==-1
        title('Winner is player 1')  
        wincounter= wincounter+[1,0,0];
    else
        print('error')
    end
    historyofgamestates{game} = historyofgamestate;
    pause(1)                                            % you can comment out this line if you don't want the script to pause after every game
end
save('game1.mat','historyofgamestate')                                      %saves the game so we can look at it later
%% subfunctions 
% I decided to include everything in one file for easier download/sharing
% but sacrificing modularity and readability. 
function drawmove(gamestate,move)
    % check that there is only 1 move 
    [row,col] = find(move);
    if length(row)  ~= 1  && length(col)  ~= 1
        print('move error')
    end
    figure(1)
    hold on
    if gamestate.turn == 1
        rectangle('Position',[row-0.8,-(col-0.2),0.6,0.6],'Curvature',[1 1])
    elseif gamestate.turn == -1
        plot([row-0.8 row-0.2],[-(col-0.2) -(col-0.8)],'k')
        plot([row-0.8 row-0.2],[-(col-0.8) -(col-0.2)],'k')
    end
    hold off
end
function gamestate = initializegamestate
    gamestate.board = zeros(3);
    gamestate.turn = 1;
	gamestate.finish = false;
    figure(1)
    plot([0 3],-[1 1], 'k','linewidth',2);
    hold on
    plot([0 3],-[2 2], 'k','linewidth',2)
    plot([1 1],-[0 3], 'k','linewidth',2)
    plot([2 2],-[0 3], 'k','linewidth',2)
    hold off
    axis off
end
function gamestate = makeamove(gamestate,move)
    % check that there is only 1 move 
    [row,col] = find(move);
    if length(row)  ~= 1  && length(col)  ~= 1
        print('move error')
    end
    %check if the move is legal
    if  gamestate.board(row,col) ~=0
        print('move error 2')
    end
    gamestate.board = gamestate.board + move*gamestate.turn;
    gamestate.turn = gamestate.turn*-1;
    %check for winner
    checkfor = [sum(gamestate.board,1), sum(gamestate.board,2)',gamestate.board(1,1)+gamestate.board(2,2)+gamestate.board(3,3),gamestate.board(1,3)+gamestate.board(2,2)+gamestate.board(3,1)] ;
    if  any( abs(checkfor) == 3)
        gamestate.finish = true;
    end
end
function move = player1(gamestate)
    legal = false;
    while legal == false
        row= randi([1,3]);
        col= randi([1,3]);
        if gamestate.board(row,col) == 0
            legal = true;
        end
    end
    move = zeros(3);
    move(row,col) = 1;
end
function move = player2(gamestate)
    legal = false;
    while legal == false
        position = ginput 
        if gamestate.board(row,col) == 0
            legal = true;
        end
    end
    move = zeros(3);
    move(row,col) = 1;
end
1 commentaire
  Rik
      
      
 le 7 Juil 2021
				You forgot to format your code as code. You also didn't ask a specific question. What exactly is your issue? Player 1 has some random calls, player 2 doesn't. How did you try to adapt the code?
Réponses (1)
  Pranesh
 le 6 Juil 2022
        To my understanding you need an input from the user for the row and col variables for player2 instead of using randi function to generate random number between 1 and 3.
A possible work around could be using input() function similar to what is given below 
function move = player2(gamestate) 
legal = false; 
while legal == false 
    Col=input ("enter col between 1 and 3"); 
    Row=input ("enter row between 1 and 3"); 
    if gamestate.board(row,col) == 0 
        legal = true; 
    end 
end 
move = zeros(3); 
move(row,col) = 1; 
End 
0 commentaires
Voir également
Catégories
				En savoir plus sur Strategy & Logic dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


