📄 cross sums.txt
字号:
//----------------------------------------------------------------------
// Cross Sums
//----------------------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class CrossSum extends Applet {
public String postpage;
String probfile;
DatabaseAccess db = null;
DatabaseIF dbif = null;
int unitsize = 0;
int fontsize = 0;
boolean first = true;
Color controlPanelcolor;
Panel controlPanel;
Checkbox hintbox;
Choice dbchoice;
PlayPanel playPanel;
Font boldfont = new java.awt.Font( "Helvetica", 0, 18 );
public void init() {
dbif = new DatabaseIF( this, getParentFrame() );
postpage = getParameter("post");
probfile = getParameter("problemfile");
db.problemid = getParameter("problemid");
String str = getParameter("unitsize");
if( str != null ) {
try {
unitsize = (Integer.valueOf(str)).intValue();
} catch( NoSuchElementException e ) {}
}
str = getParameter("fontsize");
if( str != null ) {
try {
fontsize = (Integer.valueOf(str)).intValue();
} catch( NoSuchElementException e ) {}
}
setLayout( new BorderLayout() );
controlPanel = new Panel();
controlPanel.setFont( boldfont );
add( "North", controlPanel );
controlPanel.setLayout( new FlowLayout( FlowLayout.CENTER, 30, 2 ) );
controlPanelcolor = controlPanel.getBackground();
hintbox = new Checkbox( "Hint" );
hintbox.setFont( boldfont );
controlPanel.add( hintbox );
controlPanel.add( new ControlButton( this, "Clear" ) );
controlPanel.add( new ControlButton( this, "Check" ) );
// if( db.problemid != null ) {
// dbchoice = new Choice();
// dbchoice.setFont(boldfont);
// dbchoice.addItem( "Database" );
// dbchoice.addItem( "Register" );
// dbchoice.addItem( "Enter" );
// dbchoice.addItem( "Save" );
// dbchoice.addItem( "Load" );
// dbchoice.addItem( "View" );
// dbchoice.addItem( "Answer" );
// controlPanel.add( dbchoice );
// }
playPanel = new PlayPanel( this );
add( "Center", playPanel );
Label copyright = new Label( "Cross Sums" + " " +
"Copyright(C).",
Label.CENTER );
add( "South", copyright );
if( probfile!=null && first ) {
first = false;
playPanel.loadData( probfile );
}
playPanel.canvas.resizeCanvas();
}
public boolean keyDown( Event evt, int key ) {
CrosssumCanvas cc = playPanel.canvas;
if( key == Event.UP ) {
cc.moveCurXY( 0, -1 );
return true;
}
if( key == Event.DOWN ) {
cc.moveCurXY( 0, +1 );
return true;
}
if( key == Event.LEFT ) {
cc.moveCurXY( -1, 0 );
return true;
}
if( key == Event.RIGHT ) {
cc.moveCurXY( +1, 0 );
return true;
}
if( key == '\n' ) {
hintbox.setState( playPanel.ishint = ! playPanel.ishint );
return true;
}
if( key == ' ' ) {
playPanel.changeValue( 0, true );
return true;
}
int keynum = key - '0';
if( 0<=keynum && keynum<10 ) {
playPanel.changeValue( keynum, true );
return true;
}
return true;
}
Frame getParentFrame() {
Component co = this;
while(!((co = co.getParent()) instanceof Frame)) {}
return (Frame) co;
}
public boolean action( Event evt, Object obj ) {
if( evt.target == hintbox ) {
playPanel.ishint = hintbox.getState();
return true;
}
return false;
}
}
class PlayPanel extends Panel {
CrossSum app;
DatabaseAccess db;
GridBagLayout gbl;
CrosssumCanvas canvas;
Cell table[][];
boolean ishint;
int curX = -1;
int curY = -1;
int bdwidth = 20;
int bdheight = 20;
int bdunit = 32;
int fw, fh;
PlayPanel( CrossSum p ) {
app = p;
db = app.db;
if( app.unitsize > 0 )
bdunit = app.unitsize;
newBoard();
canvas = new CrosssumCanvas( this );
add( canvas );
}
Cell cell() {
return cell( curX, curY );
}
Cell cell( int x, int y ) {
if( x < 0 || bdwidth <= x || y < 0 || bdheight <= y )
return null;
return table[x][y];
}
void newBoard() {
table = new Cell[bdwidth][bdheight];
for( int x=0; x<bdwidth; ++x ) {
for( int y=0; y<bdheight; ++y ) {
table[x][y] = new Cell();
}
}
int curX = -1;
int curY = -1;
db.history.clear();
db.isviewdata = false;
}
void loadData( String filename ) {
URL url;
DataInputStream file;
try {
url = new URL( app.getCodeBase(), filename );
file = new DataInputStream( url.openStream() );
} catch( MalformedURLException e ) {
app.showStatus( "file name error : " + filename );
return;
} catch( IOException e ) {
app.showStatus( "file open error : " + filename );
return;
}
String line;
try {
int x, y, v, h;
nextline:
for(;;) {
line=file.readLine();
if( line == null )
break;
if( '#' == line.charAt(0) )
continue;
StringTokenizer st = new StringTokenizer(line);
try {
String tk = st.nextToken();
if( tk.equals("size") ) {
tk = st.nextToken();
bdwidth = (Integer.valueOf(tk)).intValue();
tk = st.nextToken();
bdheight = (Integer.valueOf(tk)).intValue();
newBoard();
continue nextline;
}
if( tk.equals("begin") ) {
continue nextline;
}
if( tk.equals("end") ) {
break nextline;
}
x = (Integer.valueOf(tk)).intValue();
tk = st.nextToken();
y = (Integer.valueOf(tk)).intValue();
tk = st.nextToken();
v = (Integer.valueOf(tk)).intValue();
tk = st.nextToken();
h = (Integer.valueOf(tk)).intValue();
table[x][y].setMark( v, h );
} catch( NoSuchElementException e ) {}
}
} catch( IOException e ) {}
}
void clearview() {
for( int x=0; x<bdwidth; ++x ) {
for( int y=0; y<bdheight; ++y ) {
table[x][y].clear();
}
}
app.hintbox.setState( ishint = false );
canvas.paintFull();
canvas.paint( canvas.getGraphics() );
app.controlPanel.setBackground( app.controlPanelcolor );
}
void clear() {
clearview();
db.isviewdata = false;
db.history.clear();
}
void check() {
boolean bad = checkerror();
if( bad ) {
app.controlPanel.setBackground( Color.red );
} else {
app.controlPanel.setBackground( Color.green );
}
canvas.paintFull();
canvas.paint( canvas.getGraphics() );
if( (!bad) && (app.postpage != null) ) {
try {
URL url = new URL( app.getCodeBase() + app.postpage );
app.getAppletContext().showDocument( url );
} catch( MalformedURLException e ) { }
}
}
boolean checkerror() {
boolean err = false;
for( int x=0; x<bdwidth; ++x ) {
for( int y=0; y<bdheight; ++y ) {
Cell c = table[x][y];
c.verror = c.herror = c.error = false;
}
}
for( int x=0; x<bdwidth; ++x ) {
for( int y=0; y<bdheight; ++y ) {
Cell c = table[x][y];
if( ! c.mark )
continue;
if( c.vsum > 0 ) {
int array[] = new int[10];
int sum = 0;
int len = 0;
for( int k=1; k<10; ++k ) {
if( y+k >= bdheight )
break;
Cell cc = table[x][y+k];
if( cc.mark )
break;
++len;
++array[cc.value];
sum += cc.value;
}
if( sum != c.vsum ) {
c.verror = true;
err = true;
}
for( int k=1; k<10; ++k ) {
if( array[k] == 1 )
array[k] = 0;
}
for( int k=1; k<=len; ++k ) {
Cell cc = table[x][y+k];
if( array[cc.value] == 0 )
continue;
cc.error = true;
c.verror = true;
err = true;
}
}
if( c.hsum > 0 ) {
int array[] = new int[10];
int sum = 0;
int len = 0;
for( int k=1; k<10; ++k ) {
if( x+k >= bdwidth )
break;
Cell cc = table[x+k][y];
if( cc.mark )
break;
++len;
++array[cc.value];
sum += cc.value;
}
if( sum != c.hsum ) {
c.herror = true;
err = true;
}
for( int k=1; k<10; ++k ) {
if( array[k] == 1 )
array[k] = 0;
}
for( int k=1; k<=len; ++k ) {
Cell cc = table[x+k][y];
if( array[cc.value] == 0 )
continue;
cc.error = true;
c.herror = true;
err = true;
}
}
}
}
return err;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -