Click to See Complete Forum and Search --> : Help Opening a File within my program


Geoff Fink
September 22nd, 2000, 06:22 PM
Help!
I am a VERY beginner programmer and I'm trying to make a Tic Tac Toe Game.
For the game i want the computer to decide it's move based on a external file which will show all the different games it played,
and in what order they played. It will then check if it won or lost by moving to a certain square if it lost pick a dif. one. If it
won pick the same and it hasn't dont his combo yet pick the first open square. That way after a while it will be impossible for
the player to win! But i can't figure out how to open a file. Can any one show me a basic way to open a file and assign the text to
varaibles.
Here what I have so far(nothing with opening a file though):

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class XO extends Applet implements Runnable
{
int Move = 0,Height = 0, Mark11 = 0,Mark12= 0, Mark13 = 0, Mark21 = 0, Mark22=0, Mark23= 0, Mark31=0,Mark32=0,Mark33=0;
Image XO;
Image X;
Image O;
Thread t;


public void init()
{
XO = getImage(getCodeBase(),"XO.jpg");
X = getImage(getCodeBase(),"X.jpg");
O = getImage(getCodeBase(),"O.jpg");
t=new Thread(this);
t.start();


}


public static File open( String path )
{
return true;

}




public void run()
{
while (true)
{

}
}


public void paint(Graphics g)
{
g.drawImage(XO,1,1,this);
if (Mark11==11)
g.drawImage(X,20,20,this);
if (Mark12==12)
g.drawImage(X,95,20,this);
if (Mark13==13)
g.drawImage(X,170,20,this);
if (Mark21==21)
g.drawImage(X,20,100,this);
if (Mark22==22)
g.drawImage(X,95,100,this);
if (Mark23==23)
g.drawImage(X,170,100,this);
if (Mark31==31)
g.drawImage(X,20,180,this);
if (Mark32==32)
g.drawImage(X,95,180,this);
if (Mark33==33)
g.drawImage(X,170,180,this);


//O






//Win!
if (Mark11==11)
if (Mark12==12)
if (Mark13==13)
g.drawString("You Win!", 85, 260);
if (Mark21==21)
if (Mark22==22)
if (Mark23==23)
g.drawString("You Win!", 85, 260);
if (Mark31==31)
if (Mark32==32)
if (Mark33==33)
g.drawString("You Win!", 85, 260);
if (Mark11==11)
if (Mark21==21)
if (Mark31==31)
g.drawString("You Win!", 85, 260);
if (Mark12==12)
if (Mark22==22)
if (Mark32==32)
g.drawString("You Win!", 85, 260);
if (Mark13==13)
if (Mark23==23)
if (Mark33==33)
g.drawString("You Win!", 85, 260);
if (Mark11==11)
if (Mark22==22)
if (Mark33==33)
g.drawString("You Win!", 85, 260);
if (Mark13==13)
if (Mark22==22)
if (Mark31==31)
g.drawString("You Win!", 85, 260);


}

public boolean mouseUp(Event e, int MouseY, int MouseX)
{
Move++;
// [Height, Width]
// [1,1] [1,2] [1,3]
// [2,1] [2,2] [2,3]
// [3,1] [3,2] [3,3]

//Height 1
if (MouseX<80)
Height = 1;
//Height 2
if (MouseX<160)
if (MouseX>80)
Height = 2;
//Height 3
if (MouseX<240)
if (MouseX>160)
Height = 3;

//Height 1, Width 1
if (Height ==1)
if (MouseY<75)
Mark11 = 11;
//Height 1, Width 2
if (Height ==1)
if (MouseY<150)
if (MouseY>75)
Mark12 = 12;
//Height 1, Width 3
if (Height == 1)
if (MouseY<225)
if (MouseY>150)
Mark13 = 13;
//Height 2, Width 1
if (Height == 2)
if (MouseY<75)
Mark21 = 21;
//Height 2, 2
if (Height == 2)
if (MouseY<150)
if (MouseY>75)
Mark22 = 22;
//Height 2,3
if (Height == 2)
if (MouseY<225)
if (MouseY>150)
Mark23 = 23;
//Height 3,1
if (Height == 3)
if (MouseY<75)
Mark31 = 31;
//Height 3,2
if (Height == 3)
if (MouseY<150)
if (MouseY>75)
Mark32 = 32;
//Height 3,3
if (Height == 3)
if (MouseY<225)
if (MouseY>150)
Mark33 = 33;




repaint();
return true;
}





}


Thanks!

santyns
September 23rd, 2000, 07:29 PM
Hi!
I found this somwhere off the web...thought it might be useful to you...When you are dealing with applets and files, you have to consider the security issues involved. An applet cannot write to a file without taking care of security issues...you'll have to dig more on that if you have to write to a file.

Here is the code that reads from a file.


import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.security.*;

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

public class ShowFile extends Applet {
private TextArea fileArea;
private TextField filename;
public void init()
{
Panel topPanel = new Panel();
topPanel.setLayout(new BorderLayout());
topPanel.add("North", new Label("File name relative to current directory ", Label.CENTER));
Panel inputPanel = new Panel();
inputPanel.add(new Label(""));
filename = new TextField(20);
inputPanel.add(filename);
inputPanel.add(new Button("Show"));
topPanel.add("Center", inputPanel);
setLayout(new BorderLayout());
add("North", topPanel);
fileArea = new TextArea();
fileArea.setFont(new Font("Courier", Font.BOLD, 12));
add("Center", fileArea);
}

public boolean action(Event e, Object o)
{
try {
URL file = new URL(getDocumentBase(),filename.getText());
BufferedInputStream buffer = new BufferedInputStream(file.openStream());
DataInputStream in = new DataInputStream(buffer);
File f = new File(filename.getText());
java.io.FilePermission perm = new java.io.FilePermission(f.toString(), "read");
System.out.println(perm.getActions());
FileInputStream dis = new FileInputStream(f);
byte b[] = new byte[dis.available()];
dis.read(b);
String str = new String(b);
fileArea.setText(str);
} catch(IOException ioe) {
System.out.println("IOException: " + ioe);
} catch(Exception f)
{
System.err.println(f);
}
return(true);
}
}