Well im writing a program in java and my program had to do with Sudoku but its only 4 by 4 not 9 by 9. My program checks to see weather the rows, columns, and regions add up to 10. The output would look like this...
Enter Row 1: 3 2 4 1
Enter Row 2: 4 1 3 2
Enter Row 3: 1 4 2 3
Enter Row 4: 2 3 1 4
REG-1:GOOD
REG-2:GOOD
REG-3:GOOD
REG-4:GOOD
ROW-1:GOOD
ROW-2:GOOD
ROW-3:GOOD
ROW-4:GOOD
COL-1:GOOD
COL-2:GOOD
COL-3:GOOD
COL-4:GOOD
SUDO:VALID
My problem is the last part where it says sudo:valid. I know i need to make a variable that counts the number of things that are invalid and if that variable's value is 0 at the end of the program then i have a valid sudoku. But im not sure how to do this. Anyone know how? Heres my code -
import java.util.Scanner;
-
-
public class test
-
{
-
public static void main( String args[] )
-
{
-
Scanner input = new Scanner( System.in );
-
int a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4;
-
-
System.out.print ( "Enter First Row:" );
-
a1 = input.nextInt();
-
a2 = input.nextInt();
-
a3 = input.nextInt();
-
a4 = input.nextInt();
-
-
System.out.print ( "Enter Second Row:" );
-
b1 = input.nextInt();
-
b2 = input.nextInt();
-
b3 = input.nextInt();
-
b4 = input.nextInt();
-
-
System.out.print ( "Enter Third Row:" );
-
c1 = input.nextInt();
-
c2 = input.nextInt();
-
c3 = input.nextInt();
-
c4 = input.nextInt();
-
-
System.out.print ( "Enter Fourth Row:" );
-
d1 = input.nextInt();
-
d2 = input.nextInt();
-
d3 = input.nextInt();
-
d4 = input.nextInt();
-
-
// Regions
-
-
if (a1 + a2 + b1 + b2 == 10)
-
{
-
System.out.println ( "\nREG-1:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "\nREG-1:BAD" );
-
}
-
-
if (c1 + c2 + d1 + d2 == 10)
-
{
-
System.out.println ( "REG-2:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "REG-2:BAD" );
-
}
-
-
if (a3 + a4 + b3 + b4 == 10)
-
{
-
System.out.println ( "REG-3:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "REG-3:BAD" );
-
}
-
-
if (c3 + c4 + d3 + d4 == 10)
-
{
-
System.out.println ( "REG-4:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "REG-4:BAD" );
-
}
-
-
// Rows
-
-
if (a1 + a2 + a3 + a4 == 10)
-
{
-
System.out.println ( "\nROW-1:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "\nROW-1:BAD" );
-
}
-
-
if (b1 + b2 + b3 + b4 == 10)
-
{
-
System.out.println ( "ROW-2:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "ROW-2:BAD" );
-
}
-
-
if (c1 + c2 + c3 + c4 == 10)
-
{
-
System.out.println ( "ROW-3:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "ROW-3:BAD" );
-
}
-
-
if (d1 + d2 + d3 + d4 == 10)
-
{
-
System.out.println ( "ROW-4:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "ROW-4:BAD" );
-
}
-
-
-
// comlumns
-
-
if (a1 + b1 + c1 + d1 == 10)
-
{
-
System.out.println ( "\nCOL-1:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "\nCOL-1:BAD" );
-
}
-
-
if (a2 + b2 + c2 + d2 == 10)
-
{
-
System.out.println ( "COL-2:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "COL-2:BAD" );
-
}
-
-
if (a3 + b3 + c3 + d3 == 10)
-
{
-
System.out.println ( "COL-3:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "COL-3:BAD" );
-
}
-
-
if (a4 + b4 + c4 + d4 == 10)
-
{
-
System.out.println ( "COL-4:GOOD" );
-
}
-
else
-
{
-
System.out.println ( "COL-4:BAD" );
-
}
-
-
-
}
-
-
}
-
-
3 1237
Check an article in the Java howTos area by Jos which talks about sudoku solvers.
Have you thought about using arrays? Here you go - -
import java.util.Scanner;
-
-
public class Sudoku4 {
-
protected int n=4, checkSum=10, failCount=0;
-
protected Scanner input = new Scanner(System.in);
-
-
public Sudoku4 () {
-
int[][] a = new int[n][n];
-
for (int iRow = 0 ; iRow < n ; iRow++) {
-
System.out.print ( "Enter Row #"+(iRow+1)+ ": ");
-
for (int jCol = 0 ; jCol < n ; jCol++)
-
a[iRow][jCol] = input.nextInt();
-
}
-
-
// Regions
-
System.out.println();
-
int regionIndex = 1, xCount = n/2, yCount = n/2;
-
for (int ix = 0 ; ix < n ; ix += xCount) {
-
for (int jy = 0 ; jy < n ; jy += yCount) {
-
failCount += checkRegion(a,
-
ix, jy, xCount, yCount,
-
"REG-" + regionIndex + ":");
-
regionIndex++;
-
}}
-
-
// Rows
-
System.out.println();
-
regionIndex = 1; xCount = 1; yCount = n;
-
for (int ix = 0 ; ix < n ; ix += xCount) {
-
for (int jy = 0 ; jy < n ; jy += yCount) {
-
failCount += checkRegion(a,
-
ix, jy, xCount, yCount,
-
"ROW-" + regionIndex + ":");
-
regionIndex++;
-
}}
-
-
// Columns
-
System.out.println();
-
regionIndex = 1; xCount = n; yCount = 1;
-
for (int ix = 0 ; ix < n ; ix += xCount) {
-
for (int jy = 0 ; jy < n ; jy += yCount) {
-
failCount += checkRegion(a,
-
ix, jy, xCount, yCount,
-
"COL-" + regionIndex + ":");
-
regionIndex++;
-
}}
-
}
-
-
private int checkRegion (int[][] a, int rowStart, int colStart,
-
int rowCount, int colCount, String identifier) {
-
int sum = 0, errCountInRegion = 1;
-
for (int i = 0; i < rowCount ; i++)
-
for (int j = 0 ; j < colCount ; j++)
-
sum += a[i+rowStart][j+colStart];
-
if (sum == checkSum)
-
errCountInRegion = 0;
-
System.out.println (identifier +
-
((errCountInRegion == 0) ? "GOOD" : "BAD"));
-
return errCountInRegion;
-
}
-
-
public void printFailCount () {
-
System.out.println();
-
if (failCount == 0)
-
System.out.println ("Sudoku: VALID");
-
else
-
System.out.println("Fail Count = " + failCount);
-
}
-
-
public static void main(String args[] ) {
-
new Sudoku4().printFailCount();
-
}
-
}
-
I learnt about the Scanner class today, thanks to you. Best wishes, - M.
you could always just add a count variable at the top ie:
Then each time it hits the code where it says - {
-
//if rows do not add to 10 output fail;
-
failCount++;
-
}
Then at the end, say - if failCount>0 output "Fail"
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Elbert Lev |
last post by:
Please correct me if I'm wrong.
Python (as I understand) uses reference counting to determine when to
delete the object. As soon as the object goes out of the scope it is
deleted. Python does not use garbage collection (as Java does).
So if the script runs a loop:
for i in range(100):
f = Obj(i)
|
by: Carl Bevil |
last post by:
I'm creating a library for internal use that relies on third-party code. I
don't want clients of this library to know anything about the third party
code, when compiling or running. Generally I've been achieving this by having
an abstract base class which defines an interface, and inheriting a concrete
class which defines the implementation. Clients of the library deal only with
the base class and request objects of that type from a...
|
by: Tony Johansson |
last post by:
Hello Experts!
I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I don't understand
completely.
It says
"A garbarage collector, such as the one used in Java, maintains a record of
whether or not
an object is currentlys being used. An unused object is tagged as garbage,
|
by: ChadDiesel |
last post by:
I appreciate the help on this group. I know I've posted a lot here the last
couple of weeks, but I was thrown into a database project at my work with
very little Access experience. No other employee knows anything about
Access. I've searched Google Groups, and that has been a lot of help, but
there are some questions that I just can't find the answer to. I'll try to
take it easy on the group after this question.
I have one more...
|
by: terry |
last post by:
I am a programmer (cobol, peoplesoft, sqr, etc.) so I am familiar
with programming logic, etc. but not very familiar with C. I need a C
program in a study I'm doing. The program is fairly simple, but not
familiar with C code it would take me some time to get it to work. A
good C programmer can probably give me the code in a few minutes.
Here's the program specs:
I'm doing a study on the italicized words in the King James Bible. The...
| |
by: Jintty |
last post by:
Hi,
I'm trying to write a program that will read a txt file, copy it into
another text file and display the number of words, lines and
paragraphs.
I was able to get the copying portion done, but i'm struggling with the
counters.
processBlank is supposed to increment the number of words whenever it
hits a nonblank, so i figured using if its not equal to blank then
update word count but no luck
please help me, i'd appreciate any help
|
by: aaronfude |
last post by:
Hi,
Please consider the following class (it's not really my class, but it's
a good example for my question):
class Vector {
int myN;
double *myX;
Vector(int n) : myN(n), myX(new double) { }
double &operator()(int i) { return myX; }
|
by: naknak4 |
last post by:
Introduction
This assignment requires you to develop solutions to the given problem
using several different approaches (which actually involves using three
different STL containers). You will implement all three techniques as
programs. In these programs, as well as solving the problem, you will
also measure how long the program takes to run. The programs are worth
80% of the total mark. The final 20% of the marks are awarded for a...
|
by: naknak |
last post by:
Introduction
This assignment requires you to develop solutions to the given problem
using several different approaches (which actually involves using three
different STL containers). You will implement all three techniques as
programs. In these programs, as well as solving the problem, you will
also measure how long the program takes to run. The programs are worth
80% of the total mark. The final 20% of the marks are awarded for a...
|
by: oec.deepak |
last post by:
Hi
Cn any one telll me what is Reference counting in C++.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |