Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help writing a MATLAB script of classdef which solves Sudoku. It must contain a handle qand have sub classes. Here's a function Sudoku but

Need help writing a MATLAB script of classdef which solves Sudoku. It must contain a handle qand have sub classes.

Here's a function Sudoku but need to convert it into a working class Sudoku.

function X = sudoku(X) % SUDOKU Solve Sudoku using recursive backtracking. % sudoku(X), expects a 9-by-9 array X. % Fill in all singletons. % C is a cell array of candidate vectors for each cell. % s is the first cell, if any, with one candidate. % e is the first cell, if any, with no candidates. [C,s,e] = candidates(X); while ~isempty(s) && isempty(e) X(s) = C{s}; [C,s,e] = candidates(X); end % Return for impossible puzzles. if ~isempty(e) return end % Recursive backtracking. if any(X(:) == 0) Y = X; z = find(X(:) == 0,1); % The first unfilled cell. for r = [C{z}] % Iterate over candidates. X = Y; X(z) = r; % Insert a tentative value. X = sudoku(X); % Recursive call. if all(X(:) > 0) % Found a solution. return end end end % ------------------------------ function [C,s,e] = candidates(X) C = cell(9,9); tri = @(k) 3*ceil(k/3-1) + (1:3); for j = 1:9 for i = 1:9 if X(i,j)==0 z = 1:9; z(nonzeros(X(i,:))) = 0; z(nonzeros(X(:,j))) = 0; z(nonzeros(X(tri(i),tri(j)))) = 0; C{i,j} = nonzeros(z); end end end L = cellfun(@length,C); % Number of candidates. s = find(X==0 & L==1,1); e = find(X==0 & L==0,1); end % candidates end % sudoku 

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Managerial Accounting

Authors: Stacey Whitecotton ,Robert Libby ,Fred Phillips

1st Edition

0071221212, 978-0071221214

Students also viewed these Databases questions