学校の宿題で困っています。助けてください。
数独というゲームをご存知でしょうか?
9X9のマスを1-9の数字を埋めていくゲームです。
このゲームにはルールがいくつかあり
1.横の列縦の列それぞれに1-9のすべて数字が入っていなければいけない
2.右上から左下、左上から右下の斜めの列それぞれにも1-9の数字すべてが入っていなければいけない
3.9個に分けれらた枠の中それぞれにも1-9のすべての数字が入っていなければいけない
----------------------------
|5 4 6 |9 2 3 |1 7 8 |
|1 9 7 |6 8 5 |3 4 2 |
|3 8 2 |1 7 4 |6 5 9 |
----------------------------
|7 3 4 |8 9 2 |5 6 1 |
|6 5 8 |4 1 7 |9 2 3 |
|2 1 9 |5 3 6 |7 8 4 |
----------------------------
|8 2 3 |7 5 1 |4 9 6 |
|4 7 1 |2 6 9 |8 3 5 |
|9 6 5 |3 4 8 |2 1 7 |
----------------------------
上にあるのが答えの例です。
課題で出されたのは、9X9に並べられた数字が入っているファイルを読み取り、
例
5 4 6 9 2 3 1 7 8
1 9 7 6 8 5 3 4 2
3 8 2 1 7 4 6 5 9
7 3 4 8 9 2 5 6 1
6 5 8 4 1 7 9 2 3
2 1 9 5 3 6 7 8 4
8 2 3 7 5 1 4 9 6
4 7 1 2 6 9 8 3 5
9 6 5 3 4 8 2 1 7
4 1 2 3 6 5 9 8 7
6 7 5 9 8 1 4 3 2
3 8 9 4 7 2 1 5 6
5 3 1 8 9 6 7 2 4
7 4 6 2 1 3 8 9 5
1 2 7 6 3 8 5 4 9
2 9 8 5 4 7 3 6 1
9 6 3 7 5 4 2 1 8
8 5 4 1 2 9 6 7 3
など
それがこのゲームのルールすべてにしたがっているか確かめるプログラムをつくることです。
それで自分なりに作ってみて試しました。
しかしいろいろ試験をしてみて、ルール3の枠の中の数字を確かめる部分でFalseが返ってしまい、
すべて不正解という結果になってしまいます。
これが僕の作ったプログラムです。
/*ファイル名SudokuXDriver.cpp*/
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
#include "SudokuXGame.h"
int main()
{
char file[12];
cout << "Enter name of the file: ";
cin >> file;
SudokuGame game1(file);
if (game1.isWinner())
cout << "Game is a Winner!" << endl;
else
cout << "Game is NOT a Winner" << endl;
game1.write();
return 0;
}
/*ファイル名SudokuXGame.h*/
class SudokuGame
{
public:
// Constructors
SudokuGame(); // Set to empty (zeros)
SudokuGame(char []); // Read initial state from file
bool isWinner(); // Determine if current game state is valid winner
void write(); // Write game state to console
bool checkDia();
bool checkCo();
bool checkRo();
bool checkBox();
private:
int gameGrid[9][9]; // Array of game elements
};
// This file includes implementation of member functions for the
// SudokuGame class
// Default Constructor: Set to empty (zeros)
SudokuGame::SudokuGame()
{
for (int row = 0; row < 9; row++)
for (int col = 0; col < 9; col++)
gameGrid[row][col] = 0;
}
// Parameterized constructor: Read initial game state from fil
SudokuGame::SudokuGame(char gameFile[])
{
ifstream inFile(gameFile); // Define and open input file
for (int row = 0; row < 9; row++)
for (int col = 0; col < 9; col++)
inFile >> gameGrid[row][col];
inFile.close();
}
// Write current game state to console. Zero values represent
// empty grid elements and are not written.
void SudokuGame::write()
{
for (int row = 0; row < 9; row++)
{
// Draw horizontal lines for groups of 3
if (row % 3 == 0)
{
for (int i = 0; i < 28; i++)
cout << "-";
cout << endl;
}
// Write info and vertical lines
for (int col = 0; col < 9; col++)
{
// Insert vertical lines after groups of 3
if (col % 3 == 0)
cout << "|";
else
cout << " ";
// Write game element digit (zero implies empty)
if (gameGrid[row][col] != 0)
cout << gameGrid[row][col] << " ";
else
cout << " ";
}
cout << "|" << endl; // Write right-side vertical line marker
}
// Draw last horizontal line
for (int i = 0; i < 28; i++)
cout << "-";
cout << endl << endl << endl;
}
// This function examines the current game state and determines if the
// configuration of integers represents a winning game state.
bool SudokuGame::isWinner()
{
bool goal = false;
if(checkRo() && checkCo() && checkDia() && checkBox())
goal = true;
return goal; // A FUNCTION STUB
}
bool SudokuGame::checkRo()
{
bool flag = false;
int total = 0;
const int size = 9;
int i = 0, j = 0, k = 0;
while(i < size)
{
while(j < size)
{
k = j + 1;
while(k < size - 1 - j)
{
if(gameGrid[i][k] == gameGrid[i][j])
return flag;
k++;
}
total += gameGrid[i][j];
j++;
k = 0;
}
if(total != 45)
return flag;
total = 0;
j = 0;
i++;
}
flag = true;
return flag;
}
bool SudokuGame::checkCo()
{
bool flag;
int total = 0;
const int size = 9;
int i = 0, j = 0, k = 0;
while(j < size)
{
while(i < size)
{
k = i + 1;
while(k < size - 1 - i)
{
if(gameGrid[k][j] == gameGrid[i][j])
return flag;
k++;
}
total += gameGrid[i][j];
i++;
k = 0;
}
if(total != 45)
return flag;
total = 0;
i = 0;
j++;
}
flag = true;
return flag;
}
bool SudokuGame::checkBox()
{
bool flag;
int x, y, r, c;
int i, j, k;
for(x = 0; x < 2; x++)
{
if(x == 0)
r = 0;
else if(x == 1)
r = 3;
else
r = 6;
for(y = 0; y < 2; y++)
{
if(y == 0)
c = 0;
else if(y == 1)
c = 3;
else
c = 6;
for(i = 1; i <= 9; i++)
{
flag = false;
for(k = r; k < r + 2; k++)
{
for(j = c; j < c + 2; j++)
{
if(gameGrid[k][j] == i)
flag = true;
}
}
if(!flag)
return flag;
}
}
}
return flag;
}
bool SudokuGame::checkDia()
{
int r, c;
bool flag;
for(int i = 1; i <= 9; i++)
{
flag = false;
for(int j = 0; j <= 8; j++)
{
r = 8 - j;
if(gameGrid[r][j] == i)
flag = true;
}
if(!flag)
return flag;
}
for(int k = 1; k <= 9; k++)
{
flag = false;
for(int l = 0; l <= 8; l++)
{
if(gameGrid[l][l] == k)
flag = true;
}
if(!flag)
return flag;
}
return flag;
}
よろしくお願いします。