Criss - cross game in MATLAB, using matrices.

2 visualizaciones (últimos 30 días)
Marino Weh
Marino Weh el 18 de Ag. de 2019
Comentada: Walter Roberson el 18 de Ag. de 2019
Hello everybody, first of all, I want to thank everyone in MATLAB community and apologise for potential grammatical mistakes. I am the student of the University of applied sciences in Bjelovar, Croatia. Not so long ago, I started using MATLAB. After I discovered this site, I was amazed with people's indepth knowledge, and overall complexity.
My problem is creating a criss - cross game using matrices. Basically, I need to create 3x3 matrix with zeros only. Then, first player enters number 1-9, this number represents one field, from top left corner to bottom right corner. When first player enters chosen number (chosen field), MATLAB needs to assing number 1 to that specific location in matrix. Then second player chooses number (field), only now MATLAB needs to assing number 2 to that specific location in matrix. After all iterations are ended, MATLAB needs to declare a winner if there are numbers 1 or 2 in a row.
This is program code I had written so far:
M1=[0 0 0;0 0 0;0 0 0];
prompt='First player. Enter field.';
prompt2='Second player.Enter field.';
for i = 1:4
a = input(prompt);
switch a
case 1
M1(1,1)=1;
case 2
M1(1,2)=1;
case 3
M1(1,3)=1;
case 4
M1(2,1)=1;
case 5
M1(2,2)=1;
case 6
M1(2,3)=1;
case 7
M1(3,1)=1;
case 8
M1(3,2)=1;
case 9
M1(3,3)=1;
end
a=input(prompt2);
switch a
case 1
M1(1,1)=2;
case 2
M1(1,2)=2;
case 3
M1(1,3)=2;
case 4
M1(2,1)=2;
case 5
M1(2,2)=2;
case 6
M1(2,3)=2;
case 7
M1(3,1)=2;
case 8
M1(3,2)=2;
case 9
M1(3,3)=2;
end
end
disp(M1);

Respuestas (0)

Categorías

Más información sobre Strategy & Logic en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by