Noughts and Crosses Grid Update!

I have a 3x3 grid image which I have assigned to a 0 0 0; 0 0 0; 0 0 0 matrix so each 0 represents a square.
I have created a script for crosses whereby when the player clicks a box on the grid the number in the matrix is updated to 1, likewise for noughts it is updated to -1.
My problem is that whilst the matrix is saving the updates during the game, the grid image itself is not updating. For example, I might put a cross in the middle square but when it comes to run the nought script the cross is removed from the grid image therefore it is impossible visually to know where the previous players counter is despite being reflected in the matrix associated with it.
I was just wondering how to save the updates to the grid image so a game of noughts and crosses can actually be recorded and played?
Thanks in advance!!! :) I have attached the script I am running.
%SCRIPT FOR NOUGHTS.
grid = imshow('g3x3.bmp')
[x,y]=ginput(1);
if (x<150)&(y<=150)
hold on
image([0 150],[0 150], so)
mapgrid(1,1)= -3
elseif (x>150)&&(x<300)&&(y<=150)
hold on
image ([151 300],[0 150],so)
mapgrid(1,2)= -3
elseif (x>300)&&(x<=450)&&(y<=150)
hold on
image ([301 450], [0 150],so)
mapgrid(1,3)= -3
elseif (x<=150)&&(y>150)&&(y<=300)
hold on
image ([0 150],[151 300],so)
mapgrid(2,1)= -3
elseif (x>150)&&(x<=300)&&(y>150)&&(y<=300)
hold on
image ([151 300],[151 300],so)
mapgrid(2,2)= -3
elseif (x>300)&&(x<=450)&&(y>150)&&(y<=300)
hold on
image([301 450],[151 300],so)
mapgrid(2,3)= -3
elseif (x<=150)&&(y>300)
hold on
image([0 150],[301 450],so)
mapgrid(3,1)= -3
elseif (x>150)&&(x<300)&&(y>300)
hold on
image([151 300],[301 450],so)
mapgrid(3,2)= -3
elseif (x>300)&&(y>300)
hold on
image ([301 450],[301 450],so)
mapgrid(3,3)= -3
hold off
end

Réponses (1)

Walter Roberson
Walter Roberson le 17 Jan 2016

0 votes

Your script starts by
grid = imshow('g3x3.bmp')
This is going to display the g3x3 image, removing all other graphics from the current axes. You do not want to do that if you are going to be running your script multiple times (once for each move.) You should be coding a loop inside your script, executed once for each move, done after you first show the image.
Later in your code you have lines similar to
image([0 150],[0 150], so)
This is a problem because your script does not define the variable "so"
After you display this image, you have a line similar to
mapgrid(2,3)= -3
but you do not appear to have initialized mapgrid anywhere. If you are running your script over and over again then the variable will accumulate the changes you make to it. But shouldn't you be checking the values that are already there before you agree that the user can put a new value there?
You do not have any code to check to see if anyone has won yet. You will find it a lot easier to implement that code if the mapgrid variable has been initialized. At the moment you have it growing as you write values further to the right or further down than what have been stored before, which is a problem if you want to check the value of a location that hasn't been grown into yet...

Catégories

En savoir plus sur Board games dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by