CS 52 Assignment 10 Solved

25.00 $ 12.50 $

Category:

Description

Project 1 – Pointers Continued (Sudoku)

The purpose of this assignment is to practice dealing with string data. The goal is to work with data declared as char *(C-string) and also as string. I think you’ll like working with strings much better. Please recall that string is officially known as std::string.

Sudoku Row Checker

Sudoku is a logic-based number placement puzzle. The objective is to fill a 9×9 grid so that each column, each row, and each of the nine 3×3 boxes (also called blocks or regions) contains the digits from 1 to 9 only one time each. For those of you not familiar with this game, you can learn more about it here: https://en.wikipedia.org/wiki/Sudoku

The Sudoku Row Checker class is can be used to validate a string of information. Using string operations, it should verify that each number 1 to 9 is used only one time. Following the class specifications shown below, implement the Sudoku Row Checker class. Implement a suitable testing suite asides from the ones already provided that proves your calculations are correct. You may choose to create any number of additional methods, but you are required to implement all of the public methods shown below.

SudokuRowChecker

public:
  SudokuRowChecker( );
  // same function name, but with different arguments. You must implement both versions of the setData() function.
  void setData( const char * s );
  void setData( std::string s );
  void clear();
  bool isValid( ) const;

private:
  string sudokuRow;

Sample Testing Driver Code

    // test 1, invalid string
    SudokuRowChecker c1;
    std::string s1 = "12345678901";
    c1.setData(s1);
    if(c1.isValid()){
        std::cout << "1.1. Invalid string, 11 characters string; test failed" << std::endl;
    } else {
        std::cout << "1.1. Invalid string, 11 characters string; test passed" << std::endl;
    }
    
    // test 2, invalid c-string
    SudokuRowChecker c2;
    std::string s2 = "12345678901";
    c2.setData(s2.c_str());
    if(c2.isValid()){
        std::cout << "2.1. Invalid string, 11 characters c-string; test failed" << std::endl;
    } else {
        std::cout << "2.1. Invalid string, 11 characters c-string; test passed" << std::endl;
    }
    
    // test 3, make sure checker is considered invalid after default constructor
    SudokuRowChecker c3;
    if(c3.isValid()){
        std::cout << "3.1. Invalid string, empty string after initialization; test failed" << std::endl;
    } else {
        std::cout << "3.1. Invalid string, empty string after initialization; test passed" << std::endl;
    }
    
    // test 4, make sure checker is considered invalid after setting empty string
    SudokuRowChecker c4;
    string s4 = "";
    c4.setData(s4);
    if(c4.isValid()){
        std::cout << "4.1. Invalid string, empty string; test failed" << std::endl;
    } else {
        std::cout << "4.1. Invalid string, empty string; test passed" << std::endl;
    }
    
    // test 5, make sure checker is considered invalid after setting empty string
    SudokuRowChecker c5;
    char s5[] = "";
    c5.setData(s5);
    if(c5.isValid()){
        std::cout << "5.1. Invalid string, empty c-string; test failed" << std::endl;
    } else {
        std::cout << "5.1. Invalid string, empty c-string; test passed" << std::endl;
    }
    
    // test 6, make sure checker is considered valid with correct string
    SudokuRowChecker c6;
    string s6 = "123456789";
    c6.setData(s6);
    if(c6.isValid()){
        std::cout << "6.1. Valid string; test passed" << std::endl;
    } else {
        std::cout << "6.1. Valid string; test failed" << std::endl;
    }
    
    // test 7, make sure checker is considered valid with correct string
    SudokuRowChecker c7;
    char s7[] = "123456789";
    c7.setData(s7);
    if(c7.isValid()){
        std::cout << "7.1. Valid string; test passed" << std::endl;
    } else {
        std::cout << "7.1. Valid string; test failed" << std::endl;
    }
    
    // test 8, invalid, string shorter than it should be
    SudokuRowChecker c8;
    string s8 = "1234";
    c8.setData(s8);
    if(c8.isValid()){
        std::cout << "8.1. Invalid string, string too short; test failed" << std::endl;
    } else {
        std::cout << "8.1. Invalid string, string too short; test passed" << std::endl;
    }
    
    // test 9, invalid, string shorter than it should be
    SudokuRowChecker c9;
    char s9[] = "1234";
    c9.setData(s9);
    if(c9.isValid()){
        std::cout << "9.1. Invalid string, string too short; test failed" << std::endl;
    } else {
        std::cout << "9.1. Invalid string, string too short; test passed" << std::endl;
    }
    
    // test 10, invalid, correct length, duplicate characters
    SudokuRowChecker c10;
    string s10 = "000000008";
    c10.setData(s10);
    if(c10.isValid()){
        std::cout << "10.1. Invalid string, duplicate characters in string; test failed" << std::endl;
    } else {
        std::cout << "10.1. Invalid string, duplicate characters in string; test passed" << std::endl;
    }
    
    // test 11, invalid, correct length, duplicate characters
    SudokuRowChecker c11;
    char s11[] = "000000008";
    c11.setData(s11);
    if(c11.isValid()){
        std::cout << "11.1. Invalid string, duplicate characters in c-string; test failed" << std::endl;
    } else {
        std::cout << "11.1. Invalid string, duplicate characters in c-string; test passed" << std::endl;
    }
    
    // test 12, check clear
    SudokuRowChecker c12;
    char s12[] = "123456789";
    c12.setData(s12);
    c12.clear();
    if(c12.isValid()){
        std::cout << "12.1. Invalid string, clear() should empty data; test failed" << std::endl;
    } else {
        std::cout << "12.1. Invalid string, clear() should empty data; test passed" << std::endl;
    }
    return 0;

Codeboard Submission

Please refer to the codeboard submission instructions here: http://mjedmonds.com/CS50/codeboard.html