Question: Need to create testing code using vitetest for following: Model Class ( Model . ts ) export class Model { board: string [ ] [

Need to create testing code using vitetest for following:
Model Class (Model.ts)
export class Model {
board: string[][];
initialBoard: string[][];
swaps: number;
score: number;
constructor(initialConfig: string[][]){
this.initialBoard = JSON.parse(JSON.stringify(initialConfig)); // Deep copy of initial config
this.board = JSON.parse(JSON.stringify(initialConfig));
this.swaps =0;
this.score =0;
}
getBoard(): string[][]{
return this.board;
}
getSwaps(): number {
return this.swaps;
}
getScore(): number {
return this.score;
}
reset(): void {
this.board = JSON.parse(JSON.stringify(this.initialBoard));
this.swaps =0;
this.score =0;
}
swapSyllables(row1: number, col1: number, row2: number, col2: number): void {
const temp = this.board[row1][col1];
this.board[row1][col1]= this.board[row2][col2];
this.board[row2][col2]= temp;
this.swaps++;
this.updateScore();
}
undoSwap(previousBoard: string[][], previousSwaps: number, previousScore: number): void {
this.board = previousBoard;
this.swaps = previousSwaps;
this.score = previousScore;
}
private updateScore(): void {
this.score =0; // Update this with your score logic
}
isSolved(): boolean {
// Compare board state with the correct solution
return false;
}
}
Puzzle Class(puzzle.ts)
export class Puzzle {
static configurations: string[][][]=[
[
["in", "vis", "i", "ble"],
["im", "mac", "u", "late"],
["af", "fil", "i", "ate"],
["un", "der", "wa", "ter"]
],
[
["ex","am","in", "ing"],
["re","in", "force", "ment"],
["in", "for", "ma", "tive"],
["ma","te","ri","al"]
],
[
["me", "chan", "i", "cal"],
["cal","cu", "lat", "ing"],
["im","me","di", "ate"],
["di","ag","on","al"]
]
];
}
Controller calss(controller.ts)
import { Model } from './model';
export class Controller {
model: Model;
previousBoards: string[][][]=[];
previousSwaps: number[]=[];
previousScores: number[]=[];
constructor(model: Model){
this.model = model;
}
getBoard(): string[][]{
return this.model.getBoard();
}
getSwaps(): number {
return this.model.getSwaps();
}
getScore(): number {
return this.model.getScore();
}
swapSyllables(row1: number, col1: number, row2: number, col2: number): void {
this.saveState(); // Save the current state before swap
this.model.swapSyllables(row1, col1, row2, col2);
}
reset(): void {
this.model.reset();
this.previousBoards =[];
this.previousSwaps =[];
this.previousScores =[];
}
undo(): void {
if (this.previousBoards.length >0){
const lastBoard = this.previousBoards.pop()!;
const lastSwaps = this.previousSwaps.pop()!;
const lastScore = this.previousScores.pop()!;
this.model.undoSwap(lastBoard, lastSwaps, lastScore);
}
}
saveState(): void {
const boardCopy = JSON.parse(JSON.stringify(this.model.getBoard()));
this.previousBoards.push(boardCopy);
this.previousSwaps.push(this.model.getSwaps());
this.previousScores.push(this.model.getScore());
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!