-
GUI and Knight's Tour
well im new to java. and wanted to try the problem i encountered in C++
the Knight's tour
here's my problem, i want to put the result of the tour in a frame
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class trialagainKT extends Frame {
public static void main(String[] args){
int x;
int y;
String Number1;
String Number2;
Number1 =JOptionPane.showInputDialog("enter x: ");
Number2 =JOptionPane.showInputDialog("enter Y: ");
x = Integer.parseInt(Number1);
y = Integer.parseInt(Number2);
PerformKT(x, y);
}
public static void PerformKT(int currentRow, int currentColumn) {
if (currentRow > 7 || currentRow < 0 || currentColumn > 7 || currentColumn < 0) {
JOptionPane.showMessageDialog(null,"Invalid Start Square ","Try Again",JOptionPane.PLAIN_MESSAGE);
}
//initialization of board
int chessBoard[][] = new int[8][8];
int horizontal[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int vertical[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int counter = 1;
boolean legalMove[] = new boolean[8];
int moveNumber = 0;
int accessibility[][] =
{{2,3,4,4,4,4,3,2},{3,4,6,6,6,6,3,4},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},{3,4,6,6,6,6,3,4},{2,3,4,4,4,4,3,2}};
chessBoard[currentRow][currentColumn] = counter;
for(counter = 2; counter <= 64; counter++) {
for(int i = 0; i < 8; i++) {
legalMove[i] = false; //reset leaglMove array to false
}
moveNumber = 0; //reset moveNumber to 0
//check the possible moves for the knight
while (moveNumber < 8) {
int checkR = currentRow + vertical[moveNumber];
int checkC = currentColumn + horizontal[moveNumber];
if (checkR >= 0 && checkR <= 7 && checkC >= 0 && checkC <= 7) {
//check if legal move is occupied
if (chessBoard[checkR][checkC] == 0) {
accessibility[checkR][checkC]--;
legalMove[moveNumber] = true;
} else {
legalMove[moveNumber] = false;
}
} else {
legalMove[moveNumber] = false;
}
++moveNumber;
}
//what is the best move?
int bestAccess = 8;
int bestMove = -1;
for(int i = 0; i < 8; i++) {
if(legalMove[i] == true) {
int checkR = currentRow + vertical[i];
int checkC = currentColumn + horizontal[i];
if (accessibility[checkR][checkC] <= bestAccess) {
bestAccess = accessibility[checkR][checkC];
bestMove = i; //Store first legal move with lowest accessibility
}
}
}
//move the knight
currentRow += vertical[bestMove];
currentColumn += horizontal[bestMove];
chessBoard[currentRow][currentColumn] = counter;
}
//this is what i want to put in the frame
System.out.println();
System.out.println(" 00 01 02 03 04 05 06 07 ");
System.out.println("*---*---*---*---*---*---*---*---*");
for(int i=0; i < chessBoard.length; i++) {
for(int j=0; j < chessBoard[i].length; j++) {
System.out.printf("|%3d", chessBoard[i][j]);
}
System.out.println("|");
System.out.println("| | | | | | | | |");
System.out.println("*---*---*---*---*---*---*---*---*");
}
//this is for the frame
JTextArea outputArea = new JTextArea (15,30);
outputArea.setFont(new Font("Courier",Font.BOLD,16));
JScrollPane scroller = new JScrollPane(outputArea);
//no particular output yet
outputArea.setText();
String title="Complete Knight's Tour";
if(counter<64){title="Knight's Tour with "+counter+" Moves";
}
else {
title="Knight's Tour Complete";
}
JOptionPane.showMessageDialog(null,scroller,title,JOptionPane.INFORMATION_MESSAGE);
}
}
-
about GUI
how can i put the output of this code snippet in to a frame or
JTextArea
Code:
System.out.println();
System.out.println(" 00 01 02 03 04 05 06 07 ");
System.out.println("*---*---*---*---*---*---*---*---*");
for(int i=0; i < chessBoard.length; i++) {
for(int j=0; j < chessBoard[i].length; j++) {
System.out.printf("|%3d", chessBoard[i][j]);
}
System.out.println("|");
System.out.println("| | | | | | | | |");
System.out.println("*---*---*---*---*---*---*---*---*");
}
-
Sample code to capture println() output
It's rather complicated. It involves changing the System.out variable to send what is println()ed to your code which then appends it to the text area.
Here's a sample program that puts println() output in a JTextArea.
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class TAConsole extends JFrame {
PipedInputStream piOut;
PipedInputStream piErr;
PipedOutputStream poOut;
PipedOutputStream poErr;
JTextArea textArea = new JTextArea();
public TAConsole() throws IOException {
// Set up System.out
piOut = new PipedInputStream();
poOut = new PipedOutputStream(piOut);
System.setOut(new PrintStream(poOut, true));
// Set up System.err
piErr = new PipedInputStream();
poErr = new PipedOutputStream(piErr);
System.setErr(new PrintStream(poErr, true));
// Add a scrolling text area
textArea.setEditable(false);
textArea.setRows(20);
textArea.setColumns(50);
getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER);
pack();
setVisible(true);
// Create reader threads
new ReaderThread(piOut).start();
new ReaderThread(piErr).start();
}
class ReaderThread extends Thread {
PipedInputStream pi;
ReaderThread(PipedInputStream pi) {
this.pi = pi;
}
public void run() {
final byte[] buf = new byte[1024];
try {
while (true) {
final int len = pi.read(buf);
if (len == -1) {
break;
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(new String(buf, 0, len));
// Make sure the last line is always visible
textArea.setCaretPosition(textArea.getDocument().getLength());
// Keep the text area down to a certain character size
int idealSize = 1000;
int maxExcess = 500;
int excess = textArea.getDocument().getLength() - idealSize;
if (excess >= maxExcess) {
textArea.replaceRange("", 0, excess);
}
}
});
}
} catch (IOException e) {
}
}
}
//----------------------------------
public static void main(String[] args) throws Exception {
new TAConsole();
System.out.println("THis is a test");
} // end main()
}
-
ahh i see... thanks..
well the code i posted is for the Knight's Tour
and what it does is to output the board with the move number..
.. does the JOptionPane have the "%d" attributes of System.out.println?
with just the console my Knight's tour runs. >_<
thanks for the reply
here my code by the way >_< a bit messy
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class trialagainKT extends Frame {
public static void main(String[] args){
int x;
int y;
String Number1;
String Number2;
Number1 =JOptionPane.showInputDialog("enter x: ");
Number2 =JOptionPane.showInputDialog("enter Y: ");
x = Integer.parseInt(Number1);
y = Integer.parseInt(Number2);
PerformKT(x, y);
}
public static void PerformKT(int currentRow, int currentColumn) {
if (currentRow > 7 || currentRow < 0 || currentColumn > 7 || currentColumn < 0) {
JOptionPane.showMessageDialog(null,"Invalid Start Square ","Try Again",JOptionPane.PLAIN_MESSAGE);
}
//initialization of board
int chessBoard[][] = new int[8][8];
int horizontal[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int vertical[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int counter = 1;
boolean legalMove[] = new boolean[8];
int moveNumber = 0;
int accessibility[][] =
{{2,3,4,4,4,4,3,2},{3,4,6,6,6,6,3,4},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},{3,4,6,6,6,6,3,4},{2,3,4,4,4,4,3,2}};
chessBoard[currentRow][currentColumn] = counter;
for(counter = 2; counter <= 64; counter++) {
for(int i = 0; i < 8; i++) {
legalMove[i] = false; //reset leaglMove array to false
}
moveNumber = 0; //reset moveNumber to 0
//check the possible moves for the knight
while (moveNumber < 8) {
int checkR = currentRow + vertical[moveNumber];
int checkC = currentColumn + horizontal[moveNumber];
if (checkR >= 0 && checkR <= 7 && checkC >= 0 && checkC <= 7) {
//check if legal move is occupied
if (chessBoard[checkR][checkC] == 0) {
accessibility[checkR][checkC]--;
legalMove[moveNumber] = true;
} else {
legalMove[moveNumber] = false;
}
} else {
legalMove[moveNumber] = false;
}
++moveNumber;
}
//what is the best move?
int bestAccess = 8;
int bestMove = -1;
for(int i = 0; i < 8; i++) {
if(legalMove[i] == true) {
int checkR = currentRow + vertical[i];
int checkC = currentColumn + horizontal[i];
if (accessibility[checkR][checkC] <= bestAccess) {
bestAccess = accessibility[checkR][checkC];
bestMove = i; //Store first legal move with lowest accessibility
}
}
}
//move the knight
currentRow += vertical[bestMove];
currentColumn += horizontal[bestMove];
chessBoard[currentRow][currentColumn] = counter;
}
// This is what i wanted to put in the frame
System.out.println();
System.out.println(" 00 01 02 03 04 05 06 07 ");
System.out.println("*---*---*---*---*---*---*---*---*");
for(int i=0; i < chessBoard.length; i++) {
for(int j=0; j < chessBoard[i].length; j++) {
System.out.printf("|%3d", chessBoard[i][j]);
}
System.out.println("|");
System.out.println("| | | | | | | | |");
System.out.println("*---*---*---*---*---*---*---*---*");
}
// this is for the frame hope its right
JTextArea outputArea = new JTextArea (15,30);
outputArea.setFont(new Font("Courier",Font.BOLD,16));
JScrollPane scroller = new JScrollPane(outputArea);
//no particulat output yet
outputArea.setText();
String title="Complete Knight's Tour";
if(counter<64){title="Knight's Tour with "+counter+" Moves";
}
else {
title="Knight's Tour Complete";
}
JOptionPane.showMessageDialog(null,scroller,title,JOptionPane.INFORMATION_MESSAGE);
}
}
Last edited by wenk; 10-16-2008 at 11:06 PM.
-
Any what problems are you encountering?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
@offtopic
wow my topic appeared late >_<
@topic
well i wanted to place the output of the one with the red comment
Code:
System.out.println();
System.out.println(" 00 01 02 03 04 05 06 07 ");
System.out.println("*---*---*---*---*---*---*---*---*");
for(int i=0; i < chessBoard.length; i++) {
for(int j=0; j < chessBoard[i].length; j++) {
System.out.printf("|%3d", chessBoard[i][j]);
}
System.out.println("|");
System.out.println("| | | | | | | | |");
System.out.println("*---*---*---*---*---*---*---*---*");
}
into the Frame.. i got confused with some tutorials >_<
anyways thanks for the reply
-
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
Similar Threads
-
By pedrotuga in forum Open Source
Replies: 2
Last Post: 07-26-2005, 07:28 AM
-
Replies: 0
Last Post: 05-29-2005, 08:54 AM
-
Replies: 1
Last Post: 11-27-2001, 06:53 AM
-
By Rob Teixeira in forum .NET
Replies: 3
Last Post: 03-17-2001, 03:17 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks