口コミ 全く需要が無い、くまのC++プログラムをご紹介(笑) - Arkansas Freeway式blog -アーカンソーフリーウェイ式ブログ-
FC2ブログ

Arkansas Freeway式blog -アーカンソーフリーウェイ式ブログ-
Arkansas Freeway式blog -アーカンソーフリーウェイ式ブログ-

cameraddict達の雑記用ブログです 
カレンダー
10 | 2019/11 | 12
- - - - - 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

2008.11.22[土] 全く需要が無い、くまのC++プログラムをご紹介(笑)

まぁぁぁぁったく、恐らく、だれぇぇぇぇぇえも見ないのを知ってて記載します。
と言うより、メモです。今思うと、もっとうまい書き方あったのになぁ。。。
そのうち、説明でも書くかもです。。。


----------------------------------------
以下、C++を読める方専用。

by くま
/* Final Project: Sudoku
Written by : kuma
Date : 12/10/07

"Sudoku".
*/
using namespace std;
#include <iostream>
#include <iomanip>
#include <string>
int main () {
int sheet[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{2, 0, 0, 4, 1, 9, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 9}};

void printDescription();
void dispSheet(int sheet[9][9]);
int input(int sheet[9][9]);
int check(int sheet[9][9]);
int check(int box[9][9]);
int check(int column[9][9]);

printDescription();
dispSheet(sheet);
input(sheet);


int box[9][9] = {{sheet[0][0], sheet[0][1], sheet[0][2], sheet[1][0], sheet[1][1], sheet[1][2], sheet[2][0], sheet[2][1], sheet[2][2]},
{sheet[0][3], sheet[0][4], sheet[0][5], sheet[1][3], sheet[1][4], sheet[1][5], sheet[2][3], sheet[2][4], sheet[2][5]},
{sheet[0][6], sheet[0][7], sheet[0][8], sheet[1][6], sheet[1][7], sheet[1][8], sheet[2][6], sheet[2][7], sheet[2][8]},
{sheet[3][0], sheet[3][1], sheet[3][2], sheet[4][0], sheet[4][1], sheet[4][2], sheet[5][0], sheet[5][1], sheet[5][2]},
{sheet[3][3], sheet[3][4], sheet[3][5], sheet[4][3], sheet[4][4], sheet[4][5], sheet[5][3], sheet[5][4], sheet[5][5]},
{sheet[3][6], sheet[3][7], sheet[3][8], sheet[4][6], sheet[4][7], sheet[4][8], sheet[5][6], sheet[5][7], sheet[5][8]},
{sheet[6][0], sheet[6][1], sheet[6][2], sheet[7][0], sheet[7][1], sheet[7][2], sheet[8][0], sheet[8][1], sheet[8][2]},
{sheet[6][3], sheet[6][4], sheet[6][5], sheet[7][3], sheet[7][4], sheet[7][5], sheet[8][3], sheet[8][4], sheet[8][5]},
{sheet[6][6], sheet[6][7], sheet[6][8], sheet[7][6], sheet[7][7], sheet[7][8], sheet[8][6], sheet[8][7], sheet[8][8]}};


int column[9][9] = {{sheet[0][0], sheet[1][0], sheet[2][0], sheet[3][0], sheet[4][0], sheet[5][0], sheet[6][0], sheet[7][0], sheet[8][0]},
{sheet[0][1], sheet[1][1], sheet[2][1], sheet[3][1], sheet[4][1], sheet[5][1], sheet[6][1], sheet[7][1], sheet[8][1]},
{sheet[0][2], sheet[1][2], sheet[2][2], sheet[3][2], sheet[4][2], sheet[5][2], sheet[6][2], sheet[7][2], sheet[8][2]},
{sheet[0][3], sheet[1][3], sheet[2][3], sheet[3][3], sheet[4][3], sheet[5][3], sheet[6][3], sheet[7][3], sheet[8][3]},
{sheet[0][4], sheet[1][4], sheet[2][4], sheet[3][4], sheet[4][4], sheet[5][4], sheet[6][4], sheet[7][4], sheet[8][4]},
{sheet[0][5], sheet[1][5], sheet[2][5], sheet[3][5], sheet[4][5], sheet[5][5], sheet[6][5], sheet[7][5], sheet[8][5]},
{sheet[0][6], sheet[1][6], sheet[2][6], sheet[3][6], sheet[4][6], sheet[5][6], sheet[6][6], sheet[7][6], sheet[8][6]},
{sheet[0][7], sheet[1][7], sheet[2][7], sheet[3][7], sheet[4][7], sheet[5][7], sheet[6][7], sheet[7][7], sheet[8][7]},
{sheet[0][8], sheet[1][8], sheet[2][8], sheet[3][8], sheet[4][8], sheet[5][8], sheet[6][8], sheet[7][8], sheet[8][8]}};

int ansSheet = check(sheet);
int ansBox = check(box);
int ansColumn = check(column);
if (ansSheet == 1 and ansBox ==1 and ansColumn ==1)
{
cout << "#########################################" << endl;
cout << "########## You Win!!!! ##########" << endl;
cout << "#########################################" << endl;
}
else
{
cout << "#########################################" << endl;
cout << "## It's wrong!! You must try it again ##" << endl;
cout << "#########################################" << endl;
}
return 0;
}

//-------------------------------------------------------------------
//-------------------------------------------------------------------

// print the description

void printDescription()
{
cout << endl;
cout << "===========================================" << endl;
cout << "| Fill each column, each row, |" << endl;
cout << "| and each of the nine 3x3 boxes, |" << endl;
cout << "| to contain the digits from 1 to 9, |" << endl;
cout << "| only one time each. |" << endl;
cout << "===========================================" << endl;
cout << endl;
}

//-------------------------------------------------------------------
//-------------------------------------------------------------------

//display the sheet

void dispSheet(int sheet[9][9])
{
cout << " [x] 0 1 2 3 4 5 6 7 8 " << endl;
cout << " [y] -----------------------------" << endl;


//display row0 to row2
for (int r = 0; r<=2; r++)
{
cout << " " << r << " ";
cout << "|";

for (int c = 0; c <=2; c++)
{
if (sheet[r][c] <= 0)
cout << " ";
else
cout << " " << sheet[r][c] << " ";
}
cout << "|";
for (int c = 3; c <= 5; c++)
{
if (sheet[r][c] <= 0)
cout << " ";
else
cout << " " << sheet[r][c] << " ";
}
cout << "|";
for (int c = 6; c <= 8; c++)
{
if (sheet[r][c] <= 0)
cout << " ";
else
cout << " " << sheet[r][c] << " ";
}
cout << "|" << endl;

}
cout << " |---------|---------|---------|" << endl;

//display row3 to row5
for (int r = 3; r<=5; r++)
{
cout << " " << r << " ";
cout << "|";

for (int c = 0; c <=2; c++)
{
if (sheet[r][c] <= 0)
cout << " ";
else
cout << " " << sheet[r][c] << " ";
}
cout << "|";
for (int c = 3; c <= 5; c++)
{
if (sheet[r][c] <= 0)
cout << " ";
else
cout << " " << sheet[r][c] << " ";
}
cout << "|";
for (int c = 6; c <= 8; c++)
{
if (sheet[r][c] <= 0)
cout << " ";
else
cout << " " << sheet[r][c] << " ";
}
cout << "|" << endl;

}
cout << " |---------|---------|---------|" << endl;

//display row6 to row8
for (int r = 6; r<=8; r++)
{
cout << " " << r << " ";
cout << "|";

for (int c = 0; c <=2; c++)
{
if (sheet[r][c] <= 0)
cout << " ";
else
cout << " " << sheet[r][c] << " ";
}
cout << "|";
for (int c = 3; c <= 5; c++)
{
if (sheet[r][c] <= 0)
cout << " ";
else
cout << " " << sheet[r][c] << " ";
}
cout << "|";
for (int c = 6; c <= 8; c++)
{
if (sheet[r][c] <= 0)
cout << " ";
else
cout << " " << sheet[r][c] << " ";
}
cout << "|" << endl;

}
cout << " -----------------------------" << endl;
}

//-------------------------------------------------------------------
//-------------------------------------------------------------------

//input a digit

int input(int sheet[9][9])
{
int digit;
int x;
int y;
void dispSheet(int sheet[9][9]);
int row;
int column;
string outOfRange;
bool empty;
do {
cout << "===========================================" << endl;
cout << "| Input a digit onto (x,y) |" << endl;
cout << "| To Delete a digit, Input 0 |" << endl;
cout << "===========================================" << endl;
cout << "digit = ";
//if player doesn't input an integer, demand to input an integer

/* In next 3 for loops, let player type something.
If it is not an integer, then clear the error and ignore what player typed.
And it will loop while player types an ingteger.*/

for (cin >> digit; !cin; cin >> digit)
{
cin.clear(); //clear the error
cin.ignore(1000, '\n'); //ignore chatacters(maximun 1000 characters) from first chatacter to end character ('ENTER')
cout << endl;
cout << "##########################################" << endl;
cout << "###### It's not an integer! ######" << endl;
cout << "##########################################" << endl;
cout << "digit = ";
}
cout << "x = ";
for (cin >> x; !cin; cin >> x)
{
cin.clear(); //clear the error
cin.ignore(1000, '\n'); //ignore chatacters(maximun 1000 characters) from first chatacter to end character ('ENTER')
cout << endl;
cout << "##########################################" << endl;
cout << "###### It's not an integer! ######" << endl;
cout << "##########################################" << endl;
cout << "x = ";
}
cout << "y = ";
for (cin >> y; !cin; cin >> y)
{
cin.clear(); //clear the error
cin.ignore(1000, '\n'); //ignore chatacters(maximun 1000 characters) from first chatacter to end character ('ENTER')
cout << endl;
cout << "##########################################" << endl;
cout << "###### It's not an integer! ######" << endl;
cout << "##########################################" << endl;
cout << "y = ";
}

//when numbers are valid, input the digit onto the sheet
if (digit>-1 && digit <10)
{
if (x>-1 && x <9)
{
if (y>-1 && y <9)
{
sheet[y][x] = digit;
dispSheet(sheet);
cout << endl;
}
}
}
//if not, warn it to the player and keep inputting
else
{
void dispSheet(int sheet[9][9]);
cout << "###########################################" << endl;
cout << "######## O U T O F R A N G E #######" << endl;
cout << "######## type something to continue #######" << endl;
cout << "###########################################" << endl;
cin >> outOfRange;
dispSheet(sheet);
}

//when there is an empty space, let empty be true to continue loop 'input'.
//if there is no empty space, end the loop.
empty = 0;
for (row=0; row<9 && !empty; row++)
{
for (column=0, empty = 0; column<9 && !empty; column++)
{
empty = sheet[row][column]==0;
}
}
} while(empty);
return sheet[9][9];
}

//-------------------------------------------------------------------
//-------------------------------------------------------------------

//check whether or not it contains digits from 1 to 9

int check(int sheet[9][9])
{
int ans = 1;
for (int row = 0; row<9 && ans == 1; row++)
{
for (int i = 0; i <8 && ans == 1; i++)
{
for (int j = i+1; j <9 && ans == 1; j++)
{
if (sheet[row][i] == sheet[row][j])
ans = 0;
else if (sheet[row][i] == 0 || sheet[row][j] == 0)
ans = 0;
}
}
}
return ans;
}


ランキングに参加しています。
↓をクリックしていただけると、
とってもありがたいです

ブログランキング・にほんブログ村へ
    17:05  Trackback : 0  Top
Trackback
http://arkansasfreeway.blog116.fc2.com/tb.php/51-2cb0ee59
プロフィール

Arkansas Freeway


Arkansas Freeway
の雑記用のブログです。

アメリカはアーカンソー州の田舎町ラッセルビル(Russellville)にあるアーカンソーテック大学(Arkansas Tech University)に在学中の
【Nikon D80使いの しゅーと】
【Canon Kiss X3使いの ちさと】
というCameraddict(camera+addict)なくせ者が適当に更新していきます。できるだけ良い作品を撮ろうと日々更新していきますので、お付き合いください。。。

【OB紹介】
Canon EOS 5DMK2使いの くま
Nikon D300使いの けいすけ
Pentax SP1000使いの たかゆき