Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java problem

Options
  • 17-02-2003 10:17pm
    #1
    Registered Users Posts: 6,420 ✭✭✭


    I'm trying to get this code to work, its a basic x's and o's game but for some reason it seems to be ****ing with my head, any1 can help,
    heres the code :


    import java.awt.*;
    import java.applet.*;
    import java.lang.String;

    public class project1 extends Applet {
    String ayl1, ayl2, ayl3, ayl4, ayl5, ayl6, ayl7; //ayl = message;
    String aray1[][] = new String[3][3]; //aray = array;
    Button ward1, ward2; // ward = button;
    TextField feef1,feef2,feef3; //feef = text;
    int a, e;
    int i = 0;
    int ao = 0;
    int eo = 0;


    public void init() {
    setBackground(new Color(0,128,128)); // changes background to green/blue;


    ward1 = new Button("Player 1: X");
    ward2 = new Button("Player 2: O");
    feef1 = new TextField(3);
    feef2 = new TextField(3);

    add(ward1);
    add(ward2);
    add(feef1);
    add(feef2);
    }

    public boolean action(Event evt, Object arg) {


    ayl7 = "";

    ayl1 = feef1.getText();
    Integer Y = new Integer(ayl1);
    a =Y.intValue(); // should allow coordinates to be entered into textfield;

    ayl2 = feef2.getText();
    Integer W = new Integer(ayl2);
    e = W.intValue(); // should allow the second coordinate to be entered;



    if(evt.target==ward1){ // when the "Player 1: X" button is pressed;
    if(i==0){
    if((aray1[a][e]=="X")||(aray1[a][e]=="O")){
    ayl7 = "Please choose another coordinate...";
    }
    else{
    ayl3 ="X"; // then ayl3 = X...;
    aray1[a][e] = ayl3; // ... and the coordinates equal the message;
    i=1;
    eo = eo +1;
    }
    }
    else{
    ayl6 = "Please wait your turn! Player 2 goes next.";
    }
    }


    if(evt.target==ward2){ // when the "Player 2: O" button is pressed;
    if(i==1){
    if((aray1[a][e]=="X")||(aray1[a][e]=="O")){
    ayl7 = "Please choose another coordinate...";
    }
    else{
    ayl4 ="O"; // then ayl4 = O...;
    aray1[a][e] = ayl4; // ... and the coordinates equal the message;
    i=0;
    eo = eo +1;
    }
    }
    else{
    ayl6 = "Please wait your turn! Player 1 goes next.";
    }
    }




    if(eo<=9){
    if((aray1[0][0].equals("X"))&&(aray1[0][1].equals("X"))&&(aray1[0][2].equals("X"))){
    ao = 1;
    ayl5 = "X Wins!";
    }

    if((aray1[1][0].equals("X"))&&(aray1[1][1].equals("X"))&&(aray1[1][2].equals("X"))){
    ayl5 = "X Wins!";
    ao = 1;
    }

    if((aray1[2][0].equals("X"))&&(aray1[2][1].equals("X"))&&(aray1[2][2].equals("X"))){
    ayl5 = "X Wins!";
    ao = 1;
    }

    if((aray1[0][0].equals("X"))&&(aray1[1][0].equals("X"))&&(aray1[2][0].equals("X"))){
    ayl5 = "X Wins!";
    ao = 1;
    }

    if((aray1[0][1].equals("X"))&&(aray1[1][1].equals("X"))&&(aray1[2][1].equals("X"))){
    ayl5 = "X Wins!";
    ao = 1;
    }

    if((aray1[0][2].equals("X"))&&(aray1[1][2].equals("X"))&&(aray1[2][2].equals("X"))){
    ayl5 = "X Wins!";
    ao = 1;
    }

    if((aray1[0][0].equals("X"))&&(aray1[1][1].equals("X"))&&(aray1[2][2].equals("X"))){
    ayl5 = "X Wins!";
    ao = 1;
    }

    if((aray1[0][2].equals("X"))&&(aray1[1][1].equals("X"))&&(aray1[2][0].equals("X"))){
    ayl5 = "X Wins!";
    ao = 1;
    }



    if((aray1[0][0].equals("O"))&&(aray1[0][1].equals("O"))&&(aray1[0][2].equals("O"))){
    ayl5 = "O Wins!";
    ao = 1;
    }

    if((aray1[1][0].equals("O"))&&(aray1[1][1].equals("O"))&&(aray1[1][2].equals("O"))){
    ayl5 = "O Wins!";
    ao = 1;
    }

    if((aray1[2][0].equals("O"))&&(aray1[2][1].equals("O"))&&(aray1[2][2].equals("O"))){
    ayl5 = "O Wins!";
    ao = 1;
    }

    if((aray1[0][0].equals("O"))&&(aray1[1][0].equals("O"))&&(aray1[2][0].equals("O"))){
    ayl5 = "O Wins!";
    ao = 1;
    }

    if((aray1[0][1].equals("O"))&&(aray1[1][1].equals("O"))&&(aray1[2][1].equals("O"))){
    ayl5 = "O Wins!";
    ao = 1;
    }

    if((aray1[0][2].equals("O"))&&(aray1[1][2].equals("O"))&&(aray1[2][2].equals("O"))){
    ayl5 = "O Wins!";
    ao = 1;
    }

    if((aray1[0][0].equals("O"))&&(aray1[1][1].equals("O"))&&(aray1[2][2].equals("O"))){
    ayl5 = "O Wins!";
    ao = 1;
    }

    if((aray1[0][2].equals("O"))&&(aray1[1][1].equals("O"))&&(aray1[2][0].equals("O"))){
    ayl5 = "O Wins!";
    ao = 1;
    }

    if((eo==9)&&(ao!=1)){
    ayl5 = "It's a draw!";
    }


    repaint();
    return(true);
    }

    public void paint(Graphics g) {


    g.setFont(new Font("Helvetica",1,36));
    g.setColor(new Color(220,250,0)); // a bright yellow;
    g.drawString(ayl5,650,300);
    g.setFont(new Font("Helvetica",0,18));
    g.setColor(new Color(0,220,178)); // a green shade;
    g.drawString(ayl6,625,350);
    g.drawString(ayl7,625,400);

    g.setColor(new Color(255,200,220)); // the box becomes a pale pink shade;
    g.drawLine(300,199,600,199);
    g.drawLine(300,200,600,200); // two lines for extra definition of the box;
    g.drawLine(300,299,600,299);
    g.drawLine(300,300,600,300); // a supporting line for one of the horizontal lines;
    g.drawLine(300,399,600,399);
    g.drawLine(300,400,600,400); //
    g.drawLine(300,499,600,499);
    g.drawLine(300,500,600,500); //
    g.drawLine(299,200,299,500); // the vertical lines of the box starts here;
    g.drawLine(300,200,300,500); //
    g.drawLine(399,200,399,500);
    g.drawLine(400,200,400,500); //
    g.drawLine(499,200,499,500);
    g.drawLine(500,200,500,500); //
    g.drawLine(599,200,599,500);
    g.drawLine(600,200,600,500); //

    g.setFont(new Font("Arial",1,24));
    g.setColor(new Color(175,220,175)); // the horizontal row of numbers are pale green now;
    g.drawString("0",350,185);
    g.drawString("1",450,185);
    g.drawString("2",550,185);
    g.drawString("x",540,45);
    g.drawLine(525,65,565,65); // lines for the arrow, to indicate direction to the right;
    g.drawLine(525,64,565,64); // supporting line to define the arrow;
    g.drawLine(565,64,550,55);
    g.drawLine(565,65,550,56); //
    g.drawLine(565,65,550,75);
    g.drawLine(565,66,550,76); //
    g.setColor(new Color(175,175,220));// the vertical row of numbers are pale blue now;
    g.drawString("0",275,250);
    g.drawString("1",275,350);
    g.drawString("2",275,450);
    g.drawString("y",590,45);
    g.drawLine(595,60,595,95); // lines for the arrow to indicate direction, downwards;
    g.drawLine(594,60,594,95); // supporting line for the arrow;
    g.drawLine(595,95,585,80);
    g.drawLine(594,95,584,80); //
    g.drawLine(595,95,605,80);
    g.drawLine(594,95,604,80); //

    g.setFont(new Font("Hevetica",0,18));
    g.setColor(new Color(0,90,90));
    g.drawString("Please enter in coordinates to start",685,25);
    g.drawString("Player 1 starts first",685,60);
    g.drawString("Refresh to play again!", 685,95);
    g.setFont(new Font("Helvetica",1,34));
    g.setColor(new Color(0,80,80)); //
    g.drawString("X's and O's game!",20,35);

    g.setFont(new Font("Arial",2,14)); // this ensures the font is "Arial", italic and bold at 12pt;
    g.setColor(new Color(220,190,220)); // this makes the following message purple;
    g.drawString("Aoife Aylward",25,655);
    g.drawString("LUDO B",25,675);

    g.setFont(new Font("Arial",0,100));
    g.setColor(new Color(128,128,220));// a shade of pale blue;
    for(int d=0; d<3; d=d+1){
    for(int l=0; l<3; l=l+1){
    g.drawString(aray1[d][l],315+100*d,285+100*l);
    }
    }
    }
    }


    any and all help would be appreciated.


Comments

  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Can we sit your exams for you too..? :rolleyes:

    Be more specific with your question and show us how you've tried to resolve the issue(s) at hand.


  • Registered Users Posts: 6,420 ✭✭✭Doodee


    right, if u look, the code is what i should be handing up for the exams,and its done
    the problem is that its not loading in any web browser, which leads me to believe that either its not compiling properly, or else that theres a few bad lines of code, its J-creator 1.5 code, done in LE, so there could be a problem right there


  • Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 10,561 Mod ✭✭✭✭Robbo


    For a start, use decent variable names, instead of those relating to Galway's most prestigious families...


  • Registered Users Posts: 6,420 ✭✭✭Doodee


    those variable are being look for by the self rightious know it all examiner, who thinks that every os apart from win me is ****ing bugged to **** and that he can get any div-x he wants from thailand, when he cant, and only goes there for the ****ing lady boys.

    ffs, how the hell did i get so pissed off so fast!!!
    i was fine when i posted the code.


  • Closed Accounts Posts: 189 ✭✭Kenshin


    Originally posted by Doodee
    the problem is that its not loading in any web browser, which leads me to believe that either its not compiling properly, or else that theres a few bad lines of code, its J-creator 1.5 code, done in LE, so there could be a problem right there
    Well is it compiling or not? Do you get error messages when you compile it? Did you make a correct html file for the applet? Tell us more... (and try posting the code in php tags next time, looks nicer :))


  • Advertisement
  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    I concurr - the code you just supplied is full of malnested loops, etc. Won't compile for me either. You really can't expect to post your entire app (un-commented/indented) and expect us to figure out what's wrong with it.

    People here are willing to help you, not carry you.


  • Registered Users Posts: 1,931 ✭✭✭Zab


    Right, to start with you will have to take a systematic approach to getting your braces correct in the action() method. Just start at the beginning and go over the lot of them.... this should get it compiling.

    A few other notes:

    Your variable names are all over the place. a? e? i? ao? eo? huh? I'm not even going to start on the feefs etc.
    Write "d++" not "d=d+1".
    You have to initialise your array ( or aray ) before you try to display it. At the moment it starts as an array of nulls.
    Same goes for the messages ( or ayls ).
    You could just have one button, that changes its name, instead of having two.
    You are doing everything by hand. Testing for a horizontal line is the same for each line, you can just use a loop. You're also testing for X and O separately, it doesn't matter: they all just have to be the same. You can work out who won after you find out that somebody won.
    Just because arrays start at zero, doesn't mean the user has to!
    Just because screen ordinates go from top to botom, doesn't mean the user has to!
    When something isn't compiling, you get told that it isn't compiling by the compiler. It's pretty staight forward.
    You could let the user use the mouse rather than enter coordinates.

    I'll be honest, and I don't mean to sound harsh, but if all you do is fix the braces and initialisations, and get it to work, you would be getting very low marks from me.

    Zab.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Write "d++" not "d=d+1".

    Actually either is fine, however do not interchange through your code.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    And did you recompile with -deprecation for details, as it suggested?


  • Registered Users Posts: 6,420 ✭✭✭Doodee


    The reason that the code is so **** is cause we are a low standard for java, if u show really complex code then the accessor begins to ask questions.


    im down to these three errors, I re-did the code to add in textfields to make the board.


    C:\Program Files\Xinox Software\JCreator\MyProjects\hello.java\project.java:181: illegal start of expression
    public void paint(Graphics g) {
    ^
    C:\Program Files\Xinox Software\JCreator\MyProjects\hello.java\project.java:262: ';' expected
    }
    ^
    C:\Program Files\Xinox Software\JCreator\MyProjects\hello.java\project.java:263: '}' expected
    }
    ^
    Note: C:\Program Files\Xinox Software\JCreator\MyProjects\hello.java\project.java uses or overrides a deprecated API.
    Note: Recompile with -deprecation for details.
    3 errors
    Process completed.



    the code i added into lines 182-188:
    g.drawLine (50,50,200,50)
    g.drawLine (50,50,200,100)
    g.drawLine (50,50,150,200)
    g.drawLine (50,50,100,200)
    g.drawLine (50,50,50,100)
    g.drawLine (50,50,50,50)
    g.drawLine (50,50,100,50)

    its **** code i know, but its all we get in this collage.


  • Advertisement
  • Registered Users Posts: 1,931 ✭✭✭Zab


    Originally posted by Hobbes
    Actually either is fine, however do not interchange through your code.
    I disagree. Well, kind of. Obviously either will work, the point is scanability. Incrementing by one is a very common operation, so writing it d++ makes it scan easier. I'll admit that this isn't that important, it's more of a style thing.

    Zab.


  • Registered Users Posts: 1,931 ✭✭✭Zab


    Doodee,

    You're still having the same problem. You have to start at the beginning of action() and match up all your braces and logic. You'll find that you have too many opening braces, which is why the compiler is complaining. Some IDEs have a bracematching function, where when you select a brace it can find the matching brace. See if your editor has one of these. Indentation can also be very handy in these circumstances.

    Zab.


  • Registered Users Posts: 660 ✭✭✭anthonymcg


    Originally posted by Doodee
    its **** code i know, but its all we get in this collage.

    What college is this?


  • Closed Accounts Posts: 189 ✭✭Kenshin


    Originally posted by Doodee
    the code i added into lines 182-188:
    g.drawLine (50,50,200,50)
    g.drawLine (50,50,200,100)
    g.drawLine (50,50,150,200)
    g.drawLine (50,50,100,200)
    g.drawLine (50,50,50,100)
    g.drawLine (50,50,50,50)
    g.drawLine (50,50,100,50)
    There should be a semicolon after every line........


This discussion has been closed.
Advertisement