-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudoku.java
More file actions
174 lines (155 loc) · 5.21 KB
/
Copy pathSudoku.java
File metadata and controls
174 lines (155 loc) · 5.21 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.InputMismatchException;
import java.util.Scanner;
class Number_Exception extends Exception {
public Number_Exception(String s) {
super(s);
}
}
class Row_Exception extends Exception {
public Row_Exception(String s) {
super(s);
}
}
class Column_Exception extends Exception {
public Column_Exception(String s) {
super(s);
}
}
class Square_Exception extends Exception {
public Square_Exception(String s) {
super(s);
}
}
public class Sudoku {
private int[][] chart = new int[9][9];
private int[][] helpful_friend = new int[3][3];
private int Win_Law = 0;
public boolean playing = true;
public int getWin_Law() {
return Win_Law;
}
public void Show_chart() {
for (int i = 0; i < chart.length; i++) {
for (int j = 0; j < chart[i].length; j++) {
System.out.print(chart[i][j] + " ");
}
System.out.println();
}
}
public boolean hasDuplicatesInColumn(int[][] inArray) {
for (int row = 0; row < inArray.length; row++) {
for (int col = 0; col < inArray[row].length; col++) {
int num = inArray[row][col];
for (int otherRow = row + 1; otherRow < inArray.length; otherRow++) {
if (num == inArray[otherRow][col] && num != 0) {
return true;
}
}
}
}
return false;
}
public boolean hasDuplicatesInRows(int[][] inArray) {
for (int[] ints : inArray) {
for (int col = 0; col < ints.length; col++) {
int num = ints[col];
for (int otherCol = col + 1; otherCol < inArray.length; otherCol++) {
if (num == ints[otherCol] && num != 0) {
return true;
}
}
}
}
return false;
}
public boolean hasDuplicatesInSquare(int[][] inArray) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
int num = inArray[row][col];
for (int k = 0; k < 3; k++) {
for (int i = 0; i < 3; i++) {
if (row == k && col == i) {
continue;
} else if (num == inArray[k][i] && num != 0) {
return true;
}
}
}
}
}
return false;
}
public void Game_Setup() {
Scanner input = new Scanner(System.in);
try {
System.out.println("enter your coordinate!");
int x = input.nextInt();
int y = input.nextInt();
System.out.println("set your number between 1 to 9!");
int answer = input.nextInt();
if (answer > 9 || answer < 1) {
throw new Number_Exception("number should be between 1 to 9!");
}
chart[x][y] = answer;
if (hasDuplicatesInRows(chart)) {
throw new Row_Exception("duplicated number detected in row!");
} else if (hasDuplicatesInColumn(chart)) {
throw new Column_Exception("duplicated number detected in column!");
} else {
Win_Law++;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
helpful_friend[k][l] = chart[(3 * i) + k][(3 * j) + l];
}
}
if (hasDuplicatesInSquare(helpful_friend)) {
throw new Square_Exception("duplicated number detected in square!");
} else {
Win_Law++;
}
}
}
if (Win_Law == 81) {
System.out.println("GAME FINISHED!");
playing = false;
}
// Show_chart();
} catch (ArrayIndexOutOfBoundsException | Column_Exception | Row_Exception | Square_Exception e) {
System.out.println(e);
System.out.println("try again!");
Game_Setup();
} catch (InputMismatchException | Number_Exception e) {
System.out.println(e);
Game_Setup();
}
}
}
class Main {
public static void main(String[] args) {
Sudoku s = new Sudoku();
Scanner in = new Scanner(System.in);
while (true) {
if (s.playing) {
System.out.println("1)START\n2)Show Chart\n3)END");
int aw = in.nextInt();
switch (aw) {
case 1:
// s.Show_chart();
s.Game_Setup();
break;
case 2:
s.Show_chart();
break;
case 3:
return;
}
}else {
System.out.println("Game has already finished!");
return;
}
}
}
}