2016-08-27 11 views
0

まず、私は初心者のコーダーであり、何時間も問題に取り組んでいきたいと思っています。入出力ファイル(Sudoku Solver)

1.私は、私のスクロールボードに自分の値を設定するために、.txtファイルを取得するために、ifstreamとofstreamの入力と出力のメカニズムを使用する方法を理解できません。 2)私はセル内の各xypositionの "可能な値"ごとにスドクボードを印刷する方法を理解できないようです。

私は読み込むテキストファイルを実装しようとしています:

puzzle.setBoardValue(0,0,1); 
    puzzle.setBoardValue(1,1,3); 
    puzzle.setBoardValue(2,2,9); 

    puzzle.setBoardValue(3,2,6); 
    puzzle.setBoardValue(4,1,2); 
    puzzle.setBoardValue(5,0,7); 

    puzzle.setBoardValue(6,2,5); 
    puzzle.setBoardValue(7,0,9); 
    puzzle.setBoardValue(8,1,8); 

    puzzle.setBoardValue(0,5,6); 
    puzzle.setBoardValue(1,4,1); 
    puzzle.setBoardValue(2,3,5); 

    puzzle.setBoardValue(3,3,3); 
    puzzle.setBoardValue(4,4,8); 
    puzzle.setBoardValue(5,5,4); 

    puzzle.setBoardValue(6,3,9); 
    puzzle.setBoardValue(8,4,2); 

    puzzle.setBoardValue(0,6,3); 
    puzzle.setBoardValue(1,7,4); 
    puzzle.setBoardValue(2,8,7); 

    puzzle.setBoardValue(6,8,3); 
    puzzle.setBoardValue(7,6,1); 
    puzzle.setBoardValue(8,7,7); ` 

と基本的には機能void setBoardValue();への方向私のプログラムに、自動的に私は考え出したすべては、私は

を入れて、私の int main();であったことを使用します
cout << "Enter filename:" << endl; 
cin >> filename; 
in.open(filename); 
while(getline(in,line)){ 
... 

第2に、各数値セルに可能な値をすべて印刷する方法を理解できません。ここで

は私の全体のプログラムコードです:

#include <iostream> 
#include <fstream> 
#include <vector> 

using namespace std; 

class SudokuPuzzle{ 

private: 
    unsigned short board[9][9]; 
    char BlankChar; 
    bool debug; 
    void printTracerTryingValue(int xpos, int ypos); 
    bool solve(int xpos, int ypos); 
    bool verifyValue(int xpos, int ypos); 

public: 
    SudokuPuzzle(); 
    void print(); 
    void setBoardValue(int xpos, int ypos, int value); 
    int getBoardValue(int xpos, int ypos); 
    bool solve(); 
}; 

SudokuPuzzle::SudokuPuzzle(){ 
    debug = false; 
    for (int i = 0; i < 9; ++i){ 
    for (int j = 0; j < 9; ++j){ 
     board[j][i] = 0; 
    } 
    } 
} 

void SudokuPuzzle::print(){ 
    for (int y = 0; y < 9; y++){ 
    if (y % 3 == 0){ 
     cout << "-------------------------------" << endl; 
    } 
    for (int x = 0; x < 9; x++){ 
     if (x % 3 == 0){ 
     cout << "|"; 
     } 
     if (board[x][y] != 0){ 
     cout << " " << board[x][y] << " "; 
     } 
     else{ 
     cout << " . "; 
     } 
    } 
    cout << "|" << endl; 
    } 
    cout << "-------------------------------" << endl; 
} 

void SudokuPuzzle::setBoardValue(int xpos, int ypos, int value){ 
    board[xpos][ypos] = value; 
} 

void SudokuPuzzle::setBoardValue(int xpos, int ypos, int value){ 
    board[xpos][ypos] = value; 
} 

bool SudokuPuzzle::solve(){ 
    return solve(0,0); 
} 

void SudokuPuzzle::setBoardValue(int xpos, int ypos, int value){ 
    board[xpos][ypos] = value; 
} 

bool SudokuPuzzle::solve(){ 
    return solve(0,0); 
} 

int SudokuPuzzle::getBoardValue(int xpos, int ypos){ 
    return board[xpos][ypos]; 
} 

bool SudokuPuzzle::solve(int xpos, int ypos){ 
    if (board[xpos][ypos] != 0){ 
    if (verifyValue(xpos, ypos)){ 
     if (xpos == 8 && ypos == 8){ 
     return true; 
     } 
     int next_x = xpos+1; 
     int next_y = ypos; 
     if (next_x >= 9){ 
     next_x = 0; 
     next_y++; 
     } 
     return solve(next_x, next_y); 
    } 
    else{ 
     return false; 
    } 
    } 
    for(int value = 1; value < 10; value++){ 
    setBoardValue(xpos, ypos, value); 
    if (verifyValue(xpos, ypos)){ 
     if (xpos == 8 && ypos == 8){ 
     return true; 
     } 
     int next_x = xpos+1; 
     int next_y = ypos; 

     if (next_x >= 9){ 
     next_x = 0; 
     next_y++; 
     } 
     if (solve(next_x, next_y)){ 
     return true; 
     } 
    } 
    } 

    board[xpos][ypos] = 0; 
    return false; 
} 
bool SudokuPuzzle::verifyValue(int xpos, int ypos){ 
    printTracerTryingValue(xpos, ypos); 
    int value = board[xpos][ypos]; 
    for (int x_verify = 0; x_verify < 9; x_verify++){ 
    if (x_verify == xpos){ 
     continue; 
    } 
    int verifyValue = board[x_verify][ypos]; 
    if (verifyValue == value){ 
     return false; 
    } 
    } 
    for (int y_verify = 0; y_verify < 9; y_verify++){ 
    if (y_verify == ypos){ 
     continue; 
    } 
    int verifyValue = board[xpos][y_verify]; 
    if (verifyValue == value){ 
     return false; 
    } 
    } 

    int box_x = xpos/3; 
    int box_y = ypos/3; 

    for (int y_verify = box_y * 3; y_verify < box_y * 3 + 3; y_verify++){ 
    for (int x_verify = box_x * 3; x_verify < box_x * 3 + 3; x_verify++){ 
     if (x_verify == xpos && y_verify == ypos){ 
     continue; 
     } 

     int verifyValue = board[x_verify][y_verify]; 
     if (verifyValue == value){ 
     return false; 
     } 
    } 
    } 
    return true; 
} 

void SudokuPuzzle::printTracerTryingValue(int xpos, int ypos){ 
    if(debug){ 
    for (int i = 0; i < xpos+ypos; i++){ 
     cout << " "; 
    } 
    cout << "Trying value " << board[xpos][ypos] << " at board[" << xpos << "][" \ 
<< ypos <<"]" << endl; 
    } 
} 

int main(int argc, char *const argv[]){ 
    SudokuPuzzle puzzle; 
    string filename; 
    string line; 
    ifstream in; 
    ofstream out; 

    cout << "Enter filename:" << endl; 
    cin >> filename; 
    in.open(filename); 
    while(getline(in, line)){ 
    puzzle.line.print() 
    } 
    cout << endl; 

    if(puzzle.solve()){ 
    cout << "Solution:" << endl; 
    puzzle.print(); 
    } 
    else{ 
    cout << "Puzzle is not solvable."; 
    } 
    cout << endl; 
    return 0; 
} 
+0

あなたは、ファイルを読み取るためのストリームを使用してトラブルを抱えていると述べました。私たちにあなたの試みを見せてもらえますか、それを試したときに何が起こったのですか? – Chris

+0

部品を実装しようとしましたが、.txt内の各行を読むことができます。 –

+0

puzzle.print()を使用する際に各行を使用する上で問題があるため、コンパイルまたは実行しませんでした。 –

答えて

1

あなたの質問は少し紛らわしい書かれているが、私はそれを理解してきたように、私はそれに答えるようにしようとするでしょう。まず、テキストファイルに実際の関数呼び出しがないことを願っています。テキストファイルから関数を直接呼び出す方法はありません。 また、print()std::stringの方法ではないため、を呼び出すことはmain()whileステートメントでは意味がありません。また、 'line'はSudokuPuzzleタイプのメンバーではありません。

あなたが入力が定期的にあることを期待し、グリッド上の位置を保存して読むことにだけ興味があれば、あなたは、単にこのようなファイルがある可能性があります:

0 0 1 
1 1 3 
2 2 9 
...(etc) 

そこから、入力やエラーチェックを心配することなく、我々はファイルからの読み取りに、このようないくつかのコードを持っているかもしれません:ファイルストリームを扱う

std::ifstream readFile("Text.txt"); //construct ifstream object from text file 
std::vector<int> vec; 
int file_input; //temporary variable to hold input 
while(readFile >> file_input) 
{ 
    vec.push_back(file_input); 
}; 
readFile.close(); 

はちょうどstd::cinまたはstd::coutでの作業のようなものです。ストリーム演算子<<>>を使用して、ストリームと他のデータ間の情報を伝達します。

ここでデータ構造上必要な処理(この場合はstd::vectorですが、何でも構いません)を行うことができます。

我々は戻ってそれを書くために必要がある場合、我々はこの種のコードを使用することができます:あなたは、出力のためのテキストの書式コードのブロックで

std::ofstream writeFile("Text.txt"); //overwrites contents! 
for(unsigned i = 0; i < vec.size(); ++i) 
{ 
    //this is just to format the text output to the same way it came in 
    writeFile << vec[i] << ' '; 
    if ((i + 1) % 3 == 0) 
    writeFile << '\n'; 
} 
writeFile.close(); 

あなたは文字を使用してのような何かをしたいことがどこであります|_のように、グリッドパターンを作成します。あなたはそれについてどうやって行くべきかについて考えなければなりません(どこに挿入する必要があるか考え、予測可能な場所に来なければなりません)。

Here's some example code as well.

関連する問題