Help with java filehandling

Hi guys!

I would need some help with a simple game. The only thing that has to be created in it is two buttons "Save" and "Load". Iam a complete beginner in java filehandling so I appreciate all help I can get...please..
How should I implement filehandling in this code..help please..



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyGame extends java.awt.Frame {
private java.awt.MenuBar menuBar1;
private java.awt.Menu menu1;
private java.awt.MenuItem menuNewGame;
private java.awt.MenuItem menuExit;
private java.awt.MenuItem menuAbout;
private static JButton button[];
private String sign = "X";
private static String status[] = new String[ 10 ];
private static String theWinner = "";
private boolean available = false;

public MyGame() {
super( "LuffarShack" );
initComponents();
}

private void initComponents() {
setLayout( new java.awt.GridLayout( 3, 3, 1, 1 ) );
setResizable( false );

menuBar1 = new java.awt.MenuBar();
menu1 = new java.awt.Menu();
menuNewGame = new java.awt.MenuItem();
menuExit = new java.awt.MenuItem();
menuAbout = new java.awt.MenuItem();

menu1.setLabel("FIL");
menuNewGame.setLabel("NYTT SPEL");
menuNewGame.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newGameActionPerformed(evt);
}
});

menu1.add(menuNewGame);
menuExit.setLabel("AVSLUTA");
menuExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});

menu1.add(menuExit);
menuAbout.setLabel("AV");
menuAbout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutActionPerformed(evt);
}
});

menu1.add(menuAbout);
menuBar1.add(menu1);

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

setMenuBar(menuBar1);

button = new JButton[ 10 ];
for ( int i = 1; i < 10; i++ ) {
button[ i ] = new JButton();
button[ i ].setFocusPainted( false );
button[ i ].setActionCommand( Integer.toString( i ) );
button[ i ].setFont( new Font( "Dialog", 0, 48 ) );
button[ i ].setPreferredSize( new Dimension( 100, 100 ) );
button[ i ].setToolTipText( "KLICKA FR ATT GRA DITT DRAG" );
button[ i ].addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent evt ) {
buttonAction( evt.getActionCommand() ); }
} );
add( button[ i ] );
}
pack();
}//

private void aboutActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog( MyGame.this, "Detta spel r skapat av ME ",
"Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE );
}

private void exitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit( 0 );
}

private void newGameActionPerformed(java.awt.event.ActionEvent evt) {
setNewGame();
}

private void exitForm(java.awt.event.WindowEvent evt) {
System.exit( 0 );
}

public static void main( String args[] ) {
new MyGame().show();
setNewGame();
}

private void buttonAction( String btn ) {
int index = Integer.parseInt( btn );
if ( button[ index ].getText() == "" ) {
button[ index ].setText( sign );
status[ index ] = sign;
checkGameStatus();
nextMove();
}
else
JOptionPane.showMessageDialog( MyGame.this,
"DENNA RUTA R REDAN UPPTAGEN, VLJ EN ANNAN LEDIG EN.");

}

private void nextMove() {
if ( button[ 5 ].getText() == "" ){
button[ 5 ].setText( "O" );
status[ 5 ] = "O";
}
else {
int move = randomMove();
if ( button[ move ].getText() != "" )
nextMove();
else {
button[ move ].setText( "O" );
status[ move ] = "O";
}
}

checkGameStatus();
}

private int randomMove() {
int attempt = 0;
if ( available ) {
attempt = ( 1 + ( int ) ( Math.random() * 9 ) ); // any number from 0 to 9
}
return attempt;
}

public static void setNewGame() {
for ( int j = 1; j < 10; j++ ) {
button[ j ].setText( "" );
status[ j ] = "";
}
theWinner = "";
}//end of setNewGame()

private void checkGameStatus() {
//check top horizontal row
if ( ( status[ 1 ] != "" ) && ( status[ 1 ] == status [ 2 ]
&& status[ 2 ] == status[ 3 ] ) ){
theWinner = status[ 1 ];
gameStop( theWinner );
}

if ( ( status[ 4 ] != "" ) && ( status[ 4 ] == status [ 5 ]
&& status[ 5 ] == status[ 6 ] ) ){
theWinner = status[ 4 ];
gameStop( theWinner );
}

if ( ( status[ 7 ] != "" ) && ( status[ 7 ] == status [ 8 ]
&& status[ 8 ] == status[ 9 ] ) ){
theWinner = status[ 7 ];
gameStop( theWinner );
}

if ( ( status[ 1 ] != "" ) && ( status[ 1 ] == status [ 4 ]
&& status[ 4 ] == status[ 7 ] ) ){
theWinner = status[ 1 ];
gameStop( theWinner );
}

if ( ( status[ 2 ] != "" ) && ( status[ 2 ] == status [ 5 ]
&& status[ 5 ] == status[ 8 ] ) ){
theWinner = status[ 2 ];
gameStop( theWinner );
}

if ( ( status[ 3 ] != "" ) && ( status[ 3 ] == status [ 6 ]
&& status[ 6 ] == status[ 9 ] ) ){
theWinner = status[ 3 ];
gameStop( theWinner );
}

if ( ( status[ 1 ] != "" ) && ( status[ 1 ] == status [ 5 ]
&& status[ 5 ] == status[ 9 ] ) ){
theWinner = status[ 1 ];
gameStop( theWinner );
}

if ( ( status[ 3 ] != "" ) && ( status[ 3 ] == status [ 5 ]
&& status[ 5 ] == status[ 7 ] ) ){
theWinner = status[ 3 ];
gameStop( theWinner );
}
s
found: {
for ( int a = 1; a < 10; a++ ) {
if ( status[ a ] == "" ){
available = true;
break found;
}
else
available = false;
}
}

if ( !available )
gameStop( "tie" );
}

private void gameStop( String win ) {
if ( win == "tie" ){
JOptionPane.showMessageDialog( MyGame.this, "DET BLEV OAVGJORT!!, SPELA GRNA IGEN");

setNewGame();
}
else {
String output = "The player "" + win + "" has won!!!
Would you like to play again?";
int choice = JOptionPane.showConfirmDialog( MyGame.this, output,
"Congratulations!", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE );
if ( choice == 0 )
setNewGame();
else
System.exit( 0 );
}
}
}

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories