(Original) Nine Palace - Java Implementation - (原创) Java 九宫图 实现

    Some days ago, my girlfriend wanted me to write a program about Nine Palace in C#. Although I was extremely busy during that time, I still wrote for her in C# and Java(I like Java too). Because I can't stand her lovely begging.

    OK, let's get down to business.
    There're two method in this class:
    1: Type a dimension, one of the solutions will be displayed.
    2: Give your solution, algorithm will tell you whether your solution is correct.

    Here is the Java source code. You can copy it to your Java IDE and run it directly.

    I'll appreciate your reply with your better algorithm.

/*
 * Michael Leo
 * 2009/05/11/1
 */
package com.NinePalacePlans;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class NinePalacePlans
{
	public static void p()
	{
		System.out.println();
	}

	public static void p1(Object o)
	{
		System.out.print(o);
	}

	public static void p(Object o)
	{
		System.out.println(o);
	}

	public NinePalacePlans()
	{
		dimension = 3;
	}

	public NinePalacePlans(int dimension)
	{
		this.dimension = dimension;
	}

	private int dimension;

	private int[][] area;

	private int total;

	public boolean manualNinePalacePlans(int[][] paraArea)
	{
		int dimension = paraArea.length;
		int centerNum = (dimension * dimension + 1) / 2;
		// Calculate the each line sum
		int total = centerNum * dimension;
		int tempSum = 0;
		for (int i = 0; i < dimension; i++)
		{
			// Check horizontal
			tempSum = 0;
			for (int j = 0; j < dimension; j++)
			{
				tempSum += paraArea[i][j];
			}
			if (tempSum != total)
			{
				return false;
			}
			// Check vertical
			tempSum = 0;
			for (int j = 0; j < dimension; j++)
			{
				tempSum += paraArea[j][i];
			}
			if (tempSum != total)
			{
				return false;
			}
		}
		// Check diagonal
		int tSumLeftDiagonal = 0;
		int tSumRigthDiagonal = 0;
		for (int i = 0; i < dimension; i++)
		{
			tSumLeftDiagonal += paraArea[i][i];
			tSumRigthDiagonal += paraArea[(dimension - 1) - i][(dimension - 1)
					- i];
		}
		if (tSumLeftDiagonal != total || tSumRigthDiagonal != total)
		{
			return false;
		}
		// All checked
		return true;
	}

	public void createNinePalacePlans()
	{
		area = new int[dimension][dimension];
		// Get the center number
		int centerNum = (dimension * dimension + 1) / 2;
		// Calculate the each line sum
		total = centerNum * dimension;
		// Set first number 1 position
		int preNumx = (dimension + 1) / 2 - 1;
		int preNumy = 0;
		area[preNumy][preNumx] = 1;
		// Set loop times
		int loop = dimension * dimension;
		int nextx = preNumx;
		int nexty = preNumy;
		for (int i = 1; i < loop; i++)
		{
			// Calculate next number position
			nextx += 1;
			nexty -= 1;
			// nexty position is out-of-range
			if (nexty < 0)
			{
				nexty = dimension - 1;
			}
			// nextx position is out-of-range
			if (nextx >= dimension)
			{
				nextx = 0;
			}
			// nextx, nexty position has already had a number
			if (area[nexty][nextx] != 0)
			{
				nextx = preNumx;
				nexty = preNumy + 1;
			}
			// Fill the number
			area[nexty][nextx] = i + 1;

			// Keep the previous position
			preNumx = nextx;
			preNumy = nexty;
		}
	}

	public void printNinePalace()
	{
		for (int i = 0; i < dimension; i++)
		{
			for (int j = 0; j < dimension; j++)
			{
				p1(area[i][j] + "\t");
			}
			p();
		}
	}

	public int getTotal()
	{
		return total;
	}

	public void setTotal(int total)
	{
		this.total = total;
	}

	public int getDimension()
	{
		return dimension;
	}

	public void setDimension(int dimension)
	{
		this.dimension = dimension;
	}

	public static void main(String[] args)
	{
		NinePalacePlans nine = new NinePalacePlans();
		p1("Please input the dimension(odd number): ");
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String inStr;
		int num = 0;
		try
		{
			inStr = in.readLine();
			num = Integer.valueOf(inStr);
			// Only odd number is available
			while (num % 2 == 0)
			{
				p("Dimension must be odd number.");
				p1("Please input the dimension(odd number): ");
				inStr = in.readLine();
				num = Integer.valueOf(inStr);
			}
			nine.setDimension(num);
		}
		catch (Exception e)
		{
			nine.setDimension(3);
			p("Entered dimension error. Using default dimesion: 3.");
		}

		nine.createNinePalacePlans();
		nine.printNinePalace();
		p();
		p("The total sum of each line: " + nine.getTotal());

		p();
		p("=== Manual NinePalacePlans ===");
		int[][] paraArea = { { 8, 1, 6 }, { 3, 5, 7 }, { 4, 9, 2 } };
		if (nine.manualNinePalacePlans(paraArea))
		{
			p("All checked.");
		}
		else
		{
			p("Not correct.");
		}
	}
}

你可能感兴趣的:(java,C++,c,J#,ide)