Need help writing a MATLAB script of classdef which generates a random Sudoku pu
ID: 3111272 • Letter: N
Question
Need help writing a MATLAB script of classdef which generates a random Sudoku puzzle. It must contain a handle and have sub classes. It should also have a GUI which plots the sudoku grid.
This is what I have so far:
classdef sudoku < handle
%SUDOKUSOLVER Summary of this class goes here
% Detailed explanation goes here
properties
candidates
player1
board
end
methods
function x = sudoku()
x.candidates = [];
x.player1 = [];
x.board = 1:9;
end
end
methods
function gamestat(x)
locations = [repmat(0.5:2.5,[1 3]); 2.5 2.5 2.5 1.5 1.5 1.5 0.5 0.5 0.5].';
numbers = locations(self.board,:);
axis([0 3 0 3])
axis off
hold on
plot([0 3],[1 1],'k','LineWidth',3)
plot([0 3],[2 2],'k','LineWidth',3)
plot([1 1],[0 3],'k','LineWidth',3)
plot([2 2],[0 3],'k','LineWidth',3)
end
function play(x)
while isempty(x.board)
x.gamestat
end
end
end
end
Must be similar to this tictactoe example of class:
classdef tictactoe < handle
%TICTACTOE Summary of this class goes here
% Detailed explanation goes here
properties
player1
player2
board
winner
end
methods
function self = tictactoe() % constructor
self.player1 = [];
self.player2 = [];
self.board = 1:9;
self.winner = [];
end
end
methods
function play(self)
while isempty(self.winner)
self.gamestate
if length(self.player1) == length(self.player2)
pick = input('Player 1: Pick an available space ');
self.player1 = [self.player1 pick]; % add pick to player list
else
pick = input('Player 2: Pick an available space ');
self.player2 = [self.player2 pick]; % add pick to player list
end
self.board(self.board == pick) = []; % remove pick from board
self.checkwin
end
end
function gamestate(self)
locations = [repmat(0.5:2.5,[1 3]); 2.5 2.5 2.5 1.5 1.5 1.5 0.5 0.5 0.5].';
{
exes = locations(self.player1,:);
ohs = locations(self.player2,:);
numbers = locations(self.board,:);
}
hold on
scatter(exes(:,1),exes(:,2),10000,'rx','LineWidth',5)
scatter(ohs(:,1),ohs(:,2),6000,'bo','LineWidth',4)
axis([0 3 0 3])
axis off
plot([0 3],[1 1],'k','LineWidth',3)
plot([0 3],[2 2],'k','LineWidth',3)
plot([1 1],[0 3],'k','LineWidth',3)
plot([2 2],[0 3],'k','LineWidth',3)
end
function checkwin(self)
wins = [1 2 3; 4 5 6; 7 8 9; 1 4 7; 2 5 8; 3 6 9; 1 5 9; 3 5 7];
for i = 1:length(wins)
if issame(intersect(self.player1,wins(i,:)),wins(i,:))
self.winner = 'Player 1';
elseif issame(intersect(self.player2,wins(i,:)),wins(i,:))
self.winner = 'Player 2';
end
end
if ~isempty(self.winner)
disp([self.winner ' is the winner!'])
self.gamestate
end
if isempty(self.board) && isempty(self.winner)
self.winner = 'Draw';
disp('The game is a draw!')
end
end
end
end
Explanation / Answer
classdef sudoku < handle
%SUDOKUSOLVER Summary of this class goes here
% Detailed explanation goes here
properties
candidates
player1
board
end
methods
function x = sudoku()
x.candidates = [];
x.player1 = [];
x.board = 1:9;
end
end
methods
function gamestat(x)
locations = [repmat(0.5:2.5,[1 3]); 2.5 2.5 2.5 1.5 1.5 1.5 0.5 0.5 0.5].';
numbers = locations(self.board,:);
axis([0 3 0 3])
axis off
hold on
plot([0 3],[1 1],'k','LineWidth',3)
plot([0 3],[2 2],'k','LineWidth',3)
plot([1 1],[0 3],'k','LineWidth',3)
plot([2 2],[0 3],'k','LineWidth',3)
end
function play(x)
while isempty(x.board)
x.gamestat
end
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.